Projects STRLCPY tun2socks Commits fb9ca959
🤬
  • Feature: add udp-rlybuf option

    * Default UDP relay buffer size: 16KiB
  • Loading...
  • xjasonlyu committed 1 year ago
    fb9ca959
    1 parent 1e99f2d5
Revision indexing in progress... (symbol navigation in revisions will be accurate after indexed)
  • ■ ■ ■ ■ ■
    Dockerfile
    skipped 21 lines
    22 22  ENV MTU=9000
    23 23  ENV RESTAPI=
    24 24  ENV UDP_TIMEOUT=
     25 +ENV UDP_RLYBUF=
    25 26  ENV TCP_SNDBUF=
    26 27  ENV TCP_RCVBUF=
    27 28  ENV TCP_AUTO_TUNING=
    skipped 6 lines
  • ■ ■ ■ ■ ■ ■
    common/pool/pool.go
    skipped 6 lines
    7 7   // MaxSegmentSize is the largest possible UDP datagram size.
    8 8   MaxSegmentSize = (1 << 16) - 1
    9 9   
    10  - // io.Copy default buffer size is 32 KiB, but the maximum packet
    11  - // size of vmess/shadowsocks is about 16 KiB, so define a buffer
    12  - // of 20 KiB to reduce the memory of each TCP relay.
     10 + // RelayBufferSize is a buffer of 20 KiB to reduce the memory
     11 + // of each TCP relay as io.Copy default buffer size is 32 KiB,
     12 + // but the maximum packet size of vmess/shadowsocks is about
     13 + // 16 KiB, so define .
    13 14   RelayBufferSize = 20 << 10
    14 15  )
    15 16   
    skipped 10 lines
  • ■ ■ ■ ■ ■ ■
    engine/engine.go
    skipped 128 lines
    129 129   }
    130 130   tunnel.SetUDPTimeout(k.UDPTimeout)
    131 131   }
     132 + 
     133 + if k.UDPRelayBufferSize != "" {
     134 + size, err := units.RAMInBytes(k.UDPRelayBufferSize)
     135 + if err != nil {
     136 + return err
     137 + }
     138 + tunnel.SetUDPRelayBufferSize(int(size))
     139 + }
    132 140   return nil
    133 141  }
    134 142   
    skipped 98 lines
  • ■ ■ ■ ■ ■
    engine/key.go
    skipped 12 lines
    13 13   TCPModerateReceiveBuffer bool `yaml:"tcp-moderate-receive-buffer"`
    14 14   TCPSendBufferSize string `yaml:"tcp-send-buffer-size"`
    15 15   TCPReceiveBufferSize string `yaml:"tcp-receive-buffer-size"`
     16 + UDPRelayBufferSize string `yaml:"udp-relay-buffer-size"`
     17 + UDPTimeout time.Duration `yaml:"udp-timeout"`
    16 18   TUNPreUp string `yaml:"tun-pre-up"`
    17 19   TUNPostUp string `yaml:"tun-post-up"`
    18  - UDPTimeout time.Duration `yaml:"udp-timeout"`
    19 20  }
    20 21   
  • ■ ■ ■ ■ ■
    main.go
    skipped 34 lines
    35 35   flag.StringVar(&key.RestAPI, "restapi", "", "HTTP statistic server listen address")
    36 36   flag.StringVar(&key.TCPSendBufferSize, "tcp-sndbuf", "", "Set TCP send buffer size for netstack")
    37 37   flag.StringVar(&key.TCPReceiveBufferSize, "tcp-rcvbuf", "", "Set TCP receive buffer size for netstack")
     38 + flag.StringVar(&key.UDPRelayBufferSize, "udp-rlybuf", "", "Set UDP relay buffer size for tunnel")
    38 39   flag.BoolVar(&key.TCPModerateReceiveBuffer, "tcp-auto-tuning", false, "Enable TCP receive buffer auto-tuning")
    39 40   flag.StringVar(&key.TUNPreUp, "tun-pre-up", "", "Execute a command before TUN device setup")
    40 41   flag.StringVar(&key.TUNPostUp, "tun-post-up", "", "Execute a command after TUN device setup")
    skipped 33 lines
  • ■ ■ ■ ■ ■
    tunnel/udp.go
    skipped 14 lines
    15 15   "github.com/xjasonlyu/tun2socks/v2/tunnel/statistic"
    16 16  )
    17 17   
    18  -// _udpSessionTimeout is the default timeout for each UDP session.
    19  -var _udpSessionTimeout = 60 * time.Second
     18 +var (
     19 + // _udpSessionTimeout is the default timeout for each UDP session.
     20 + _udpSessionTimeout = 60 * time.Second
     21 + 
     22 + // _udpRelayBufferSize is the default size for UDP packets relay.
     23 + _udpRelayBufferSize = 16 << 10
     24 +)
    20 25   
    21 26  func SetUDPTimeout(t time.Duration) {
    22 27   _udpSessionTimeout = t
     28 +}
     29 + 
     30 +func SetUDPRelayBufferSize(size int) {
     31 + _udpRelayBufferSize = size
    23 32  }
    24 33   
    25 34  // TODO: Port Restricted NAT support.
    skipped 58 lines
    84 93  }
    85 94   
    86 95  func copyPacketBuffer(dst net.PacketConn, src net.PacketConn, to net.Addr, timeout time.Duration) error {
    87  - buf := pool.Get(pool.MaxSegmentSize)
     96 + buf := pool.Get(_udpRelayBufferSize)
    88 97   defer pool.Put(buf)
    89 98   
    90 99   for {
    skipped 44 lines
Please wait...
Page is in error, reload to recover