Skip to content

Commit e06c06d

Browse files
committed
fix api, smux, nat
1 parent 4a33464 commit e06c06d

26 files changed

+46
-39
lines changed

Diff for: api/service/client.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,17 @@ func (s *ClientAPI) GetTraffic(ctx context.Context, req *GetTrafficRequest) (*Ge
5353
}
5454

5555
func RunClientAPI(ctx context.Context, auth statistic.Authenticator) error {
56+
cfg := config.FromContext(ctx, Name).(*Config)
57+
if !cfg.API.Enabled {
58+
return nil
59+
}
5660
server := grpc.NewServer()
5761
service := &ClientAPI{
5862
ctx: ctx,
5963
auth: auth,
6064
}
6165
RegisterTrojanClientServiceServer(server, service)
62-
cfg := config.FromContext(ctx, Name).(*Config)
63-
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.APIHost, cfg.APIPort))
66+
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.API.APIHost, cfg.API.APIPort))
6467
if err != nil {
6568
return err
6669
}

Diff for: api/service/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type APIConfig struct {
1111
}
1212

1313
type Config struct {
14-
APIConfig `json,yaml:"api"`
14+
API APIConfig `json,yaml:"api"`
1515
}
1616

1717
func init() {

Diff for: api/service/server.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,16 @@ func (s *ServerAPI) ListUsers(req *ListUsersRequest, stream TrojanServerService_
166166
}
167167

168168
func RunServerAPI(ctx context.Context, auth statistic.Authenticator) error {
169+
cfg := config.FromContext(ctx, Name).(*Config)
170+
if !cfg.API.Enabled {
171+
return nil
172+
}
169173
server := grpc.NewServer()
170174
service := &ServerAPI{
171175
auth: auth,
172176
}
173177
RegisterTrojanServerServiceServer(server, service)
174-
cfg := config.FromContext(ctx, Name).(*Config)
175-
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.APIHost, cfg.APIPort))
178+
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.API.APIHost, cfg.API.APIPort))
176179
if err != nil {
177180
return err
178181
}

Diff for: build/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build client full standard
1+
// +build client full mini
22

33
package build
44

Diff for: build/forward.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build forward full standard
1+
// +build forward full mini
22

33
package build
44

Diff for: build/mysql.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build mysql full standard
1+
// +build mysql full mini
22

33
package build
44

Diff for: build/nat.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build nat full standard
1+
// +build nat full mini
22

33
package build
44

Diff for: build/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build server full standard
1+
// +build server full mini
22

33
package build
44

Diff for: docs/content/developer/build.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ Trojan-Go的大多数模块是可插拔的。在build文件夹下可以找到各
2828
```shell
2929
go build -tags "full" #编译所有模块
3030
go build -tags "client" -ldflags="-s -w" #只有客户端功能,且去除符号表缩小体积
31-
go build -tags "server auth_mysql" #只有服务端和mysql支持
32-
go build -tags "client router" #只有客户端和路由模块
31+
go build -tags "server mysql" #只有服务端和mysql支持
3332
```
3433

3534
使用full标签等价于
3635

3736
```shell
38-
go build -tags "api client router server auth_mysql auth_redis relay cert other"
37+
go build -tags "api client server forward nat other"
3938
```

Diff for: go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/refraction-networking/utls v0.0.0-20200601200209-ada0bb9b38a0
1111
github.com/shadowsocks/go-shadowsocks2 v0.1.0
1212
github.com/smartystreets/goconvey v1.6.4
13-
github.com/xtaci/smux v1.5.15-0.20200523091831-637399ad4398
13+
github.com/xtaci/smux v1.5.14
1414
golang.org/x/crypto v0.0.0-20200602180216-279210d13fed
1515
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3
1616
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
@@ -19,7 +19,7 @@ require (
1919
google.golang.org/protobuf v1.24.0
2020
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
2121
gopkg.in/yaml.v2 v2.3.0
22-
v2ray.com/core v0.0.0-00010101000000-000000000000
22+
v2ray.com/core v0.0.0-20190603071532-16e9d39fff74
2323
)
2424

