Skip to content

Commit 06beeba

Browse files
authored
docs: enhance documentation for STUN NAT traversal features and examples across all relevant markdown files
1 parent da92584 commit 06beeba

File tree

8 files changed

+594
-11
lines changed

8 files changed

+594
-11
lines changed

docs/en/configuration.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,13 @@ For client instances, the `mode` parameter controls the connection strategy:
6262

6363
- **Mode 1**: Force single-end forwarding mode
6464
- Binds to tunnel address locally and forwards traffic directly to target
65-
- Uses direct connection establishment for high performance
65+
- Uses direct connection establishment for high performance forwarding
6666
- No handshake with server required
67+
- **Hybrid Mode**: If local binding fails, automatically enables STUN NAT traversal
68+
- Uses the tunnel address as the STUN server address
69+
- Discovers external (public) IP:port through STUN protocol
70+
- Enables services behind NAT to be accessed without port forwarding
71+
- Suitable for home servers, IoT devices, and peer-to-peer scenarios
6772

6873
- **Mode 2**: Force dual-end handshake mode
6974
- Always connects to remote server for tunnel establishment
@@ -321,6 +326,60 @@ nodepass "client://127.0.0.1:3306/db-primary.local:3306,db-secondary.local:3306?
321326
# nodepass "server://host1:10101,host2:10101/target:8080" # ✗ Wrong usage
322327
```
323328

329+
## STUN NAT Traversal
330+
331+
NodePass client mode 1 supports automatic NAT traversal using the STUN (Session Traversal Utilities for NAT) protocol. When the client cannot bind to the specified tunnel address, it automatically enables hybrid mode with STUN support.
332+
333+
### How STUN NAT Traversal Works
334+
335+
1. **Automatic Fallback**: When binding to the tunnel address fails, NodePass attempts STUN discovery
336+
2. **STUN Server Discovery**: The tunnel address in the client URL is used as the STUN server address
337+
3. **Public Endpoint Discovery**: Client queries the STUN server to discover its public IP and port
338+
4. **Local Binding**: Client binds to a random local port and logs the NAT mapping
339+
5. **External Access**: External clients can connect to the discovered public endpoint
340+
341+
### Recommended STUN Servers
342+
343+
Google provides free public STUN servers that work well with NodePass:
344+
345+
- `stun.l.google.com:19302` (Primary)
346+
- `stun1.l.google.com:19302`
347+
- `stun2.l.google.com:19302`
348+
- `stun3.l.google.com:19302`
349+
- `stun4.l.google.com:19302`
350+
351+
### STUN Configuration Examples
352+
353+
```bash
354+
# Use Google's primary STUN server for NAT traversal
355+
nodepass "client://stun.l.google.com:19302/localhost:8080?mode=1"
356+
357+
# Expose SSH service behind NAT
358+
nodepass "client://stun1.l.google.com:19302/localhost:22?mode=1&log=info"
359+
360+
# Home web server with STUN NAT traversal
361+
nodepass "client://stun2.l.google.com:19302/192.168.1.100:80?mode=1"
362+
363+
# IoT device management behind NAT
364+
nodepass "client://stun3.l.google.com:19302/127.0.0.1:8883?mode=1&log=event"
365+
```
366+
367+
### STUN Use Cases
368+
369+
- **Home Servers**: Access home services without router port forwarding
370+
- **IoT Devices**: Connect to devices behind carrier-grade NAT (CGNAT)
371+
- **Peer-to-Peer Applications**: Direct connections between clients behind NAT
372+
- **Development Testing**: Test applications with external accessibility
373+
- **Remote Access**: Secure access to services in restricted network environments
374+
375+
### Important Notes
376+
377+
- STUN only discovers the public endpoint; the NAT must allow incoming connections
378+
- Symmetric NAT may prevent successful connections even with STUN discovery
379+
- The discovered public endpoint is logged: `External endpoint: <public_ip:port> -> <local_ip:port> -> <target>`
380+
- STUN traffic is unencrypted; only used for endpoint discovery, not data transfer
381+
- Other mode 1 features remain fully functional with STUN
382+
324383
## URL Query Parameter Scope and Applicability
325384

326385
NodePass allows flexible configuration via URL query parameters. The following table shows which parameters are applicable in server, client, and master modes:

docs/en/examples.md

Lines changed: 133 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,73 @@ This configuration:
194194

195195
## IoT Device Management
196196

197-
### Example 12: IoT Gateway
197+
### Example 12: IoT Gateway with STUN NAT Traversal
198198

199-
Create a central access point for IoT devices:
199+
Create a central access point for IoT devices behind NAT without port forwarding:
200+
201+
```bash
202+
# IoT device behind home router - expose web interface
203+
nodepass "client://stun.l.google.com:19302/127.0.0.1:80?mode=1&log=info"
204+
205+
# IoT device with MQTT broker behind CGNAT
206+
nodepass "client://stun1.l.google.com:19302/127.0.0.1:1883?mode=1&log=event"
207+
208+
# Smart home hub with multiple services
209+
nodepass "client://stun2.l.google.com:19302/192.168.1.100:8080?mode=1"
210+
```
211+
212+
This configuration:
213+
- Enables secure connections from distributed IoT devices to external clients
214+
- Uses Google's STUN servers to discover public endpoints
215+
- Allows embedded devices to expose their local interfaces without router configuration
216+
- Automatically handles NAT traversal for devices behind carrier-grade NAT
217+
- Logs the external endpoint for connecting from outside: `External endpoint: <public_ip:port> -> <local_ip:port> -> <target>`
218+
219+
### Example 13: Home Server Access Through NAT
220+
221+
Access home services without opening router ports:
222+
223+
```bash
224+
# Home NAS file server
225+
nodepass "client://stun3.l.google.com:19302/localhost:445?mode=1&log=info"
226+
227+
# Home media server (Plex, Jellyfin, etc.)
228+
nodepass "client://stun4.l.google.com:19302/127.0.0.1:32400?mode=1"
229+
230+
# Home automation server
231+
nodepass "client://stun.l.google.com:19302/192.168.1.50:8123?mode=1&log=event"
232+
```
233+
234+
This setup:
235+
- Provides external access to home services without DMZ or port forwarding
236+
- Maintains security by avoiding router configuration changes
237+
- Works with dynamic IP addresses from ISPs
238+
- Suitable for residential internet connections with NAT
239+
240+
### Example 14: Remote Development Environment
241+
242+
Enable remote access to development servers behind NAT:
243+
244+
```bash
245+
# Development web server behind corporate NAT
246+
nodepass "client://stun1.l.google.com:19302/localhost:3000?mode=1&log=debug"
247+
248+
# Database development instance
249+
nodepass "client://stun2.l.google.com:19302/127.0.0.1:5432?mode=1"
250+
251+
# API development server with hot reload
252+
nodepass "client://stun3.l.google.com:19302/localhost:8080?mode=1&log=info"
253+
```
254+
255+
This configuration:
256+
- Allows team members to access development environments remotely
257+
- Works through corporate NAT and firewall restrictions
258+
- No VPN or complex network configuration required
259+
- Ideal for distributed development teams
260+
261+
### Example 15: Traditional IoT Gateway (Dual-End Mode)
262+
263+
Create a central management server for traditional tunneling scenarios:
200264

201265
```bash
202266
# Central management server
@@ -212,9 +276,74 @@ This configuration:
212276
- Allows embedded devices to expose their local web interfaces securely
213277
- Centralizes device management through a single endpoint
214278

