README
ΒΆ
π EdTunnel
A high-performance Go relay/tunnel for forwarding TCP & UDP traffic through one or more relay servers β built for high throughput, low latency, zero-allocation hot paths, and resilient multi-relay failover.
Ideal for relaying WireGuard, OpenVPN, and Ocserv traffic through a public relay into a private/NAT'd network.
β¨ Overview
EdTunnel lets a VPN/application server sitting behind NAT or in a restricted network expose TCP and UDP services to the public internet through one or more relay nodes. The relay accepts public connections and ships traffic over a multiplexed, authenticated tunnel to the VPN-side client, which forwards it to local target ports.
It's built around two cooperating processes:
| Mode | Role |
|---|---|
relay |
Public-facing node. Accepts TCP/UDP client traffic and tunnels it to the VPN client. |
vpn |
Private-side node. Connects out to one or more relays and forwards traffic to local services. |
π Key Features
- β‘ High throughput, low latency β zero-allocation hot paths, pooled buffers,
TCP_NODELAY, andSO_REUSEPORTlisteners keep forwarding overhead minimal even under heavy load. - π VPN-friendly forwarding β works great as a relay in front of WireGuard, OpenVPN, and Ocserv, letting you expose them publicly while the actual server stays behind NAT or in a restricted network.
- π TCP multiplexing via
smuxβ a single authenticated TCP connection carries many forwarded streams, avoiding per-connection handshake overhead. - π‘ Raw UDP tunneling β UDP traffic is encapsulated with a lightweight 14-byte header (auth hash + session ID + target port) and shipped directly over a dedicated UDP socket, with zero-allocation read/write loops via
sync.Poolbuffer pooling. - π©Ή Multi-relay support β connect to several relay servers at once with two selectable strategies:
multiβ all healthy relays stay active simultaneously.failoverβ only one relay is active at a time, automatically promoting the next healthy relay on disconnect.
- π Token authentication β relay connections are authenticated with a shared token; UDP packets are authenticated per-packet via an FNV-1a hash of the token.
- β»οΈ Hot-reconnect β a new VPN connection to a relay immediately tears down and replaces the previous session, freeing bound ports without downtime gaps.
- β‘
SO_REUSEPORTlisteners β TCP and UDP listeners are created withSO_REUSEADDR/SO_REUSEPORTfor fast rebinding. - π§Ή Automatic UDP session cleanup β stale relay-side UDP sessions are garbage-collected on a 30-second sweep after 2 minutes of inactivity.
- π NAT keep-alive β the VPN client periodically sends a session-ID-0 keep-alive packet so the relay always has a fresh public endpoint for the VPN client, even behind NAT.
ποΈ Architecture
ββββββββββββββββββββββ
Public Client βTCPβββΆβ β
β relay mode βββsmux streamβββΆ VPN mode ββTCPβββΆ Local Target
Public Client βUDPβββΆβ (public-facing) βββUDP tunnelββββΆ (private) ββUDPβββΆ Local Target
ββββββββββββββββββββββ
- TCP path: Relay accepts a raw TCP connection β opens an
smuxstream over the authenticated tunnel β sends a small header (protocol,target-port) β bidirectionalio.CopyBufferrelay using pooled 32 KB buffers. - UDP path: Relay assigns each public client a numeric session ID β wraps payloads in a custom 14-byte frame β sends over a dedicated UDP socket to the VPN client's last-known public endpoint β VPN client unwraps, forwards to the local target, and reverses the process for replies.
π Usage
Build
go build -o edtunnel .
Run as a relay (public-facing node)
./edtunnel -mode=relay \
-port=27015 \
-token="your-shared-secret"
Run as a VPN client (private-side node)
./edtunnel -mode=vpn \
-host="relay1.example.com:27015,relay2.example.com:27015" \
-token="your-shared-secret" \
-forward="943,943" \
-forwardudp="51820,51820" \
-strategy=multi
Example: forwarding WireGuard (UDP
51820), OpenVPN (UDP1194or TCP943), and Ocserv (TCP/UDP443) just means adding the matchingsrcPort,targetPortpair to-forward/-forwardudp.
βοΈ Flags
| Flag | Used in | Description |
|---|---|---|
-mode |
both | relay or vpn |
-port |
relay | Port the relay listens on (TCP and UDP tunnel) |
-host |
vpn | Comma-separated list of host:port relay servers |
-token |
both | Shared authentication secret |
-forward |
vpn | TCP forwarding rules β srcPort,targetPort;srcPort,targetPort |
-forwardudp |
vpn | UDP forwarding rules β same format as -forward |
-strategy |
vpn | multi (all relays active) or failover (one active at a time) |
Note:
srcPortis the port opened on the relay;targetPortis the port on the local target the VPN client forwards to.
π§ Design Notes
- Buffer pooling: Both the 32 KB TCP copy buffers and the 65,550-byte UDP frame buffers (
65535max UDP payload +14-byte header) are recycled throughsync.Poolto keep the hot path allocation-free. - Session bookkeeping: Relay-side UDP sessions are tracked in two maps β
clientAddr β sessionIDandsessionID β *RelayUDPSessionβ protected by anRWMutex, allowing concurrent reads on the hot path. - Graceful relay handover: When a VPN client reconnects to a relay,
closeCurrentSession()tears down all previously bound listeners and the priorsmuxsession before the new one is established, so ports are released immediately rather than waiting on a timeout.
π Requirements
- Go 1.21+
- Linux (uses
SO_REUSEPORTviasyscall) github.com/xtaci/smux
π License
MIT β feel free to fork, adapt, and build on it.
Built for fast, resilient, multiplexed tunneling.
Documentation
ΒΆ
There is no documentation for this package.