Skip to content

Commit

Permalink
chore: replace zhangyunhao116/fastrand to our metacubex/randv2
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed May 31, 2024
1 parent d3fea90 commit 932485d
Show file tree
Hide file tree
Showing 35 changed files with 106 additions and 112 deletions.
4 changes: 2 additions & 2 deletions adapter/outbound/hysteria2.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (

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

"github.com/metacubex/randv2"
M "github.com/sagernet/sing/common/metadata"
"github.com/zhangyunhao116/fastrand"
)

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

if option.HopInterval == 0 {
Expand Down
8 changes: 4 additions & 4 deletions adapter/outbound/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/metacubex/mihomo/component/proxydialer"
C "github.com/metacubex/mihomo/constant"

"github.com/zhangyunhao116/fastrand"
"github.com/metacubex/randv2"
"golang.org/x/crypto/ssh"
)

Expand Down Expand Up @@ -180,10 +180,10 @@ func NewSsh(option SshOption) (*Ssh, error) {
}

version := "SSH-2.0-OpenSSH_"
if fastrand.Intn(2) == 0 {
version += "7." + strconv.Itoa(fastrand.Intn(10))
if randv2.IntN(2) == 0 {
version += "7." + strconv.Itoa(randv2.IntN(10))
} else {
version += "8." + strconv.Itoa(fastrand.Intn(9))
version += "8." + strconv.Itoa(randv2.IntN(9))
}
config.ClientVersion = version

Expand Down
6 changes: 3 additions & 3 deletions common/convert/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

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

"github.com/metacubex/randv2"
"github.com/metacubex/sing-shadowsocks/shadowimpl"
"github.com/zhangyunhao116/fastrand"
)

