Skip to content

router, sdk/js: a browser reaches a router by IP over WebTransport - #920

Merged
aojea merged 1 commit into
google:mainfrom
kaisoz:kaisoz/router-webtransport
Nov 20, 2026
Merged

aojea merged 1 commit into
google:mainfrom
kaisoz:kaisoz/router-webtransport

Conversation

@kaisoz

@kaisoz kaisoz commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

A browser can now join the testnets. A page reaches a router only over WebSocket or WebTransport, and the testnet routers offered neither: they listen on TCP and QUIC and lease only their --external-addr /dnsaddr, whose TXT records hold TCP and QUIC addresses. wss would need a hostname and a CA certificate per router.

WebTransport needs neither. The browser checks the router's self-signed certificate against the certhash in the router's address, so it dials the router by IP.

What changes

  • Router. With --external-addr, the router also leases a /webtransport address for each of its external QUIC addresses. It appends the /webtransport/certhash/… end of its own WebTransport listen address to the QUIC address. The certhash changes with the certificate about every two weeks, so no operator can configure the address. The relay circuit addresses the router hands to members follow the lease, so they carry this address too.
  • Router. An external QUIC address is one given in --external-addr, by IP or by name, or one found under the router's peer ID in a /dnsaddr --external-addr. The router looks up the /dnsaddr before each lease, with a 5-second limit. A failed lookup keeps the last result, so a DNS outage does not withdraw a leased address.
  • Testnet routers listen on /udp/4501/quic-v1/webtransport. It shares the QUIC socket, so no new port or firewall rule is needed.
  • JS SDK. The browser build dials WebTransport beside WebSocket (@libp2p/webtransport).
  • Docs. The router flag reference and the SDK browser guide.

Nothing changes for a router that does not listen on WebTransport: it makes no DNS lookup and leases the same addresses as before. The router's default --listen is TCP only. A router without --external-addr leases all its host addresses as before, and those include its WebTransport listen addresses.

Testing

  • TestRouterLeasesWebTransportForItsExternalQUICAddrs checks the leased WebTransport addresses for an external QUIC address by public IP, by private IP and by name, for the router's own entry in a /dnsaddr, and for the same address given twice. It also checks that the router leases none for a /dnsaddr without a record, for TCP-only addresses, for an IPv6 address with an IPv4 listener, and without a WebTransport listener. TestRouterCircuitAddrsFollowTheExternalAddrs checks that the circuit addresses carry the WebTransport address.
  • A local run with a control plane (agentmesh-one) and one agentmesh-router that listens on QUIC and WebTransport on one UDP port, with --external-addr /ip4/<LAN IP>/udp/<port>/quic-v1 and no other member. The router log and /info listed /ip4/<LAN IP>/udp/<port>/quic-v1/webtransport/certhash/…. A headless Chromium 158 page running the JS SDK browser example joined, held an open WebTransport connection to that router and passed the mesh handshake.
  • Not run on a testnet. The /dnsaddr path is covered by the unit test with a mock resolver only. The JS transport change has no automated test.

Notes

  • The router does not lease an address that peers report. go-libp2p can infer a public WebTransport address from identify observations, but only while 4 peers on distinct IPs hold QUIC connections to the router. A mesh of browsers and TCP-only members would never get the address, and identify runs before /mesh/auth, so unauthenticated peers would count as observers.
  • A lease stays valid across a certificate rotation. go-libp2p puts the certhash of the next certificate in the address before it switches to that certificate, and the router renews its lease every --control-plane-sync-interval.
  • A new testnet router gets its WebTransport address at the first lease after the DNS sync job has published its /dnsaddr record.
  • The router assumes that an external QUIC address maps to the QUIC listener that shares its socket with the WebTransport listener.
  • @libp2p/webtransport depends on @libp2p/noise, the same project as @chainsafe/libp2p-noise under its new name, so the browser bundle carries both.
  • A page with a Content Security Policy needs https: in connect-src: WebTransport dials https://<ip>:<port>.