279+
## STUN NAT Traversal for Remote Access
280+
281+
### Example 16: SSH Access Behind NAT
282+
283+
Provide SSH access to machines behind NAT without port forwarding:
284+
285+
```bash
286+
# Home computer SSH access
287+
nodepass "client://stun.l.google.com:19302/localhost:22?mode=1&log=info"
288+
289+
# Raspberry Pi SSH behind carrier-grade NAT
290+
nodepass "client://stun1.l.google.com:19302/127.0.0.1:22?mode=1"
291+
292+
# Remote server behind corporate firewall
293+
nodepass "client://stun2.l.google.com:19302/192.168.1.10:22?mode=1&log=event"
294+
```
295+
296+
This setup:
297+
- Enables SSH access without router configuration or VPN
298+
- Works through carrier-grade NAT (CGNAT) that blocks port forwarding
299+
- Maintains security with SSH's built-in authentication
300+
- Ideal for remote system administration and troubleshooting
301+
302+
### Example 17: Game Server Hosting Behind NAT
303+
304+
Host game servers from home networks without port forwarding:
305+
306+
```bash
307+
# Minecraft server behind NAT
308+
nodepass "client://stun3.l.google.com:19302/localhost:25565?mode=1&log=info"
309+
310+
# Counter-Strike server
311+
nodepass "client://stun4.l.google.com:19302/127.0.0.1:27015?mode=1"
312+
313+
# Terraria server with NAT traversal
314+
nodepass "client://stun.l.google.com:19302/192.168.1.100:7777?mode=1&log=event"
315+
```
316+
317+
This configuration:
318+
- Allows hosting game servers from residential connections
319+
- No need for router port forwarding or DMZ setup
320+
- Works with dynamic IP addresses
321+
- Players connect to the discovered public endpoint
322+
323+
### Example 18: VoIP and Real-Time Communication
324+
325+
Enable peer-to-peer VoIP and communication services behind NAT:
326+
327+
```bash
328+
# SIP server behind NAT
329+
nodepass "client://stun1.l.google.com:19302/localhost:5060?mode=1&log=info"
330+
331+
# Mumble voice chat server
332+
nodepass "client://stun2.l.google.com:19302/127.0.0.1:64738?mode=1"
333+
334+
# TeamSpeak server with NAT traversal
335+
nodepass "client://stun3.l.google.com:19302/192.168.1.50:9987?mode=1&log=event"
336+
```
337+
338+
This setup:
339+
- Provides external access to VoIP and voice chat servers
340+
- Eliminates need for complex NAT traversal in the application itself
341+
- Works with existing SIP phones and voice chat clients
342+
- Suitable for home offices and small businesses
343+
215344
## Multi-environment Development
216345

