leverage global object creator in proxies.

This commit is contained in:
Darien Raymond
2017-01-13 00:56:21 +01:00
parent db1c9131f0
commit 148e4832eb
23 changed files with 353 additions and 269 deletions
+6 -2
View File
@@ -1,6 +1,7 @@
package proxy_test
import (
"context"
"testing"
"v2ray.com/core/app"
@@ -21,14 +22,17 @@ func TestProxyDial(t *testing.T) {
assert := assert.On(t)
space := app.NewSpace()
ctx := app.ContextWithSpace(context.Background(), space)
assert.Error(space.AddApp(new(proxyman.OutboundConfig)))
outboundManager := proxyman.OutboundHandlerManagerFromSpace(space)
common.Must(outboundManager.SetHandler("tag", freedom.New(&freedom.Config{}, space, &proxy.OutboundHandlerMeta{
freedom, err := freedom.New(proxy.ContextWithOutboundMeta(ctx, &proxy.OutboundHandlerMeta{
Tag: "tag",
StreamSettings: &internet.StreamConfig{
Protocol: internet.TransportProtocol_TCP,
},
})))
}), &freedom.Config{})
assert.Error(err).IsNil()
common.Must(outboundManager.SetHandler("tag", freedom))
assert.Error(space.AddApp(new(Config))).IsNil()
proxy := OutboundProxyFromSpace(space)
+16
View File
@@ -1,6 +1,8 @@
package app
import (
"context"
"github.com/golang/protobuf/proto"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/log"
@@ -104,3 +106,17 @@ func (v *spaceImpl) AddApp(config proto.Message) error {
func (v *spaceImpl) AddAppLegacy(name string, application Application) {
v.cache[name] = application
}
type contextKey int
const (
spaceKey = contextKey(0)
)
func SpaceFromContext(ctx context.Context) Space {
return ctx.Value(spaceKey).(Space)
}
func ContextWithSpace(ctx context.Context, space Space) context.Context {
return context.WithValue(ctx, spaceKey, space)
}