From d615dc087e7353f4a93f119762ef85d0422c94c2 Mon Sep 17 00:00:00 2001 From: nadoo <287492+nadoo@users.noreply.github.com> Date: Sat, 24 Jul 2021 23:45:53 +0800 Subject: [PATCH] config: added `tcpbufsize` & `udpbufsize`(default: 2048) --- README.md | 4 ++++ config.go | 19 +++++++++++++++++-- proxy/conn.go | 4 ++-- proxy/protocol/socks/socks.go | 3 +++ proxy/ssr/internal/client.go | 2 +- proxy/tproxy/tproxy_linux.go | 3 +-- proxy/trojan/packet.go | 7 ++++--- 7 files changed, 32 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 76bbdc66..a62cc277 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,10 @@ glider 0.15.0 usage: run specified services, format: SERVICE_NAME[,SERVICE_CONFIG] -strategy string forward strategy, default: rr (default "rr") + -tcpbufsize int + tcp buffer size in Bytes (default 32768) + -udpbufsize int + udp buffer size in Bytes (default 2048) -verbose verbose mode ``` diff --git a/config.go b/config.go index 6a27f4f7..0931cbb0 100644 --- a/config.go +++ b/config.go @@ -9,6 +9,7 @@ import ( "github.com/nadoo/glider/dns" "github.com/nadoo/glider/log" + "github.com/nadoo/glider/proxy" "github.com/nadoo/glider/rule" ) @@ -16,8 +17,10 @@ var flag = conflag.New() // Config is global config struct. type Config struct { - Verbose bool - LogFlags int + Verbose bool + LogFlags int + TCPBufSize int + UDPBufSize int Listens []string @@ -42,6 +45,8 @@ func parseConfig() *Config { flag.BoolVar(&conf.Verbose, "verbose", false, "verbose mode") flag.IntVar(&conf.LogFlags, "logflags", 19, "log flags, do not change it if you do not know what it is, ref: https://pkg.go.dev/log#pkg-constants") + flag.IntVar(&conf.TCPBufSize, "tcpbufsize", 32768, "tcp buffer size in Bytes") + flag.IntVar(&conf.UDPBufSize, "udpbufsize", 2048, "udp buffer size in Bytes") flag.StringSliceUniqVar(&conf.Listens, "listen", nil, "listen url, format: SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS") flag.StringSliceUniqVar(&conf.Forwards, "forward", nil, "forward url, format: SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS[,SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS]") @@ -93,6 +98,16 @@ func parseConfig() *Config { os.Exit(-1) } + // tcpbufsize + if conf.TCPBufSize > 0 { + proxy.TCPBufSize = conf.TCPBufSize + } + + // udpbufsize + if conf.UDPBufSize > 0 { + proxy.UDPBufSize = conf.UDPBufSize + } + // rulefiles for _, ruleFile := range conf.RuleFiles { if !path.IsAbs(ruleFile) { diff --git a/proxy/conn.go b/proxy/conn.go index 8b0837b2..cddea85f 100644 --- a/proxy/conn.go +++ b/proxy/conn.go @@ -13,12 +13,12 @@ import ( "github.com/nadoo/glider/pool" ) -const ( +var ( // TCPBufSize is the size of tcp buffer. TCPBufSize = 32 << 10 // UDPBufSize is the size of udp buffer. - UDPBufSize = 64 << 10 + UDPBufSize = 2 << 10 ) // Conn is a connection with buffered reader. diff --git a/proxy/protocol/socks/socks.go b/proxy/protocol/socks/socks.go index 09a68590..d62df844 100644 --- a/proxy/protocol/socks/socks.go +++ b/proxy/protocol/socks/socks.go @@ -67,6 +67,9 @@ func (a Addr) String() string { return net.JoinHostPort(host, port) } +// Network returns network name. Implements net.Addr interface. +func (a Addr) Network() string { return "socks" } + // ReadAddrBuf reads just enough bytes from r to get a valid Addr. func ReadAddrBuf(r io.Reader, b []byte) (Addr, error) { if len(b) < MaxAddrLen { diff --git a/proxy/ssr/internal/client.go b/proxy/ssr/internal/client.go index bf36fef7..4236150c 100644 --- a/proxy/ssr/internal/client.go +++ b/proxy/ssr/internal/client.go @@ -19,7 +19,7 @@ import ( "github.com/nadoo/glider/proxy/ssr/internal/protocol" ) -const bufSize = proxy.TCPBufSize +var bufSize = proxy.TCPBufSize func init() { rand.Seed(time.Now().UnixNano()) diff --git a/proxy/tproxy/tproxy_linux.go b/proxy/tproxy/tproxy_linux.go index 64bde566..82852122 100644 --- a/proxy/tproxy/tproxy_linux.go +++ b/proxy/tproxy/tproxy_linux.go @@ -80,7 +80,7 @@ func (s *TProxy) ListenAndServeUDP() { } var session *natEntry - sessionKey := lraddr.String() + dstAddr.String() + sessionKey := lraddr.String() v, ok := nm.Load(sessionKey) if !ok && v == nil { @@ -115,7 +115,6 @@ func (s *TProxy) ListenAndServeUDP() { _, err = session.WriteTo(buf[:n], session.writeTo) if err != nil { log.F("[tproxyu] writeTo %s error: %v", session.writeTo, err) - continue } } } diff --git a/proxy/trojan/packet.go b/proxy/trojan/packet.go index 2e13af03..348b6dd6 100644 --- a/proxy/trojan/packet.go +++ b/proxy/trojan/packet.go @@ -13,7 +13,6 @@ import ( // PktConn is a udp Packet.Conn. type PktConn struct { net.Conn - tgtAddr socks.Addr } @@ -27,7 +26,8 @@ func NewPktConn(c net.Conn, tgtAddr socks.Addr) *PktConn { } // ReadFrom implements the necessary function of net.PacketConn. -// TODO: we know that we use it in proxy.RelayUDP and the length of b is enough, check it later. +// NOTE: the underlying connection is not udp, we returned the target address here, +// it's not the vless server's address, do not WriteTo it. func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) { // ATYP, DST.ADDR, DST.PORT _, err := socks.ReadAddr(pc.Conn) @@ -35,6 +35,7 @@ func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) { return 0, nil, err } + // TODO: we know that we use it in proxy.RelayUDP and the length of b is enough, check it later. if len(b) < 2 { return 0, nil, errors.New("buf size is not enough") } @@ -62,7 +63,7 @@ func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) { } // TODO: check the addr in return value, it's a fake packetConn so the addr is not valid - return n, nil, err + return n, pc.tgtAddr, err } // WriteTo implements the necessary function of net.PacketConn.