Skip to content

Port hopping

Source files: 3 · checked against shinku-quic v0.1.0
  • shinku-quic/shinku-quic/src/hop.rs
  • shinku-quic/shinku-quic/tests/hop.rs
  • shinku-quic/shinku-quic/tests/support/mod.rs

Port hopping spreads one QUIC connection over a set of server ports, as Hysteria’s udphop does. Every so often the client binds a fresh local socket and moves to another port of the set. The server side is only a firewall rule that forwards the whole range to one listening port, so to the server a hop looks like the client’s address changing: QUIC connection migration, which quinn follows on its own.

quinn sees none of the hopping. HoppingSocket is the client endpoint’s socket: it implements quinn’s AsyncUdpSocket, sends whatever quinn addresses to its remote to the current port instead, and reports every packet from the server as coming from remote, so the client never thinks its peer moved.

shinku-quic/src/hop.rs
pub struct HopInterval { min: Duration, max: Duration }
impl HopInterval {
pub const DEFAULT: Duration; // 30 s
pub const MIN_CONFIGURED: Duration; // 5 s, not enforced here
pub fn fixed(interval: Duration) -> Option<Self>;
pub fn random(min: Duration, max: Duration) -> Option<Self>; // None for a zero min or min > max
pub fn min(&self) -> Duration;
pub fn max(&self) -> Duration;
}
pub type BindSocket = Box<dyn Fn() -> io::Result<Arc<dyn AsyncUdpSocket>> + Send + Sync>;
pub struct HoppingSocket { /* server, ports, interval, remote, bind, state, recv_waker */ }
impl HoppingSocket {
pub fn new(server: IpAddr, ports: Vec<u16>, interval: HopInterval, bind: BindSocket)
-> io::Result<Arc<Self>>;
pub fn remote(&self) -> SocketAddr;
pub fn current_port(&self) -> u16;
pub fn hop_now(&self);
}
impl AsyncUdpSocket for HoppingSocket {}
  • HopInterval::random draws a uniformly random interval between min and max, inclusive, anew at every hop (upstream’s minHopInterval and maxHopInterval); fixed is the case min == max. MIN_CONFIGURED is the shortest interval upstream’s configuration accepts; enforcing it is the caller’s job, so tests can hop faster. Etemenanki’s supervisor refuses a shorter interval at validation.
  • BindSocket binds a fresh local socket, called at every hop. It decides the family, the address and the socket options, so the caller’s socket policy applies to every hop.
  • HoppingSocket::new refuses an empty port set or port 0 with port hopping needs at least one port, and port 0 is not one, binds the first socket and picks the first port at random. remote() is the server with that first port: the address quinn connects to.
Operation What happens
Hop Checked lazily, on send: when the interval has run out, the socket binds a fresh local socket, keeps the current one as previous, picks another port at random and draws the next interval. hop_now hops at once.
A failed bind The hop is skipped and logged at debug (port hopping: skipped a hop, could not bind: …); the connection stays on its current socket and port, as upstream does.
Send A transmit addressed to remote goes to the server’s current port from the current socket. Anything else is sent unchanged.
Receive The current socket is read first, then the previous one. Packets from the server’s address are reported as coming from remote.
The previous socket Still read until the next hop, so packets the server sent before it saw the move still arrive. If it fails, it is dropped and logged at debug (port hopping: dropped the previous socket: …); the error does not reach the endpoint.
Waking A hop wakes the endpoint driver’s stored waker so it polls the new socket, and the I/O poller follows each socket generation.

The server is compared by IP address in canonical form, so an IPv4 server reached over an IPv4-mapped IPv6 socket is still recognised.

In Etemenanki, the Hysteria 2 client builds a HoppingSocket in bind_socket (protocols/src/hysteria/connection.rs) when the outbound has ports; Salamander obfuscation wraps the hopping socket, so obfuscation stays the outermost layer on the wire.

  • Client only. Nothing here runs on a server; a server listens on one port behind a forwarding rule.
  • quinn’s peer address never changes. Every packet from the server arrives as from remote, whichever port it came from, so quinn does not treat a hop as a peer migration.
  • A hop never fails the connection. A failed bind skips the hop, and a failing previous socket is dropped silently.
Test Shows
hop.rs unit test Interval validation: a zero min, min > max, and the default
tests/hop.rs a_connection_survives_a_hop_after_every_round 30 hops over 4 front ports, one after every echo round, without losing the connection
tests/hop.rs the_interval_moves_the_connection_on_its_own With an 80 to 160 ms random interval, the connection moves between ports by itself

The tests run against the relay in tests/support/mod.rs, which has several front ports forwarding to one server and answers out of the front port the client used, as a DNAT rule’s connection tracking does.