2525
replace v2ray.com/core => github.com/v2ray/v2ray-core v0.0.0-20200603100350-6b5d2fed91c0

Diff for: go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIK
5656
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
5757
github.com/v2ray/v2ray-core v0.0.0-20200603100350-6b5d2fed91c0 h1:beJRvss6cKPj/Qy8RLI/O8EKYaxaKlsXBjsXgmNqSUQ=
5858
github.com/v2ray/v2ray-core v0.0.0-20200603100350-6b5d2fed91c0/go.mod h1:6qvbJidjCnQWxyTc9SBD/cLCtN4qLs2neS/VzwSTnTY=
59-
github.com/xtaci/smux v1.5.15-0.20200523091831-637399ad4398 h1:1nJafFt4SJPzJ5RbWBP2OUJ7Xcx7pdjyjldEdFrLfKs=
60-
github.com/xtaci/smux v1.5.15-0.20200523091831-637399ad4398/go.mod h1:OMlQbT5vcgl2gb49mFkYo6SMf+zP3rcjcwQz7ZU7IGY=
59+
github.com/xtaci/smux v1.5.14 h1:1j+zJYDZRv9FHaWqCJfH5RPizIm0fSzJIFbfVn8zsfg=
60+
github.com/xtaci/smux v1.5.14/go.mod h1:OMlQbT5vcgl2gb49mFkYo6SMf+zP3rcjcwQz7ZU7IGY=
6161
go.starlark.net v0.0.0-20190919145610-979af19b165c h1:WR7X1xgXJlXhQBdorVc9Db3RhwG+J/kp6bLuMyJjfVw=
6262
go.starlark.net v0.0.0-20190919145610-979af19b165c/go.mod h1:c1/X6cHgvdXj6pUlmWKMkuqRnW4K8x2vwt6JAaaircg=
6363
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

Diff for: proxy/nat/nat.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build linux
1+
// +build linux,!386
22

33
package nat
44

Diff for: proxy/nat/nat_stub.go

-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
// +build !linux
2-
31
package nat
4-
5-
const Name = "NAT"

Diff for: proxy/server/server.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/p4gefau1t/trojan-go/proxy/client"
88
"github.com/p4gefau1t/trojan-go/tunnel/mux"
99
"github.com/p4gefau1t/trojan-go/tunnel/raw"
10+
"github.com/p4gefau1t/trojan-go/tunnel/router"
1011
"github.com/p4gefau1t/trojan-go/tunnel/shadowsocks"
1112
"github.com/p4gefau1t/trojan-go/tunnel/simplesocks"
1213
"github.com/p4gefau1t/trojan-go/tunnel/transport"
@@ -18,12 +19,15 @@ const Name = "SERVER"
1819

