mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-04 14:13:03 +00:00
Export instance context
This commit is contained in:
parent
486f96838d
commit
390a76ff61
@ -25,3 +25,10 @@ func MustFromContext(ctx context.Context) *Instance {
|
|||||||
}
|
}
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithContext(ctx context.Context, v *Instance) context.Context {
|
||||||
|
if FromContext(ctx) != v {
|
||||||
|
ctx = context.WithValue(ctx, xrayKey, v)
|
||||||
|
}
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
@ -15,7 +15,7 @@ import (
|
|||||||
func CreateObject(v *Instance, config interface{}) (interface{}, error) {
|
func CreateObject(v *Instance, config interface{}) (interface{}, error) {
|
||||||
ctx := v.ctx
|
ctx := v.ctx
|
||||||
if v != nil {
|
if v != nil {
|
||||||
ctx = context.WithValue(ctx, xrayKey, v)
|
ctx = WithContext(v.ctx, v)
|
||||||
}
|
}
|
||||||
return common.CreateObject(ctx, config)
|
return common.CreateObject(ctx, config)
|
||||||
}
|
}
|
||||||
@ -46,6 +46,8 @@ func StartInstance(configFormat string, configBytes []byte) (*Instance, error) {
|
|||||||
//
|
//
|
||||||
// xray:api:stable
|
// xray: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 = WithContext(ctx, v)
|
||||||
|
|
||||||
dispatcher := v.GetFeature(routing.DispatcherType())
|
dispatcher := v.GetFeature(routing.DispatcherType())
|
||||||
if dispatcher == nil {
|
if dispatcher == nil {
|
||||||
return nil, newError("routing.Dispatcher is not registered in Xray core")
|
return nil, newError("routing.Dispatcher is not registered in Xray core")
|
||||||
@ -70,6 +72,8 @@ func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, err
|
|||||||
//
|
//
|
||||||
// xray:api:beta
|
// xray:api:beta
|
||||||
func DialUDP(ctx context.Context, v *Instance) (net.PacketConn, error) {
|
func DialUDP(ctx context.Context, v *Instance) (net.PacketConn, error) {
|
||||||
|
ctx = WithContext(ctx, v)
|
||||||
|
|
||||||
dispatcher := v.GetFeature(routing.DispatcherType())
|
dispatcher := v.GetFeature(routing.DispatcherType())
|
||||||
if dispatcher == nil {
|
if dispatcher == nil {
|
||||||
return nil, newError("routing.Dispatcher is not registered in Xray core")
|
return nil, newError("routing.Dispatcher is not registered in Xray core")
|
||||||
|
Loading…
Reference in New Issue
Block a user