Browse Source

propagate context

Shelikhoo 5 years ago
parent
commit
85bd352bc1

+ 1 - 0
app/proxyman/inbound/always.go

@@ -103,6 +103,7 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *
 				sniffingConfig:  receiverConfig.GetEffectiveSniffingSettings(),
 				uplinkCounter:   uplinkCounter,
 				downlinkCounter: downlinkCounter,
+				ctx:             ctx,
 			}
 			h.workers = append(h.workers, worker)
 		}

+ 4 - 0
app/proxyman/inbound/dynamic.go

@@ -28,6 +28,8 @@ type DynamicInboundHandler struct {
 	lastRefresh    time.Time
 	mux            *mux.Server
 	task           *task.Periodic
+
+	ctx context.Context
 }
 
 func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*DynamicInboundHandler, error) {
@@ -39,6 +41,7 @@ func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *p
 		portsInUse:     make(map[net.Port]bool),
 		mux:            mux.NewServer(ctx),
 		v:              v,
+		ctx:            ctx,
 	}
 
 	mss, err := internet.ToMemoryStreamConfig(receiverConfig.StreamSettings)
@@ -134,6 +137,7 @@ func (h *DynamicInboundHandler) refresh() error {
 				sniffingConfig:  h.receiverConfig.GetEffectiveSniffingSettings(),
 				uplinkCounter:   uplinkCounter,
 				downlinkCounter: downlinkCounter,
+				ctx:             h.ctx,
 			}
 			if err := worker.Start(); err != nil {
 				newError("failed to create TCP worker").Base(err).AtWarning().WriteToLog()

+ 3 - 1
app/proxyman/inbound/worker.go

@@ -43,6 +43,8 @@ type tcpWorker struct {
 	downlinkCounter stats.Counter
 
 	hub internet.Listener
+
+	ctx context.Context
 }
 
 func getTProxyType(s *internet.MemoryStreamConfig) internet.SocketConfig_TProxyMode {
@@ -53,7 +55,7 @@ func getTProxyType(s *internet.MemoryStreamConfig) internet.SocketConfig_TProxyM
 }
 
 func (w *tcpWorker) callback(conn internet.Connection) {
-	ctx, cancel := context.WithCancel(context.Background())
+	ctx, cancel := context.WithCancel(w.ctx)
 	sid := session.NewID()
 	ctx = session.ContextWithID(ctx, sid)
 

+ 1 - 1
functions.go

@@ -14,7 +14,7 @@ import (
 
 // CreateObject creates a new object based on the given V2Ray instance and config. The V2Ray instance may be nil.
 func CreateObject(v *Instance, config interface{}) (interface{}, error) {
-	ctx := context.Background()
+	ctx := v.ctx
 	if v != nil {
 		ctx = context.WithValue(ctx, v2rayKey, v)
 	}