Skip to content

Commit 4e66cee

Browse files
authored
feat: add a query parameter to control whether UDP functionality is disabled
1 parent e7e9665 commit 4e66cee

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

internal/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ func NewClient(parsedURL *url.URL, logger *logs.Logger) (*Client, error) {
6262
// Run 管理客户端生命周期
6363
func (c *Client) Run() {
6464
logInfo := func(prefix string) {
65-
c.logger.Info("%v: client://%v@%v/%v?min=%v&mode=%v&read=%v&rate=%v&slot=%v&proxy=%v",
65+
c.logger.Info("%v: client://%v@%v/%v?min=%v&mode=%v&read=%v&rate=%v&slot=%v&proxy=%v&noudp=%v",
6666
prefix, c.tunnelKey, c.tunnelTCPAddr, c.getTargetAddrsString(),
67-
c.minPoolCapacity, c.runMode, c.readTimeout, c.rateLimit/125000, c.slotLimit, c.proxyProtocol)
67+
c.minPoolCapacity, c.runMode, c.readTimeout, c.rateLimit/125000, c.slotLimit, c.proxyProtocol, c.disableUDP)
6868
}
6969
logInfo("Client started")
7070

@@ -83,7 +83,7 @@ func (c *Client) Run() {
8383
return
8484
case <-time.After(serviceCooldown):
8585
}
86-
logInfo("Client restarting")
86+
logInfo("Client restart")
8787
}
8888
}
8989
}()

internal/common.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type Common struct {
4747
minPoolCapacity int // 最小池容量
4848
maxPoolCapacity int // 最大池容量
4949
proxyProtocol string // 代理协议
50+
disableUDP string // 禁用UDP
5051
rateLimit int // 速率限制
5152
rateLimiter *conn.RateLimiter // 全局限速器
5253
readTimeout time.Duration // 读取超时
@@ -98,6 +99,7 @@ const (
9899
defaultRateLimit = 0 // 默认速率限制
99100
defaultSlotLimit = 65536 // 默认槽位限制
100101
defaultProxyProtocol = "0" // 默认代理协议
102+
defaultUDPStrategy = "0" // 默认UDP策略
101103
)
102104

103105
// getTCPBuffer 获取TCP缓冲区
@@ -402,6 +404,15 @@ func (c *Common) getProxyProtocol(parsedURL *url.URL) {
402404
}
403405
}
404406

