Skip to content

Commit 932485d

Browse files
committed
chore: replace zhangyunhao116/fastrand to our metacubex/randv2
1 parent d3fea90 commit 932485d

35 files changed

+106
-112
lines changed

adapter/outbound/hysteria2.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121

2222
"github.com/metacubex/sing-quic/hysteria2"
2323

24+
"github.com/metacubex/randv2"
2425
M "github.com/sagernet/sing/common/metadata"
25-
"github.com/zhangyunhao116/fastrand"
2626
)
2727

2828
func init() {
@@ -165,7 +165,7 @@ func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) {
165165
})
166166
if len(serverAddress) > 0 {
167167
clientOptions.ServerAddress = func(ctx context.Context) (*net.UDPAddr, error) {
168-
return resolveUDPAddrWithPrefer(ctx, "udp", serverAddress[fastrand.Intn(len(serverAddress))], C.NewDNSPrefer(option.IPVersion))
168+
return resolveUDPAddrWithPrefer(ctx, "udp", serverAddress[randv2.IntN(len(serverAddress))], C.NewDNSPrefer(option.IPVersion))
169169
}
170170

171171
if option.HopInterval == 0 {

adapter/outbound/ssh.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/metacubex/mihomo/component/proxydialer"
1818
C "github.com/metacubex/mihomo/constant"
1919

20-
"github.com/zhangyunhao116/fastrand"
20+
"github.com/metacubex/randv2"
2121
"golang.org/x/crypto/ssh"
2222
)
2323

@@ -180,10 +180,10 @@ func NewSsh(option SshOption) (*Ssh, error) {
180180
}
181181

182182
version := "SSH-2.0-OpenSSH_"
183-
if fastrand.Intn(2) == 0 {
184-
version += "7." + strconv.Itoa(fastrand.Intn(10))
183+
if randv2.IntN(2) == 0 {
184+
version += "7." + strconv.Itoa(randv2.IntN(10))
185185
} else {
186-
version += "8." + strconv.Itoa(fastrand.Intn(9))
186+
version += "8." + strconv.Itoa(randv2.IntN(9))
187187
}
188188
config.ClientVersion = version
189189

common/convert/util.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88

99
"github.com/metacubex/mihomo/common/utils"
1010

11+
"github.com/metacubex/randv2"
1112
"github.com/metacubex/sing-shadowsocks/shadowimpl"
12-
"github.com/zhangyunhao116/fastrand"
1313
)
1414

1515
var hostsSuffix = []string{
@@ -302,11 +302,11 @@ func RandHost() string {
302302
prefix += string(buf[6:8]) + "-"
303303
prefix += string(buf[len(buf)-8:])
304304

305-
return prefix + hostsSuffix[fastrand.Intn(hostsLen)]
305+
return prefix + hostsSuffix[randv2.IntN(hostsLen)]
306306
}
307307

308308
func RandUserAgent() string {
309-
return userAgents[fastrand.Intn(uaLen)]
309+
return userAgents[randv2.IntN(uaLen)]
310310
}
311311

312312
func SetUserAgent(header http.Header) {

common/pool/alloc_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package pool
33
import (
44
"testing"
55

6+
"github.com/metacubex/randv2"
67
"github.com/stretchr/testify/assert"
7-
"github.com/zhangyunhao116/fastrand"
88
)
99

1010
func TestAllocGet(t *testing.T) {
@@ -43,6 +43,6 @@ func TestAllocPutThenGet(t *testing.T) {
4343

4444
func BenchmarkMSB(b *testing.B) {
4545
for i := 0; i < b.N; i++ {
46-
msb(fastrand.Int())
46+
msb(randv2.Int())
4747
}
4848
}

common/utils/uuid.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@ package utils
22

33
import (
44
"github.com/gofrs/uuid/v5"
5-
"github.com/zhangyunhao116/fastrand"
5+
"github.com/metacubex/randv2"
66
)
77

8-
type fastRandReader struct{}
9-
10-
func (r fastRandReader) Read(p []byte) (int, error) {
11-
return fastrand.Read(p)
12-
}
13-
14-
var UnsafeUUIDGenerator = uuid.NewGenWithOptions(uuid.WithRandomReader(fastRandReader{}))
8+
var UnsafeUUIDGenerator = uuid.NewGenWithOptions(uuid.WithRandomReader(randv2.Reader))
159

1610
func NewUUIDV1() uuid.UUID {
17-
u, _ := UnsafeUUIDGenerator.NewV1() // fastrand.Read wouldn't cause error, so ignore err is safe
11+
u, _ := UnsafeUUIDGenerator.NewV1() // randv2.Read wouldn't cause error, so ignore err is safe
1812
return u
1913
}
2014

@@ -23,7 +17,7 @@ func NewUUIDV3(ns uuid.UUID, name string) uuid.UUID {
2317
}
2418

2519
func NewUUIDV4() uuid.UUID {
26-
u, _ := UnsafeUUIDGenerator.NewV4() // fastrand.Read wouldn't cause error, so ignore err is safe
20+
u, _ := UnsafeUUIDGenerator.NewV4() // randv2.Read wouldn't cause error, so ignore err is safe
2721
return u
2822
}
2923

@@ -32,12 +26,12 @@ func NewUUIDV5(ns uuid.UUID, name string) uuid.UUID {
3226
}
3327

3428
func NewUUIDV6() uuid.UUID {
35-
u, _ := UnsafeUUIDGenerator.NewV6() // fastrand.Read wouldn't cause error, so ignore err is safe
29+
u, _ := UnsafeUUIDGenerator.NewV6() // randv2.Read wouldn't cause error, so ignore err is safe
3630
return u
3731
}
3832

3933
func NewUUIDV7() uuid.UUID {
40-
u, _ := UnsafeUUIDGenerator.NewV7() // fastrand.Read wouldn't cause error, so ignore err is safe
34+
u, _ := UnsafeUUIDGenerator.NewV7() // randv2.Read wouldn't cause error, so ignore err is safe
4135
return u
4236
}
4337

component/resolver/host.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/metacubex/mihomo/common/utils"
1212
"github.com/metacubex/mihomo/component/resolver/hosts"
1313
"github.com/metacubex/mihomo/component/trie"
14-
"github.com/zhangyunhao116/fastrand"
14+
"github.com/metacubex/randv2"
1515
)
1616

1717
var (
@@ -125,5 +125,5 @@ func (hv HostValue) RandIP() (netip.Addr, error) {
125125
if hv.IsDomain {
126126
return netip.Addr{}, errors.New("value type is error")
127127
}
128-
return hv.IPs[fastrand.Intn(len(hv.IPs))], nil
128+
return hv.IPs[randv2.IntN(len(hv.IPs))], nil
129129
}

component/resolver/resolver.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"github.com/metacubex/mihomo/common/utils"
1313
"github.com/metacubex/mihomo/component/trie"
1414

15+
"github.com/metacubex/randv2"
1516
"github.com/miekg/dns"
16-
"github.com/zhangyunhao116/fastrand"
1717
)
1818

1919
var (
@@ -93,7 +93,7 @@ func ResolveIPv4WithResolver(ctx context.Context, host string, r Resolver) (neti
9393
} else if len(ips) == 0 {
9494
return netip.Addr{}, fmt.Errorf("%w: %s", ErrIPNotFound, host)
9595
}
96-
return ips[fastrand.Intn(len(ips))], nil
96+
return ips[randv2.IntN(len(ips))], nil
9797
}
9898

9999
// ResolveIPv4 with a host, return ipv4
@@ -149,7 +149,7 @@ func ResolveIPv6WithResolver(ctx context.Context, host string, r Resolver) (neti
149149
} else if len(ips) == 0 {
150150
return netip.Addr{}, fmt.Errorf("%w: %s", ErrIPNotFound, host)
151151
}
152-
return ips[fastrand.Intn(len(ips))], nil
152+
return ips[randv2.IntN(len(ips))], nil
153153
}
154154

155155
func ResolveIPv6(ctx context.Context, host string) (netip.Addr, error) {
@@ -200,9 +200,9 @@ func ResolveIPWithResolver(ctx context.Context, host string, r Resolver) (netip.
200200
}
201201
ipv4s, ipv6s := SortationAddr(ips)
202202
if len(ipv4s) > 0 {
203-
return ipv4s[fastrand.Intn(len(ipv4s))], nil
203+
return ipv4s[randv2.IntN(len(ipv4s))], nil
204204
}
205-
return ipv6s[fastrand.Intn(len(ipv6s))], nil
205+
return ipv6s[randv2.IntN(len(ipv6s))], nil
206206
}
207207

208208
// ResolveIP with a host, return ip and priority return TypeA

component/tls/reality.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"github.com/metacubex/mihomo/log"
2323
"github.com/metacubex/mihomo/ntp"
2424

25+
"github.com/metacubex/randv2"
2526
utls "github.com/metacubex/utls"
26-
"github.com/zhangyunhao116/fastrand"
2727
"golang.org/x/crypto/chacha20poly1305"
2828
"golang.org/x/crypto/hkdf"
2929
"golang.org/x/net/http2"
@@ -138,13 +138,13 @@ func realityClientFallback(uConn net.Conn, serverName string, fingerprint utls.C
138138
return
139139
}
140140
request.Header.Set("User-Agent", fingerprint.Client)
141-
request.AddCookie(&http.Cookie{Name: "padding", Value: strings.Repeat("0", fastrand.Intn(32)+30)})
141+
request.AddCookie(&http.Cookie{Name: "padding", Value: strings.Repeat("0", randv2.IntN(32)+30)})
142142
response, err := client.Do(request)
143143
if err != nil {
144144
return
145145
}
146146
//_, _ = io.Copy(io.Discard, response.Body)
147-
time.Sleep(time.Duration(5+fastrand.Int63n(10)) * time.Second)
147+
time.Sleep(time.Duration(5+randv2.IntN(10)) * time.Second)
148148
response.Body.Close()
149149
client.CloseIdleConnections()
150150
}

dns/client.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
C "github.com/metacubex/mihomo/constant"
1515
"github.com/metacubex/mihomo/log"
1616

17+
"github.com/metacubex/randv2"
1718
D "github.com/miekg/dns"
18-
"github.com/zhangyunhao116/fastrand"
1919
)
2020

2121
type client struct {
@@ -65,7 +65,7 @@ func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error)
6565
} else if len(ips) == 0 {
6666
return nil, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, c.host)
6767
}
68-
ip = ips[fastrand.Intn(len(ips))]
68+
ip = ips[randv2.IntN(len(ips))]
6969
}
7070

