chore: use fastrand to replace math/rand

This commit is contained in:
wwqgtxx
2023-03-06 18:10:14 +08:00
parent ad6336231c
commit 6a97ab9ecb
26 changed files with 109 additions and 111 deletions

View File

@ -4,7 +4,6 @@ import (
"context"
"crypto/tls"
"fmt"
"math/rand"
"net"
"net/netip"
"strings"
@ -16,6 +15,7 @@ import (
"github.com/Dreamacro/clash/component/resolver"
D "github.com/miekg/dns"
"github.com/zhangyunhao116/fastrand"
)
type client struct {
@ -68,7 +68,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[rand.Intn(len(ips))]
ip = ips[fastrand.Intn(len(ips))]
}
network := "udp"

View File

@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"math/rand"
"net/netip"
"strings"
"time"
@ -20,6 +19,7 @@ import (
"github.com/Dreamacro/clash/log"
D "github.com/miekg/dns"
"github.com/zhangyunhao116/fastrand"
"golang.org/x/sync/singleflight"
)
@ -113,7 +113,7 @@ func (r *Resolver) ResolveIP(ctx context.Context, host string) (ip netip.Addr, e
} else if len(ips) == 0 {
return netip.Addr{}, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, host)
}
return ips[rand.Intn(len(ips))], nil
return ips[fastrand.Intn(len(ips))], nil
}
// LookupIPv4 request with TypeA
@ -129,7 +129,7 @@ func (r *Resolver) ResolveIPv4(ctx context.Context, host string) (ip netip.Addr,
} else if len(ips) == 0 {
return netip.Addr{}, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, host)
}
return ips[rand.Intn(len(ips))], nil
return ips[fastrand.Intn(len(ips))], nil
}
// LookupIPv6 request with TypeAAAA
@ -145,7 +145,7 @@ func (r *Resolver) ResolveIPv6(ctx context.Context, host string) (ip netip.Addr,
} else if len(ips) == 0 {
return netip.Addr{}, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, host)
}
return ips[rand.Intn(len(ips))], nil
return ips[fastrand.Intn(len(ips))], nil
}
func (r *Resolver) shouldIPFallback(ip netip.Addr) bool {