mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-07-05 16:38:17 -04:00
remove exported API: toContext
This commit is contained in:
parent
7731405389
commit
d023f68f07
28
context.go
28
context.go
@ -28,17 +28,35 @@ func MustFromContext(ctx context.Context) *Instance {
|
|||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToContext returns ctx from the given context, or creates an Instance if the context doesn't find that.
|
/* 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 {
|
|
||||||
|
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 {
|
if FromContext(ctx) != v {
|
||||||
ctx = context.WithValue(ctx, v2rayKey, v)
|
ctx = context.WithValue(ctx, v2rayKey, v)
|
||||||
}
|
}
|
||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustToContext returns ctx from the given context, or panics if not found that.
|
/* 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 {
|
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.")
|
panic("V is not in context.")
|
||||||
}
|
}
|
||||||
return ctx
|
return ctx
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
. "github.com/v2fly/v2ray-core/v4"
|
. "github.com/v2fly/v2ray-core/v4"
|
||||||
|
_ "unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFromContextPanic(t *testing.T) {
|
func TestFromContextPanic(t *testing.T) {
|
||||||
@ -18,6 +19,9 @@ func TestFromContextPanic(t *testing.T) {
|
|||||||
MustFromContext(context.Background())
|
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) {
|
func TestToContextPanic(t *testing.T) {
|
||||||
defer func() {
|
defer func() {
|
||||||
r := recover()
|
r := recover()
|
||||||
@ -26,5 +30,5 @@ func TestToContextPanic(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
MustToContext(context.Background(), &Instance{})
|
mustToContextForced(context.Background(), &Instance{})
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ import (
|
|||||||
func CreateObject(v *Instance, config interface{}) (interface{}, error) {
|
func CreateObject(v *Instance, config interface{}) (interface{}, error) {
|
||||||
var ctx context.Context
|
var ctx context.Context
|
||||||
if v != nil {
|
if v != nil {
|
||||||
ctx = ToContext(v.ctx, v)
|
ctx = toContext(v.ctx, v)
|
||||||
}
|
}
|
||||||
return common.CreateObject(ctx, config)
|
return common.CreateObject(ctx, config)
|
||||||
}
|
}
|
||||||
@ -47,7 +47,7 @@ func StartInstance(configFormat string, configBytes []byte) (*Instance, error) {
|
|||||||
//
|
//
|
||||||
// v2ray:api:stable
|
// v2ray:api:stable
|
||||||
func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, error) {
|
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())
|
dispatcher := v.GetFeature(routing.DispatcherType())
|
||||||
if dispatcher == nil {
|
if dispatcher == nil {
|
||||||
@ -74,7 +74,7 @@ func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, err
|
|||||||
//
|
//
|
||||||
// v2ray:api:beta
|
// v2ray:api:beta
|
||||||
func DialUDP(ctx context.Context, v *Instance) (net.PacketConn, error) {
|
func DialUDP(ctx context.Context, v *Instance) (net.PacketConn, error) {
|
||||||
ctx = ToContext(ctx, v)
|
ctx = toContext(ctx, v)
|
||||||
|
|
||||||
dispatcher := v.GetFeature(routing.DispatcherType())
|
dispatcher := v.GetFeature(routing.DispatcherType())
|
||||||
if dispatcher == nil {
|
if dispatcher == nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user