mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-06 01:07:03 +08:00
Feature: add udp-rlybuf option
* Default UDP relay buffer size: 16KiB
This commit is contained in:
@@ -22,6 +22,7 @@ ENV PROXY=direct://
|
|||||||
ENV MTU=9000
|
ENV MTU=9000
|
||||||
ENV RESTAPI=
|
ENV RESTAPI=
|
||||||
ENV UDP_TIMEOUT=
|
ENV UDP_TIMEOUT=
|
||||||
|
ENV UDP_RLYBUF=
|
||||||
ENV TCP_SNDBUF=
|
ENV TCP_SNDBUF=
|
||||||
ENV TCP_RCVBUF=
|
ENV TCP_RCVBUF=
|
||||||
ENV TCP_AUTO_TUNING=
|
ENV TCP_AUTO_TUNING=
|
||||||
|
@@ -7,9 +7,10 @@ const (
|
|||||||
// MaxSegmentSize is the largest possible UDP datagram size.
|
// MaxSegmentSize is the largest possible UDP datagram size.
|
||||||
MaxSegmentSize = (1 << 16) - 1
|
MaxSegmentSize = (1 << 16) - 1
|
||||||
|
|
||||||
// io.Copy default buffer size is 32 KiB, but the maximum packet
|
// RelayBufferSize is a buffer of 20 KiB to reduce the memory
|
||||||
// size of vmess/shadowsocks is about 16 KiB, so define a buffer
|
// of each TCP relay as io.Copy default buffer size is 32 KiB,
|
||||||
// of 20 KiB to reduce the memory of each TCP relay.
|
// but the maximum packet size of vmess/shadowsocks is about
|
||||||
|
// 16 KiB, so define .
|
||||||
RelayBufferSize = 20 << 10
|
RelayBufferSize = 20 << 10
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@@ -129,6 +129,14 @@ func general(k *Key) error {
|
|||||||
}
|
}
|
||||||
tunnel.SetUDPTimeout(k.UDPTimeout)
|
tunnel.SetUDPTimeout(k.UDPTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if k.UDPRelayBufferSize != "" {
|
||||||
|
size, err := units.RAMInBytes(k.UDPRelayBufferSize)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tunnel.SetUDPRelayBufferSize(int(size))
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -13,7 +13,8 @@ type Key struct {
|
|||||||
TCPModerateReceiveBuffer bool `yaml:"tcp-moderate-receive-buffer"`
|
TCPModerateReceiveBuffer bool `yaml:"tcp-moderate-receive-buffer"`
|
||||||
TCPSendBufferSize string `yaml:"tcp-send-buffer-size"`
|
TCPSendBufferSize string `yaml:"tcp-send-buffer-size"`
|
||||||
TCPReceiveBufferSize string `yaml:"tcp-receive-buffer-size"`
|
TCPReceiveBufferSize string `yaml:"tcp-receive-buffer-size"`
|
||||||
|
UDPRelayBufferSize string `yaml:"udp-relay-buffer-size"`
|
||||||
|
UDPTimeout time.Duration `yaml:"udp-timeout"`
|
||||||
TUNPreUp string `yaml:"tun-pre-up"`
|
TUNPreUp string `yaml:"tun-pre-up"`
|
||||||
TUNPostUp string `yaml:"tun-post-up"`
|
TUNPostUp string `yaml:"tun-post-up"`
|
||||||
UDPTimeout time.Duration `yaml:"udp-timeout"`
|
|
||||||
}
|
}
|
||||||
|
1
main.go
1
main.go
@@ -35,6 +35,7 @@ func init() {
|
|||||||
flag.StringVar(&key.RestAPI, "restapi", "", "HTTP statistic server listen address")
|
flag.StringVar(&key.RestAPI, "restapi", "", "HTTP statistic server listen address")
|
||||||
flag.StringVar(&key.TCPSendBufferSize, "tcp-sndbuf", "", "Set TCP send buffer size for netstack")
|
flag.StringVar(&key.TCPSendBufferSize, "tcp-sndbuf", "", "Set TCP send buffer size for netstack")
|
||||||
flag.StringVar(&key.TCPReceiveBufferSize, "tcp-rcvbuf", "", "Set TCP receive buffer size for netstack")
|
flag.StringVar(&key.TCPReceiveBufferSize, "tcp-rcvbuf", "", "Set TCP receive buffer size for netstack")
|
||||||
|
flag.StringVar(&key.UDPRelayBufferSize, "udp-rlybuf", "", "Set UDP relay buffer size for tunnel")
|
||||||
flag.BoolVar(&key.TCPModerateReceiveBuffer, "tcp-auto-tuning", false, "Enable TCP receive buffer auto-tuning")
|
flag.BoolVar(&key.TCPModerateReceiveBuffer, "tcp-auto-tuning", false, "Enable TCP receive buffer auto-tuning")
|
||||||
flag.StringVar(&key.TUNPreUp, "tun-pre-up", "", "Execute a command before TUN device setup")
|
flag.StringVar(&key.TUNPreUp, "tun-pre-up", "", "Execute a command before TUN device setup")
|
||||||
flag.StringVar(&key.TUNPostUp, "tun-post-up", "", "Execute a command after TUN device setup")
|
flag.StringVar(&key.TUNPostUp, "tun-post-up", "", "Execute a command after TUN device setup")
|
||||||
|
@@ -15,13 +15,22 @@ import (
|
|||||||
"github.com/xjasonlyu/tun2socks/v2/tunnel/statistic"
|
"github.com/xjasonlyu/tun2socks/v2/tunnel/statistic"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
// _udpSessionTimeout is the default timeout for each UDP session.
|
// _udpSessionTimeout is the default timeout for each UDP session.
|
||||||
var _udpSessionTimeout = 60 * time.Second
|
_udpSessionTimeout = 60 * time.Second
|
||||||
|
|
||||||
|
// _udpRelayBufferSize is the default size for UDP packets relay.
|
||||||
|
_udpRelayBufferSize = 16 << 10
|
||||||
|
)
|
||||||
|
|
||||||
func SetUDPTimeout(t time.Duration) {
|
func SetUDPTimeout(t time.Duration) {
|
||||||
_udpSessionTimeout = t
|
_udpSessionTimeout = t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetUDPRelayBufferSize(size int) {
|
||||||
|
_udpRelayBufferSize = size
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Port Restricted NAT support.
|
// TODO: Port Restricted NAT support.
|
||||||
func handleUDPConn(uc adapter.UDPConn) {
|
func handleUDPConn(uc adapter.UDPConn) {
|
||||||
defer uc.Close()
|
defer uc.Close()
|
||||||
@@ -84,7 +93,7 @@ func relayPacket(left net.PacketConn, right net.PacketConn, to net.Addr) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func copyPacketBuffer(dst net.PacketConn, src net.PacketConn, to net.Addr, timeout time.Duration) error {
|
func copyPacketBuffer(dst net.PacketConn, src net.PacketConn, to net.Addr, timeout time.Duration) error {
|
||||||
buf := pool.Get(pool.MaxSegmentSize)
|
buf := pool.Get(_udpRelayBufferSize)
|
||||||
defer pool.Put(buf)
|
defer pool.Put(buf)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
Reference in New Issue
Block a user