Skip to content

Commit

Permalink
config: added tcpbufsize & udpbufsize(default: 2048)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadoo committed Jul 24, 2021
1 parent 41861ff commit d615dc0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
19 changes: 17 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ import (

"github.com/nadoo/glider/dns"
"github.com/nadoo/glider/log"
"github.com/nadoo/glider/proxy"
"github.com/nadoo/glider/rule"
)

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

Expand All @@ -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]")
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions proxy/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions proxy/protocol/socks/socks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion proxy/ssr/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 1 addition & 2 deletions proxy/tproxy/tproxy_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions proxy/trojan/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
// PktConn is a udp Packet.Conn.
type PktConn struct {
net.Conn

tgtAddr socks.Addr
}

Expand All @@ -27,14 +26,16 @@ 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)
if err != nil {
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")
}
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit d615dc0

Please sign in to comment.