remove exported API: toContext

This commit is contained in:
Shelikhoo 2021-04-05 18:13:24 +01:00
parent 7731405389
commit d023f68f07
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316
3 changed files with 31 additions and 9 deletions

View File

@ -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

View File

@ -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{})
}

View File

@ -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 {