fix context leak

This commit is contained in:
Page Fault
2020-07-15 16:27:42 +00:00
parent c010e7dda1
commit 4e33a37f82
9 changed files with 57 additions and 14 deletions
+1
View File
@@ -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
}
}
+1
View File
@@ -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
}
}
+3 -5
View File
@@ -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
})
}
+2 -1
View File
@@ -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
})
}
+4 -1
View File
@@ -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
})
}
+4 -1
View File
@@ -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
})
}
+1 -2
View File
@@ -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,
+4 -4
View File
@@ -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
})
}
+37
View File
@@ -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)