Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div align="center">
<img src="https://cdn.yobc.de/assets/np-poster.png" alt="nodepass" width="448">
<img src="https://cdn.yobc.de/assets/np-gopher.png" alt="nodepass" width="300">

[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)
[![GitHub release](https://img.shields.io/github/v/release/yosebyte/nodepass)](https://github.com/yosebyte/nodepass/releases)
Expand Down
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div align="center">
<img src="https://cdn.yobc.de/assets/np-poster.png" alt="nodepass" width="448">
<img src="https://cdn.yobc.de/assets/np-gopher.png" alt="nodepass" width="300">

[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)
[![GitHub release](https://img.shields.io/github/v/release/yosebyte/nodepass)](https://github.com/yosebyte/nodepass/releases)
Expand Down
8 changes: 4 additions & 4 deletions docs/en/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ NodePass behavior can be fine-tuned using environment variables. Below is the co
|----------|-------------|---------|---------|
| `NP_SEMAPHORE_LIMIT` | Maximum number of concurrent connections | 1024 | `export NP_SEMAPHORE_LIMIT=2048` |
| `NP_UDP_DATA_BUF_SIZE` | Buffer size for UDP packets | 8192 | `export NP_UDP_DATA_BUF_SIZE=16384` |
| `NP_UDP_READ_TIMEOUT` | Timeout for UDP read operations | 10s | `export NP_UDP_READ_TIMEOUT=30s` |
| `NP_UDP_DIAL_TIMEOUT` | Timeout for establishing UDP connections | 10s | `export NP_UDP_DIAL_TIMEOUT=30s` |
| `NP_TCP_READ_TIMEOUT` | Timeout for TCP read operations | 10s | `export NP_TCP_READ_TIMEOUT=30s` |
| `NP_TCP_DIAL_TIMEOUT` | Timeout for establishing TCP connections | 10s | `export NP_TCP_DIAL_TIMEOUT=30s` |
| `NP_UDP_READ_TIMEOUT` | Timeout for UDP read operations | 20s | `export NP_UDP_READ_TIMEOUT=30s` |
| `NP_UDP_DIAL_TIMEOUT` | Timeout for establishing UDP connections | 20s | `export NP_UDP_DIAL_TIMEOUT=30s` |
| `NP_TCP_READ_TIMEOUT` | Timeout for TCP read operations | 20s | `export NP_TCP_READ_TIMEOUT=30s` |
| `NP_TCP_DIAL_TIMEOUT` | Timeout for establishing TCP connections | 20s | `export NP_TCP_DIAL_TIMEOUT=30s` |
| `NP_MIN_POOL_INTERVAL` | Minimum interval between connection creations | 1s | `export NP_MIN_POOL_INTERVAL=500ms` |
| `NP_MAX_POOL_INTERVAL` | Maximum interval between connection creations | 5s | `export NP_MAX_POOL_INTERVAL=3s` |
| `NP_REPORT_INTERVAL` | Interval for health check reports | 5s | `export NP_REPORT_INTERVAL=10s` |
Expand Down
8 changes: 4 additions & 4 deletions docs/zh/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ nodepass "client://server.example.com:10101/127.0.0.1:8080?min=32&max=4096"
|----------|-------------|---------|---------|
| `NP_SEMAPHORE_LIMIT` | 最大并发连接数 | 1024 | `export NP_SEMAPHORE_LIMIT=2048` |
| `NP_UDP_DATA_BUF_SIZE` | UDP数据包缓冲区大小 | 8192 | `export NP_UDP_DATA_BUF_SIZE=16384` |
| `NP_UDP_READ_TIMEOUT` | UDP读取操作超时 | 10s | `export NP_UDP_READ_TIMEOUT=30s` |
| `NP_UDP_DIAL_TIMEOUT` | UDP连接建立超时 | 10s | `export NP_UDP_DIAL_TIMEOUT=30s` |
| `NP_TCP_READ_TIMEOUT` | TCP读取操作超时 | 10s | `export NP_TCP_READ_TIMEOUT=30s` |
| `NP_TCP_DIAL_TIMEOUT` | TCP连接建立超时 | 10s | `export NP_TCP_DIAL_TIMEOUT=30s` |
| `NP_UDP_READ_TIMEOUT` | UDP读取操作超时 | 20s | `export NP_UDP_READ_TIMEOUT=30s` |
| `NP_UDP_DIAL_TIMEOUT` | UDP连接建立超时 | 20s | `export NP_UDP_DIAL_TIMEOUT=30s` |
| `NP_TCP_READ_TIMEOUT` | TCP读取操作超时 | 20s | `export NP_TCP_READ_TIMEOUT=30s` |
| `NP_TCP_DIAL_TIMEOUT` | TCP连接建立超时 | 20s | `export NP_TCP_DIAL_TIMEOUT=30s` |
| `NP_MIN_POOL_INTERVAL` | 连接创建之间的最小间隔 | 1s | `export NP_MIN_POOL_INTERVAL=500ms` |
| `NP_MAX_POOL_INTERVAL` | 连接创建之间的最大间隔 | 5s | `export NP_MAX_POOL_INTERVAL=3s` |
| `NP_REPORT_INTERVAL` | 健康检查报告间隔 | 5s | `export NP_REPORT_INTERVAL=10s` |
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.24.3

require (
github.com/NodePassProject/cert v1.0.0
github.com/NodePassProject/conn v1.0.0
github.com/NodePassProject/conn v1.0.1
github.com/NodePassProject/logs v1.0.1
github.com/NodePassProject/pool v1.0.7
github.com/NodePassProject/pool v1.0.8
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/NodePassProject/cert v1.0.0 h1:cBNNvR+ja22AgNlUmeGWLcCM1vmnLTqpbCQ4Hdn5was=
github.com/NodePassProject/cert v1.0.0/go.mod h1:4EJDS3GozJ74dtICJ/xcq42WKKvF0tiTM9/M7Q9NF9c=
github.com/NodePassProject/conn v1.0.0 h1:rN0wf/N3gB8BJJ2ZwD6ij/ZKgd6CDRY/MeEZ40BJ98U=
github.com/NodePassProject/conn v1.0.0/go.mod h1:mWe3Rylunp6Sx4v6pkSGgYZe2R+I/O+7nZ2od0yJ3aQ=
github.com/NodePassProject/conn v1.0.1 h1:vuzcQQj+cqENagzEYPwse9Vvlj/8vfkyNZCp5RvQMKk=
github.com/NodePassProject/conn v1.0.1/go.mod h1:mWe3Rylunp6Sx4v6pkSGgYZe2R+I/O+7nZ2od0yJ3aQ=
github.com/NodePassProject/logs v1.0.1 h1:WDHY1DcTO+7NydBzuRpxhEw6pWYayBdDjjZzU1uDKac=
github.com/NodePassProject/logs v1.0.1/go.mod h1:ocFTMNXBTnQFJFAhF+qobAzu7+y+wYPik7D+a1jPfis=
github.com/NodePassProject/pool v1.0.7 h1:uLMSDutf2wTy77ep2mmeeCaxWVD57qaZSYb7+pXdnRg=
github.com/NodePassProject/pool v1.0.7/go.mod h1:kdRAEDK45j/+iHH4kRTpXt/wI28NIguJ13n/5NDXxkw=
github.com/NodePassProject/pool v1.0.8 h1:zuqVdQj0OBarIo/P/BdpTxXk8kbjU2GYJJaVA5T+LwQ=
github.com/NodePassProject/pool v1.0.8/go.mod h1:kdRAEDK45j/+iHH4kRTpXt/wI28NIguJ13n/5NDXxkw=
55 changes: 25 additions & 30 deletions internal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ type Common struct {
var (
semaphoreLimit = getEnvAsInt("NP_SEMAPHORE_LIMIT", 1024) // 信号量限制
udpDataBufSize = getEnvAsInt("NP_UDP_DATA_BUF_SIZE", 8192) // UDP缓冲区大小
udpReadTimeout = getEnvAsDuration("NP_UDP_READ_TIMEOUT", 10*time.Second) // UDP读取超时
udpDialTimeout = getEnvAsDuration("NP_UDP_DIAL_TIMEOUT", 10*time.Second) // UDP拨号超时
tcpReadTimeout = getEnvAsDuration("NP_TCP_READ_TIMEOUT", 10*time.Second) // TCP读取超时
tcpDialTimeout = getEnvAsDuration("NP_TCP_DIAL_TIMEOUT", 10*time.Second) // TCP拨号超时
udpReadTimeout = getEnvAsDuration("NP_UDP_READ_TIMEOUT", 20*time.Second) // UDP读取超时
udpDialTimeout = getEnvAsDuration("NP_UDP_DIAL_TIMEOUT", 20*time.Second) // UDP拨号超时
tcpReadTimeout = getEnvAsDuration("NP_TCP_READ_TIMEOUT", 20*time.Second) // TCP读取超时
tcpDialTimeout = getEnvAsDuration("NP_TCP_DIAL_TIMEOUT", 20*time.Second) // TCP拨号超时
minPoolInterval = getEnvAsDuration("NP_MIN_POOL_INTERVAL", 1*time.Second) // 最小池间隔
maxPoolInterval = getEnvAsDuration("NP_MAX_POOL_INTERVAL", 5*time.Second) // 最大池间隔
reportInterval = getEnvAsDuration("NP_REPORT_INTERVAL", 5*time.Second) // 报告间隔
Expand Down Expand Up @@ -234,7 +234,7 @@ func (c *Common) stop() {
if c.tunnelPool != nil {
active := c.tunnelPool.Active()
c.tunnelPool.Close()
c.logger.Debug("Tunnel connection closed: active %v", active)
c.logger.Debug("Tunnel connection closed: pool active %v", active)
}

// 清理目标UDP会话
Expand Down Expand Up @@ -419,17 +419,16 @@ func (c *Common) commonTCPLoop() {
// 从连接池获取连接
id, remoteConn := c.tunnelPool.ServerGet()
if remoteConn == nil {
c.logger.Error("Get failed: %v", id)
c.logger.Error("Get failed: %v not found", id)
c.tunnelPool.AddError()
return
}

c.logger.Debug("Tunnel connection: %v <- active %v", id, c.tunnelPool.Active())
c.logger.Debug("Tunnel connection: get %v <- pool active %v", id, c.tunnelPool.Active())

defer func() {
if remoteConn != nil {
remoteConn.Close()
}
c.tunnelPool.Put(id, remoteConn)
c.logger.Debug("Tunnel connection: put %v -> pool active %v", id, c.tunnelPool.Active())
}()

c.logger.Debug("Tunnel connection: %v <-> %v", remoteConn.LocalAddr(), remoteConn.RemoteAddr())
Expand All @@ -453,7 +452,7 @@ func (c *Common) commonTCPLoop() {
c.logger.Debug("Starting exchange: %v <-> %v", remoteConn.LocalAddr(), targetConn.LocalAddr())

// 交换数据
rx, tx, _ := conn.DataExchange(remoteConn, targetConn)
rx, tx, _ := conn.DataExchange(remoteConn, targetConn, tcpReadTimeout)

// 交换完成,广播统计信息
c.logger.Event("Exchange complete: TRAFFIC_STATS|TCP_RX=%v|TCP_TX=%v|UDP_RX=0|UDP_TX=0", rx, tx)
Expand Down Expand Up @@ -481,16 +480,16 @@ func (c *Common) commonUDPLoop() {
// 从连接池获取连接
id, remoteConn := c.tunnelPool.ServerGet()
if remoteConn == nil {
c.logger.Error("Get failed: %v not found", id)
c.tunnelPool.AddError()
continue
}

c.logger.Debug("Tunnel connection: %v <- active %v", id, c.tunnelPool.Active())
c.logger.Debug("Tunnel connection: get %v <- pool active %v", id, c.tunnelPool.Active())

defer func() {
if remoteConn != nil {
remoteConn.Close()
}
c.tunnelPool.Put(id, remoteConn)
c.logger.Debug("Tunnel connection: put %v -> pool active %v", id, c.tunnelPool.Active())
}()

c.logger.Debug("Tunnel connection: %v <-> %v", remoteConn.LocalAddr(), remoteConn.RemoteAddr())
Expand Down Expand Up @@ -582,17 +581,15 @@ func (c *Common) commonTCPOnce(id string) {
// 从连接池获取连接
remoteConn := c.tunnelPool.ClientGet(id)
if remoteConn == nil {
c.logger.Error("Get failed: %v", id)
c.logger.Error("Get failed: %v not found", id)
return
}

c.logger.Debug("Tunnel connection: %v <- active %v", id, c.tunnelPool.Active())
c.logger.Debug("Tunnel connection: get %v <- pool active %v", id, c.tunnelPool.Active())

// 确保连接关闭
defer func() {
if remoteConn != nil {
remoteConn.Close()
}
c.tunnelPool.Put(id, remoteConn)
c.logger.Debug("Tunnel connection: put %v -> pool active %v", id, c.tunnelPool.Active())
}()

c.logger.Debug("Tunnel connection: %v <-> %v", remoteConn.LocalAddr(), remoteConn.RemoteAddr())
Expand All @@ -615,7 +612,7 @@ func (c *Common) commonTCPOnce(id string) {
c.logger.Debug("Starting exchange: %v <-> %v", remoteConn.LocalAddr(), targetConn.LocalAddr())

// 交换数据
rx, tx, _ := conn.DataExchange(remoteConn, targetConn)
rx, tx, _ := conn.DataExchange(remoteConn, targetConn, tcpReadTimeout)

// 交换完成,广播统计信息
c.logger.Event("Exchange complete: TRAFFIC_STATS|TCP_RX=%v|TCP_TX=%v|UDP_RX=0|UDP_TX=0", rx, tx)
Expand All @@ -628,17 +625,15 @@ func (c *Common) commonUDPOnce(id string) {
// 从连接池获取连接
remoteConn := c.tunnelPool.ClientGet(id)
if remoteConn == nil {
c.logger.Error("Get failed: %v", id)
c.logger.Error("Get failed: %v not found", id)
return
}

c.logger.Debug("Tunnel connection: %v <- active %v", id, c.tunnelPool.Active())
c.logger.Debug("Tunnel connection: get %v <- pool active %v", id, c.tunnelPool.Active())

// 确保连接关闭
defer func() {
if remoteConn != nil {
remoteConn.Close()
}
c.tunnelPool.Put(id, remoteConn)
c.logger.Debug("Tunnel connection: put %v -> pool active %v", id, c.tunnelPool.Active())
}()

c.logger.Debug("Tunnel connection: %v <-> %v", remoteConn.LocalAddr(), remoteConn.RemoteAddr())
Expand Down Expand Up @@ -727,7 +722,7 @@ func (c *Common) singleTCPLoop() error {
return
}

c.logger.Debug("Target connection: active %v / %v per %v", c.tunnelPool.Active(), c.tunnelPool.Capacity(), c.tunnelPool.Interval())
c.logger.Debug("Target connection: pool active %v / %v per %v", c.tunnelPool.Active(), c.tunnelPool.Capacity(), c.tunnelPool.Interval())

defer func() {
if targetConn != nil {
Expand All @@ -740,7 +735,7 @@ func (c *Common) singleTCPLoop() error {
c.logger.Debug("Starting exchange: %v <-> %v", tunnelConn.LocalAddr(), targetConn.LocalAddr())

// 交换数据
rx, tx, _ := conn.DataExchange(tunnelConn, targetConn)
rx, tx, _ := conn.DataExchange(tunnelConn, targetConn, tcpReadTimeout)

// 交换完成,广播统计信息
c.logger.Event("Exchange complete: TRAFFIC_STATS|TCP_RX=%v|TCP_TX=%v|UDP_RX=0|UDP_TX=0", rx, tx)
Expand Down