7171
network := "udp"

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/mdlayher/netlink v1.7.2
2121
github.com/metacubex/gopacket v1.1.20-0.20230608035415-7e2f98a3e759
2222
github.com/metacubex/quic-go v0.44.1-0.20240521004242-fcd70d587e22
23+
github.com/metacubex/randv2 v0.1.2
2324
github.com/metacubex/sing-quic v0.0.0-20240518034124-7696d3f7da72
2425
github.com/metacubex/sing-shadowsocks v0.2.6
2526
github.com/metacubex/sing-shadowsocks2 v0.2.0
@@ -44,7 +45,6 @@ require (
4445
github.com/sirupsen/logrus v1.9.3
4546
github.com/stretchr/testify v1.9.0
4647
github.com/wk8/go-ordered-map/v2 v2.1.8
47-
github.com/zhangyunhao116/fastrand v0.4.0
4848
go.uber.org/automaxprocs v1.5.3
4949
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
5050
golang.org/x/crypto v0.23.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ github.com/metacubex/gvisor v0.0.0-20240320004321-933faba989ec h1:HxreOiFTUrJXJa
106106
github.com/metacubex/gvisor v0.0.0-20240320004321-933faba989ec/go.mod h1:8BVmQ+3cxjqzWElafm24rb2Ae4jRI6vAXNXWqWjfrXw=
107107
github.com/metacubex/quic-go v0.44.1-0.20240521004242-fcd70d587e22 h1:hsQ0b2A509b6ubnLtLOcUgZ8vOb+d/667zVEJ1T2fao=
108108
github.com/metacubex/quic-go v0.44.1-0.20240521004242-fcd70d587e22/go.mod h1:88wAATpevav4xdy5N8oejQ2cbbI6EcLYEklFeo+qywA=
109+
github.com/metacubex/randv2 v0.1.2 h1:k3T2yI/hNymhBsYXWahMjKKaEfG+yI7mH3ymgjgeBr8=
110+
github.com/metacubex/randv2 v0.1.2/go.mod h1:kFi2SzrQ5WuneuoLLCMkABtiBu6VRrMrWFqSPyj2cxY=
109111
github.com/metacubex/sing v0.0.0-20240518125217-e63d65a914d1 h1:7hDHLTmjgtRoAp59STwPQpe5Pinwi4cWex+FB3Ohvco=
110112
github.com/metacubex/sing v0.0.0-20240518125217-e63d65a914d1/go.mod h1:+60H3Cm91RnL9dpVGWDPHt0zTQImO9Vfqt9a4rSambI=
111113
github.com/metacubex/sing-quic v0.0.0-20240518034124-7696d3f7da72 h1:Wr4g1HCb5Z/QIFwFiVNjO2qL+dRu25+Mdn9xtAZZ+ew=
@@ -210,8 +212,6 @@ github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/
210212
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
211213
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
212214
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
213-
github.com/zhangyunhao116/fastrand v0.4.0 h1:86QB6Y+GGgLZRFRDCjMmAS28QULwspK9sgL5d1Bx3H4=
214-
github.com/zhangyunhao116/fastrand v0.4.0/go.mod h1:vIyo6EyBhjGKpZv6qVlkPl4JVAklpMM4DSKzbAkMguA=
215215
gitlab.com/yawning/bsaes.git v0.0.0-20190805113838-0a714cd429ec h1:FpfFs4EhNehiVfzQttTuxanPIT43FtkkCFypIod8LHo=
216216
gitlab.com/yawning/bsaes.git v0.0.0-20190805113838-0a714cd429ec/go.mod h1:BZ1RAoRPbCxum9Grlv5aeksu2H8BiKehBYooU2LFiOQ=
217217
go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=

transport/hysteria/conns/udp/hop.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/metacubex/mihomo/transport/hysteria/obfs"
1313
"github.com/metacubex/mihomo/transport/hysteria/utils"
1414

15-
"github.com/zhangyunhao116/fastrand"
15+
"github.com/metacubex/randv2"
1616
)
1717

