Skip to content

Commit d7adc59

Browse files
committed
webrtc: add certhashes to discovered webrtc addresses
1 parent a8cbee8 commit d7adc59

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

p2p/host/basic/basic_host.go

+17
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
2828
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
2929
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
30+
libp2pwebrtc "github.com/libp2p/go-libp2p/p2p/transport/webrtc"
3031
libp2pwebtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"
3132
"github.com/prometheus/client_golang/prometheus"
3233

@@ -801,6 +802,22 @@ func (h *BasicHost) Addrs() []ma.Multiaddr {
801802
addrs[i] = addrWithCerthash
802803
}
803804
}
805+
806+
for i, addr := range addrs {
807+
if ok, n := libp2pwebrtc.IsWebRTCDirectMultiaddr(addr); ok && n == 0 {
808+
t := s.TransportForListening(addr)
809+
tpt, ok := t.(addCertHasher)
810+
if !ok {
811+
continue
812+
}
813+
addrWithCerthash, added := tpt.AddCertHashes(addr)
814+
if !added {
815+
log.Debug("Couldn't add certhashes to webtransport multiaddr because we aren't listening on webtransport")
816+
continue
817+
}
818+
addrs[i] = addrWithCerthash
819+
}
820+
}
804821
return addrs
805822
}
806823

p2p/transport/webrtc/transport.go

+50
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,56 @@ func (t *WebRTCTransport) RemoveMux(mux *udpmux.UDPMux) {
576576
t.v6Reuse.Delete(mux)
577577
}
578578

579+
func (t *WebRTCTransport) AddCertHashes(addr ma.Multiaddr) (ma.Multiaddr, bool) {
580+
listenerFingerprint, err := t.getCertificateFingerprint()
581+
if err != nil {
582+
return nil, false
583+
}
584+
585+
encodedLocalFingerprint, err := encodeDTLSFingerprint(listenerFingerprint)
586+
if err != nil {
587+
return nil, false
588+
}
589+
590+
certComp, err := ma.NewComponent(ma.ProtocolWithCode(ma.P_CERTHASH).Name, encodedLocalFingerprint)
591+
if err != nil {
592+
return nil, false
593+
}
594+
595+
return addr.Encapsulate(certComp), true
596+
}
597+
598+
// IsWebRTCDirectMultiaddr returns whether addr is a /webrtc-direct multiaddr and the number of
599+
// certhashes found
600+
func IsWebRTCDirectMultiaddr(addr ma.Multiaddr) (bool, int) {
601+
const (
602+
init = iota
603+
foundUDP
604+
foundWebRTCDirect
605+
)
606+
state := init
607+
certhashCount := 0
608+
609+
ma.ForEach(addr, func(c ma.Component) bool {
610+
switch c.Protocol().Code {
611+
case ma.P_UDP:
612+
if state == init {
613+
state = foundUDP
614+
}
615+
case ma.P_WEBRTC_DIRECT:
616+
if state == foundUDP {
617+
state = foundWebRTCDirect
618+
}
619+
case ma.P_CERTHASH:
620+
if state == foundWebRTCDirect {
621+
certhashCount++
622+
}
623+
}
624+
return true
625+
})
626+
return state == foundWebRTCDirect, certhashCount
627+
}
628+
579629
type fakeStreamConn struct{ *stream }
580630

581631
func (fakeStreamConn) LocalAddr() net.Addr { return nil }

0 commit comments

Comments
 (0)