Gumdrop

Gumdrop MASQUE: proxying UDP and IP over HTTP

MASQUE (Multiplexed Application Substrate over QUIC Encryption) is the family of IETF standards that turns an HTTP server into a proxy for traffic that is not HTTP: UDP datagrams, or whole IP packets. A client asks for a tunnel with an Extended CONNECT request, and the tunnel's contents travel as HTTP Datagrams alongside the request, over QUIC datagrams on HTTP/3 or as capsules on HTTP/2 and HTTP/1.1. Because it all looks like ordinary HTTPS to the network, it passes where VPN protocols and raw UDP are blocked, and it is what privacy relays and browser-based VPNs are built on.

Gumdrop implements both ends: a server that accepts and relays tunnels, and clients that open them. Both are plain HTTP handlers and clients, so they share the transports, TLS and connection management described on the HTTP page.

Contents

What is supported

StandardWhatIn Gumdrop
RFC 9297HTTP Datagrams and the Capsule Protocol HttpResponse.sendDatagram, sendCapsule, HttpRequestHandler.datagramReceived, capsuleReceived; Capsule, CapsuleParser, HttpDatagramContext
RFC 9298Proxying UDP in HTTP (CONNECT-UDP) ConnectUdpRequestHandler, ConnectUdpClient
RFC 9484Proxying IP in HTTP (CONNECT-IP) ConnectIpRequestHandler, ConnectIpClient
RFC 8441, RFC 9220Extended CONNECT on HTTP/2 and HTTP/3 used by both of the above, as for WebSocket

A tunnel works the same way over HTTP/3, HTTP/2 and HTTP/1.1 (where the request is an Upgrade: connect-udp or Upgrade: connect-ip). The tunnels are addressed by the URI templates in the RFCs, under /.well-known/masque/udp/{host}/{port}/ and /.well-known/masque/ip/{target}/{ipproto}/. ConnectUdpTarget and ConnectIpTarget parse and build those paths.

Policies: why there is no default

