edtunnel

command module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2026 License: MIT Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

Go License Platform


✨ 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, and SO_REUSEPORT listeners 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.Pool buffer 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_REUSEPORT listeners β€” TCP and UDP listeners are created with SO_REUSEADDR/SO_REUSEPORT for 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 smux stream over the authenticated tunnel β†’ sends a small header (protocol, target-port) β†’ bidirectional io.CopyBuffer relay 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 (UDP 1194 or TCP 943), and Ocserv (TCP/UDP 443) just means adding the matching srcPort,targetPort pair 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: srcPort is the port opened on the relay; targetPort is 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 (65535 max UDP payload + 14-byte header) are recycled through sync.Pool to keep the hot path allocation-free.
  • Session bookkeeping: Relay-side UDP sessions are tracked in two maps β€” clientAddr β†’ sessionID and sessionID β†’ *RelayUDPSession β€” protected by an RWMutex, 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 prior smux session before the new one is established, so ports are released immediately rather than waiting on a timeout.

πŸ“‹ Requirements


πŸ“„ License

MIT β€” feel free to fork, adapt, and build on it.


Built for fast, resilient, multiplexed tunneling.

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 捒网址