Skip to content

Commit 5139e3c

Browse files
authored
Merge pull request #75 from NodePassProject/main
Add TCP support control, TLS certificate verification improvements, Connection pool and codebase refactoring
2 parents 1263661 + 494b60b commit 5139e3c

File tree

10 files changed

+285
-74
lines changed

10 files changed

+285
-74
lines changed

docs/en/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,4 +1582,5 @@ Examples:
15821582
| `rate` | Bandwidth rate limit | Integer (Mbps), 0=unlimited | `0` | Both |
15831583
| `slot` | Connection slot count | Integer (1-65536) | `65536` | Both |
15841584
| `proxy` | PROXY protocol support | `0`(disabled), `1`(enabled) | `0` | Both |
1585+
| `notcp` | TCP support control | `0`(enabled), `1`(disabled) | `0` | Both |
15851586
| `noudp` | UDP support control | `0`(enabled), `1`(disabled) | `0` | Both |

docs/en/configuration.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,43 @@ nodepass "server://0.0.0.0:10101/0.0.0.0:8080?log=info&tls=1&proxy=1&rate=100"
262262
- The header format follows the HAProxy PROXY protocol v1 specification
263263
- If the target service doesn't support PROXY protocol, connections may fail or behave unexpectedly
264264

265+
## TCP Support Control
266+
267+
NodePass supports TCP traffic tunneling by default. The `notcp` parameter allows you to disable TCP support when only UDP traffic needs to be handled, which can reduce resource usage and simplify configuration.
268+
269+
- `notcp`: TCP support control (default: 0)
270+
- Value 0: TCP support enabled - both TCP and UDP traffic will be tunneled
271+
- Value 1: TCP support disabled - only UDP traffic will be tunneled, TCP connections are ignored
272+
- Applies to both client and server modes
273+
- When disabled, TCP-related resources (buffers, connections, sessions) are not allocated
274+
275+
Example:
276+
```bash
277+
# Enable TCP support (default behavior)
278+
nodepass "server://0.0.0.0:10101/0.0.0.0:8080?notcp=0"
279+
280+
# Disable TCP support for UDP-only scenarios
281+
nodepass "server://0.0.0.0:10101/0.0.0.0:8080?notcp=1"
282+
283+
# Client with TCP disabled
284+
nodepass "client://server.example.com:10101/127.0.0.1:8080?notcp=1"
285+
286+
# Combined with other parameters
287+
nodepass "server://0.0.0.0:10101/0.0.0.0:8080?log=info&tls=1&notcp=1"
288+
```
289+
290+
**TCP Support Control Use Cases:**
291+
- **UDP-Only Services**: Disable TCP when tunneling only UDP-based applications
292+
- **Resource Optimization**: Reduce memory and CPU usage by avoiding TCP processing overhead
293+
- **Security**: Prevent TCP-based attacks or unwanted traffic in restricted environments
294+
- **Simplified Configuration**: Easier setup when TCP tunneling is not required
295+
- **Network Isolation**: Isolate TCP and UDP traffic handling for better control
296+
297+
**Important Notes:**
298+
- When TCP is disabled, any TCP connections sent to the tunnel will be silently dropped
299+
- Existing TCP sessions will be terminated when switching to notcp=1
300+
- TCP buffer pools and session management are disabled when notcp=1
301+
265302
## UDP Support Control
266303

267304
NodePass supports UDP traffic tunneling in addition to TCP. The `noudp` parameter allows you to disable UDP support when only TCP traffic needs to be handled, which can reduce resource usage and simplify configuration.
@@ -373,6 +410,7 @@ NodePass allows flexible configuration via URL query parameters. The following t
373410
| `rate` | Bandwidth rate limit | `0` | O | O | X |
374411
| `slot` | Maximum connection limit | `65536` | O | O | X |
375412
| `proxy` | PROXY protocol support| `0` | O | O | X |
413+
| `notcp` | TCP support control | `0` | O | O | X |
376414
| `noudp` | UDP support control | `0` | O | O | X |
377415

378416
- O: Parameter is valid and recommended for configuration
@@ -383,6 +421,7 @@ NodePass allows flexible configuration via URL query parameters. The following t
383421
- For client/server dual-end handshake modes, adjust connection pool capacity (`min`, `max`) based on traffic and resource constraints for optimal performance.
384422
- Use run mode control (`mode`) when automatic detection doesn't match your deployment requirements or for consistent behavior across environments.
385423
- Configure rate limiting (`rate`) to control bandwidth usage and prevent network congestion in shared environments.
424+
- Set `notcp=1` when only UDP traffic needs to be tunneled to reduce resource usage and simplify configuration.
386425
- Set `noudp=1` when only TCP traffic needs to be tunneled to reduce resource usage and simplify configuration.
387426
- Log level (`log`) can be set in all modes for easier operations and troubleshooting.
388427