1920
func init() {
2021
proxy.RegisterProxyCreator(Name, func(ctx context.Context) (*proxy.Proxy, error) {
21-
clientStack := []string{raw.Name}
2222
cfg := config.FromContext(ctx, Name).(*client.Config)
2323
s, err := transport.NewServer(ctx, nil)
2424
if err != nil {
2525
return nil, err
2626
}
27+
clientStack := []string{raw.Name}
28+
if cfg.Router.Enabled {
29+
clientStack = []string{raw.Name, router.Name}
30+
}
2731
root := &proxy.Node{
2832
Name: transport.Name,
2933
Next: make(map[string]*proxy.Node),

Diff for: tunnel/mux/client.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (c *Client) newMuxClient() (*smuxClientInfo, error) {
108108
conn = newStickyConn(conn)
109109

110110
smuxConfig := smux.DefaultConfig()
111-
smuxConfig.KeepAliveDisabled = true
111+
//smuxConfig.KeepAliveDisabled = true
112112
client, err := smux.Client(conn, smuxConfig)
113113
info := &smuxClientInfo{
114114
client: client,
@@ -129,6 +129,9 @@ func (c *Client) DialConn(addr *tunnel.Address, _ tunnel.Tunnel) (tunnel.Conn, e
129129
rwc, err := info.client.Open()
130130
info.lastActiveTime = time.Now()
131131
if err != nil {
132+
c.clientPoolLock.Lock()
133+
defer c.clientPoolLock.Unlock()
134+
delete(c.clientPool, info.id)
132135
return nil, common.NewError("mux failed to open stream from client").Base(err)
133136
}
134137
return &Conn{

Diff for: tunnel/mux/conn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (c *stickyConn) Write(p []byte) (int, error) {
6666
return 8, nil
6767
}
6868
} else {
69-
log.Debug("Unknown 8 bytes header")
69+
log.Debug("other 8 bytes header")
7070
}
7171
}
7272
_, err := c.Conn.Write(c.stickToPayload(p))

Diff for: tunnel/mux/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (s *Server) acceptConnWorker() {
3131
continue
3232
}
3333
smuxConfig := smux.DefaultConfig()
34-
smuxConfig.KeepAliveDisabled = true
34+
//smuxConfig.KeepAliveDisabled = true
3535
smuxSession, err := smux.Server(conn, smuxConfig)
3636
if err != nil {
3737
s.errChan <- err

Diff for: tunnel/raw/client.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func (c *Client) Close() error {
4949
return nil
5050
}
5151

52-
func NewFreeClient(ctx context.Context, client tunnel.Client) (*Client, error) {
52+
func NewClient(ctx context.Context, client tunnel.Client) (*Client, error) {
53+
// TODO implement dns
5354
cfg := config.FromContext(ctx, Name).(*Config)
5455
return &Client{
5556
noDelay: cfg.TCP.NoDelay,

Diff for: tunnel/raw/tunnel.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ func (*Tunnel) Name() string {
1616
return Name
1717
}
1818

19-
// NewClient creates a raw client
20-
func (*Tunnel) NewClient(ctx context.Context, _ tunnel.Client) (tunnel.Client, error) {
21-
return &Client{}, nil
19+
func (*Tunnel) NewClient(ctx context.Context, client tunnel.Client) (tunnel.Client, error) {
20+
return NewClient(ctx, client)
2221
}
2322

24-
// NewServer creates a raw server, which is used by "Forward"
25-
func (*Tunnel) NewServer(ctx context.Context, _ tunnel.Server) (tunnel.Server, error) {
23+
func (*Tunnel) NewServer(ctx context.Context, client tunnel.Server) (tunnel.Server, error) {
2624
serverConfig := config.FromContext(ctx, Name).(*Config)
2725
addr := tunnel.NewAddressFromHostPort("tcp", serverConfig.LocalHost, serverConfig.LocalPort)
2826

Diff for: tunnel/router/client.go

+2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ func (c *Client) DialConn(address *tunnel.Address, overlay tunnel.Tunnel) (tunne
176176
case Block:
177177
return nil, common.NewError("router blocked address: " + address.String())
178178
case Bypass:
179+
// TODO use raw.Client
179180
conn, err := net.Dial("tcp", address.String())
180181
if err != nil {
181182
return nil, common.NewError("router dial error").Base(err)
@@ -188,6 +189,7 @@ func (c *Client) DialConn(address *tunnel.Address, overlay tunnel.Tunnel) (tunne
188189
}
189190

190191
func (c *Client) DialPacket(overlay tunnel.Tunnel) (tunnel.PacketConn, error) {
192+
// TODO use raw.Client
191193
direct, err := net.ListenPacket("udp", "")
192194
if err != nil {
193195
return nil, common.NewError("router failed to dial udp (direct)").Base(err)

Diff for: tunnel/tproxy/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build linux
1+
// +build linux,!386
22

33
package tproxy
44

Diff for: tunnel/tproxy/conn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build linux
1+
// +build linux,!386
22

33
package tproxy
44

Diff for: tunnel/tproxy/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build linux
1+
// +build linux,!386
22

33
package tproxy
44

Diff for: tunnel/tproxy/tproxy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build linux
1+
// +build linux,!386
22

33
package tproxy
44

Diff for: tunnel/tproxy/tproxy_stub.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
// +build !linux
2-
31
package tproxy

Diff for: tunnel/tproxy/tunnel.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build linux
1+
// +build linux,!386
22

33
package tproxy
44

0 commit comments

Comments
 (0)