diff --git a/app/dns/server/nameserver.go b/app/dns/server/nameserver.go index 9d654d9e..30538158 100644 --- a/app/dns/server/nameserver.go +++ b/app/dns/server/nameserver.go @@ -84,7 +84,7 @@ func (v *UDPNameServer) AssignUnusedID(response chan<- *ARecord) uint16 { } for { - id = uint16(dice.Roll(65536)) + id = dice.RandomUint16() if _, found := v.requests[id]; found { continue } diff --git a/common/dice/dice.go b/common/dice/dice.go index c737e1cf..8ffc1d28 100644 --- a/common/dice/dice.go +++ b/common/dice/dice.go @@ -15,6 +15,11 @@ func Roll(n int) int { return rand.Intn(n) } +// RandomUint16 returns a random uint16 value. +func RandomUint16() uint16 { + return uint16(rand.Intn(65536)) +} + func init() { rand.Seed(time.Now().Unix()) } diff --git a/transport/internet/headers/srtp/srtp.go b/transport/internet/headers/srtp/srtp.go index 57969298..cb6c41e3 100644 --- a/transport/internet/headers/srtp/srtp.go +++ b/transport/internet/headers/srtp/srtp.go @@ -2,9 +2,9 @@ package srtp import ( "context" - "math/rand" "v2ray.com/core/common" + "v2ray.com/core/common/dice" "v2ray.com/core/common/serial" ) @@ -27,7 +27,7 @@ func (v *SRTP) Write(b []byte) (int, error) { func NewSRTP(ctx context.Context, config interface{}) (interface{}, error) { return &SRTP{ header: 0xB5E8, - number: uint16(rand.Intn(65536)), + number: dice.RandomUint16(), }, nil } diff --git a/transport/internet/headers/utp/utp.go b/transport/internet/headers/utp/utp.go index 6aeb1bae..c1b894fd 100644 --- a/transport/internet/headers/utp/utp.go +++ b/transport/internet/headers/utp/utp.go @@ -2,9 +2,9 @@ package utp import ( "context" - "math/rand" "v2ray.com/core/common" + "v2ray.com/core/common/dice" "v2ray.com/core/common/serial" ) @@ -29,7 +29,7 @@ func NewUTP(ctx context.Context, config interface{}) (interface{}, error) { return &UTP{ header: 1, extension: 0, - connectionId: uint16(rand.Intn(65536)), + connectionId: dice.RandomUint16(), }, nil } diff --git a/transport/internet/headers/wechat/wechat.go b/transport/internet/headers/wechat/wechat.go index a949bad4..2995d359 100644 --- a/transport/internet/headers/wechat/wechat.go +++ b/transport/internet/headers/wechat/wechat.go @@ -26,7 +26,7 @@ func (vc *VideoChat) Write(b []byte) (int, error) { func NewVideoChat(ctx context.Context, config interface{}) (interface{}, error) { return &VideoChat{ - sn: dice.Roll(65535), + sn: int(dice.RandomUint16()), }, nil } diff --git a/transport/internet/kcp/dialer.go b/transport/internet/kcp/dialer.go index d6b81e1f..ad8604c1 100644 --- a/transport/internet/kcp/dialer.go +++ b/transport/internet/kcp/dialer.go @@ -20,7 +20,7 @@ import ( ) var ( - globalConv = uint32(dice.Roll(65536)) + globalConv = uint32(dice.RandomUint16()) globalPool = internal.NewConnectionPool() ) diff --git a/v2ray_test.go b/v2ray_test.go index 2566520d..70011eb1 100644 --- a/v2ray_test.go +++ b/v2ray_test.go @@ -20,7 +20,7 @@ import ( func TestV2RayClose(t *testing.T) { assert := assert.On(t) - port := v2net.Port(dice.Roll(65535)) + port := v2net.Port(dice.RandomUint16()) config := &Config{ Inbound: []*proxyman.InboundHandlerConfig{ {