var hostsSuffix = []string{
Expand Down Expand Up @@ -302,11 +302,11 @@ func RandHost() string {
prefix += string(buf[6:8]) + "-"
prefix += string(buf[len(buf)-8:])

return prefix + hostsSuffix[fastrand.Intn(hostsLen)]
return prefix + hostsSuffix[randv2.IntN(hostsLen)]
}

func RandUserAgent() string {
return userAgents[fastrand.Intn(uaLen)]
return userAgents[randv2.IntN(uaLen)]
}

func SetUserAgent(header http.Header) {
Expand Down
4 changes: 2 additions & 2 deletions common/pool/alloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package pool
import (
"testing"

"github.com/metacubex/randv2"
"github.com/stretchr/testify/assert"
"github.com/zhangyunhao116/fastrand"
)

func TestAllocGet(t *testing.T) {
Expand Down Expand Up @@ -43,6 +43,6 @@ func TestAllocPutThenGet(t *testing.T) {

func BenchmarkMSB(b *testing.B) {
for i := 0; i < b.N; i++ {
msb(fastrand.Int())
msb(randv2.Int())
}
}
18 changes: 6 additions & 12 deletions common/utils/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ package utils

import (
"github.com/gofrs/uuid/v5"
"github.com/zhangyunhao116/fastrand"
"github.com/metacubex/randv2"
)

type fastRandReader struct{}

func (r fastRandReader) Read(p []byte) (int, error) {
return fastrand.Read(p)
}

var UnsafeUUIDGenerator = uuid.NewGenWithOptions(uuid.WithRandomReader(fastRandReader{}))
var UnsafeUUIDGenerator = uuid.NewGenWithOptions(uuid.WithRandomReader(randv2.Reader))

func NewUUIDV1() uuid.UUID {
u, _ := UnsafeUUIDGenerator.NewV1() // fastrand.Read wouldn't cause error, so ignore err is safe
u, _ := UnsafeUUIDGenerator.NewV1() // randv2.Read wouldn't cause error, so ignore err is safe
return u
}

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

func NewUUIDV4() uuid.UUID {
u, _ := UnsafeUUIDGenerator.NewV4() // fastrand.Read wouldn't cause error, so ignore err is safe
u, _ := UnsafeUUIDGenerator.NewV4() // randv2.Read wouldn't cause error, so ignore err is safe
return u
}

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

func NewUUIDV6() uuid.UUID {
u, _ := UnsafeUUIDGenerator.NewV6() // fastrand.Read wouldn't cause error, so ignore err is safe
u, _ := UnsafeUUIDGenerator.NewV6() // randv2.Read wouldn't cause error, so ignore err is safe
return u
}

func NewUUIDV7() uuid.UUID {
u, _ := UnsafeUUIDGenerator.NewV7() // fastrand.Read wouldn't cause error, so ignore err is safe
u, _ := UnsafeUUIDGenerator.NewV7() // randv2.Read wouldn't cause error, so ignore err is safe
return u
}

Expand Down
4 changes: 2 additions & 2 deletions component/resolver/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/metacubex/mihomo/common/utils"
"github.com/metacubex/mihomo/component/resolver/hosts"
"github.com/metacubex/mihomo/component/trie"
"github.com/zhangyunhao116/fastrand"
"github.com/metacubex/randv2"
)

var (
Expand Down Expand Up @@ -125,5 +125,5 @@ func (hv HostValue) RandIP() (netip.Addr, error) {
if hv.IsDomain {
return netip.Addr{}, errors.New("value type is error")
}
return hv.IPs[fastrand.Intn(len(hv.IPs))], nil
return hv.IPs[randv2.IntN(len(hv.IPs))], nil
}
10 changes: 5 additions & 5 deletions component/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/metacubex/mihomo/common/utils"
"github.com/metacubex/mihomo/component/trie"

"github.com/metacubex/randv2"
"github.com/miekg/dns"
"github.com/zhangyunhao116/fastrand"
)

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

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

func ResolveIPv6(ctx context.Context, host string) (netip.Addr, error) {
Expand Down Expand Up @@ -200,9 +200,9 @@ func ResolveIPWithResolver(ctx context.Context, host string, r Resolver) (netip.
}
ipv4s, ipv6s := SortationAddr(ips)
if len(ipv4s) > 0 {
return ipv4s[fastrand.Intn(len(ipv4s))], nil
return ipv4s[randv2.IntN(len(ipv4s))], nil
}
return ipv6s[fastrand.Intn(len(ipv6s))], nil
return ipv6s[randv2.IntN(len(ipv6s))], nil
}

// ResolveIP with a host, return ip and priority return TypeA
Expand Down
6 changes: 3 additions & 3 deletions component/tls/reality.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"github.com/metacubex/mihomo/log"
"github.com/metacubex/mihomo/ntp"

"github.com/metacubex/randv2"
utls "github.com/metacubex/utls"
"github.com/zhangyunhao116/fastrand"
"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/hkdf"
"golang.org/x/net/http2"
Expand Down Expand Up @@ -138,13 +138,13 @@ func realityClientFallback(uConn net.Conn, serverName string, fingerprint utls.C
return
}
request.Header.Set("User-Agent", fingerprint.Client)
request.AddCookie(&http.Cookie{Name: "padding", Value: strings.Repeat("0", fastrand.Intn(32)+30)})
request.AddCookie(&http.Cookie{Name: "padding", Value: strings.Repeat("0", randv2.IntN(32)+30)})
response, err := client.Do(request)
if err != nil {
return
}
//_, _ = io.Copy(io.Discard, response.Body)
time.Sleep(time.Duration(5+fastrand.Int63n(10)) * time.Second)
time.Sleep(time.Duration(5+randv2.IntN(10)) * time.Second)
response.Body.Close()
client.CloseIdleConnections()
}
Expand Down
4 changes: 2 additions & 2 deletions dns/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/log"

"github.com/metacubex/randv2"
D "github.com/miekg/dns"
"github.com/zhangyunhao116/fastrand"
)

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

network := "udp"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/mdlayher/netlink v1.7.2
github.com/metacubex/gopacket v1.1.20-0.20230608035415-7e2f98a3e759
github.com/metacubex/quic-go v0.44.1-0.20240521004242-fcd70d587e22
github.com/metacubex/randv2 v0.1.2
github.com/metacubex/sing-quic v0.0.0-20240518034124-7696d3f7da72
github.com/metacubex/sing-shadowsocks v0.2.6
github.com/metacubex/sing-shadowsocks2 v0.2.0
Expand All @@ -44,7 +45,6 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/wk8/go-ordered-map/v2 v2.1.8
github.com/zhangyunhao116/fastrand v0.4.0
go.uber.org/automaxprocs v1.5.3
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
golang.org/x/crypto v0.23.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ github.com/metacubex/gvisor v0.0.0-20240320004321-933faba989ec h1:HxreOiFTUrJXJa
github.com/metacubex/gvisor v0.0.0-20240320004321-933faba989ec/go.mod h1:8BVmQ+3cxjqzWElafm24rb2Ae4jRI6vAXNXWqWjfrXw=
github.com/metacubex/quic-go v0.44.1-0.20240521004242-fcd70d587e22 h1:hsQ0b2A509b6ubnLtLOcUgZ8vOb+d/667zVEJ1T2fao=
github.com/metacubex/quic-go v0.44.1-0.20240521004242-fcd70d587e22/go.mod h1:88wAATpevav4xdy5N8oejQ2cbbI6EcLYEklFeo+qywA=
github.com/metacubex/randv2 v0.1.2 h1:k3T2yI/hNymhBsYXWahMjKKaEfG+yI7mH3ymgjgeBr8=
github.com/metacubex/randv2 v0.1.2/go.mod h1:kFi2SzrQ5WuneuoLLCMkABtiBu6VRrMrWFqSPyj2cxY=
github.com/metacubex/sing v0.0.0-20240518125217-e63d65a914d1 h1:7hDHLTmjgtRoAp59STwPQpe5Pinwi4cWex+FB3Ohvco=
github.com/metacubex/sing v0.0.0-20240518125217-e63d65a914d1/go.mod h1:+60H3Cm91RnL9dpVGWDPHt0zTQImO9Vfqt9a4rSambI=
github.com/metacubex/sing-quic v0.0.0-20240518034124-7696d3f7da72 h1:Wr4g1HCb5Z/QIFwFiVNjO2qL+dRu25+Mdn9xtAZZ+ew=
Expand Down Expand Up @@ -210,8 +212,6 @@ github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
github.com/zhangyunhao116/fastrand v0.4.0 h1:86QB6Y+GGgLZRFRDCjMmAS28QULwspK9sgL5d1Bx3H4=
github.com/zhangyunhao116/fastrand v0.4.0/go.mod h1:vIyo6EyBhjGKpZv6qVlkPl4JVAklpMM4DSKzbAkMguA=
gitlab.com/yawning/bsaes.git v0.0.0-20190805113838-0a714cd429ec h1:FpfFs4EhNehiVfzQttTuxanPIT43FtkkCFypIod8LHo=
gitlab.com/yawning/bsaes.git v0.0.0-20190805113838-0a714cd429ec/go.mod h1:BZ1RAoRPbCxum9Grlv5aeksu2H8BiKehBYooU2LFiOQ=
go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
Expand Down
6 changes: 3 additions & 3 deletions transport/hysteria/conns/udp/hop.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/metacubex/mihomo/transport/hysteria/obfs"
"github.com/metacubex/mihomo/transport/hysteria/utils"

"github.com/zhangyunhao116/fastrand"
"github.com/metacubex/randv2"
)