@kaisoz
kaisoz requested a review from aojea as a code owner November 19, 2026 20:53

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds WebTransport support to the router and JS SDK, enabling browser clients to connect over WebTransport in addition to WebSockets. It updates the router to share its QUIC socket with WebTransport, resolve its own WebTransport addresses, and advertise them. However, there are critical compile-time errors in internal/router/router.go where multiaddr.Multiaddr and multiaddr.Component are incorrectly treated as slices or having non-existent methods. These must be resolved using the proper Protocols() and Protocol().Code APIs as suggested.

Comment thread internal/router/router.go Outdated
Comment on lines +198 to +206
for _, addr := range external {
if addr[len(addr)-1].Code() != multiaddr.P_QUIC_V1 || seen[addr.String()] {
continue
}
seen[addr.String()] = true
if suffix := webTransportSuffixFor(suffixes, addr[0].Code()); suffix != nil {
addrs = append(addrs, addr.Encapsulate(suffix))
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The multiaddr.Multiaddr type is an interface, not a slice, so indexing it directly (e.g., addr[len(addr)-1] or addr[0]) and calling len(addr) will result in compile-time errors. Instead, use addr.Protocols() to inspect the protocols of the multiaddr.

Suggested change
for _, addr := range external {
if addr[len(addr)-1].Code() != multiaddr.P_QUIC_V1 || seen[addr.String()] {
continue
}
seen[addr.String()] = true
if suffix := webTransportSuffixFor(suffixes, addr[0].Code()); suffix != nil {
addrs = append(addrs, addr.Encapsulate(suffix))
}
}
for _, addr := range external {
protocols := addr.Protocols()
if len(protocols) == 0 || protocols[len(protocols)-1].Code != multiaddr.P_QUIC_V1 || seen[addr.String()] {
continue
}
seen[addr.String()] = true
if suffix := webTransportSuffixFor(suffixes, protocols[0].Code); suffix != nil {
addrs = append(addrs, addr.Encapsulate(suffix))
}
}

Comment thread internal/router/router.go Outdated
Comment on lines +213 to +225
func (r *Router) webTransportSuffixesByIPVersion() map[int]multiaddr.Multiaddr {
listen := r.Host.Network().ListenAddresses()
suffixes := map[int]multiaddr.Multiaddr{}
for _, addr := range listen {
// Cut at /webtransport: the QUIC address before it, and the rest with the certhashes.
quic, suffix := multiaddr.SplitFunc(addr, func(c multiaddr.Component) bool { return c.Code() == multiaddr.P_WEBTRANSPORT })
if len(suffix) == 0 || !multiaddr.Contains(listen, quic) {
continue
}
suffixes[quic[0].Code()] = suffix
}
return suffixes
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The multiaddr.Component interface does not have a Code() method; use c.Protocol().Code instead. Additionally, quic is a multiaddr.Multiaddr interface, not a slice, so quic[0] is invalid. Use quic.Protocols() to access the protocol codes.

Suggested change
func (r *Router) webTransportSuffixesByIPVersion() map[int]multiaddr.Multiaddr {
listen := r.Host.Network().ListenAddresses()
suffixes := map[int]multiaddr.Multiaddr{}
for _, addr := range listen {
// Cut at /webtransport: the QUIC address before it, and the rest with the certhashes.
quic, suffix := multiaddr.SplitFunc(addr, func(c multiaddr.Component) bool { return c.Code() == multiaddr.P_WEBTRANSPORT })
if len(suffix) == 0 || !multiaddr.Contains(listen, quic) {
continue
}
suffixes[quic[0].Code()] = suffix
}
return suffixes
}
func (r *Router) webTransportSuffixesByIPVersion() map[int]multiaddr.Multiaddr {
listen := r.Host.Network().ListenAddresses()
suffixes := map[int]multiaddr.Multiaddr{}
for _, addr := range listen {
// Cut at /webtransport: the QUIC address before it, and the rest with the certhashes.
quic, suffix := multiaddr.SplitFunc(addr, func(c multiaddr.Component) bool { return c.Protocol().Code == multiaddr.P_WEBTRANSPORT })
if len(suffix) == 0 || !multiaddr.Contains(listen, quic) {
continue
}
quicProtos := quic.Protocols()
if len(quicProtos) > 0 {
suffixes[quicProtos[0].Code] = suffix
}
}
return suffixes
}

Comment thread internal/router/router.go Outdated
Comment on lines +257 to +261
for _, s := range r.config.ExternalAddrs {
ma, err := multiaddr.NewMultiaddr(s)
if err != nil || ma[0].Code() != multiaddr.P_DNSADDR {
continue
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The multiaddr.Multiaddr interface does not support indexing, so ma[0] will cause a compile-time error. Use ma.Protocols() to check the first protocol code.

Suggested change
for _, s := range r.config.ExternalAddrs {
ma, err := multiaddr.NewMultiaddr(s)
if err != nil || ma[0].Code() != multiaddr.P_DNSADDR {
continue
}
for _, s := range r.config.ExternalAddrs {
ma, err := multiaddr.NewMultiaddr(s)
if err != nil {
continue
}
protocols := ma.Protocols()
if len(protocols) == 0 || protocols[0].Code != multiaddr.P_DNSADDR {
continue
}

@kaisoz
kaisoz force-pushed the kaisoz/router-webtransport branch from 2fa260f to 71488af Compare November 20, 2026 14:39
for _, addr := range listen {
// Cut at /webtransport: the QUIC address before it, and the rest with the certhashes.
_, suffix := multiaddr.SplitFunc(addr, func(c multiaddr.Component) bool { return c.Code() == multiaddr.P_WEBTRANSPORT })
ipVersion := addr[0].Code()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these indexes safe or should we check that value exist?

@aojea

aojea commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

/gemini review , are all indexes [0] safe or should we check

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces WebTransport support to allow browser clients to connect to routers directly by IP, sharing the QUIC port and validating self-signed certificates via certhashes. It updates the router's address resolution logic, Helm chart documentation, JS SDK, and integration tests to support WebTransport. The code review feedback suggests adding defensive checks to ensure multiaddr.Multiaddr instances are not empty before indexing them in internal/router/address.go, preventing potential out-of-bounds panics.

Comment on lines +73 to +79
for _, addr := range external {
transport := addr[len(addr)-1].Code()
if transport != multiaddr.P_QUIC_V1 {
continue
}
// ip4, ip6, dns4, dns6 or dns.
hostProtocol := addr[0].Code()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent potential out-of-bounds panics when processing resolved or configured multiaddrs, we should defensively check that the multiaddr is not empty (len(addr) > 0) before indexing it. This is especially important for correctness and defensive programming when handling dynamic inputs like DNS-resolved addresses.

	for _, addr := range external {
		if len(addr) == 0 {
			continue
		}
		transport := addr[len(addr)-1].Code()
		if transport != multiaddr.P_QUIC_V1 {
			continue
		}
		// ip4, ip6, dns4, dns6 or dns.
		hostProtocol := addr[0].Code()

Comment on lines +96 to +99
for _, addr := range listen {
// Cut at /webtransport: the QUIC address before it, and the rest with the certhashes.
_, suffix := multiaddr.SplitFunc(addr, func(c multiaddr.Component) bool { return c.Code() == multiaddr.P_WEBTRANSPORT })
ipVersion := addr[0].Code()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Defensively check that the listen multiaddr is not empty (len(addr) > 0) before indexing it to avoid potential out-of-bounds panics.

Suggested change
for _, addr := range listen {
// Cut at /webtransport: the QUIC address before it, and the rest with the certhashes.
_, suffix := multiaddr.SplitFunc(addr, func(c multiaddr.Component) bool { return c.Code() == multiaddr.P_WEBTRANSPORT })
ipVersion := addr[0].Code()
for _, addr := range listen {
if len(addr) == 0 {
continue
}
// Cut at /webtransport: the QUIC address before it, and the rest with the certhashes.
_, suffix := multiaddr.SplitFunc(addr, func(c multiaddr.Component) bool { return c.Code() == multiaddr.P_WEBTRANSPORT })
ipVersion := addr[0].Code()

Comment on lines +135 to +138
for _, ma := range r.externalAddrs() {
if hostProtocol := ma[0].Code(); hostProtocol != multiaddr.P_DNSADDR {
continue
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Defensively check that the external multiaddr is not empty (len(ma) > 0) before indexing it to avoid potential out-of-bounds panics.

Suggested change
for _, ma := range r.externalAddrs() {
if hostProtocol := ma[0].Code(); hostProtocol != multiaddr.P_DNSADDR {
continue
}
for _, ma := range r.externalAddrs() {
if len(ma) == 0 {
continue
}
if hostProtocol := ma[0].Code(); hostProtocol != multiaddr.P_DNSADDR {
continue
}

Comment on lines +191 to +193
for _, addr := range external {
transport := addr[len(addr)-1].Code()
hostProtocol := addr[0].Code()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Defensively check that the external multiaddr is not empty (len(addr) > 0) before indexing it to avoid potential out-of-bounds panics.

Suggested change
for _, addr := range external {
transport := addr[len(addr)-1].Code()
hostProtocol := addr[0].Code()
for _, addr := range external {
if len(addr) == 0 {
continue
}
transport := addr[len(addr)-1].Code()
hostProtocol := addr[0].Code()

A browser could not join a mesh whose routers have no hostname and CA
certificate. A page reaches a router over WebSocket or WebTransport
only, and a router with --external-addr leased the addresses the
operator gave, which cannot include a WebTransport address: its certhash
changes with the certificate.

The router now leases a /webtransport address for each of its external
QUIC addresses. It appends the "/webtransport/certhash/..." end of its
own WebTransport listen address to the QUIC address, when it listens for
WebTransport on the IP version the address reaches. An external QUIC
address is one given in --external-addr, by IP or by name, or one found
under the router's peer ID in a /dnsaddr --external-addr. The router
looks the /dnsaddr up before each lease, with a 5-second limit. A failed
lookup keeps the last result and warns once, then counts at debug until
DNS answers again. A record that is gone withdraws the address. The
router warns about an external QUIC address that gets no WebTransport
address because of its IP version. A router that does not listen on
WebTransport makes no lookup and leases what it leased before.

The address code moves from router.go to address.go. The chart README
says what a chart operator adds to let a browser in.

The testnet routers, on Kubernetes and on the GCE VM, listen on
/udp/4501/quic-v1/webtransport, which shares the QUIC socket. The
browser build of the JS SDK dials WebTransport beside WebSocket.

The tests pin: the derived address for an external QUIC address by IP,
by name and from a /dnsaddr; none for TCP, for an IP version without a
WebTransport listener, and without a WebTransport listener; a failed
lookup keeps the address and is counted until DNS answers again, a
lookup with no answer ends at the limit, a changed record replaces it, a
removed record withdraws it; no DNS lookup unless a /dnsaddr and a
WebTransport listener are both present; an empty address is skipped
and does not stop the router; and a lease renewal sends an address that
a second host dials.

A browser test (tests/ui/browser-sdk.spec.js) starts a router with QUIC
and WebTransport on one port and no WebSocket listener: a Chromium page
joins, holds an open connection on the leased WebTransport address and
passes the mesh handshake with the router.
@kaisoz
kaisoz force-pushed the kaisoz/router-webtransport branch from 71488af to 2362700 Compare November 20, 2026 15:42
@aojea
aojea merged commit cbff686 into google:main Nov 20, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants