mirror of
https://github.com/p4gefau1t/trojan-go.git
synced 2024-04-21 12:21:34 +00:00
fix server clash when accepting too many connections
This commit is contained in:
@@ -90,7 +90,7 @@ weight: 30
|
||||
"enabled": false,
|
||||
"type": "",
|
||||
"command": "",
|
||||
"plugin_option": "",
|
||||
"option": "",
|
||||
"arg": [],
|
||||
"env": []
|
||||
},
|
||||
|
||||
@@ -55,6 +55,12 @@ func init() {
|
||||
cfg := config.FromContext(ctx, Name).(*Config)
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
success := false
|
||||
defer func() {
|
||||
if !success {
|
||||
cancel()
|
||||
}
|
||||
}()
|
||||
// inbound
|
||||
nodes, err := buildNodes(ctx, cfg.Inbound.Node)
|
||||
if err != nil {
|
||||
@@ -119,6 +125,8 @@ func init() {
|
||||
return nil, common.NewError("failed to create client").Base(err)
|
||||
}
|
||||
}
|
||||
|
||||
success = true
|
||||
return proxy.NewProxy(ctx, cancel, servers, client), nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -83,13 +83,13 @@ func TestMemoryAuth(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
time.Sleep(time.Second * 4)
|
||||
if sent, recv := user.GetSpeed(); sent > 200 || sent < 180 || recv > 100 || recv < 90 {
|
||||
if sent, recv := user.GetSpeed(); sent > 300 || sent < 100 || recv > 150 || recv < 50 {
|
||||
t.Fatal("GetSpeed", sent, recv)
|
||||
}
|
||||
|
||||
user.SetSpeedLimit(30, 20)
|
||||
time.Sleep(time.Second * 4)
|
||||
if sent, recv := user.GetSpeed(); sent > 30 || recv > 20 {
|
||||
if sent, recv := user.GetSpeed(); sent > 45 || recv > 30 {
|
||||
t.Fatal("SetSpeedLimit", sent, recv)
|
||||
}
|
||||
|
||||
|
||||
@@ -49,8 +49,6 @@ func (c *Client) DialConn(*tunnel.Address, tunnel.Tunnel) (tunnel.Conn, error) {
|
||||
func NewClient(ctx context.Context, _ tunnel.Client) (*Client, error) {
|
||||
cfg := config.FromContext(ctx, Name).(*Config)
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
|
||||
var cmd *exec.Cmd
|
||||
serverAddress := tunnel.NewAddressFromHostPort("tcp", cfg.RemoteHost, cfg.RemotePort)
|
||||
|
||||
@@ -66,7 +64,7 @@ func NewClient(ctx context.Context, _ tunnel.Client) (*Client, error) {
|
||||
"SS_LOCAL_PORT="+strconv.FormatInt(int64(pluginPort), 10),
|
||||
"SS_REMOTE_HOST="+cfg.RemoteHost,
|
||||
"SS_REMOTE_PORT="+strconv.FormatInt(int64(cfg.RemotePort), 10),
|
||||
"SS_PLUGIN_OPTIONS="+cfg.TransportPlugin.PluginOption,
|
||||
"SS_PLUGIN_OPTIONS="+cfg.TransportPlugin.Option,
|
||||
)
|
||||
cfg.RemoteHost = pluginHost
|
||||
cfg.RemotePort = pluginPort
|
||||
@@ -91,6 +89,8 @@ func NewClient(ctx context.Context, _ tunnel.Client) (*Client, error) {
|
||||
return nil, common.NewError("invalid plugin type: " + cfg.TransportPlugin.Type)
|
||||
}
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
client := &Client{
|
||||
serverAddress: serverAddress,
|
||||
cmd: cmd,
|
||||
|
||||
@@ -13,12 +13,12 @@ type Config struct {
|
||||
}
|
||||
|
||||
type TransportPluginConfig struct {
|
||||
Enabled bool `json:"enabled" yaml:"enabled"`
|
||||
Type string `json:"type" yaml:"type"`
|
||||
Command string `json:"command" yaml:"command"`
|
||||
PluginOption string `json:"plugin_option" yaml:"plugin-option"`
|
||||
Arg []string `json:"arg" yaml:"arg"`
|
||||
Env []string `json:"env" yaml:"env"`
|
||||
Enabled bool `json:"enabled" yaml:"enabled"`
|
||||
Type string `json:"type" yaml:"type"`
|
||||
Command string `json:"command" yaml:"command"`
|
||||
Option string `json:"option" yaml:"option"`
|
||||
Arg []string `json:"arg" yaml:"arg"`
|
||||
Env []string `json:"env" yaml:"env"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/p4gefau1t/trojan-go/common"
|
||||
"github.com/p4gefau1t/trojan-go/config"
|
||||
@@ -41,7 +42,8 @@ func (s *Server) acceptLoop() {
|
||||
select {
|
||||
case <-s.ctx.Done():
|
||||
default:
|
||||
log.Fatal(common.NewError("transport accept error"))
|
||||
log.Error(common.NewError("transport accept error").Base(err))
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -105,7 +107,6 @@ func (s *Server) AcceptPacket(tunnel.Tunnel) (tunnel.PacketConn, error) {
|
||||
// NewServer creates a transport layer server
|
||||
func NewServer(ctx context.Context, _ tunnel.Server) (*Server, error) {
|
||||
cfg := config.FromContext(ctx, Name).(*Config)
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
listenAddress := tunnel.NewAddressFromHostPort("tcp", cfg.LocalHost, cfg.LocalPort)
|
||||
|
||||
var cmd *exec.Cmd
|
||||
@@ -121,7 +122,7 @@ func NewServer(ctx context.Context, _ tunnel.Server) (*Server, error) {
|
||||
"SS_REMOTE_PORT="+strconv.FormatInt(int64(cfg.LocalPort), 10),
|
||||
"SS_LOCAL_HOST="+trojanHost,
|
||||
"SS_LOCAL_PORT="+strconv.FormatInt(int64(trojanPort), 10),
|
||||
"SS_PLUGIN_OPTIONS="+cfg.TransportPlugin.PluginOption,
|
||||
"SS_PLUGIN_OPTIONS="+cfg.TransportPlugin.Option,
|
||||
)
|
||||
|
||||
cfg.LocalHost = trojanHost
|
||||
@@ -151,6 +152,8 @@ func NewServer(ctx context.Context, _ tunnel.Server) (*Server, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
server := &Server{
|
||||
tcpListener: tcpListener,
|
||||
cmd: cmd,
|
||||
|
||||
@@ -62,12 +62,12 @@ func TestClientPlugin(t *testing.T) {
|
||||
RemoteHost: "127.0.0.1",
|
||||
RemotePort: 12345,
|
||||
TransportPlugin: TransportPluginConfig{
|
||||
Enabled: true,
|
||||
Type: "shadowsocks",
|
||||
Command: "echo $SS_REMOTE_PORT",
|
||||
PluginOption: "",
|
||||
Arg: nil,
|
||||
Env: nil,
|
||||
Enabled: true,
|
||||
Type: "shadowsocks",
|
||||
Command: "echo $SS_REMOTE_PORT",
|
||||
Option: "",
|
||||
Arg: nil,
|
||||
Env: nil,
|
||||
},
|
||||
}
|
||||
ctx := config.WithConfig(context.Background(), Name, clientCfg)
|
||||
@@ -83,12 +83,12 @@ func TestServerPlugin(t *testing.T) {
|
||||
RemoteHost: "127.0.0.1",
|
||||
RemotePort: 12345,
|
||||
TransportPlugin: TransportPluginConfig{
|
||||
Enabled: true,
|
||||
Type: "shadowsocks",
|
||||
Command: "echo $SS_REMOTE_PORT",
|
||||
PluginOption: "",
|
||||
Arg: nil,
|
||||
Env: nil,
|
||||
Enabled: true,
|
||||
Type: "shadowsocks",
|
||||
Command: "echo $SS_REMOTE_PORT",
|
||||
Option: "",
|
||||
Arg: nil,
|
||||
Env: nil,
|
||||
},
|
||||
}
|
||||
ctx := config.WithConfig(context.Background(), Name, cfg)
|
||||
|
||||
Reference in New Issue
Block a user