docs/zh/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,4 +1582,5 @@ client://<server_host>:<server_port>/<local_host>:<local_port>?<parameters>
15821582
| `rate` | 带宽速率限制 | 整数 (Mbps), 0=无限制 | `0` | 两者 |
15831583
| `slot` | 连接槽位数 | 整数 (1-65536) | `65536` | 两者 |
15841584
| `proxy` | PROXY协议支持 | `0`(禁用), `1`(启用) | `0` | 两者 |
1585+
| `notcp` | TCP支持控制 | `0`(启用), `1`(禁用) | `0` | 两者 |
15851586
| `noudp` | UDP支持控制 | `0`(启用), `1`(禁用) | `0` | 两者 |

docs/zh/configuration.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,43 @@ nodepass "server://0.0.0.0:10101/0.0.0.0:8080?log=info&tls=1&proxy=1&rate=100"
262262
- 头部格式遵循HAProxy PROXY协议v1规范
263263
- 如果目标服务不支持PROXY协议,将导致连接失败
264264

265+
## TCP支持控制
266+
267+
NodePass默认支持TCP流量隧道。`notcp`参数允许您在只需要处理UDP流量时禁用TCP支持,这样可以减少资源使用并简化配置。
268+
269+
- `notcp`: TCP支持控制(默认: 0)
270+
- 值为0:启用TCP支持 - TCP和UDP流量都将被隧道传输
271+
- 值为1:禁用TCP支持 - 仅UDP流量将被隧道传输,TCP连接被忽略
272+
- 适用于客户端和服务器模式
273+
- 禁用时,不分配TCP相关资源(缓冲区、连接、会话)
274+
275+
示例:
276+
```bash
277+
# 启用TCP支持(默认行为)
278+
nodepass "server://0.0.0.0:10101/0.0.0.0:8080?notcp=0"
279+
280+
# 禁用TCP支持,仅处理UDP场景
281+
nodepass "server://0.0.0.0:10101/0.0.0.0:8080?notcp=1"
282+
283+
# 客户端禁用TCP
284+
nodepass "client://server.example.com:10101/127.0.0.1:8080?notcp=1"
285+
286+
# 与其他参数结合
287+
nodepass "server://0.0.0.0:10101/0.0.0.0:8080?log=info&tls=1&notcp=1"
288+
```
289+
290+
**TCP支持控制使用场景:**
291+
- **仅UDP服务**:仅需要隧道传输UDP应用时禁用TCP
292+
- **资源优化**:通过避免TCP处理开销减少内存和CPU使用
293+
- **安全性**:防止受限环境中的TCP攻击或不需要的流量
294+
- **简化配置**:不需要TCP隧道时更容易设置
295+
- **网络隔离**:更好地控制TCP和UDP流量处理
296+
297+
**重要说明:**
298+
- 禁用TCP时,发送到隧道的任何TCP连接将被静默丢弃
299+
- 切换到notcp=1时,现有的TCP会话将被终止
300+
- 当notcp=1时,TCP缓冲池和会话管理被禁用
301+
265302
## UDP支持控制
266303

267304
除了TCP之外,NodePass还支持UDP流量隧道。`noudp`参数允许您在只需要处理TCP流量时禁用UDP支持,这样可以减少资源使用并简化配置。
@@ -373,6 +410,7 @@ NodePass支持通过URL查询参数进行灵活配置,不同参数在 server
373410
| `rate` | 带宽速率限制 | `0` | O | O | X |
374411
| `slot` | 最大连接数限制 | `65536` | O | O | X |
375412
| `proxy` | PROXY协议支持 | `0` | O | O | X |
413+
| `notcp` | TCP支持控制 | `0` | O | O | X |
376414
| `noudp` | UDP支持控制 | `0` | O | O | X |
377415