217-
### Example 13: Development Environment Access
346+
### Example 19: Development Environment Access
218347

219348
Access different development environments through tunnels:
220349

@@ -237,7 +366,7 @@ This setup:
237366

238367
## High Availability and Load Balancing
239368

240-
### Example 14: Multi-Backend Server Load Balancing
369+
### Example 20: Multi-Backend Server Load Balancing
241370

242371
Use target address groups for even traffic distribution and automatic failover:
243372

docs/en/troubleshooting.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,96 @@ This guide helps you diagnose and resolve common issues you might encounter when
7878
- Adjust `max` parameter and `NP_SEMAPHORE_LIMIT` to handle the load
7979
- Consider scaling horizontally with multiple NodePass instances
8080

81+
## STUN NAT Traversal Issues
82+
83+
### STUN Discovery Fails
84+
85+
**Symptoms**: Client cannot discover external endpoint, or hybrid mode fails with STUN errors.
86+
87+
**Possible Causes and Solutions**:
88+
89+
1. **STUN Server Unreachable**
90+
- Verify the STUN server address is correct and accessible
91+
- Test connectivity: `nc -u stun.l.google.com 19302`
92+
- Try alternative STUN servers (stun1-4.l.google.com:19302)
93+
- Check if UDP traffic is blocked by firewall or ISP
94+
95+
2. **Invalid STUN Response**
96+
- Ensure the server at the tunnel address is a valid STUN server
97+
- Google's STUN servers: stun.l.google.com, stun1-4.l.google.com (port 19302)
98+
- STUN response must contain XOR-MAPPED-ADDRESS attribute
99+
- Check for network middleware interfering with UDP packets
100+
101+
3. **UDP Port Blocking**
102+
- Verify UDP traffic is not blocked by local firewall
103+
- Check router/firewall settings for UDP restrictions
104+
- Some corporate networks block all UDP traffic
105+
- Try using a different network (mobile hotspot) for testing
106+
107+
4. **Timeout Issues**
108+
- STUN discovery uses `NP_UDP_READ_TIMEOUT` (default: 3s)
109+
- Increase timeout if network latency is high
110+
- Check for packet loss affecting STUN request/response
111+
112+
### NAT Prevents External Connections
113+
114+
**Symptoms**: STUN discovers external endpoint but external clients cannot connect.
115+
116+
**Possible Causes and Solutions**:
117+
118+
1. **Symmetric NAT**
119+
- Symmetric NAT creates different mappings for different remote endpoints
120+
- STUN can discover the endpoint but connections from other IPs fail
121+
- Check NAT type: use online NAT detection tools
122+
- Consider using dual-end handshake mode (mode=2) with a server instead
123+
124+
2. **Port Restricted NAT**
125+
- NAT only allows connections from IPs that the client has contacted first
126+
- This is a security feature of many consumer routers
127+
- Solution: Have both peers perform STUN discovery and exchange endpoints
128+
- Or use server relay mode (mode=2) for guaranteed connectivity
129+
130+
3. **Firewall Rules**
131+
- Local firewall may block incoming connections even with STUN
132+
- Check iptables/Windows Firewall rules for the discovered port
133+
- Allow incoming UDP/TCP traffic on the mapped port
134+
- Verify with: `netstat -tuln | grep <port>`
135+
136+
4. **Dynamic Port Mapping**
137+
- Some NATs change port mapping after period of inactivity
138+
- The discovered endpoint may become invalid
139+
- Implement periodic keepalive traffic to maintain mapping
140+
- Consider restarting NodePass to rediscover endpoint
141+
142+
### Hybrid Mode Not Activating
143+
144+
**Symptoms**: Client in mode=1 doesn't fallback to STUN when local binding fails.
145+
146+
**Possible Causes and Solutions**:
147+
148+
1. **Port Already in Use**
149+
- If local port is in use, binding fails as expected
150+
- Check: `netstat -tuln | grep <port>`
151+
- Release the port or choose a different tunnel address
152+
- Hybrid mode should activate automatically after binding failure
153+
154+
2. **Permission Issues**
155+
- Binding to ports <1024 requires root/administrator privileges
156+
- Use higher port numbers (>1024) for regular users
157+
- Or run NodePass with elevated privileges: `sudo nodepass ...`
158+
159+
3. **Configuration Error**
160+
- Ensure tunnel address points to a valid STUN server
161+
- Format: `client://stun.l.google.com:19302/target:port?mode=1`
162+
- Check logs for STUN-related error messages
163+
- Verify mode is set to 1 or 0 (auto) for hybrid mode support
164+
165+
4. **Network Interface Issues**
166+
- Binding may fail due to network interface not available
167+
- Check network connectivity: `ip addr` or `ifconfig`
168+
- Ensure the system has an active network connection
169+
- Try binding to 0.0.0.0 instead of specific interface
170+
81171
## Certificate Issues
82172