1818
const (
@@ -86,7 +86,7 @@ func NewObfsUDPHopClientPacketConn(server string, serverPorts string, hopInterva
8686
serverAddrs: serverAddrs,
8787
hopInterval: hopInterval,
8888
obfs: obfs,
89-
addrIndex: fastrand.Intn(len(serverAddrs)),
89+
addrIndex: randv2.IntN(len(serverAddrs)),
9090
recvQueue: make(chan *udpPacket, packetQueueSize),
9191
closeChan: make(chan struct{}),
9292
bufPool: sync.Pool{
@@ -177,7 +177,7 @@ func (c *ObfsUDPHopClientPacketConn) hop(dialer utils.PacketDialer, rAddr net.Ad
177177
_ = trySetPacketConnWriteBuffer(c.currentConn, c.writeBufferSize)
178178
}
179179
go c.recvRoutine(c.currentConn)
180-
c.addrIndex = fastrand.Intn(len(c.serverAddrs))
180+
c.addrIndex = randv2.IntN(len(c.serverAddrs))
181181
}
182182

183183
func (c *ObfsUDPHopClientPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {

transport/hysteria/conns/wechat/obfs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/metacubex/mihomo/log"
1010
"github.com/metacubex/mihomo/transport/hysteria/obfs"
1111

12-
"github.com/zhangyunhao116/fastrand"
12+
"github.com/metacubex/randv2"
1313
)
1414

1515
const udpBufferSize = 65535
@@ -31,7 +31,7 @@ func NewObfsWeChatUDPConn(orig net.PacketConn, obfs obfs.Obfuscator) *ObfsWeChat
3131
obfs: obfs,
3232
readBuf: make([]byte, udpBufferSize),
3333
writeBuf: make([]byte, udpBufferSize),
34-
sn: fastrand.Uint32() & 0xFFFF,
34+
sn: randv2.Uint32() & 0xFFFF,
3535
}
3636
}
3737

transport/hysteria/core/client.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"github.com/lunixbochs/struc"
2020
"github.com/metacubex/quic-go"
2121
"github.com/metacubex/quic-go/congestion"
22-
"github.com/zhangyunhao116/fastrand"
22+
"github.com/metacubex/randv2"
2323
)
2424

