1
0
Fork 0
mirror of https://github.com/XTLS/Xray-core.git synced 2025-06-07 11:58:41 +00:00

Fix context api and document ()

* Fix Dial function crash instance when there is no instance context in the ctx

* check ctx to fix. ()

* Feat: core.ToContext(ctx, v) for ctx initialization ()

* remove exported API: toContext

* Remove unnecessary API

* rework document for API

* fix: make sure the ctx is propagated to connections by detached connection

Co-authored-by: Shelikhoo <xiaokangwang@outlook.com>
Co-authored-by: rurirei <72071920+rurirei@users.noreply.github.com>
This commit is contained in:
yuhan6665 2022-02-19 22:45:41 -05:00 committed by GitHub
parent cf1ee095a2
commit 496b2c02c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 3 deletions

View file

@ -15,7 +15,7 @@ import (
func CreateObject(v *Instance, config interface{}) (interface{}, error) {
ctx := v.ctx
if v != nil {
ctx = context.WithValue(ctx, xrayKey, v)
ctx = toContext(v.ctx, v)
}
return common.CreateObject(ctx, config)
}
@ -46,10 +46,13 @@ func StartInstance(configFormat string, configBytes []byte) (*Instance, error) {
//
// xray:api:stable
func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, error) {
ctx = toContext(ctx, v)
dispatcher := v.GetFeature(routing.DispatcherType())
if dispatcher == nil {
return nil, newError("routing.Dispatcher is not registered in Xray core")
}
r, err := dispatcher.(routing.Dispatcher).Dispatch(ctx, dest)
if err != nil {
return nil, err
@ -70,6 +73,8 @@ func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, err
//
// xray:api:beta
func DialUDP(ctx context.Context, v *Instance) (net.PacketConn, error) {
ctx = toContext(ctx, v)
dispatcher := v.GetFeature(routing.DispatcherType())
if dispatcher == nil {
return nil, newError("routing.Dispatcher is not registered in Xray core")