const (
Expand Down Expand Up @@ -86,7 +86,7 @@ func NewObfsUDPHopClientPacketConn(server string, serverPorts string, hopInterva
serverAddrs: serverAddrs,
hopInterval: hopInterval,
obfs: obfs,
addrIndex: fastrand.Intn(len(serverAddrs)),
addrIndex: randv2.IntN(len(serverAddrs)),
recvQueue: make(chan *udpPacket, packetQueueSize),
closeChan: make(chan struct{}),
bufPool: sync.Pool{
Expand Down Expand Up @@ -177,7 +177,7 @@ func (c *ObfsUDPHopClientPacketConn) hop(dialer utils.PacketDialer, rAddr net.Ad
_ = trySetPacketConnWriteBuffer(c.currentConn, c.writeBufferSize)
}
go c.recvRoutine(c.currentConn)
c.addrIndex = fastrand.Intn(len(c.serverAddrs))
c.addrIndex = randv2.IntN(len(c.serverAddrs))
}

func (c *ObfsUDPHopClientPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
Expand Down
4 changes: 2 additions & 2 deletions transport/hysteria/conns/wechat/obfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/metacubex/mihomo/log"
"github.com/metacubex/mihomo/transport/hysteria/obfs"

"github.com/zhangyunhao116/fastrand"
"github.com/metacubex/randv2"
)

const udpBufferSize = 65535
Expand All @@ -31,7 +31,7 @@ func NewObfsWeChatUDPConn(orig net.PacketConn, obfs obfs.Obfuscator) *ObfsWeChat
obfs: obfs,
readBuf: make([]byte, udpBufferSize),
writeBuf: make([]byte, udpBufferSize),
sn: fastrand.Uint32() & 0xFFFF,
sn: randv2.Uint32() & 0xFFFF,
}
}