83173
### TLS Handshake Failures

docs/en/usage.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ Client mode supports automatic mode detection or forced mode selection through t
134134
4. No handshake with server required, enables point-to-point direct forwarding
135135
5. Suitable for local proxy and simple forwarding scenarios
136136

137+
**Mode 1 with STUN NAT Traversal (Hybrid Mode)**:
138+
When the client cannot bind to the specified tunnel address, NodePass automatically enables hybrid mode:
139+
1. Discovers external (public) endpoint using STUN protocol
140+
2. The STUN server address is derived from the tunnel address specified in the client URL
141+
3. Binds to a local random port and obtains the NAT-mapped public IP:port
142+
4. Enables external access to services behind NAT without port forwarding
143+
5. Logs the mapping: `External endpoint: <public_ip:port> -> <local_ip:port> -> <target>`
144+
6. Suitable for peer-to-peer connections, home servers, and IoT devices behind NAT
145+
137146
**Mode 2: Dual-End Handshake Mode**
138147
- **Client Receives Traffic** (when server sends traffic)
139148
1. Connects to the server's TCP tunnel endpoint (control channel)
@@ -155,6 +164,9 @@ nodepass "client://127.0.0.1:1080/target.example.com:8080?log=debug"
155164
# Force single-end forwarding mode - High performance local proxy
156165
nodepass "client://127.0.0.1:1080/target.example.com:8080?mode=1&log=debug"
157166

167+
# STUN NAT traversal - Use Google's STUN server for public endpoint discovery
168+
nodepass "client://stun.l.google.com:19302/local.service:8080?mode=1&log=info"
169+
158170
# Force dual-end handshake mode - Connect to NodePass server and adopt its TLS security policy
159171
nodepass "client://server.example.com:10101/127.0.0.1:8080?mode=2"
160172

@@ -163,6 +175,9 @@ nodepass "client://server.example.com:10101/192.168.1.100:8080?log=debug&min=128
163175

164176
# Resource-constrained configuration with forced mode
165177
nodepass "client://server.example.com:10101/127.0.0.1:8080?mode=2&min=16&log=info"
178+
179+
# Resource-constrained configuration - Small connection pool
180+
nodepass "client://server.example.com:10101/127.0.0.1:8080?min=16&log=info"
166181
```
167182

168183
### Master Mode (API)

0 commit comments

Comments
 (0)