@@ -54,10 +54,10 @@ type Common struct {
5454var (
5555 semaphoreLimit = getEnvAsInt ("NP_SEMAPHORE_LIMIT" , 1024 ) // 信号量限制
5656 udpDataBufSize = getEnvAsInt ("NP_UDP_DATA_BUF_SIZE" , 8192 ) // UDP缓冲区大小
57- udpReadTimeout = getEnvAsDuration ("NP_UDP_READ_TIMEOUT" , 10 * time .Second ) // UDP读取超时
58- udpDialTimeout = getEnvAsDuration ("NP_UDP_DIAL_TIMEOUT" , 10 * time .Second ) // UDP拨号超时
59- tcpReadTimeout = getEnvAsDuration ("NP_TCP_READ_TIMEOUT" , 10 * time .Second ) // TCP读取超时
60- tcpDialTimeout = getEnvAsDuration ("NP_TCP_DIAL_TIMEOUT" , 10 * time .Second ) // TCP拨号超时
57+ udpReadTimeout = getEnvAsDuration ("NP_UDP_READ_TIMEOUT" , 20 * time .Second ) // UDP读取超时
58+ udpDialTimeout = getEnvAsDuration ("NP_UDP_DIAL_TIMEOUT" , 20 * time .Second ) // UDP拨号超时
59+ tcpReadTimeout = getEnvAsDuration ("NP_TCP_READ_TIMEOUT" , 20 * time .Second ) // TCP读取超时
60+ tcpDialTimeout = getEnvAsDuration ("NP_TCP_DIAL_TIMEOUT" , 20 * time .Second ) // TCP拨号超时
6161 minPoolInterval = getEnvAsDuration ("NP_MIN_POOL_INTERVAL" , 1 * time .Second ) // 最小池间隔
6262 maxPoolInterval = getEnvAsDuration ("NP_MAX_POOL_INTERVAL" , 5 * time .Second ) // 最大池间隔
6363 reportInterval = getEnvAsDuration ("NP_REPORT_INTERVAL" , 5 * time .Second ) // 报告间隔
@@ -234,7 +234,7 @@ func (c *Common) stop() {
234234 if c .tunnelPool != nil {
235235 active := c .tunnelPool .Active ()
236236 c .tunnelPool .Close ()
237- c .logger .Debug ("Tunnel connection closed: active %v" , active )
237+ c .logger .Debug ("Tunnel connection closed: pool active %v" , active )
238238 }
239239
240240 // 清理目标UDP会话
@@ -419,17 +419,16 @@ func (c *Common) commonTCPLoop() {
419419 // 从连接池获取连接
420420 id , remoteConn := c .tunnelPool .ServerGet ()
421421 if remoteConn == nil {
422- c .logger .Error ("Get failed: %v" , id )
422+ c .logger .Error ("Get failed: %v not found " , id )
423423 c .tunnelPool .AddError ()
424424 return
425425 }
426426
427- c .logger .Debug ("Tunnel connection: %v <- active %v" , id , c .tunnelPool .Active ())
427+ c .logger .Debug ("Tunnel connection: get %v <- pool active %v" , id , c .tunnelPool .Active ())
428428
429429 defer func () {
430- if remoteConn != nil {
431- remoteConn .Close ()
432- }
430+ c .tunnelPool .Put (id , remoteConn )
431+ c .logger .Debug ("Tunnel connection: put %v -> pool active %v" , id , c .tunnelPool .Active ())
433432 }()
434433
435434 c .logger .Debug ("Tunnel connection: %v <-> %v" , remoteConn .LocalAddr (), remoteConn .RemoteAddr ())
@@ -453,7 +452,7 @@ func (c *Common) commonTCPLoop() {
453452 c .logger .Debug ("Starting exchange: %v <-> %v" , remoteConn .LocalAddr (), targetConn .LocalAddr ())
454453
455454 // 交换数据
456- rx , tx , _ := conn .DataExchange (remoteConn , targetConn )
455+ rx , tx , _ := conn .DataExchange (remoteConn , targetConn , tcpReadTimeout )
457456
458457 // 交换完成,广播统计信息
459458 c .logger .Event ("Exchange complete: TRAFFIC_STATS|TCP_RX=%v|TCP_TX=%v|UDP_RX=0|UDP_TX=0" , rx , tx )
@@ -481,16 +480,16 @@ func (c *Common) commonUDPLoop() {
481480 // 从连接池获取连接
482481 id , remoteConn := c .tunnelPool .ServerGet ()
483482 if remoteConn == nil {
483+ c .logger .Error ("Get failed: %v not found" , id )
484484 c .tunnelPool .AddError ()
485485 continue
486486 }
487487
488- c .logger .Debug ("Tunnel connection: %v <- active %v" , id , c .tunnelPool .Active ())
488+ c .logger .Debug ("Tunnel connection: get %v <- pool active %v" , id , c .tunnelPool .Active ())
489489
490490 defer func () {
491- if remoteConn != nil {
492- remoteConn .Close ()
493- }
491+ c .tunnelPool .Put (id , remoteConn )
492+ c .logger .Debug ("Tunnel connection: put %v -> pool active %v" , id , c .tunnelPool .Active ())
494493 }()
495494
496495 c .logger .Debug ("Tunnel connection: %v <-> %v" , remoteConn .LocalAddr (), remoteConn .RemoteAddr ())
@@ -582,17 +581,15 @@ func (c *Common) commonTCPOnce(id string) {
582581 // 从连接池获取连接
583582 remoteConn := c .tunnelPool .ClientGet (id )
584583 if remoteConn == nil {
585- c .logger .Error ("Get failed: %v" , id )
584+ c .logger .Error ("Get failed: %v not found " , id )
586585 return
587586 }
588587
589- c .logger .Debug ("Tunnel connection: %v <- active %v" , id , c .tunnelPool .Active ())
588+ c .logger .Debug ("Tunnel connection: get %v <- pool active %v" , id , c .tunnelPool .Active ())
590589
591- // 确保连接关闭
592590 defer func () {
593- if remoteConn != nil {
594- remoteConn .Close ()
595- }
591+ c .tunnelPool .Put (id , remoteConn )
592+ c .logger .Debug ("Tunnel connection: put %v -> pool active %v" , id , c .tunnelPool .Active ())
596593 }()
597594
598595 c .logger .Debug ("Tunnel connection: %v <-> %v" , remoteConn .LocalAddr (), remoteConn .RemoteAddr ())
@@ -615,7 +612,7 @@ func (c *Common) commonTCPOnce(id string) {
615612 c .logger .Debug ("Starting exchange: %v <-> %v" , remoteConn .LocalAddr (), targetConn .LocalAddr ())
616613
617614 // 交换数据
618- rx , tx , _ := conn .DataExchange (remoteConn , targetConn )
615+ rx , tx , _ := conn .DataExchange (remoteConn , targetConn , tcpReadTimeout )
619616
620617 // 交换完成,广播统计信息
621618 c .logger .Event ("Exchange complete: TRAFFIC_STATS|TCP_RX=%v|TCP_TX=%v|UDP_RX=0|UDP_TX=0" , rx , tx )
@@ -628,17 +625,15 @@ func (c *Common) commonUDPOnce(id string) {
628625 // 从连接池获取连接
629626 remoteConn := c .tunnelPool .ClientGet (id )
630627 if remoteConn == nil {
631- c .logger .Error ("Get failed: %v" , id )
628+ c .logger .Error ("Get failed: %v not found " , id )
632629 return
633630 }
634631
635- c .logger .Debug ("Tunnel connection: %v <- active %v" , id , c .tunnelPool .Active ())
632+ c .logger .Debug ("Tunnel connection: get %v <- pool active %v" , id , c .tunnelPool .Active ())
636633
637- // 确保连接关闭
638634 defer func () {
639- if remoteConn != nil {
640- remoteConn .Close ()
641- }
635+ c .tunnelPool .Put (id , remoteConn )
636+ c .logger .Debug ("Tunnel connection: put %v -> pool active %v" , id , c .tunnelPool .Active ())
642637 }()
643638
644639 c .logger .Debug ("Tunnel connection: %v <-> %v" , remoteConn .LocalAddr (), remoteConn .RemoteAddr ())
@@ -727,7 +722,7 @@ func (c *Common) singleTCPLoop() error {
727722 return
728723 }
729724
730- c .logger .Debug ("Target connection: active %v / %v per %v" , c .tunnelPool .Active (), c .tunnelPool .Capacity (), c .tunnelPool .Interval ())
725+ c .logger .Debug ("Target connection: pool active %v / %v per %v" , c .tunnelPool .Active (), c .tunnelPool .Capacity (), c .tunnelPool .Interval ())
731726
732727 defer func () {
733728 if targetConn != nil {
@@ -740,7 +735,7 @@ func (c *Common) singleTCPLoop() error {
740735 c .logger .Debug ("Starting exchange: %v <-> %v" , tunnelConn .LocalAddr (), targetConn .LocalAddr ())
741736
742737 // 交换数据
743- rx , tx , _ := conn .DataExchange (tunnelConn , targetConn )
738+ rx , tx , _ := conn .DataExchange (tunnelConn , targetConn , tcpReadTimeout )
744739
745740 // 交换完成,广播统计信息
746741 c .logger .Event ("Exchange complete: TRAFFIC_STATS|TCP_RX=%v|TCP_TX=%v|UDP_RX=0|UDP_TX=0" , rx , tx )
0 commit comments