Skip to content

Commit

Permalink
webrtc: fix ufrag prefix for dialing (#2832)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Jun 12, 2024
1 parent 1be87de commit 398c0b4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 5 additions & 3 deletions p2p/transport/webrtc/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,15 +415,17 @@ func genUfrag() string {
uFragAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
uFragPrefix = "libp2p+webrtc+v1/"
uFragIdLength = 32
uFragIdOffset = len(uFragPrefix)
uFragLength = uFragIdOffset + uFragIdLength
uFragLength = len(uFragPrefix) + uFragIdLength
)

seed := [8]byte{}
rand.Read(seed[:])
r := mrand.New(mrand.NewSource(binary.BigEndian.Uint64(seed[:])))
b := make([]byte, uFragLength)
for i := uFragIdOffset; i < uFragLength; i++ {
for i := 0; i < len(uFragPrefix); i++ {
b[i] = uFragPrefix[i]
}
for i := len(uFragPrefix); i < uFragLength; i++ {
b[i] = uFragAlphabet[r.Intn(len(uFragAlphabet))]
}
return string(b)
Expand Down
7 changes: 7 additions & 0 deletions p2p/transport/webrtc/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,3 +860,10 @@ func TestMaxInFlightRequests(t *testing.T) {
require.Equal(t, count, int(success.Load()), "expected exactly 3 dial successes")
require.Equal(t, 1, int(fails.Load()), "expected exactly 1 dial failure")
}

func TestGenUfrag(t *testing.T) {
for i := 0; i < 10; i++ {
s := genUfrag()
require.True(t, strings.HasPrefix(s, "libp2p+webrtc+v1/"))
}
}

0 comments on commit 398c0b4

Please sign in to comment.