Browse Source

check ctx to fix. (#841)

rurirei 4 years ago
parent
commit
44151f3420
1 changed files with 8 additions and 4 deletions
  1. 8 4
      functions.go

+ 8 - 4
functions.go

@@ -47,15 +47,15 @@ func StartInstance(configFormat string, configBytes []byte) (*Instance, error) {
 //
 // v2ray:api:stable
 func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, error) {
+	if FromContext(ctx) == nil {
+		ctx = context.WithValue(ctx, v2rayKey, v)
+	}
+
 	dispatcher := v.GetFeature(routing.DispatcherType())
 	if dispatcher == nil {
 		return nil, newError("routing.Dispatcher is not registered in V2Ray core")
 	}
 
-	if ctx.Value(v2rayKey) == nil {
-		ctx = context.WithValue(ctx, v2rayKey, v)
-	}
-
 	r, err := dispatcher.(routing.Dispatcher).Dispatch(ctx, dest)
 	if err != nil {
 		return nil, err
@@ -76,6 +76,10 @@ 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) {
+	if FromContext(ctx) == nil {
+		ctx = context.WithValue(ctx, v2rayKey, v)
+	}
+
 	dispatcher := v.GetFeature(routing.DispatcherType())
 	if dispatcher == nil {
 		return nil, newError("routing.Dispatcher is not registered in V2Ray core")