diff --git a/api/service/client.go b/api/service/client.go index 7de0607..7b7bbed 100644 --- a/api/service/client.go +++ b/api/service/client.go @@ -88,6 +88,7 @@ func RunClientAPI(ctx context.Context, auth statistic.Authenticator) error { case err := <-errChan: return err case <-ctx.Done(): + log.Debug("closed") return nil } } diff --git a/api/service/server.go b/api/service/server.go index 8f51c7a..8758982 100644 --- a/api/service/server.go +++ b/api/service/server.go @@ -250,6 +250,7 @@ func RunServerAPI(ctx context.Context, auth statistic.Authenticator) error { case err := <-errChan: return err case <-ctx.Done(): + log.Debug("closed") return nil } } diff --git a/proxy/client/client.go b/proxy/client/client.go index bf16d82..51c9f96 100644 --- a/proxy/client/client.go +++ b/proxy/client/client.go @@ -45,11 +45,11 @@ func GenerateClientTree(transportPlugin bool, muxEnabled bool, wsEnabled bool, s func init() { proxy.RegisterProxyCreator(Name, func(ctx context.Context) (*proxy.Proxy, error) { cfg := config.FromContext(ctx, Name).(*Config) - adapterServer, err := adapter.NewServer(ctx, nil) if err != nil { return nil, err } + ctx, cancel := context.WithCancel(ctx) root := &proxy.Node{ Name: adapter.Name, @@ -65,12 +65,10 @@ func init() { clientStack := GenerateClientTree(cfg.TransportPlugin.Enabled, cfg.Mux.Enabled, cfg.Websocket.Enabled, cfg.Shadowsocks.Enabled, cfg.Router.Enabled) c, err := proxy.CreateClientStack(ctx, clientStack) if err != nil { + cancel() return nil, err } s := proxy.FindAllEndpoints(root) - if err != nil { - return nil, err - } - return proxy.NewProxy(ctx, s, c), nil + return proxy.NewProxy(ctx, cancel, s, c), nil }) } diff --git a/proxy/custom/custom.go b/proxy/custom/custom.go index 8f015eb..c31d74a 100644 --- a/proxy/custom/custom.go +++ b/proxy/custom/custom.go @@ -54,6 +54,7 @@ func init() { proxy.RegisterProxyCreator(Name, func(ctx context.Context) (*proxy.Proxy, error) { cfg := config.FromContext(ctx, Name).(*Config) + ctx, cancel := context.WithCancel(ctx) // inbound nodes, err := buildNodes(ctx, cfg.Inbound.Node) if err != nil { @@ -118,6 +119,6 @@ func init() { return nil, common.NewError("failed to create client").Base(err) } } - return proxy.NewProxy(ctx, servers, client), nil + return proxy.NewProxy(ctx, cancel, servers, client), nil }) } diff --git a/proxy/forward/forward.go b/proxy/forward/forward.go index a48420e..3776509 100644 --- a/proxy/forward/forward.go +++ b/proxy/forward/forward.go @@ -15,17 +15,20 @@ const Name = "FORWARD" func init() { proxy.RegisterProxyCreator(Name, func(ctx context.Context) (*proxy.Proxy, error) { cfg := config.FromContext(ctx, Name).(*client.Config) + ctx, cancel := context.WithCancel(ctx) serverStack := []string{dokodemo.Name} clientStack := client.GenerateClientTree(cfg.TransportPlugin.Enabled, cfg.Mux.Enabled, cfg.Websocket.Enabled, cfg.Shadowsocks.Enabled, cfg.Router.Enabled) c, err := proxy.CreateClientStack(ctx, clientStack) if err != nil { + cancel() return nil, err } s, err := proxy.CreateServerStack(ctx, serverStack) if err != nil { + cancel() return nil, err } - return proxy.NewProxy(ctx, []tunnel.Server{s}, c), nil + return proxy.NewProxy(ctx, cancel, []tunnel.Server{s}, c), nil }) } diff --git a/proxy/nat/nat.go b/proxy/nat/nat.go index 61cef16..c03171e 100644 --- a/proxy/nat/nat.go +++ b/proxy/nat/nat.go @@ -17,17 +17,20 @@ const Name = "NAT" func init() { proxy.RegisterProxyCreator(Name, func(ctx context.Context) (*proxy.Proxy, error) { cfg := config.FromContext(ctx, Name).(*client.Config) + ctx, cancel := context.WithCancel(ctx) serverStack := []string{tproxy.Name} clientStack := client.GenerateClientTree(cfg.TransportPlugin.Enabled, cfg.Mux.Enabled, cfg.Websocket.Enabled, cfg.Shadowsocks.Enabled, cfg.Router.Enabled) c, err := proxy.CreateClientStack(ctx, clientStack) if err != nil { + cancel() return nil, err } s, err := proxy.CreateServerStack(ctx, serverStack) if err != nil { + cancel() return nil, err } - return proxy.NewProxy(ctx, []tunnel.Server{s}, c), nil + return proxy.NewProxy(ctx, cancel, []tunnel.Server{s}, c), nil }) } diff --git a/proxy/proxy.go b/proxy/proxy.go index e754be8..631bcda 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -151,8 +151,7 @@ func (p *Proxy) relayPacketLoop() { } } -func NewProxy(ctx context.Context, sources []tunnel.Server, sink tunnel.Client) *Proxy { - ctx, cancel := context.WithCancel(ctx) +func NewProxy(ctx context.Context, cancel context.CancelFunc, sources []tunnel.Server, sink tunnel.Client) *Proxy { return &Proxy{ sources: sources, sink: sink, diff --git a/proxy/server/server.go b/proxy/server/server.go index 83c7d75..baa5366 100644 --- a/proxy/server/server.go +++ b/proxy/server/server.go @@ -22,8 +22,10 @@ const Name = "SERVER" func init() { proxy.RegisterProxyCreator(Name, func(ctx context.Context) (*proxy.Proxy, error) { cfg := config.FromContext(ctx, Name).(*client.Config) + ctx, cancel := context.WithCancel(ctx) transportServer, err := transport.NewServer(ctx, nil) if err != nil { + cancel() return nil, err } clientStack := []string{freedom.Name} @@ -60,12 +62,10 @@ func init() { serverList := proxy.FindAllEndpoints(root) clientList, err := proxy.CreateClientStack(ctx, clientStack) if err != nil { + cancel() return nil, err } - if err != nil { - return nil, err - } - return proxy.NewProxy(ctx, serverList, clientList), nil + return proxy.NewProxy(ctx, cancel, serverList, clientList), nil }) } diff --git a/test/scenario/proxy_test.go b/test/scenario/proxy_test.go index 264d519..c554eb2 100644 --- a/test/scenario/proxy_test.go +++ b/test/scenario/proxy_test.go @@ -14,6 +14,8 @@ import ( _ "net/http/pprof" + _ "github.com/p4gefau1t/trojan-go/api" + _ "github.com/p4gefau1t/trojan-go/api/service" "github.com/p4gefau1t/trojan-go/common" _ "github.com/p4gefau1t/trojan-go/log/golog" "github.com/p4gefau1t/trojan-go/proxy" @@ -426,6 +428,41 @@ shadowsocks: } } +func TestLeak(t *testing.T) { + serverPort := common.PickPort("tcp", "127.0.0.1") + socksPort := common.PickPort("tcp", "127.0.0.1") + clientData := fmt.Sprintf(` +run-type: client +local-addr: 127.0.0.1 +local-port: %d +remote-addr: 127.0.0.1 +remote-port: %d +log-level: 0 +password: + - password +ssl: + verify: false + fingerprint: firefox + sni: localhost +shadowsocks: + enabled: true + method: AEAD_CHACHA20_POLY1305 + password: 12345678 +mux: + enabled: true +api: + enabled: true + api-port: 0 +`, socksPort, serverPort) + client, err := proxy.NewProxyFromConfigData([]byte(clientData), false) + common.Must(err) + go client.Run() + time.Sleep(time.Second * 3) + client.Close() + time.Sleep(time.Second * 3) + //http.ListenAndServe("localhost:6060", nil) +} + func SingleThreadBenchmark(clientData, serverData string, socksPort int) { server, err := proxy.NewProxyFromConfigData([]byte(clientData), false) common.Must(err)