forked from quic-go/quic-go
@@ -3,7 +3,6 @@ package quic
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"math/rand"
|
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -52,9 +51,10 @@ func NewClient(host string, cryptoChangeCallback CryptoChangeCallback, versionNe
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: generate cryptographically secure random ConnectionID
|
connectionID, err := utils.GenerateConnectionID()
|
||||||
rand.Seed(time.Now().UTC().UnixNano())
|
if err != nil {
|
||||||
connectionID := protocol.ConnectionID(rand.Int63())
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
hostname, _, err := net.SplitHostPort(host)
|
hostname, _, err := net.SplitHostPort(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
18
utils/connection_id.go
Normal file
18
utils/connection_id.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/binary"
|
||||||
|
|
||||||
|
"github.com/lucas-clemente/quic-go/protocol"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GenerateConnectionID generates a connection ID using cryptographic random
|
||||||
|
func GenerateConnectionID() (protocol.ConnectionID, error) {
|
||||||
|
b := make([]byte, 8, 8)
|
||||||
|
_, err := rand.Read(b)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return protocol.ConnectionID(binary.LittleEndian.Uint64(b)), nil
|
||||||
|
}
|
||||||
17
utils/connection_id_test.go
Normal file
17
utils/connection_id_test.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
. "github.com/onsi/ginkgo"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ = Describe("Connection ID generation", func() {
|
||||||
|
It("generates random connection IDs", func() {
|
||||||
|
c1, err := GenerateConnectionID()
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
Expect(c1).ToNot(BeZero())
|
||||||
|
c2, err := GenerateConnectionID()
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
Expect(c1).ToNot(Equal(c2))
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user