A CONNECT-UDP server forwards datagrams to an address chosen by the client, and a CONNECT-IP server forwards arbitrary IP packets. Without checks, either is an open relay into your network and an amplifier for abuse. Gumdrop therefore has no permissive default: the request handlers will not even construct without a policy, and you decide what is allowed. Always combine a policy with authentication (the server's Realm) when the endpoint is reachable from the internet.

CONNECT-UDP server

ConnectUdpRequestHandler is a ready-made HttpRequestHandler. It checks that the request is a well-formed CONNECT-UDP request, resolves the target host with the asynchronous DnsResolver, asks your ConnectUdpPolicy about each resolved address in turn, and relays datagrams between the tunnel and the first allowed address over one UDP socket for the life of the request. The request must be Extended CONNECT with :protocol connect-udp (or an HTTP/1.1 Upgrade: connect-udp), carry Capsule-Protocol: ?1 and use the URI template path. A malformed request gets 400, a target the policy refuses gets 403 and a name that does not resolve gets 502.

ConnectUdpPolicy policy = new ConnectUdpPolicy() {
    public boolean isTargetAllowed(InetAddress address, int port) {
        if (address.isLoopbackAddress() || address.isLinkLocalAddress()
                || address.isSiteLocalAddress() || address.isAnyLocalAddress()
                || address.isMulticastAddress()) {
            return false;
        }
        byte[] a = address.getAddress();
        if (a.length == 16 && (a[0] & 0xfe) == 0xfc) {
            return false;                  // IPv6 unique local, fc00::/7
        }
        return port == 53 || port == 443;  // only what this relay is for
    }
};

HttpServer server = HttpServer.compose()
    .secureEndpoint(443, TlsConfig.pem(Path.of("cert.pem"), Path.of("key.pem")))
    .realm(realm)
    .streamHandler(new HttpStreamHandler() {
        public HttpRequestHandler openStream(HttpResponse response) {
            return new ConnectUdpRequestHandler(response, policy);
        }
    })
    .server();

Gumdrop gumdrop = Gumdrop.boot();
gumdrop.addServer(server);

The policy is called once per request, after resolution, on the request's own connection thread, so it must not block. The relay closes after five minutes with no datagrams in either direction; pass a different idle timeout, in milliseconds, as a third constructor argument (0 disables it). Only Context ID 0 (UDP payloads) is relayed; datagrams with other context IDs are ignored, as RFC 9298 requires.

A stream handler that returns this handler serves only tunnels. To share a port with ordinary requests, route in your own handler, which receives the request events first and decides which delegate to create; the HTTP request routing pattern applies, and HttpMessageRecorder can replay the events already seen to the delegate, as WebSocketRequestHandler does.

CONNECT-IP server

CONNECT-IP carries whole IP packets, so the server needs somewhere to send them. Gumdrop's core has no kernel network stack and deliberately does not bind a TUN device: ConnectIpRequestHandler accepts and manages the tunnel, and hands the packets to an IpPacketHandler that you supply. That can be a userspace forwarder or NAT table, a TUN or raw-socket adapter from outside Gumdrop, or an echo for testing. Two things are needed:

public class AssigningHandler implements IpPacketHandler {

    public void opened(ConnectIpSession session) {
        // routes reachable through this tunnel: everything
        session.sendRouteAdvertisement(Collections.singletonList(
            new ConnectIpRoute(InetAddress.getByName("0.0.0.0"),
                               InetAddress.getByName("255.255.255.255"), 0)));
    }

    public void addressRequested(ConnectIpSession session,
                                 List<ConnectIpAddress> requested) {
        // assign the client an address from your pool, echoing the request ID
        List<ConnectIpAddress> assigned = new ArrayList<ConnectIpAddress>();
        assigned.add(new ConnectIpAddress(requested.get(0).getRequestId(),
                InetAddress.getByName("10.8.0.2"), 32));
        session.sendAddressAssign(assigned);
    }

    public void packetReceived(ConnectIpSession session, ByteBuffer packet) {
        // forward to your backend; send replies with session.sendPacket(...)
        // the buffer is only valid during this call: copy it to keep it
    }

    public void closed(ConnectIpSession session) {
    }

    public void failed(ConnectIpSession session, Exception cause) {
    }
}

ConnectIpPolicy policy = new ConnectIpPolicy() {
    public boolean isRequestAllowed(ConnectIpTarget target) {
        return target.isTargetUnspecified();
    }
};

HttpServer server = HttpServer.compose()
    .secureEndpoint(443, tls)
    .realm(realm)
    .streamHandler(new HttpStreamHandler() {
        public HttpRequestHandler openStream(HttpResponse response) {
            return new ConnectIpRequestHandler(response, policy, new AssigningHandler());
        }
    })
    .server();

(Address literals are shown for brevity; InetAddress.getByName throws UnknownHostException, which real code handles.)

ConnectIpSession sends IP packets (sendPacket), the ADDRESS_ASSIGN and ROUTE_ADVERTISEMENT capsules (sendAddressAssign, sendRouteAdvertisement) and closes the tunnel (close). The client's ADDRESS_REQUEST capsule arrives as addressRequested. Each send returns false if the packet or capsule could not be queued.

Clients

In the client examples on this page, gumdrop is the Gumdrop instance returned by Gumdrop.boot(); a client is given it when it connects. Both clients are asynchronous and deliver their events on the connection's selector loop.

CONNECT-UDP client

ConnectUdpClient connects to a MASQUE proxy and asks it to open a UDP flow to a target host and port. Once the proxy accepts, opened gives you a session to send datagrams on, and datagrams from the target arrive in datagramReceived.

ConnectUdpClient client = new ConnectUdpClient("proxy.example.com", 443)
        .secure(true);

client.connect(gumdrop, "dns.example.net", 53, new ConnectUdpEventHandler() {
    public void opened(ConnectUdpSession session) {
        try {
            session.sendDatagram(query);          // a UDP payload
        } catch (IOException e) {
            // the tunnel closed
        }
    }
    public void datagramReceived(ByteBuffer payload) {
        // a UDP payload from dns.example.net:53; valid only during this call
    }
    public void closed() {
    }
    public void error(Throwable cause) {
        cause.printStackTrace();
    }
});

session.close() ends the tunnel. The target host can be a name or an address literal; a name is resolved by the proxy.

CONNECT-IP client

ConnectIpClient opens an IP tunnel. The target and IP protocol scope what the tunnel may carry; use ConnectIpTarget.WILDCARD for both to ask for an unrestricted one. After opened the client can ask the proxy for an address, then exchange packets.

ConnectIpClient client = new ConnectIpClient("proxy.example.com", 443)
        .secure(true);

client.connect(gumdrop, ConnectIpTarget.WILDCARD, ConnectIpTarget.WILDCARD,
        new ConnectIpEventHandler() {
    public void opened(ConnectIpClientSession session) {
        try {
            session.sendAddressRequest(Collections.singletonList(
                new ConnectIpAddress(1, InetAddress.getByName("0.0.0.0"), 0)));
        } catch (IOException e) {
            // the tunnel closed
        }
    }
    public void addressAssigned(List<ConnectIpAddress> assignments) {
        // configure your side of the tunnel with these addresses
    }
    public void routeAdvertised(List<ConnectIpRoute> routes) {
        // the ranges the proxy will carry
    }
    public void packetReceived(ByteBuffer packet) {
        // a full IP packet from the proxy; valid only during this call
    }
    public void closed() {
    }
    public void error(Throwable cause) {
        cause.printStackTrace();
    }
});

session.sendPacket(packet) sends an IP packet into the tunnel and session.close() ends it.

Client configuration

Both clients are configured like HttpClient and take the same fluent settings:

As with every Gumdrop client, the proxy's host name is resolved asynchronously through DnsResolver, with no blocking InetAddress lookup; see DNS: hostname resolution in every Gumdrop client.

Transports

The client chooses the transport the way HttpClient does: HTTP/3 if a DNS HTTPS record or a cached Alt-Svc advertises it, falling back to TCP if the QUIC attempt fails or takes longer than three seconds, and otherwise HTTP/2 by ALPN, with the HTTP/1.1 upgrade as the last resort. See Choosing HTTP/3 or TCP. Permitting only HTTP_3 forces QUIC with no discovery and no fallback, and is not available over a UNIX domain socket.

Lower-level building blocks

The handlers and clients above are built on a small set of public pieces, which you can use directly for other protocols that ride on HTTP Datagrams:


← Back to Main Page | HTTP Server & Client | WebSocket service and client | DNS | TLS, DTLS and QUIC

Gumdrop MASQUE