2525
var (
@@ -405,7 +405,7 @@ func (c *quicPktConn) WriteTo(p []byte, addr string) error {
405405
var errSize *quic.DatagramTooLargeError
406406
if errors.As(err, &errSize) {
407407
// need to frag
408-
msg.MsgID = uint16(fastrand.Intn(0xFFFF)) + 1 // msgID must be > 0 when fragCount > 1
408+
msg.MsgID = uint16(randv2.IntN(0xFFFF)) + 1 // msgID must be > 0 when fragCount > 1
409409
fragMsgs := fragUDPMessage(msg, int(errSize.MaxDatagramPayloadSize))
410410
for _, fragMsg := range fragMsgs {
411411
msgBuf.Reset()

transport/hysteria/obfs/xplus.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package obfs
33
import (
44
"crypto/sha256"
55

6-
"github.com/zhangyunhao116/fastrand"
6+
"github.com/metacubex/randv2"
77
)
88

99
// [salt][obfuscated payload]
@@ -35,7 +35,7 @@ func (x *XPlusObfuscator) Deobfuscate(in []byte, out []byte) int {
3535
}
3636

3737
func (x *XPlusObfuscator) Obfuscate(in []byte, out []byte) int {
38-
_, _ = fastrand.Read(out[:saltLen]) // salt
38+
_, _ = randv2.Read(out[:saltLen]) // salt
3939
// Obfuscate the payload
4040
key := sha256.Sum256(append(x.Key, out[:saltLen]...))
4141
for i, c := range in {

transport/simple-obfs/http.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
"github.com/metacubex/mihomo/common/pool"
1212

13-
"github.com/zhangyunhao116/fastrand"
13+
"github.com/metacubex/randv2"
1414
)
1515

1616
// HTTPObfs is shadowsocks http simple-obfs implementation
@@ -64,12 +64,12 @@ func (ho *HTTPObfs) Read(b []byte) (int, error) {
6464
func (ho *HTTPObfs) Write(b []byte) (int, error) {
6565
if ho.firstRequest {
6666
randBytes := make([]byte, 16)
67-
fastrand.Read(randBytes)
67+
randv2.Read(randBytes)
6868
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/", ho.host), bytes.NewBuffer(b[:]))
6969
if err != nil {
7070
return 0, err
7171
}
72-
req.Header.Set("User-Agent", fmt.Sprintf("curl/7.%d.%d", fastrand.Int()%54, fastrand.Int()%2))
72+
req.Header.Set("User-Agent", fmt.Sprintf("curl/7.%d.%d", randv2.Int()%54, randv2.Int()%2))
7373
req.Header.Set("Upgrade", "websocket")
7474
req.Header.Set("Connection", "Upgrade")
7575
req.Host = ho.host

transport/simple-obfs/tls.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"github.com/metacubex/mihomo/common/pool"
1111

12-
"github.com/zhangyunhao116/fastrand"
12+
"github.com/metacubex/randv2"
1313
)
1414

1515
const (
@@ -127,8 +127,8 @@ func NewTLSObfs(conn net.Conn, server string) net.Conn {
127127
func makeClientHelloMsg(data []byte, server string) []byte {
128128
random := make([]byte, 28)
129129
sessionID := make([]byte, 32)
130-
fastrand.Read(random)
131-
fastrand.Read(sessionID)
130+
randv2.Read(random)
131+
randv2.Read(sessionID)
132132

133133
buf := &bytes.Buffer{}
134134

0 commit comments

Comments
 (0)