File tree 2 files changed +67
-0
lines changed
2 files changed +67
-0
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ import (
27
27
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
28
28
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
29
29
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
30
+ libp2pwebrtc "github.com/libp2p/go-libp2p/p2p/transport/webrtc"
30
31
libp2pwebtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"
31
32
"github.com/prometheus/client_golang/prometheus"
32
33
@@ -801,6 +802,22 @@ func (h *BasicHost) Addrs() []ma.Multiaddr {
801
802
addrs [i ] = addrWithCerthash
802
803
}
803
804
}
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
+ }
804
821
return addrs
805
822
}
806
823
Original file line number Diff line number Diff line change @@ -576,6 +576,56 @@ func (t *WebRTCTransport) RemoveMux(mux *udpmux.UDPMux) {
576
576
t .v6Reuse .Delete (mux )
577
577
}
578
578
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
+
579
629
type fakeStreamConn struct { * stream }
580
630
581
631
func (fakeStreamConn ) LocalAddr () net.Addr { return nil }
You can’t perform that action at this time.
0 commit comments