Skip to content

Commit 39eda25

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

File tree

35 files changed

+136
-111
lines changed

35 files changed

+136
-111
lines changed

adapter/outbound/hysteria2.go

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,39 @@ 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{}
8+
type unsafeRandReader struct{}
99

10-
func (r fastRandReader) Read(p []byte) (int, error) {
11-
return fastrand.Read(p)
10+
func (r unsafeRandReader) Read(p []byte) (n int, err error) {
11+
// modify from https://github.com/golang/go/blob/587c3847da81aa7cfc3b3db2677c8586c94df13a/src/runtime/rand.go#L70-L89
12+
// Inspired by wyrand.
13+
n = len(p)
14+
v := randv2.Uint64()
15+
for len(p) > 0 {
16+
v ^= 0xa0761d6478bd642f
17+
v *= 0xe7037ed1a0b428db
18+
size := 8
19+
if len(p) < 8 {
20+
size = len(p)
21+
}
22+
for i := 0; i < size; i++ {
23+
p[i] ^= byte(v >> (8 * i))
24+
}
25+
p = p[size:]
26+
v = v>>32 | v<<32
27+
}
28+
29+
return
1230
}
1331

14-
var UnsafeUUIDGenerator = uuid.NewGenWithOptions(uuid.WithRandomReader(fastRandReader{}))
32+
var UnsafeRandReader = unsafeRandReader{}
33+
34+
var UnsafeUUIDGenerator = uuid.NewGenWithOptions(uuid.WithRandomReader(UnsafeRandReader))
1535

1636
func NewUUIDV1() uuid.UUID {
17-
u, _ := UnsafeUUIDGenerator.NewV1() // fastrand.Read wouldn't cause error, so ignore err is safe
37+
u, _ := UnsafeUUIDGenerator.NewV1() // unsafeRandReader wouldn't cause error, so ignore err is safe
1838
return u
1939
}
2040

@@ -23,7 +43,7 @@ func NewUUIDV3(ns uuid.UUID, name string) uuid.UUID {
2343
}
2444

2545
func NewUUIDV4() uuid.UUID {
26-
u, _ := UnsafeUUIDGenerator.NewV4() // fastrand.Read wouldn't cause error, so ignore err is safe
46+
u, _ := UnsafeUUIDGenerator.NewV4() // unsafeRandReader wouldn't cause error, so ignore err is safe
2747
return u
2848
}
2949

@@ -32,12 +52,12 @@ func NewUUIDV5(ns uuid.UUID, name string) uuid.UUID {
3252
}
3353

3454
func NewUUIDV6() uuid.UUID {
35-
u, _ := UnsafeUUIDGenerator.NewV6() // fastrand.Read wouldn't cause error, so ignore err is safe
55+
u, _ := UnsafeUUIDGenerator.NewV6() // unsafeRandReader wouldn't cause error, so ignore err is safe
3656
return u
3757
}
3858

3959
func NewUUIDV7() uuid.UUID {
40-
u, _ := UnsafeUUIDGenerator.NewV7() // fastrand.Read wouldn't cause error, so ignore err is safe
60+
u, _ := UnsafeUUIDGenerator.NewV7() // unsafeRandReader wouldn't cause error, so ignore err is safe
4161
return u
4262
}
4363

component/resolver/host.go

Lines changed: 2 additions & 2 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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.2.0
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

0 commit comments

Comments
 (0)