@@ -1078,7 +1078,7 @@ func (c *Common) commonTCPOnce(signalURL *url.URL) {
10781078 // 从连接池获取连接
10791079 remoteConn , err := c .tunnelPool .ClientGet (id , poolGetTimeout )
10801080 if err != nil {
1081- c .logger .Warn ("commonTCPOnce: clientGet failed : %v" , err )
1081+ c .logger .Error ("commonTCPOnce: request timeout : %v" , err )
10821082 c .tunnelPool .AddError ()
10831083 return
10841084 }
@@ -1184,14 +1184,38 @@ func (c *Common) commonUDPOnce(signalURL *url.URL) {
11841184 // 获取池连接
11851185 remoteConn , err := c .tunnelPool .ClientGet (id , poolGetTimeout )
11861186 if err != nil {
1187- c .logger .Warn ("commonUDPOnce: clientGet failed : %v" , err )
1187+ c .logger .Error ("commonUDPOnce: request timeout : %v" , err )
11881188 c .tunnelPool .AddError ()
11891189 return
11901190 }
11911191
11921192 c .logger .Debug ("Tunnel connection: get %v <- pool active %v" , id , c .tunnelPool .Active ())
11931193 c .logger .Debug ("Tunnel connection: %v <-> %v" , remoteConn .LocalAddr (), remoteConn .RemoteAddr ())
11941194
1195+ defer func () {
1196+ // 重置池连接的读取超时
1197+ remoteConn .SetReadDeadline (time.Time {})
1198+ c .tunnelPool .Put (id , remoteConn )
1199+ c .logger .Debug ("Tunnel connection: put %v -> pool active %v" , id , c .tunnelPool .Active ())
1200+
1201+ // 发送关闭信号到对端
1202+ closeURL := & url.URL {
1203+ Scheme : "np" ,
1204+ Path : url .PathEscape (id ),
1205+ Fragment : "0" , // 关闭隧道
1206+ }
1207+
1208+ if c .ctx .Err () == nil && c .tunnelTCPConn != nil {
1209+ c .mu .Lock ()
1210+ _ , err := c .tunnelTCPConn .Write (c .encode ([]byte (closeURL .String ())))
1211+ c .mu .Unlock ()
1212+
1213+ if err != nil {
1214+ c .logger .Error ("commonUDPOnce: write close signal failed: %v" , err )
1215+ }
1216+ }
1217+ }()
1218+
11951219 var targetConn net.Conn
11961220 sessionKey := signalURL .Host
11971221
@@ -1210,6 +1234,15 @@ func (c *Common) commonUDPOnce(signalURL *url.URL) {
12101234 c .targetUDPSession .Store (sessionKey , targetConn )
12111235 c .logger .Debug ("Target connection: %v <-> %v" , targetConn .LocalAddr (), targetConn .RemoteAddr ())
12121236 }
1237+
1238+ defer func () {
1239+ // 清理UDP会话
1240+ c .targetUDPSession .Delete (sessionKey )
1241+ if targetConn != nil {
1242+ targetConn .Close ()
1243+ }
1244+ }()
1245+
12131246 c .logger .Debug ("Starting transfer: %v <-> %v" , remoteConn .LocalAddr (), targetConn .LocalAddr ())
12141247
12151248 done := make (chan struct {}, 2 )
@@ -1290,34 +1323,6 @@ func (c *Common) commonUDPOnce(signalURL *url.URL) {
12901323
12911324 // 等待任一协程完成
12921325 <- done
1293-
1294- // 清理连接和会话
1295- c .targetUDPSession .Delete (sessionKey )
1296- if targetConn != nil {
1297- targetConn .Close ()
1298- }
1299-
1300- // 重置池连接的读取超时
1301- remoteConn .SetReadDeadline (time.Time {})
1302- c .tunnelPool .Put (id , remoteConn )
1303- c .logger .Debug ("Tunnel connection: put %v -> pool active %v" , id , c .tunnelPool .Active ())
1304-
1305- // 发送关闭信号到对端
1306- closeURL := & url.URL {
1307- Scheme : "np" ,
1308- Path : url .PathEscape (id ),
1309- Fragment : "0" , // 关闭隧道
1310- }
1311-
1312- if c .ctx .Err () == nil && c .tunnelTCPConn != nil {
1313- c .mu .Lock ()
1314- _ , err = c .tunnelTCPConn .Write (c .encode ([]byte (closeURL .String ())))
1315- c .mu .Unlock ()
1316-
1317- if err != nil {
1318- c .logger .Error ("commonUDPOnce: write close signal failed: %v" , err )
1319- }
1320- }
13211326}
13221327
13231328// commonOnceKiller 共用处理关闭隧道请求
0 commit comments