From 85bd352bc1e21ca17b799093511ad84c846f728e Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Thu, 18 Jun 2020 12:37:10 +0800 Subject: [PATCH] propagate context --- app/proxyman/inbound/always.go | 1 + app/proxyman/inbound/dynamic.go | 4 ++++ app/proxyman/inbound/worker.go | 4 +++- functions.go | 2 +- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/proxyman/inbound/always.go b/app/proxyman/inbound/always.go index e67603576..f5fa108d2 100644 --- a/app/proxyman/inbound/always.go +++ b/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) } diff --git a/app/proxyman/inbound/dynamic.go b/app/proxyman/inbound/dynamic.go index 36e4b50b3..3dbd89abe 100644 --- a/app/proxyman/inbound/dynamic.go +++ b/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() diff --git a/app/proxyman/inbound/worker.go b/app/proxyman/inbound/worker.go index d2a9bc027..54a717bf8 100644 --- a/app/proxyman/inbound/worker.go +++ b/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) diff --git a/functions.go b/functions.go index 2ea4aa456..ee4fab81e 100644 --- a/functions.go +++ b/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) }