config: added tcpbufsize & udpbufsize(default: 2048)

This commit is contained in:
nadoo 2021-07-24 23:45:53 +08:00
parent 41861ff48e
commit d615dc087e
7 changed files with 32 additions and 10 deletions

View File

@ -157,6 +157,10 @@ glider 0.15.0 usage:
run specified services, format: SERVICE_NAME[,SERVICE_CONFIG] run specified services, format: SERVICE_NAME[,SERVICE_CONFIG]
-strategy string -strategy string
forward strategy, default: rr (default "rr") 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
verbose mode verbose mode
``` ```

View File

@ -9,6 +9,7 @@ import (
"github.com/nadoo/glider/dns" "github.com/nadoo/glider/dns"
"github.com/nadoo/glider/log" "github.com/nadoo/glider/log"
"github.com/nadoo/glider/proxy"
"github.com/nadoo/glider/rule" "github.com/nadoo/glider/rule"
) )
@ -16,8 +17,10 @@ var flag = conflag.New()
// Config is global config struct. // Config is global config struct.
type Config struct { type Config struct {
Verbose bool Verbose bool
LogFlags int LogFlags int
TCPBufSize int
UDPBufSize int
Listens []string Listens []string
@ -42,6 +45,8 @@ func parseConfig() *Config {
flag.BoolVar(&conf.Verbose, "verbose", false, "verbose mode") 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.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.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]") 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) os.Exit(-1)
} }
// tcpbufsize
if conf.TCPBufSize > 0 {
proxy.TCPBufSize = conf.TCPBufSize
}
// udpbufsize
if conf.UDPBufSize > 0 {
proxy.UDPBufSize = conf.UDPBufSize
}
// rulefiles // rulefiles
for _, ruleFile := range conf.RuleFiles { for _, ruleFile := range conf.RuleFiles {
if !path.IsAbs(ruleFile) { if !path.IsAbs(ruleFile) {

View File

@ -13,12 +13,12 @@ import (
"github.com/nadoo/glider/pool" "github.com/nadoo/glider/pool"
) )
const ( var (
// TCPBufSize is the size of tcp buffer. // TCPBufSize is the size of tcp buffer.
TCPBufSize = 32 << 10 TCPBufSize = 32 << 10
// UDPBufSize is the size of udp buffer. // UDPBufSize is the size of udp buffer.
UDPBufSize = 64 << 10 UDPBufSize = 2 << 10
) )
// Conn is a connection with buffered reader. // Conn is a connection with buffered reader.

View File

@ -67,6 +67,9 @@ func (a Addr) String() string {
return net.JoinHostPort(host, port) 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. // ReadAddrBuf reads just enough bytes from r to get a valid Addr.
func ReadAddrBuf(r io.Reader, b []byte) (Addr, error) { func ReadAddrBuf(r io.Reader, b []byte) (Addr, error) {
if len(b) < MaxAddrLen { if len(b) < MaxAddrLen {

View File

@ -19,7 +19,7 @@ import (
"github.com/nadoo/glider/proxy/ssr/internal/protocol" "github.com/nadoo/glider/proxy/ssr/internal/protocol"
) )
const bufSize = proxy.TCPBufSize var bufSize = proxy.TCPBufSize
func init() { func init() {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())

View File

@ -80,7 +80,7 @@ func (s *TProxy) ListenAndServeUDP() {
} }
var session *natEntry var session *natEntry
sessionKey := lraddr.String() + dstAddr.String() sessionKey := lraddr.String()
v, ok := nm.Load(sessionKey) v, ok := nm.Load(sessionKey)
if !ok && v == nil { if !ok && v == nil {
@ -115,7 +115,6 @@ func (s *TProxy) ListenAndServeUDP() {
_, err = session.WriteTo(buf[:n], session.writeTo) _, err = session.WriteTo(buf[:n], session.writeTo)
if err != nil { if err != nil {
log.F("[tproxyu] writeTo %s error: %v", session.writeTo, err) log.F("[tproxyu] writeTo %s error: %v", session.writeTo, err)
continue
} }
} }
} }

View File

@ -13,7 +13,6 @@ import (
// PktConn is a udp Packet.Conn. // PktConn is a udp Packet.Conn.
type PktConn struct { type PktConn struct {
net.Conn net.Conn
tgtAddr socks.Addr tgtAddr socks.Addr
} }
@ -27,7 +26,8 @@ func NewPktConn(c net.Conn, tgtAddr socks.Addr) *PktConn {
} }
// ReadFrom implements the necessary function of net.PacketConn. // 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) { func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) {
// ATYP, DST.ADDR, DST.PORT // ATYP, DST.ADDR, DST.PORT
_, err := socks.ReadAddr(pc.Conn) _, err := socks.ReadAddr(pc.Conn)
@ -35,6 +35,7 @@ func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) {
return 0, nil, err 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 { if len(b) < 2 {
return 0, nil, errors.New("buf size is not enough") 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 // 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. // WriteTo implements the necessary function of net.PacketConn.