@@ -635,11 +635,7 @@ func (c *Common) commonControl() error {
635635
636636// commonQueue 共用信号队列
637637func (c * Common ) commonQueue () error {
638- for {
639- if c .ctx .Err () != nil {
640- return fmt .Errorf ("commonQueue: context error: %w" , c .ctx .Err ())
641- }
642-
638+ for c .ctx .Err () == nil {
643639 // 读取原始信号
644640 rawSignal , err := c .bufReader .ReadBytes ('\n' )
645641 if err != nil {
@@ -671,18 +667,16 @@ func (c *Common) commonQueue() error {
671667 }
672668 }
673669 }
670+
671+ return fmt .Errorf ("commonQueue: context error: %w" , c .ctx .Err ())
674672}
675673
676674// healthCheck 共用健康度检查
677675func (c * Common ) healthCheck () error {
678676 ticker := time .NewTicker (reportInterval )
679677 defer ticker .Stop ()
680678
681- for {
682- if c .ctx .Err () != nil {
683- return fmt .Errorf ("healthCheck: context error: %w" , c .ctx .Err ())
684- }
685-
679+ for c .ctx .Err () == nil {
686680 // 尝试获取锁
687681 if ! c .mu .TryLock () {
688682 continue
@@ -742,15 +736,13 @@ func (c *Common) healthCheck() error {
742736 case <- ticker .C :
743737 }
744738 }
739+
740+ return fmt .Errorf ("healthCheck: context error: %w" , c .ctx .Err ())
745741}
746742
747743// commonLoop 共用处理循环
748744func (c * Common ) commonLoop () {
749- for {
750- if c .ctx .Err () != nil {
751- return
752- }
753-
745+ for c .ctx .Err () == nil {
754746 // 等待连接池准备就绪
755747 if c .tunnelPool .Ready () {
756748 go c .commonTCPLoop ()
@@ -768,11 +760,7 @@ func (c *Common) commonLoop() {
768760
769761// commonTCPLoop 共用TCP请求处理循环
770762func (c * Common ) commonTCPLoop () {
771- for {
772- if c .ctx .Err () != nil {
773- return
774- }
775-
763+ for c .ctx .Err () == nil {
776764 // 接受来自目标的TCP连接
777765 targetConn , err := c .targetListener .Accept ()
778766 if err != nil {
@@ -867,11 +855,7 @@ func (c *Common) commonTCPLoop() {
867855
868856// commonUDPLoop 共用UDP请求处理循环
869857func (c * Common ) commonUDPLoop () {
870- for {
871- if c .ctx .Err () != nil {
872- return
873- }
874-
858+ for c .ctx .Err () == nil {
875859 buffer := c .getUDPBuffer ()
876860
877861 // 读取来自目标的UDP数据
@@ -943,7 +927,7 @@ func (c *Common) commonUDPLoop() {
943927 defer c .putUDPBuffer (buffer )
944928 reader := & conn.TimeoutReader {Conn : remoteConn , Timeout : udpReadTimeout }
945929
946- for {
930+ for c . ctx . Err () == nil {
947931 // 从池连接读取数据
948932 x , err := reader .Read (buffer )
949933 if err != nil {
@@ -1010,7 +994,7 @@ func (c *Common) commonUDPLoop() {
1010994
1011995// commonOnce 共用处理单个请求
1012996func (c * Common ) commonOnce () error {
1013- for {
997+ for c . ctx . Err () == nil {
1014998 // 等待连接池准备就绪
1015999 if ! c .tunnelPool .Ready () {
10161000 select {
@@ -1089,6 +1073,8 @@ func (c *Common) commonOnce() error {
10891073 }
10901074 }
10911075 }
1076+
1077+ return fmt .Errorf ("commonOnce: context error: %w" , c .ctx .Err ())
10921078}
10931079
10941080// commonTCPOnce 共用处理单个TCP请求
@@ -1254,7 +1240,7 @@ func (c *Common) commonUDPOnce(signalURL *url.URL) {
12541240 defer c .putUDPBuffer (buffer )
12551241 reader := & conn.TimeoutReader {Conn : remoteConn , Timeout : udpReadTimeout }
12561242
1257- for {
1243+ for c . ctx . Err () == nil {
12581244 // 从隧道连接读取数据
12591245 x , err := reader .Read (buffer )
12601246 if err != nil {
@@ -1287,7 +1273,7 @@ func (c *Common) commonUDPOnce(signalURL *url.URL) {
12871273 defer c .putUDPBuffer (buffer )
12881274 reader := & conn.TimeoutReader {Conn : targetConn , Timeout : udpReadTimeout }
12891275
1290- for {
1276+ for c . ctx . Err () == nil {
12911277 // 从目标UDP连接读取数据
12921278 x , err := reader .Read (buffer )
12931279 if err != nil {
@@ -1345,11 +1331,7 @@ func (c *Common) singleEventLoop() error {
13451331 ticker := time .NewTicker (reportInterval )
13461332 defer ticker .Stop ()
13471333
1348- for {
1349- if c .ctx .Err () != nil {
1350- return fmt .Errorf ("singleEventLoop: context error: %w" , c .ctx .Err ())
1351- }
1352-
1334+ for c .ctx .Err () == nil {
13531335 ping := 0
13541336 now := time .Now ()
13551337
@@ -1372,15 +1354,13 @@ func (c *Common) singleEventLoop() error {
13721354 case <- ticker .C :
13731355 }
13741356 }
1357+
1358+ return fmt .Errorf ("singleEventLoop: context error: %w" , c .ctx .Err ())
13751359}
13761360
13771361// singleTCPLoop 单端转发TCP处理循环
13781362func (c * Common ) singleTCPLoop () error {
1379- for {
1380- if c .ctx .Err () != nil {
1381- return fmt .Errorf ("singleTCPLoop: context error: %w" , c .ctx .Err ())
1382- }
1383-
1363+ for c .ctx .Err () == nil {
13841364 // 接受来自隧道的TCP连接
13851365 tunnelConn , err := c .tunnelListener .Accept ()
13861366 if err != nil {
@@ -1447,15 +1427,13 @@ func (c *Common) singleTCPLoop() error {
14471427 c .logger .Debug ("Exchange complete: %v" , conn .DataExchange (tunnelConn , targetConn , c .readTimeout , buffer1 , buffer2 ))
14481428 }(tunnelConn )
14491429 }
1430+
1431+ return fmt .Errorf ("singleTCPLoop: context error: %w" , c .ctx .Err ())
14501432}
14511433
14521434// singleUDPLoop 单端转发UDP处理循环
14531435func (c * Common ) singleUDPLoop () error {
1454- for {
1455- if c .ctx .Err () != nil {
1456- return fmt .Errorf ("singleUDPLoop: context error: %w" , c .ctx .Err ())
1457- }
1458-
1436+ for c .ctx .Err () == nil {
14591437 buffer := c .getUDPBuffer ()
14601438
14611439 // 读取来自隧道的UDP数据
@@ -1518,11 +1496,7 @@ func (c *Common) singleUDPLoop() error {
15181496 defer c .putUDPBuffer (buffer )
15191497 reader := & conn.TimeoutReader {Conn : targetConn , Timeout : udpReadTimeout }
15201498
1521- for {
1522- if c .ctx .Err () != nil {
1523- return
1524- }
1525-
1499+ for c .ctx .Err () == nil {
15261500 // 从UDP读取响应
15271501 x , err := reader .Read (buffer )
15281502 if err != nil {
@@ -1575,4 +1549,6 @@ func (c *Common) singleUDPLoop() error {
15751549 c .logger .Debug ("Transfer complete: %v <-> %v" , targetConn .LocalAddr (), c .tunnelUDPConn .LocalAddr ())
15761550 c .putUDPBuffer (buffer )
15771551 }
1552+
1553+ return fmt .Errorf ("singleUDPLoop: context error: %w" , c .ctx .Err ())
15781554}
0 commit comments