From d023f68f07c236b62fdfcc873b852a6d614cb9d1 Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Mon, 5 Apr 2021 18:13:24 +0100 Subject: [PATCH] remove exported API: toContext --- context.go | 28 +++++++++++++++++++++++----- context_test.go | 6 +++++- functions.go | 6 +++--- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/context.go b/context.go index 3146689ac..b0b0f8f62 100644 --- a/context.go +++ b/context.go @@ -28,17 +28,35 @@ func MustFromContext(ctx context.Context) *Instance { return v } -// ToContext returns ctx from the given context, or creates an Instance if the context doesn't find that. -func ToContext(ctx context.Context, v *Instance) context.Context { +/* toContext returns ctx from the given context, or creates an Instance if the context doesn't find that. + +It is unsupported to use this function to create a context that is suitable to invoke V2Ray's internal component +in third party code, you shouldn't use //go:linkname to alias of this function into your own package and +use this function in your third party code. + +For third party code, usage enabled by creating a context to interact with V2Ray's internal component is unsupported, +and may break at any time. + +*/ +func toContext(ctx context.Context, v *Instance) context.Context { if FromContext(ctx) != v { ctx = context.WithValue(ctx, v2rayKey, v) } return ctx } -// MustToContext returns ctx from the given context, or panics if not found that. -func MustToContext(ctx context.Context, v *Instance) context.Context { - if c := ToContext(ctx, v); c != ctx { +/* mustToContext returns ctx from the given context, or panics if not found that. + +It is unsupported to use this function to create a context that is suitable to invoke V2Ray's internal component +in third party code, you shouldn't use //go:linkname to alias of this function into your own package and +use this function in your third party code. + +For third party code, usage enabled by creating a context to interact with V2Ray's internal component is unsupported, +and may break at any time. + +*/ +func mustToContext(ctx context.Context, v *Instance) context.Context { + if c := toContext(ctx, v); c != ctx { panic("V is not in context.") } return ctx diff --git a/context_test.go b/context_test.go index cb069fd8c..d5caa37d4 100644 --- a/context_test.go +++ b/context_test.go @@ -5,6 +5,7 @@ import ( "testing" . "github.com/v2fly/v2ray-core/v4" + _ "unsafe" ) func TestFromContextPanic(t *testing.T) { @@ -18,6 +19,9 @@ func TestFromContextPanic(t *testing.T) { MustFromContext(context.Background()) } +//go:linkname mustToContextForced github.com/v2fly/v2ray-core/v4.mustToContext +func mustToContextForced(ctx context.Context, v *Instance) context.Context + func TestToContextPanic(t *testing.T) { defer func() { r := recover() @@ -26,5 +30,5 @@ func TestToContextPanic(t *testing.T) { } }() - MustToContext(context.Background(), &Instance{}) + mustToContextForced(context.Background(), &Instance{}) } diff --git a/functions.go b/functions.go index 115df1b08..f5f750366 100644 --- a/functions.go +++ b/functions.go @@ -16,7 +16,7 @@ import ( func CreateObject(v *Instance, config interface{}) (interface{}, error) { var ctx context.Context if v != nil { - ctx = ToContext(v.ctx, v) + ctx = toContext(v.ctx, v) } return common.CreateObject(ctx, config) } @@ -47,7 +47,7 @@ func StartInstance(configFormat string, configBytes []byte) (*Instance, error) { // // v2ray:api:stable func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, error) { - ctx = ToContext(ctx, v) + ctx = toContext(ctx, v) dispatcher := v.GetFeature(routing.DispatcherType()) if dispatcher == nil { @@ -74,7 +74,7 @@ func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, err // // v2ray:api:beta func DialUDP(ctx context.Context, v *Instance) (net.PacketConn, error) { - ctx = ToContext(ctx, v) + ctx = toContext(ctx, v) dispatcher := v.GetFeature(routing.DispatcherType()) if dispatcher == nil {