378416
- O:参数有效,推荐根据实际场景配置
@@ -383,6 +421,7 @@ NodePass支持通过URL查询参数进行灵活配置,不同参数在 server
383421
- client/server 双端握手模式建议根据流量和资源情况调整连接池容量(min/max),优化性能。
384422
- 当自动检测不符合部署需求时或需要跨环境一致行为时,使用运行模式控制(mode)。
385423
- 配置速率限制(rate)以控制带宽使用,防止共享环境中的网络拥塞。
424+
- 仅需要隧道传输UDP流量时设置`notcp=1`,以减少资源使用并简化配置。
386425
- 仅需要隧道传输TCP流量时设置`noudp=1`,以减少资源使用并简化配置。
387426
- 日志级别(log)可在所有模式下灵活调整,便于运维和排查。
388427

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ require (
66
github.com/NodePassProject/cert v1.0.1
77
github.com/NodePassProject/conn v1.0.16
88
github.com/NodePassProject/logs v1.0.3
9-
github.com/NodePassProject/pool v1.0.48
9+
github.com/NodePassProject/pool v1.0.49
1010
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ github.com/NodePassProject/conn v1.0.16 h1:ojHfyBveZMcyOikdUs1SOW4yKp92NOBnNhfNe
44
github.com/NodePassProject/conn v1.0.16/go.mod h1:xfQ7ZLUxrtdLsljGHYYCToW+Hdg6DAbmL1Cs94n5h6E=
55
github.com/NodePassProject/logs v1.0.3 h1:CDUZVQ477vmmFQHazrQCWM0gJPNINm0C2N3FzC4jVyw=
66
github.com/NodePassProject/logs v1.0.3/go.mod h1:TwtPXOzLtb8iH+fdduQjEEywICXivsM39cy9AinMSks=
7-
github.com/NodePassProject/pool v1.0.48 h1:99pCHQYtmH5sVIB0vY+KbV4zyWSH6ptHgkKtxDnjpqQ=
8-
github.com/NodePassProject/pool v1.0.48/go.mod h1:joQFk1oocg56QpJ1QK/2g5Jv/AyqYUQgPXMG1gWe8iA=
7+
github.com/NodePassProject/pool v1.0.49 h1:gktVmE+GsQ0/C0MF8qgRraR7eS3na4k0QrQfR6o4fkM=
8+
github.com/NodePassProject/pool v1.0.49/go.mod h1:joQFk1oocg56QpJ1QK/2g5Jv/AyqYUQgPXMG1gWe8iA=

internal/client.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ func NewClient(parsedURL *url.URL, logger *logs.Logger) (*Client, error) {
4545
return &buf
4646
},
4747
},
48-
cleanURL: &url.URL{Scheme: "np", Fragment: "c"},
4948
flushURL: &url.URL{Scheme: "np", Fragment: "f"},
5049
pingURL: &url.URL{Scheme: "np", Fragment: "i"},
5150
pongURL: &url.URL{Scheme: "np", Fragment: "o"},
@@ -62,9 +61,9 @@ func NewClient(parsedURL *url.URL, logger *logs.Logger) (*Client, error) {
6261
// Run 管理客户端生命周期
6362
func (c *Client) Run() {
6463
logInfo := func(prefix string) {
65-
c.logger.Info("%v: client://%v@%v/%v?min=%v&mode=%v&read=%v&rate=%v&slot=%v&proxy=%v&noudp=%v",
64+
c.logger.Info("%v: client://%v@%v/%v?min=%v&mode=%v&read=%v&rate=%v&slot=%v&proxy=%v&notcp=%v&noudp=%v",
6665
prefix, c.tunnelKey, c.tunnelTCPAddr, c.getTargetAddrsString(),
67-
c.minPoolCapacity, c.runMode, c.readTimeout, c.rateLimit/125000, c.slotLimit, c.proxyProtocol, c.disableUDP)
66+
c.minPoolCapacity, c.runMode, c.readTimeout, c.rateLimit/125000, c.slotLimit, c.proxyProtocol, c.disableTCP, c.disableUDP)
6867
}
6968
logInfo("Client started")
7069

@@ -157,13 +156,15 @@ func (c *Client) commonStart() error {
157156
})
158157
go c.tunnelPool.ClientManager()
159158

159+
// 判断数据流向
160160
if c.dataFlow == "+" {
161-
// 初始化目标监听器
162161
if err := c.initTargetListener(); err != nil {
163162
return fmt.Errorf("commonStart: initTargetListener failed: %w", err)
164163
}
165164
go c.commonLoop()
166165
}
166+
167+
// 启动共用控制
167168
if err := c.commonControl(); err != nil {
168169
return fmt.Errorf("commonStart: commonControl failed: %w", err)
169170
}

0 commit comments

Comments
 (0)