mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-01 16:18:24 +00:00
leverage global object creator in proxies.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user