mirror of
https://github.com/xtaci/kcp-go.git
synced 2024-04-21 12:32:15 +00:00
enhance convid security and fix a race of random number generator
This commit is contained in:
@@ -20,7 +20,6 @@ import (
|
||||
var (
|
||||
errTimeout = errors.New("i/o timeout")
|
||||
errBrokenPipe = errors.New("broken pipe")
|
||||
rng = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -58,6 +57,7 @@ type (
|
||||
ackNoDelay bool
|
||||
keepAliveInterval time.Duration
|
||||
xmitBuf sync.Pool
|
||||
rng *rand.Rand
|
||||
}
|
||||
)
|
||||
|
||||
@@ -76,6 +76,7 @@ func newUDPSession(conv uint32, dataShards, parityShards int, l *Listener, conn
|
||||
sess.l = l
|
||||
sess.block = block
|
||||
sess.fec = newFEC(rxFecLimit, dataShards, parityShards)
|
||||
sess.rng = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
sess.xmitBuf.New = func() interface{} {
|
||||
return make([]byte, mtuLimit)
|
||||
}
|
||||
@@ -452,7 +453,7 @@ func (s *UDPSession) outputTask() {
|
||||
interval := s.keepAliveInterval
|
||||
s.mu.Unlock()
|
||||
if interval > 0 && time.Now().After(lastPing.Add(interval)) {
|
||||
sz := rng.Intn(IKCP_MTU_DEF - s.headerSize - IKCP_OVERHEAD)
|
||||
sz := s.rng.Intn(IKCP_MTU_DEF - s.headerSize - IKCP_OVERHEAD)
|
||||
sz += s.headerSize + IKCP_OVERHEAD
|
||||
ping := make([]byte, sz)
|
||||
io.ReadFull(crand.Reader, ping)
|
||||
@@ -824,7 +825,10 @@ func DialWithOptions(raddr string, block BlockCrypt, dataShards, parityShards in
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newUDPSession(rng.Uint32(), dataShards, parityShards, nil, udpconn, udpaddr, block), nil
|
||||
|
||||
convid := make([]byte, 4)
|
||||
io.ReadFull(crand.Reader, convid)
|
||||
return newUDPSession(binary.LittleEndian.Uint32(convid), dataShards, parityShards, nil, udpconn, udpaddr, block), nil
|
||||
}
|
||||
|
||||
func currentMs() uint32 {
|
||||
|
||||
Reference in New Issue
Block a user