Skip to content

Commit b0794df

Browse files
authored
Merge pull request #54 from NodePassProject/main
Improve Network Timeout Defaults, Update Dependencies, and Enhance Logging
2 parents 246c2f0 + 3f76d60 commit b0794df

File tree

7 files changed

+41
-46
lines changed

7 files changed

+41
-46
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div align="center">
2-
<img src="https://cdn.yobc.de/assets/np-poster.png" alt="nodepass" width="448">
2+
<img src="https://cdn.yobc.de/assets/np-gopher.png" alt="nodepass" width="300">
33

44
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)
55
[![GitHub release](https://img.shields.io/github/v/release/yosebyte/nodepass)](https://github.com/yosebyte/nodepass/releases)

README_zh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div align="center">
2-
<img src="https://cdn.yobc.de/assets/np-poster.png" alt="nodepass" width="448">
2+
<img src="https://cdn.yobc.de/assets/np-gopher.png" alt="nodepass" width="300">
33

44
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)
55
[![GitHub release](https://img.shields.io/github/v/release/yosebyte/nodepass)](https://github.com/yosebyte/nodepass/releases)

docs/en/configuration.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ NodePass behavior can be fine-tuned using environment variables. Below is the co
6767
|----------|-------------|---------|---------|
6868
| `NP_SEMAPHORE_LIMIT` | Maximum number of concurrent connections | 1024 | `export NP_SEMAPHORE_LIMIT=2048` |
6969
| `NP_UDP_DATA_BUF_SIZE` | Buffer size for UDP packets | 8192 | `export NP_UDP_DATA_BUF_SIZE=16384` |
70-
| `NP_UDP_READ_TIMEOUT` | Timeout for UDP read operations | 10s | `export NP_UDP_READ_TIMEOUT=30s` |
71-
| `NP_UDP_DIAL_TIMEOUT` | Timeout for establishing UDP connections | 10s | `export NP_UDP_DIAL_TIMEOUT=30s` |
72-
| `NP_TCP_READ_TIMEOUT` | Timeout for TCP read operations | 10s | `export NP_TCP_READ_TIMEOUT=30s` |
73-
| `NP_TCP_DIAL_TIMEOUT` | Timeout for establishing TCP connections | 10s | `export NP_TCP_DIAL_TIMEOUT=30s` |
70+
| `NP_UDP_READ_TIMEOUT` | Timeout for UDP read operations | 20s | `export NP_UDP_READ_TIMEOUT=30s` |
71+
| `NP_UDP_DIAL_TIMEOUT` | Timeout for establishing UDP connections | 20s | `export NP_UDP_DIAL_TIMEOUT=30s` |
72+
| `NP_TCP_READ_TIMEOUT` | Timeout for TCP read operations | 20s | `export NP_TCP_READ_TIMEOUT=30s` |
73+
| `NP_TCP_DIAL_TIMEOUT` | Timeout for establishing TCP connections | 20s | `export NP_TCP_DIAL_TIMEOUT=30s` |
7474
| `NP_MIN_POOL_INTERVAL` | Minimum interval between connection creations | 1s | `export NP_MIN_POOL_INTERVAL=500ms` |
7575
| `NP_MAX_POOL_INTERVAL` | Maximum interval between connection creations | 5s | `export NP_MAX_POOL_INTERVAL=3s` |
7676
| `NP_REPORT_INTERVAL` | Interval for health check reports | 5s | `export NP_REPORT_INTERVAL=10s` |

docs/zh/configuration.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ nodepass "client://server.example.com:10101/127.0.0.1:8080?min=32&max=4096"
6767
|----------|-------------|---------|---------|
6868
| `NP_SEMAPHORE_LIMIT` | 最大并发连接数 | 1024 | `export NP_SEMAPHORE_LIMIT=2048` |
6969
| `NP_UDP_DATA_BUF_SIZE` | UDP数据包缓冲区大小 | 8192 | `export NP_UDP_DATA_BUF_SIZE=16384` |
70-
| `NP_UDP_READ_TIMEOUT` | UDP读取操作超时 | 10s | `export NP_UDP_READ_TIMEOUT=30s` |
71-
| `NP_UDP_DIAL_TIMEOUT` | UDP连接建立超时 | 10s | `export NP_UDP_DIAL_TIMEOUT=30s` |
72-
| `NP_TCP_READ_TIMEOUT` | TCP读取操作超时 | 10s | `export NP_TCP_READ_TIMEOUT=30s` |
73-
| `NP_TCP_DIAL_TIMEOUT` | TCP连接建立超时 | 10s | `export NP_TCP_DIAL_TIMEOUT=30s` |
70+
| `NP_UDP_READ_TIMEOUT` | UDP读取操作超时 | 20s | `export NP_UDP_READ_TIMEOUT=30s` |
71+
| `NP_UDP_DIAL_TIMEOUT` | UDP连接建立超时 | 20s | `export NP_UDP_DIAL_TIMEOUT=30s` |
72+
| `NP_TCP_READ_TIMEOUT` | TCP读取操作超时 | 20s | `export NP_TCP_READ_TIMEOUT=30s` |
73+
| `NP_TCP_DIAL_TIMEOUT` | TCP连接建立超时 | 20s | `export NP_TCP_DIAL_TIMEOUT=30s` |
7474
| `NP_MIN_POOL_INTERVAL` | 连接创建之间的最小间隔 | 1s | `export NP_MIN_POOL_INTERVAL=500ms` |
7575
| `NP_MAX_POOL_INTERVAL` | 连接创建之间的最大间隔 | 5s | `export NP_MAX_POOL_INTERVAL=3s` |
7676
| `NP_REPORT_INTERVAL` | 健康检查报告间隔 | 5s | `export NP_REPORT_INTERVAL=10s` |

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.24.3
44

55
require (
66
github.com/NodePassProject/cert v1.0.0
7-
github.com/NodePassProject/conn v1.0.0
7+
github.com/NodePassProject/conn v1.0.1
88
github.com/NodePassProject/logs v1.0.1
9-
github.com/NodePassProject/pool v1.0.7
9+
github.com/NodePassProject/pool v1.0.8
1010
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
github.com/NodePassProject/cert v1.0.0 h1:cBNNvR+ja22AgNlUmeGWLcCM1vmnLTqpbCQ4Hdn5was=
22
github.com/NodePassProject/cert v1.0.0/go.mod h1:4EJDS3GozJ74dtICJ/xcq42WKKvF0tiTM9/M7Q9NF9c=
3-
github.com/NodePassProject/conn v1.0.0 h1:rN0wf/N3gB8BJJ2ZwD6ij/ZKgd6CDRY/MeEZ40BJ98U=
4-
github.com/NodePassProject/conn v1.0.0/go.mod h1:mWe3Rylunp6Sx4v6pkSGgYZe2R+I/O+7nZ2od0yJ3aQ=
3+
github.com/NodePassProject/conn v1.0.1 h1:vuzcQQj+cqENagzEYPwse9Vvlj/8vfkyNZCp5RvQMKk=
4+
github.com/NodePassProject/conn v1.0.1/go.mod h1:mWe3Rylunp6Sx4v6pkSGgYZe2R+I/O+7nZ2od0yJ3aQ=
55
github.com/NodePassProject/logs v1.0.1 h1:WDHY1DcTO+7NydBzuRpxhEw6pWYayBdDjjZzU1uDKac=
66
github.com/NodePassProject/logs v1.0.1/go.mod h1:ocFTMNXBTnQFJFAhF+qobAzu7+y+wYPik7D+a1jPfis=
7-
github.com/NodePassProject/pool v1.0.7 h1:uLMSDutf2wTy77ep2mmeeCaxWVD57qaZSYb7+pXdnRg=
8-
github.com/NodePassProject/pool v1.0.7/go.mod h1:kdRAEDK45j/+iHH4kRTpXt/wI28NIguJ13n/5NDXxkw=
7+
github.com/NodePassProject/pool v1.0.8 h1:zuqVdQj0OBarIo/P/BdpTxXk8kbjU2GYJJaVA5T+LwQ=
8+
github.com/NodePassProject/pool v1.0.8/go.mod h1:kdRAEDK45j/+iHH4kRTpXt/wI28NIguJ13n/5NDXxkw=

internal/common.go

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ type Common struct {
5454
var (
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

Comments
 (0)