407+
// getUDPStrategy 获取UDP策略
408+
func (c *Common) getUDPStrategy(parsedURL *url.URL) {
409+
if udpStrategy := parsedURL.Query().Get("noudp"); udpStrategy != "" {
410+
c.disableUDP = udpStrategy
411+
} else {
412+
c.disableUDP = defaultUDPStrategy
413+
}
414+
}
415+
405416
// initConfig 初始化配置
406417
func (c *Common) initConfig(parsedURL *url.URL) error {
407418
if err := c.getAddress(parsedURL); err != nil {
@@ -415,6 +426,7 @@ func (c *Common) initConfig(parsedURL *url.URL) error {
415426
c.getRateLimit(parsedURL)
416427
c.getSlotLimit(parsedURL)
417428
c.getProxyProtocol(parsedURL)
429+
c.getUDPStrategy(parsedURL)
418430

419431
return nil
420432
}
@@ -487,7 +499,7 @@ func (c *Common) initTunnelListener() error {
487499
}
488500

489501
// 初始化隧道UDP监听器
490-
if c.tunnelUDPAddr != nil {
502+
if c.tunnelUDPAddr != nil && c.disableUDP != "1" {
491503
tunnelUDPConn, err := net.ListenUDP("udp", c.tunnelUDPAddr)
492504
if err != nil {
493505
return fmt.Errorf("initTunnelListener: listenUDP failed: %w", err)
@@ -514,7 +526,7 @@ func (c *Common) initTargetListener() error {
514526
}
515527

516528
// 初始化目标UDP监听器
517-
if len(c.targetUDPAddrs) > 0 {
529+
if len(c.targetUDPAddrs) > 0 && c.disableUDP != "1" {
518530
targetUDPConn, err := net.ListenUDP("udp", c.targetUDPAddrs[0])
519531
if err != nil {
520532
return fmt.Errorf("initTargetListener: listenUDP failed: %w", err)
@@ -743,8 +755,12 @@ func (c *Common) commonLoop() {
743755
for c.ctx.Err() == nil {
744756
// 等待连接池准备就绪
745757
if c.tunnelPool.Ready() {
746-
go c.commonTCPLoop()
747-
go c.commonUDPLoop()
758+
if c.targetListener != nil {
759+
go c.commonTCPLoop()
760+
}
761+
if c.targetUDPConn != nil {
762+
go c.commonUDPLoop()
763+
}
748764
return
749765
}
750766

@@ -1014,9 +1030,13 @@ func (c *Common) commonOnce() error {
10141030
// 处理信号
10151031
switch signalURL.Fragment {
10161032
case "1": // TCP
1017-
go c.commonTCPOnce(signalURL)
1033+
if len(c.targetTCPAddrs) > 0 {
1034+
go c.commonTCPOnce(signalURL)
1035+
}
10181036
case "2": // UDP
1019-
go c.commonUDPOnce(signalURL)
1037+
if c.disableUDP != "1" {
1038+
go c.commonUDPOnce(signalURL)
1039+
}
10201040
case "c": // 连接池清理
10211041
go func() {
10221042
c.tunnelPool.Clean()

internal/master.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,7 +1775,7 @@ func (m *Master) generateConfigURL(instance *Instance) string {
17751775
// 根据实例类型设置默认参数
17761776
switch instance.Type {
17771777
case "client":
1778-
// client参数: min, mode, read, rate, slot, proxy
1778+
// client参数: min, mode, read, rate, slot, proxy, noudp
17791779
if query.Get("min") == "" {
17801780
query.Set("min", strconv.Itoa(defaultMinPool))
17811781
}
@@ -1794,8 +1794,11 @@ func (m *Master) generateConfigURL(instance *Instance) string {
17941794
if query.Get("proxy") == "" {
17951795
query.Set("proxy", defaultProxyProtocol)
17961796
}
1797+
if query.Get("noudp") == "" {
1798+
query.Set("noudp", defaultUDPStrategy)
1799+
}
17971800
case "server":
1798-
// server参数: max, mode, read, rate, slot, proxy
1801+
// server参数: max, mode, read, rate, slot, proxy, noudp
17991802
if query.Get("max") == "" {
18001803
query.Set("max", strconv.Itoa(defaultMaxPool))
18011804
}
@@ -1814,6 +1817,9 @@ func (m *Master) generateConfigURL(instance *Instance) string {
18141817
if query.Get("proxy") == "" {
18151818
query.Set("proxy", defaultProxyProtocol)
18161819
}
1820+
if query.Get("noudp") == "" {
1821+
query.Set("noudp", defaultUDPStrategy)
1822+
}
18171823
}
18181824

18191825
parsedURL.RawQuery = query.Encode()

internal/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ func NewServer(parsedURL *url.URL, tlsCode string, tlsConfig *tls.Config, logger
6464
// Run 管理服务端生命周期
6565
func (s *Server) Run() {
6666
logInfo := func(prefix string) {
67-
s.logger.Info("%v: server://%v@%v/%v?max=%v&mode=%v&read=%v&rate=%v&slot=%v&proxy=%v",
67+
s.logger.Info("%v: server://%v@%v/%v?max=%v&mode=%v&read=%v&rate=%v&slot=%v&proxy=%v&noudp=%v",
6868
prefix, s.tunnelKey, s.tunnelTCPAddr, s.getTargetAddrsString(),
69-
s.maxPoolCapacity, s.runMode, s.readTimeout, s.rateLimit/125000, s.slotLimit, s.proxyProtocol)
69+
s.maxPoolCapacity, s.runMode, s.readTimeout, s.rateLimit/125000, s.slotLimit, s.proxyProtocol, s.disableUDP)
7070
}
7171
logInfo("Server started")
7272

@@ -85,7 +85,7 @@ func (s *Server) Run() {
8585
return
8686
case <-time.After(serviceCooldown):
8787
}
88-
logInfo("Server restarting")
88+
logInfo("Server restart")
8989
}
9090
}
9191
}()

0 commit comments

Comments
 (0)