Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support disable udp #437

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ func general(k *Key) error {
}
tunnel.T().SetUDPTimeout(k.UDPTimeout)
}

if k.UDPDisabled {
tunnel.T().SetUDPDisabled(true)
}
return nil
}

Expand Down
1 change: 1 addition & 0 deletions engine/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ type Key struct {
TUNPreUp string `yaml:"tun-pre-up"`
TUNPostUp string `yaml:"tun-post-up"`
UDPTimeout time.Duration `yaml:"udp-timeout"`
UDPDisabled bool `yaml:"udp-disabled"`
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func init() {
flag.IntVar(&key.Mark, "fwmark", 0, "Set firewall MARK (Linux only)")
flag.IntVar(&key.MTU, "mtu", 0, "Set device maximum transmission unit (MTU)")
flag.DurationVar(&key.UDPTimeout, "udp-timeout", 0, "Set timeout for each UDP session")
flag.BoolVar(&key.UDPDisabled, "udp-disabled", false, "Disable UDP")
flag.StringVar(&configFile, "config", "", "YAML format configuration file")
flag.StringVar(&key.Device, "device", "", "Use this device [driver://]name")
flag.StringVar(&key.Interface, "interface", "", "Use network INTERFACE (Linux/MacOS only)")
Expand Down
9 changes: 8 additions & 1 deletion proxy/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@ type Socks5 struct {
}

func NewSocks5(addr, user, pass string) (*Socks5, error) {
unix := len(addr) > 0 && addr[0] == '/'

// For support Linux abstract namespace
if len(addr) > 2 && addr[1] == '@' || addr[1] == 0x00 {
addr = addr[1:]
}

return &Socks5{
Base: &Base{
addr: addr,
proto: proto.Socks5,
},
user: user,
pass: pass,
unix: len(addr) > 0 && addr[0] == '/',
unix: unix,
}, nil
}

Expand Down
20 changes: 14 additions & 6 deletions tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type Tunnel struct {
// UDP session timeout.
udpTimeout *atomic.Duration

// UDP disabled flag.
udpDisabled *atomic.Bool

// Internal proxy.Dialer for Tunnel.
dialerMu sync.RWMutex
dialer proxy.Dialer
Expand All @@ -44,12 +47,13 @@ type Tunnel struct {

func New(dialer proxy.Dialer, manager *statistic.Manager) *Tunnel {
return &Tunnel{
tcpQueue: make(chan adapter.TCPConn),
udpQueue: make(chan adapter.UDPConn),
udpTimeout: atomic.NewDuration(udpSessionTimeout),
dialer: dialer,
manager: manager,
procCancel: func() { /* nop */ },
tcpQueue: make(chan adapter.TCPConn),
udpQueue: make(chan adapter.UDPConn),
udpTimeout: atomic.NewDuration(udpSessionTimeout),
udpDisabled: atomic.NewBool(false),
dialer: dialer,
manager: manager,
procCancel: func() { /* nop */ },
}
}

Expand Down Expand Up @@ -114,3 +118,7 @@ func (t *Tunnel) SetDialer(dialer proxy.Dialer) {
func (t *Tunnel) SetUDPTimeout(timeout time.Duration) {
t.udpTimeout.Store(timeout)
}

func (t *Tunnel) SetUDPDisabled(disabled bool) {
t.udpDisabled.Store(disabled)
}
5 changes: 5 additions & 0 deletions tunnel/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (
func (t *Tunnel) handleUDPConn(uc adapter.UDPConn) {
defer uc.Close()

if t.udpDisabled.Load() {
log.Warnf("[UDP] dial %s: blocked", uc.ID().RemoteAddress)
return
}

id := uc.ID()
metadata := &M.Metadata{
Network: M.UDP,
Expand Down
Loading