Skip to content

Commit 8b6032c

Browse files
committed
Feat: add option to disable UDP functionality
1 parent 61d8269 commit 8b6032c

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

engine/engine.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ func general(k *Key) error {
133133
}
134134
tunnel.T().SetUDPTimeout(k.UDPTimeout)
135135
}
136+
137+
if k.UDPDisabled {
138+
tunnel.T().SetUDPDisabled(true)
139+
}
136140
return nil
137141
}
138142

engine/key.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ type Key struct {
1717
TUNPreUp string `yaml:"tun-pre-up"`
1818
TUNPostUp string `yaml:"tun-post-up"`
1919
UDPTimeout time.Duration `yaml:"udp-timeout"`
20+
UDPDisabled bool `yaml:"udp-disabled"`
2021
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func init() {
2727
flag.IntVar(&key.Mark, "fwmark", 0, "Set firewall MARK (Linux only)")
2828
flag.IntVar(&key.MTU, "mtu", 0, "Set device maximum transmission unit (MTU)")
2929
flag.DurationVar(&key.UDPTimeout, "udp-timeout", 0, "Set timeout for each UDP session")
30+
flag.BoolVar(&key.UDPDisabled, "udp-disabled", false, "Disable UDP")
3031
flag.StringVar(&configFile, "config", "", "YAML format configuration file")
3132
flag.StringVar(&key.Device, "device", "", "Use this device [driver://]name")
3233
flag.StringVar(&key.Interface, "interface", "", "Use network INTERFACE (Linux/MacOS only)")

tunnel/tunnel.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ type Tunnel struct {
3131
// UDP session timeout.
3232
udpTimeout *atomic.Duration
3333

34+
// UDP disabled flag.
35+
udpDisabled *atomic.Bool
36+
3437
// Internal proxy.Dialer for Tunnel.
3538
dialerMu sync.RWMutex
3639
dialer proxy.Dialer
@@ -44,12 +47,13 @@ type Tunnel struct {
4447

4548
func New(dialer proxy.Dialer, manager *statistic.Manager) *Tunnel {
4649
return &Tunnel{
47-
tcpQueue: make(chan adapter.TCPConn),
48-
udpQueue: make(chan adapter.UDPConn),
49-
udpTimeout: atomic.NewDuration(udpSessionTimeout),
50-
dialer: dialer,
51-
manager: manager,
52-
procCancel: func() { /* nop */ },
50+
tcpQueue: make(chan adapter.TCPConn),
51+
udpQueue: make(chan adapter.UDPConn),
52+
udpTimeout: atomic.NewDuration(udpSessionTimeout),
53+
udpDisabled: atomic.NewBool(false),
54+
dialer: dialer,
55+
manager: manager,
56+
procCancel: func() { /* nop */ },
5357
}
5458
}
5559

@@ -114,3 +118,7 @@ func (t *Tunnel) SetDialer(dialer proxy.Dialer) {
114118
func (t *Tunnel) SetUDPTimeout(timeout time.Duration) {
115119
t.udpTimeout.Store(timeout)
116120
}
121+
122+
func (t *Tunnel) SetUDPDisabled(disabled bool) {
123+
t.udpDisabled.Store(disabled)
124+
}

tunnel/udp.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import (
1717
func (t *Tunnel) handleUDPConn(uc adapter.UDPConn) {
1818
defer uc.Close()
1919

20+
if t.udpDisabled.Load() {
21+
log.Warnf("[UDP] dial %s: blocked", uc.ID().RemoteAddress)
22+
return
23+
}
24+
2025
id := uc.ID()
2126
metadata := &M.Metadata{
2227
Network: M.UDP,

0 commit comments

Comments
 (0)