Expand Down
4 changes: 2 additions & 2 deletions transport/hysteria/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/lunixbochs/struc"
"github.com/metacubex/quic-go"
"github.com/metacubex/quic-go/congestion"
"github.com/zhangyunhao116/fastrand"
"github.com/metacubex/randv2"
)

var (
Expand Down Expand Up @@ -405,7 +405,7 @@ func (c *quicPktConn) WriteTo(p []byte, addr string) error {
var errSize *quic.DatagramTooLargeError
if errors.As(err, &errSize) {
// need to frag
msg.MsgID = uint16(fastrand.Intn(0xFFFF)) + 1 // msgID must be > 0 when fragCount > 1
msg.MsgID = uint16(randv2.IntN(0xFFFF)) + 1 // msgID must be > 0 when fragCount > 1
fragMsgs := fragUDPMessage(msg, int(errSize.MaxDatagramPayloadSize))
for _, fragMsg := range fragMsgs {
msgBuf.Reset()
Expand Down
4 changes: 2 additions & 2 deletions transport/hysteria/obfs/xplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package obfs
import (
"crypto/sha256"

"github.com/zhangyunhao116/fastrand"
"github.com/metacubex/randv2"
)

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

func (x *XPlusObfuscator) Obfuscate(in []byte, out []byte) int {
_, _ = fastrand.Read(out[:saltLen]) // salt
_, _ = randv2.Read(out[:saltLen]) // salt
// Obfuscate the payload
key := sha256.Sum256(append(x.Key, out[:saltLen]...))
for i, c := range in {
Expand Down
6 changes: 3 additions & 3 deletions transport/simple-obfs/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

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

"github.com/zhangyunhao116/fastrand"
"github.com/metacubex/randv2"
)

// HTTPObfs is shadowsocks http simple-obfs implementation
Expand Down Expand Up @@ -64,12 +64,12 @@ func (ho *HTTPObfs) Read(b []byte) (int, error) {
func (ho *HTTPObfs) Write(b []byte) (int, error) {
if ho.firstRequest {
randBytes := make([]byte, 16)
fastrand.Read(randBytes)
randv2.Read(randBytes)
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/", ho.host), bytes.NewBuffer(b[:]))
if err != nil {
return 0, err
}
req.Header.Set("User-Agent", fmt.Sprintf("curl/7.%d.%d", fastrand.Int()%54, fastrand.Int()%2))
req.Header.Set("User-Agent", fmt.Sprintf("curl/7.%d.%d", randv2.Int()%54, randv2.Int()%2))
req.Header.Set("Upgrade", "websocket")
req.Header.Set("Connection", "Upgrade")
req.Host = ho.host
Expand Down
6 changes: 3 additions & 3 deletions transport/simple-obfs/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

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

"github.com/zhangyunhao116/fastrand"
"github.com/metacubex/randv2"
)

const (
Expand Down Expand Up @@ -127,8 +127,8 @@ func NewTLSObfs(conn net.Conn, server string) net.Conn {
func makeClientHelloMsg(data []byte, server string) []byte {
random := make([]byte, 28)
sessionID := make([]byte, 32)
fastrand.Read(random)
fastrand.Read(sessionID)
randv2.Read(random)
randv2.Read(sessionID)

buf := &bytes.Buffer{}

Expand Down
Loading

0 comments on commit 932485d

Please sign in to comment.