chore: Generate UUID from fastrand

This commit is contained in:
H1JK 2023-03-05 11:00:14 +08:00
parent 3b037acb01
commit ae966833a4
11 changed files with 34 additions and 19 deletions

View file

@ -8,10 +8,9 @@ import (
"strings" "strings"
N "github.com/Dreamacro/clash/common/net" N "github.com/Dreamacro/clash/common/net"
"github.com/Dreamacro/clash/common/utils"
"github.com/Dreamacro/clash/component/dialer" "github.com/Dreamacro/clash/component/dialer"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/gofrs/uuid"
) )
type Base struct { type Base struct {
@ -35,7 +34,7 @@ func (b *Base) Name() string {
// Id implements C.ProxyAdapter // Id implements C.ProxyAdapter
func (b *Base) Id() string { func (b *Base) Id() string {
if b.id == "" { if b.id == "" {
id, err := uuid.NewV6() id, err := utils.UnsafeUUIDGenerator.NewV6()
if err != nil { if err != nil {
b.id = b.name b.id = b.name
} else { } else {

View file

@ -6,10 +6,10 @@ import (
"github.com/Dreamacro/clash/common/batch" "github.com/Dreamacro/clash/common/batch"
"github.com/Dreamacro/clash/common/singledo" "github.com/Dreamacro/clash/common/singledo"
"github.com/Dreamacro/clash/common/utils"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/log"
"github.com/gofrs/uuid"
"go.uber.org/atomic" "go.uber.org/atomic"
) )
@ -77,7 +77,7 @@ func (hc *HealthCheck) touch() {
func (hc *HealthCheck) check() { func (hc *HealthCheck) check() {
_, _, _ = hc.singleDo.Do(func() (struct{}, error) { _, _, _ = hc.singleDo.Do(func() (struct{}, error) {
id := "" id := ""
if uid, err := uuid.NewV4(); err == nil { if uid, err := utils.UnsafeUUIDGenerator.NewV4(); err == nil {
id = uid.String() id = uid.String()
} }
log.Debugln("Start New Health Checking {%s}", id) log.Debugln("Start New Health Checking {%s}", id)

View file

@ -2,13 +2,14 @@ package convert
import ( import (
"encoding/base64" "encoding/base64"
"github.com/metacubex/sing-shadowsocks/shadowimpl"
"math/rand" "math/rand"
"net/http" "net/http"
"strings" "strings"
"time" "time"
"github.com/gofrs/uuid" "github.com/Dreamacro/clash/common/utils"
"github.com/metacubex/sing-shadowsocks/shadowimpl"
) )
var hostsSuffix = []string{ var hostsSuffix = []string{
@ -293,7 +294,7 @@ var (
) )
func RandHost() string { func RandHost() string {
id, _ := uuid.NewV4() id, _ := utils.UnsafeUUIDGenerator.NewV4()
base := strings.ToLower(base64.RawURLEncoding.EncodeToString(id.Bytes())) base := strings.ToLower(base64.RawURLEncoding.EncodeToString(id.Bytes()))
base = strings.ReplaceAll(base, "-", "") base = strings.ReplaceAll(base, "-", "")
base = strings.ReplaceAll(base, "_", "") base = strings.ReplaceAll(base, "_", "")

View file

@ -2,15 +2,22 @@ package utils
import ( import (
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"github.com/zhangyunhao116/fastrand"
) )
var uuidNamespace, _ = uuid.FromString("00000000-0000-0000-0000-000000000000") type fastRandReader struct{}
func (r fastRandReader) Read(p []byte) (int, error) {
return fastrand.Read(p)
}
var UnsafeUUIDGenerator = uuid.NewGenWithOptions(uuid.WithRandomReader(fastRandReader{}))
// UUIDMap https://github.com/XTLS/Xray-core/issues/158#issue-783294090 // UUIDMap https://github.com/XTLS/Xray-core/issues/158#issue-783294090
func UUIDMap(str string) (uuid.UUID, error) { func UUIDMap(str string) (uuid.UUID, error) {
u, err := uuid.FromString(str) u, err := uuid.FromString(str)
if err != nil { if err != nil {
return uuid.NewV5(uuidNamespace, str), nil return UnsafeUUIDGenerator.NewV5(uuid.Nil, str), nil
} }
return u, nil return u, nil
} }

View file

@ -1,6 +1,7 @@
package context package context
import ( import (
"github.com/Dreamacro/clash/common/utils"
"net" "net"
N "github.com/Dreamacro/clash/common/net" N "github.com/Dreamacro/clash/common/net"
@ -16,7 +17,7 @@ type ConnContext struct {
} }
func NewConnContext(conn net.Conn, metadata *C.Metadata) *ConnContext { func NewConnContext(conn net.Conn, metadata *C.Metadata) *ConnContext {
id, _ := uuid.NewV4() id, _ := utils.UnsafeUUIDGenerator.NewV4()
return &ConnContext{ return &ConnContext{
id: id, id: id,

View file

@ -2,6 +2,7 @@ package context
import ( import (
"context" "context"
"github.com/Dreamacro/clash/common/utils"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -22,7 +23,7 @@ type DNSContext struct {
} }
func NewDNSContext(ctx context.Context, msg *dns.Msg) *DNSContext { func NewDNSContext(ctx context.Context, msg *dns.Msg) *DNSContext {
id, _ := uuid.NewV4() id, _ := utils.UnsafeUUIDGenerator.NewV4()
return &DNSContext{ return &DNSContext{
Context: ctx, Context: ctx,

View file

@ -3,6 +3,7 @@ package context
import ( import (
"net" "net"
"github.com/Dreamacro/clash/common/utils"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
@ -15,7 +16,7 @@ type PacketConnContext struct {
} }
func NewPacketConnContext(metadata *C.Metadata) *PacketConnContext { func NewPacketConnContext(metadata *C.Metadata) *PacketConnContext {
id, _ := uuid.NewV4() id, _ := utils.UnsafeUUIDGenerator.NewV4()
return &PacketConnContext{ return &PacketConnContext{
id: id, id: id,
metadata: metadata, metadata: metadata,

1
go.mod
View file

@ -36,6 +36,7 @@ require (
github.com/sirupsen/logrus v1.9.0 github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.1 github.com/stretchr/testify v1.8.1
github.com/xtls/go v0.0.0-20220914232946-0441cf4cf837 github.com/xtls/go v0.0.0-20220914232946-0441cf4cf837
github.com/zhangyunhao116/fastrand v0.3.0
go.etcd.io/bbolt v1.3.6 go.etcd.io/bbolt v1.3.6
go.uber.org/atomic v1.10.0 go.uber.org/atomic v1.10.0
go.uber.org/automaxprocs v1.5.1 go.uber.org/automaxprocs v1.5.1

2
go.sum
View file

@ -162,6 +162,8 @@ github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74/go.mod h1:DD4vA1
github.com/xtls/go v0.0.0-20220914232946-0441cf4cf837 h1:AHhUwwFJGl27E46OpdJHplZkK09m7aETNBNzhT6t15M= github.com/xtls/go v0.0.0-20220914232946-0441cf4cf837 h1:AHhUwwFJGl27E46OpdJHplZkK09m7aETNBNzhT6t15M=
github.com/xtls/go v0.0.0-20220914232946-0441cf4cf837/go.mod h1:YJTRELIWrGxR1s8xcEBgxcxBfwQfMGjdvNLTjN9XFgY= github.com/xtls/go v0.0.0-20220914232946-0441cf4cf837/go.mod h1:YJTRELIWrGxR1s8xcEBgxcxBfwQfMGjdvNLTjN9XFgY=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/zhangyunhao116/fastrand v0.3.0 h1:7bwe124xcckPulX6fxtr2lFdO2KQqaefdtbk+mqO/Ig=
github.com/zhangyunhao116/fastrand v0.3.0/go.mod h1:0v5KgHho0VE6HU192HnY15de/oDS8UrbBChIFjIhBtc=
go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU= go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU=
go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4= go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4=
go.uber.org/automaxprocs v1.5.1 h1:e1YG66Lrk73dn4qhg8WFSvhF0JuFQF0ERIp4rpuV8Qk= go.uber.org/automaxprocs v1.5.1 h1:e1YG66Lrk73dn4qhg8WFSvhF0JuFQF0ERIp4rpuV8Qk=

View file

@ -11,13 +11,14 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/gofrs/uuid"
"github.com/metacubex/quic-go"
N "github.com/Dreamacro/clash/common/net" N "github.com/Dreamacro/clash/common/net"
"github.com/Dreamacro/clash/common/pool" "github.com/Dreamacro/clash/common/pool"
"github.com/Dreamacro/clash/common/utils"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/transport/socks5" "github.com/Dreamacro/clash/transport/socks5"
"github.com/gofrs/uuid"
"github.com/metacubex/quic-go"
) )
type ServerOption struct { type ServerOption struct {
@ -55,7 +56,7 @@ func (s *Server) Serve() error {
return err return err
} }
SetCongestionController(conn, s.CongestionController) SetCongestionController(conn, s.CongestionController)
uuid, err := uuid.NewV4() uuid, err := utils.UnsafeUUIDGenerator.NewV4()
if err != nil { if err != nil {
return err return err
} }

View file

@ -6,6 +6,7 @@ import (
"github.com/Dreamacro/clash/common/buf" "github.com/Dreamacro/clash/common/buf"
N "github.com/Dreamacro/clash/common/net" N "github.com/Dreamacro/clash/common/net"
"github.com/Dreamacro/clash/common/utils"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
@ -82,7 +83,7 @@ func (tt *tcpTracker) Upstream() any {
} }
func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64) *tcpTracker { func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64) *tcpTracker {
uuid, _ := uuid.NewV4() uuid, _ := utils.UnsafeUUIDGenerator.NewV4()
if conn != nil { if conn != nil {
if tcpAddr, ok := conn.RemoteAddr().(*net.TCPAddr); ok { if tcpAddr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
metadata.RemoteDst = tcpAddr.IP.String() metadata.RemoteDst = tcpAddr.IP.String()
@ -148,7 +149,7 @@ func (ut *udpTracker) Close() error {
} }
func NewUDPTracker(conn C.PacketConn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64) *udpTracker { func NewUDPTracker(conn C.PacketConn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64) *udpTracker {
uuid, _ := uuid.NewV4() uuid, _ := utils.UnsafeUUIDGenerator.NewV4()
metadata.RemoteDst = conn.RemoteDestination() metadata.RemoteDst = conn.RemoteDestination()
ut := &udpTracker{ ut := &udpTracker{