fix api, smux, nat

This commit is contained in:
Page Fault
2020-06-11 06:06:05 +00:00
parent 4a33464097
commit e06c06dc91
26 changed files with 46 additions and 39 deletions
+5 -2
View File
@@ -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
}
+1 -1
View File
@@ -11,7 +11,7 @@ type APIConfig struct {
}
type Config struct {
APIConfig `json,yaml:"api"`
API APIConfig `json,yaml:"api"`
}
func init() {
+5 -2
View File
@@ -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
}
+1 -1
View File
@@ -1,4 +1,4 @@
// +build client full standard
// +build client full mini
package build
+1 -1
View File
@@ -1,4 +1,4 @@
// +build forward full standard
// +build forward full mini
package build
+1 -1
View File
@@ -1,4 +1,4 @@
// +build mysql full standard
// +build mysql full mini
package build
+1 -1
View File
@@ -1,4 +1,4 @@
// +build nat full standard
// +build nat full mini
package build
+1 -1
View File
@@ -1,4 +1,4 @@
// +build server full standard
// +build server full mini
package build
+2 -3
View File
@@ -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"
```
+2 -2
View File
@@ -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
+2 -2
View File
@@ -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=
+1 -1
View File
@@ -1,4 +1,4 @@
// +build linux
// +build linux,!386
package nat
-4
View File
@@ -1,5 +1 @@
// +build !linux
package nat
const Name = "NAT"
+5 -1
View File
@@ -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),
+4 -1
View File
@@ -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{
+1 -1
View File
@@ -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))
+1 -1
View File
@@ -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
+2 -1
View File
@@ -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,
+3 -5
View File
@@ -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)
+2
View File
@@ -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)
+1 -1
View File
@@ -1,4 +1,4 @@
// +build linux
// +build linux,!386
package tproxy
+1 -1
View File
@@ -1,4 +1,4 @@
// +build linux
// +build linux,!386
package tproxy
+1 -1
View File
@@ -1,4 +1,4 @@
// +build linux
// +build linux,!386
package tproxy
+1 -1
View File
@@ -1,4 +1,4 @@
// +build linux
// +build linux,!386
package tproxy
-2
View File
@@ -1,3 +1 @@
// +build !linux
package tproxy
+1 -1
View File
@@ -1,4 +1,4 @@
// +build linux
// +build linux,!386
package tproxy