diff --git a/api/service/client.go b/api/service/client.go index 6252e64..321f40b 100644 --- a/api/service/client.go +++ b/api/service/client.go @@ -53,14 +53,17 @@ func (s *ClientAPI) GetTraffic(ctx context.Context, req *GetTrafficRequest) (*Ge } func RunClientAPI(ctx context.Context, auth statistic.Authenticator) error { + cfg := config.FromContext(ctx, Name).(*Config) + if !cfg.API.Enabled { + return nil + } server := grpc.NewServer() service := &ClientAPI{ ctx: ctx, auth: auth, } RegisterTrojanClientServiceServer(server, service) - cfg := config.FromContext(ctx, Name).(*Config) - listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.APIHost, cfg.APIPort)) + listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.API.APIHost, cfg.API.APIPort)) if err != nil { return err } diff --git a/api/service/config.go b/api/service/config.go index e8ce2ed..55cd4b9 100644 --- a/api/service/config.go +++ b/api/service/config.go @@ -11,7 +11,7 @@ type APIConfig struct { } type Config struct { - APIConfig `json,yaml:"api"` + API APIConfig `json,yaml:"api"` } func init() { diff --git a/api/service/server.go b/api/service/server.go index f6faa04..38cb2bf 100644 --- a/api/service/server.go +++ b/api/service/server.go @@ -166,13 +166,16 @@ func (s *ServerAPI) ListUsers(req *ListUsersRequest, stream TrojanServerService_ } func RunServerAPI(ctx context.Context, auth statistic.Authenticator) error { + cfg := config.FromContext(ctx, Name).(*Config) + if !cfg.API.Enabled { + return nil + } server := grpc.NewServer() service := &ServerAPI{ auth: auth, } RegisterTrojanServerServiceServer(server, service) - cfg := config.FromContext(ctx, Name).(*Config) - listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.APIHost, cfg.APIPort)) + listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.API.APIHost, cfg.API.APIPort)) if err != nil { return err } diff --git a/build/client.go b/build/client.go index 6f578cb..003e474 100644 --- a/build/client.go +++ b/build/client.go @@ -1,4 +1,4 @@ -// +build client full standard +// +build client full mini package build diff --git a/build/forward.go b/build/forward.go index 9c87964..6a7cff3 100644 --- a/build/forward.go +++ b/build/forward.go @@ -1,4 +1,4 @@ -// +build forward full standard +// +build forward full mini package build diff --git a/build/mysql.go b/build/mysql.go index 2c546f2..52c6faa 100644 --- a/build/mysql.go +++ b/build/mysql.go @@ -1,4 +1,4 @@ -// +build mysql full standard +// +build mysql full mini package build diff --git a/build/nat.go b/build/nat.go index 596f6e7..3408b42 100644 --- a/build/nat.go +++ b/build/nat.go @@ -1,4 +1,4 @@ -// +build nat full standard +// +build nat full mini package build diff --git a/build/server.go b/build/server.go index 90c1742..995da04 100644 --- a/build/server.go +++ b/build/server.go @@ -1,4 +1,4 @@ -// +build server full standard +// +build server full mini package build diff --git a/docs/content/developer/build.md b/docs/content/developer/build.md index 0d2ce45..493ad0f 100644 --- a/docs/content/developer/build.md +++ b/docs/content/developer/build.md @@ -28,12 +28,11 @@ Trojan-Go的大多数模块是可插拔的。在build文件夹下可以找到各 ```shell go build -tags "full" #编译所有模块 go build -tags "client" -ldflags="-s -w" #只有客户端功能,且去除符号表缩小体积 -go build -tags "server auth_mysql" #只有服务端和mysql支持 -go build -tags "client router" #只有客户端和路由模块 +go build -tags "server mysql" #只有服务端和mysql支持 ``` 使用full标签等价于 ```shell -go build -tags "api client router server auth_mysql auth_redis relay cert other" +go build -tags "api client server forward nat other" ``` diff --git a/go.mod b/go.mod index a720266..3f4bdd3 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/refraction-networking/utls v0.0.0-20200601200209-ada0bb9b38a0 github.com/shadowsocks/go-shadowsocks2 v0.1.0 github.com/smartystreets/goconvey v1.6.4 - github.com/xtaci/smux v1.5.15-0.20200523091831-637399ad4398 + github.com/xtaci/smux v1.5.14 golang.org/x/crypto v0.0.0-20200602180216-279210d13fed golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect @@ -19,7 +19,7 @@ require ( google.golang.org/protobuf v1.24.0 gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect gopkg.in/yaml.v2 v2.3.0 - v2ray.com/core v0.0.0-00010101000000-000000000000 + v2ray.com/core v0.0.0-20190603071532-16e9d39fff74 ) replace v2ray.com/core => github.com/v2ray/v2ray-core v0.0.0-20200603100350-6b5d2fed91c0 diff --git a/go.sum b/go.sum index a04cb20..c2ef27d 100644 --- a/go.sum +++ b/go.sum @@ -56,8 +56,8 @@ github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIK github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/v2ray/v2ray-core v0.0.0-20200603100350-6b5d2fed91c0 h1:beJRvss6cKPj/Qy8RLI/O8EKYaxaKlsXBjsXgmNqSUQ= github.com/v2ray/v2ray-core v0.0.0-20200603100350-6b5d2fed91c0/go.mod h1:6qvbJidjCnQWxyTc9SBD/cLCtN4qLs2neS/VzwSTnTY= -github.com/xtaci/smux v1.5.15-0.20200523091831-637399ad4398 h1:1nJafFt4SJPzJ5RbWBP2OUJ7Xcx7pdjyjldEdFrLfKs= -github.com/xtaci/smux v1.5.15-0.20200523091831-637399ad4398/go.mod h1:OMlQbT5vcgl2gb49mFkYo6SMf+zP3rcjcwQz7ZU7IGY= +github.com/xtaci/smux v1.5.14 h1:1j+zJYDZRv9FHaWqCJfH5RPizIm0fSzJIFbfVn8zsfg= +github.com/xtaci/smux v1.5.14/go.mod h1:OMlQbT5vcgl2gb49mFkYo6SMf+zP3rcjcwQz7ZU7IGY= go.starlark.net v0.0.0-20190919145610-979af19b165c h1:WR7X1xgXJlXhQBdorVc9Db3RhwG+J/kp6bLuMyJjfVw= go.starlark.net v0.0.0-20190919145610-979af19b165c/go.mod h1:c1/X6cHgvdXj6pUlmWKMkuqRnW4K8x2vwt6JAaaircg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/proxy/nat/nat.go b/proxy/nat/nat.go index a7e99e9..9b74bba 100644 --- a/proxy/nat/nat.go +++ b/proxy/nat/nat.go @@ -1,4 +1,4 @@ -// +build linux +// +build linux,!386 package nat diff --git a/proxy/nat/nat_stub.go b/proxy/nat/nat_stub.go index 06304a1..7aac936 100644 --- a/proxy/nat/nat_stub.go +++ b/proxy/nat/nat_stub.go @@ -1,5 +1 @@ -// +build !linux - package nat - -const Name = "NAT" diff --git a/proxy/server/server.go b/proxy/server/server.go index 48c5e93..f1417e1 100644 --- a/proxy/server/server.go +++ b/proxy/server/server.go @@ -7,6 +7,7 @@ import ( "github.com/p4gefau1t/trojan-go/proxy/client" "github.com/p4gefau1t/trojan-go/tunnel/mux" "github.com/p4gefau1t/trojan-go/tunnel/raw" + "github.com/p4gefau1t/trojan-go/tunnel/router" "github.com/p4gefau1t/trojan-go/tunnel/shadowsocks" "github.com/p4gefau1t/trojan-go/tunnel/simplesocks" "github.com/p4gefau1t/trojan-go/tunnel/transport" @@ -18,12 +19,15 @@ const Name = "SERVER" func init() { proxy.RegisterProxyCreator(Name, func(ctx context.Context) (*proxy.Proxy, error) { - clientStack := []string{raw.Name} cfg := config.FromContext(ctx, Name).(*client.Config) s, err := transport.NewServer(ctx, nil) if err != nil { return nil, err } + clientStack := []string{raw.Name} + if cfg.Router.Enabled { + clientStack = []string{raw.Name, router.Name} + } root := &proxy.Node{ Name: transport.Name, Next: make(map[string]*proxy.Node), diff --git a/tunnel/mux/client.go b/tunnel/mux/client.go index e0f303f..d87d412 100644 --- a/tunnel/mux/client.go +++ b/tunnel/mux/client.go @@ -108,7 +108,7 @@ func (c *Client) newMuxClient() (*smuxClientInfo, error) { conn = newStickyConn(conn) smuxConfig := smux.DefaultConfig() - smuxConfig.KeepAliveDisabled = true + //smuxConfig.KeepAliveDisabled = true client, err := smux.Client(conn, smuxConfig) info := &smuxClientInfo{ client: client, @@ -129,6 +129,9 @@ func (c *Client) DialConn(addr *tunnel.Address, _ tunnel.Tunnel) (tunnel.Conn, e rwc, err := info.client.Open() info.lastActiveTime = time.Now() if err != nil { + c.clientPoolLock.Lock() + defer c.clientPoolLock.Unlock() + delete(c.clientPool, info.id) return nil, common.NewError("mux failed to open stream from client").Base(err) } return &Conn{ diff --git a/tunnel/mux/conn.go b/tunnel/mux/conn.go index fd02596..cf852d3 100644 --- a/tunnel/mux/conn.go +++ b/tunnel/mux/conn.go @@ -66,7 +66,7 @@ func (c *stickyConn) Write(p []byte) (int, error) { return 8, nil } } else { - log.Debug("Unknown 8 bytes header") + log.Debug("other 8 bytes header") } } _, err := c.Conn.Write(c.stickToPayload(p)) diff --git a/tunnel/mux/server.go b/tunnel/mux/server.go index b3ae6a0..871b1cf 100644 --- a/tunnel/mux/server.go +++ b/tunnel/mux/server.go @@ -31,7 +31,7 @@ func (s *Server) acceptConnWorker() { continue } smuxConfig := smux.DefaultConfig() - smuxConfig.KeepAliveDisabled = true + //smuxConfig.KeepAliveDisabled = true smuxSession, err := smux.Server(conn, smuxConfig) if err != nil { s.errChan <- err diff --git a/tunnel/raw/client.go b/tunnel/raw/client.go index 3337d86..a413df2 100644 --- a/tunnel/raw/client.go +++ b/tunnel/raw/client.go @@ -49,7 +49,8 @@ func (c *Client) Close() error { return nil } -func NewFreeClient(ctx context.Context, client tunnel.Client) (*Client, error) { +func NewClient(ctx context.Context, client tunnel.Client) (*Client, error) { + // TODO implement dns cfg := config.FromContext(ctx, Name).(*Config) return &Client{ noDelay: cfg.TCP.NoDelay, diff --git a/tunnel/raw/tunnel.go b/tunnel/raw/tunnel.go index 83cbecb..f328289 100644 --- a/tunnel/raw/tunnel.go +++ b/tunnel/raw/tunnel.go @@ -16,13 +16,11 @@ func (*Tunnel) Name() string { return Name } -// NewClient creates a raw client -func (*Tunnel) NewClient(ctx context.Context, _ tunnel.Client) (tunnel.Client, error) { - return &Client{}, nil +func (*Tunnel) NewClient(ctx context.Context, client tunnel.Client) (tunnel.Client, error) { + return NewClient(ctx, client) } -// NewServer creates a raw server, which is used by "Forward" -func (*Tunnel) NewServer(ctx context.Context, _ tunnel.Server) (tunnel.Server, error) { +func (*Tunnel) NewServer(ctx context.Context, client tunnel.Server) (tunnel.Server, error) { serverConfig := config.FromContext(ctx, Name).(*Config) addr := tunnel.NewAddressFromHostPort("tcp", serverConfig.LocalHost, serverConfig.LocalPort) diff --git a/tunnel/router/client.go b/tunnel/router/client.go index c4b5194..49fbcc6 100644 --- a/tunnel/router/client.go +++ b/tunnel/router/client.go @@ -176,6 +176,7 @@ func (c *Client) DialConn(address *tunnel.Address, overlay tunnel.Tunnel) (tunne case Block: return nil, common.NewError("router blocked address: " + address.String()) case Bypass: + // TODO use raw.Client conn, err := net.Dial("tcp", address.String()) if err != nil { return nil, common.NewError("router dial error").Base(err) @@ -188,6 +189,7 @@ func (c *Client) DialConn(address *tunnel.Address, overlay tunnel.Tunnel) (tunne } func (c *Client) DialPacket(overlay tunnel.Tunnel) (tunnel.PacketConn, error) { + // TODO use raw.Client direct, err := net.ListenPacket("udp", "") if err != nil { return nil, common.NewError("router failed to dial udp (direct)").Base(err) diff --git a/tunnel/tproxy/config.go b/tunnel/tproxy/config.go index f1db318..619ff66 100644 --- a/tunnel/tproxy/config.go +++ b/tunnel/tproxy/config.go @@ -1,4 +1,4 @@ -// +build linux +// +build linux,!386 package tproxy diff --git a/tunnel/tproxy/conn.go b/tunnel/tproxy/conn.go index aeec74d..e7254d2 100644 --- a/tunnel/tproxy/conn.go +++ b/tunnel/tproxy/conn.go @@ -1,4 +1,4 @@ -// +build linux +// +build linux,!386 package tproxy diff --git a/tunnel/tproxy/server.go b/tunnel/tproxy/server.go index b216ece..c827487 100644 --- a/tunnel/tproxy/server.go +++ b/tunnel/tproxy/server.go @@ -1,4 +1,4 @@ -// +build linux +// +build linux,!386 package tproxy diff --git a/tunnel/tproxy/tproxy.go b/tunnel/tproxy/tproxy.go index 6d60717..aeef650 100644 --- a/tunnel/tproxy/tproxy.go +++ b/tunnel/tproxy/tproxy.go @@ -1,4 +1,4 @@ -// +build linux +// +build linux,!386 package tproxy diff --git a/tunnel/tproxy/tproxy_stub.go b/tunnel/tproxy/tproxy_stub.go index 6cd32ee..28ce431 100644 --- a/tunnel/tproxy/tproxy_stub.go +++ b/tunnel/tproxy/tproxy_stub.go @@ -1,3 +1 @@ -// +build !linux - package tproxy diff --git a/tunnel/tproxy/tunnel.go b/tunnel/tproxy/tunnel.go index bc51978..c1c8932 100644 --- a/tunnel/tproxy/tunnel.go +++ b/tunnel/tproxy/tunnel.go @@ -1,4 +1,4 @@ -// +build linux +// +build linux,!386 package tproxy