net.udprelay
Carries UDP datagrams over a byte stream, such as an SSH connection.
import net.udprelay
A relay forwards traffic on behalf of someone else. Some links, such as SSH port forwarding, carry only byte streams, so a UDP packet cannot cross them as it is. net.udprelay puts a short header in front of every datagram, sends it over the stream, and the server half sends it on with net.udp and brings the replies back.
app -> Client == SSH stream ==> Serve -> UDP -> internet
The server is one small program. Build it for the machine it runs on and start it with the command given to NewClient:
import net.udprelay
func main:
ok := udprelay.Serve(0 as i64, 1 as i64, udprelay.ServerOptions {})
ret ok ? 0 : 1
end
net.tun uses a client as its udp option when relay_udp is on.
- The server needs POSIX.
Servereturns false on Windows. The client works everywhere. - IPv4 only.
- Datagrams may be lost, as with UDP itself: when the queue is full or the stream is down they are dropped.
The server program
The far side is a program that only calls Serve on its standard input and output. Build it for the server, copy it there, and let the client start it over SSH. It ends when the input ends, so nothing keeps running after the connection closes.
import net.udprelay
func main:
ok := udprelay.Serve(0 as i64, 1 as i64, udprelay.ServerOptions {})
ret ok ? 0 : 1
end
Using it with net.tun
Give NewClient the command that starts the server, then pass the client to the tunnel. Blocking port 443 makes browsers use TCP instead of QUIC.
import net.tun
import net.udprelay
import os.process
func options(): tun.Options:
mut cmd := process.NewCommand("ssh")
cmd.Arg("user@server")
cmd.Arg("./udprelay")
mut blocked := Vector {} as Vector
blocked.Push(443)
ret tun.Options {
relay_udp = true,
udp = udprelay.NewClient(cmd),
udp_blocked = blocked
}
end
Types
Datagram
struct Datagram| Name | Type | Description |
|---|---|---|
flow | int = 0 | A number that keeps the datagrams of one local socket together; the tun stack uses the source port. |
ip | i64 = 0 | The remote IPv4 address as a number. |
port | int = 0 | The remote port. |
length | int = 0 | Bytes of payload. |
PacketRelay
interface PacketRelaynet.tun.NoRelay
struct NoRelaynet.tun.Send
func Send(_flow: int, _ip: i64, _port: int, _ptr: void*, _n: int): boolReceive
func Receive(_buf: void*, _max: int, _info &: Datagram, timeout_ms: int): intClose
func Close()ServerOptions
struct ServerOptions| Name | Type | Description |
|---|---|---|
max_flows | int = 256 | Sockets open at once; the oldest is closed to make room. |
idle_ms | int = 120000 | A socket without traffic for this long is closed. |
allow_loopback | bool = false | Lets datagrams reach 127.0.0.0/8 on the server. Off by default. |
Client
struct Client| Name | Type | Description |
|---|---|---|
core | void* | Internal state. |
Send
func Send(flow: int, ip: i64, port: int, ptr: void*, n: int): boolReceive
func Receive(buf: void*, max: int, info &: Datagram, timeout_ms: int): intReady
func Ready(): boolDropped
func Dropped(): i64Close
func Close()Constants and variables
header_size
const header_size: i32 = 10max_payload
const max_payload: i32 = 65507max_frame
const max_frame: i32 = 65517Functions
EncodeHeader
func EncodeHeader(dst: void*, info: Datagram)DecodeHeader
func DecodeHeader(src: void*, info &: Datagram)ParseIPv4
func ParseIPv4(text: str): i64FormatIPv4
func FormatIPv4(ip: i64): strNewClient
func NewClient(cmd: process.Command): Clientssh host relay-program, and talks to it over its input and output.NewPipeClient
func NewPipeClient(read_fd: i64, write_fd: i64): ClientNewSocketClient
func NewSocketClient(connect: func(): i64, token: str): ClientListen. It connects again after the link drops.Listen
func Listen(port: int, token: str, control_fd: i64, opts: ServerOptions): boolServe
func Serve(in_fd: i64, out_fd: i64, opts: ServerOptions): bool