From 09a23950d3c96630308ce23477bfdf0c3a311b90 Mon Sep 17 00:00:00 2001 From: Jorropo Date: Wed, 16 Aug 2023 13:05:22 +0200 Subject: [PATCH] fix: check for UDP in the statemachine webtransport address matcher --- p2p/transport/webtransport/multiaddr.go | 28 ++++++++++++-------- p2p/transport/webtransport/multiaddr_test.go | 1 + 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/p2p/transport/webtransport/multiaddr.go b/p2p/transport/webtransport/multiaddr.go index d6930af363..bd90638b63 100644 --- a/p2p/transport/webtransport/multiaddr.go +++ b/p2p/transport/webtransport/multiaddr.go @@ -89,17 +89,23 @@ func IsWebtransportMultiaddr(multiaddr ma.Multiaddr) (bool, int) { certhashCount := 0 ma.ForEach(multiaddr, func(c ma.Component) bool { - if c.Protocol().Code == ma.P_QUIC_V1 && state == init { - state = foundUDP - } - if c.Protocol().Code == ma.P_QUIC_V1 && state == foundUDP { - state = foundQuicV1 - } - if c.Protocol().Code == ma.P_WEBTRANSPORT && state == foundQuicV1 { - state = foundWebTransport - } - if c.Protocol().Code == ma.P_CERTHASH && state == foundWebTransport { - certhashCount++ + switch c.Protocol().Code { + case ma.P_UDP: + if state == init { + state = foundUDP + } + case ma.P_QUIC_V1: + if state == foundUDP { + state = foundQuicV1 + } + case ma.P_WEBTRANSPORT: + if state == foundQuicV1 { + state = foundWebTransport + } + case ma.P_CERTHASH: + if state == foundWebTransport { + certhashCount++ + } } return true }) diff --git a/p2p/transport/webtransport/multiaddr_test.go b/p2p/transport/webtransport/multiaddr_test.go index 4e9d7c336c..3f0a3ec0bf 100644 --- a/p2p/transport/webtransport/multiaddr_test.go +++ b/p2p/transport/webtransport/multiaddr_test.go @@ -117,6 +117,7 @@ func TestIsWebtransportMultiaddr(t *testing.T) { {addr: "/ip4/1.2.3.4/udp/60042/quic-v1/webtransport/certhash/" + fooHash, want: true, certhashCount: 1}, {addr: "/ip4/1.2.3.4/udp/60042/quic-v1/webtransport/certhash/" + fooHash + "/certhash/" + barHash, want: true, certhashCount: 2}, {addr: "/dns4/example.com/udp/60042/quic-v1/webtransport/certhash/" + fooHash, want: true, certhashCount: 1}, + {addr: "/dns4/example.com/tcp/60042/quic-v1/webtransport/certhash/" + fooHash, want: false}, {addr: "/dns4/example.com/udp/60042/webrtc/certhash/" + fooHash, want: false}, }