diff --git a/adapter/outbound/base.go b/adapter/outbound/base.go index a9615be0c..156bc1141 100644 --- a/adapter/outbound/base.go +++ b/adapter/outbound/base.go @@ -34,12 +34,7 @@ func (b *Base) Name() string { // Id implements C.ProxyAdapter func (b *Base) Id() string { if b.id == "" { - id, err := utils.UnsafeUUIDGenerator.NewV6() - if err != nil { - b.id = b.name - } else { - b.id = id.String() - } + b.id = utils.NewUUIDV6().String() } return b.id @@ -235,8 +230,7 @@ func (c *packetConn) LocalAddr() net.Addr { } func newPacketConn(pc net.PacketConn, a C.ProxyAdapter) C.PacketConn { - id, _ := utils.UnsafeUUIDGenerator.NewV4() - return &packetConn{pc, []string{a.Name()}, a.Name(), id.String(), parseRemoteDestination(a.Addr())} + return &packetConn{pc, []string{a.Name()}, a.Name(), utils.NewUUIDV4().String(), parseRemoteDestination(a.Addr())} } func parseRemoteDestination(addr string) string { diff --git a/adapter/provider/healthcheck.go b/adapter/provider/healthcheck.go index d1624cadd..9deb7b820 100644 --- a/adapter/provider/healthcheck.go +++ b/adapter/provider/healthcheck.go @@ -64,10 +64,7 @@ func (hc *HealthCheck) touch() { func (hc *HealthCheck) check() { _, _, _ = hc.singleDo.Do(func() (struct{}, error) { - id := "" - if uid, err := utils.UnsafeUUIDGenerator.NewV4(); err == nil { - id = uid.String() - } + id := utils.NewUUIDV4().String() log.Debugln("Start New Health Checking {%s}", id) b, _ := batch.New[bool](context.Background(), batch.WithConcurrencyNum[bool](10)) for _, proxy := range hc.proxies { diff --git a/common/convert/util.go b/common/convert/util.go index 4f86e6161..0ec35acdd 100644 --- a/common/convert/util.go +++ b/common/convert/util.go @@ -294,8 +294,7 @@ var ( ) func RandHost() string { - id, _ := utils.UnsafeUUIDGenerator.NewV4() - base := strings.ToLower(base64.RawURLEncoding.EncodeToString(id.Bytes())) + base := strings.ToLower(base64.RawURLEncoding.EncodeToString(utils.NewUUIDV4().Bytes())) base = strings.ReplaceAll(base, "-", "") base = strings.ReplaceAll(base, "_", "") buf := []byte(base) diff --git a/common/utils/uuid.go b/common/utils/uuid.go index d043ae374..930fda7d8 100644 --- a/common/utils/uuid.go +++ b/common/utils/uuid.go @@ -13,11 +13,39 @@ func (r fastRandReader) Read(p []byte) (int, error) { var UnsafeUUIDGenerator = uuid.NewGenWithOptions(uuid.WithRandomReader(fastRandReader{})) +func NewUUIDV1() uuid.UUID { + u, _ := UnsafeUUIDGenerator.NewV1() // fastrand.Read wouldn't cause error, so ignore err is safe + return u +} + +func NewUUIDV3(ns uuid.UUID, name string) uuid.UUID { + return UnsafeUUIDGenerator.NewV3(ns, name) +} + +func NewUUIDV4() uuid.UUID { + u, _ := UnsafeUUIDGenerator.NewV4() // fastrand.Read wouldn't cause error, so ignore err is safe + return u +} + +func NewUUIDV5(ns uuid.UUID, name string) uuid.UUID { + return UnsafeUUIDGenerator.NewV5(ns, name) +} + +func NewUUIDV6() uuid.UUID { + u, _ := UnsafeUUIDGenerator.NewV6() // fastrand.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 + return u +} + // UUIDMap https://github.com/XTLS/Xray-core/issues/158#issue-783294090 func UUIDMap(str string) (uuid.UUID, error) { u, err := uuid.FromString(str) if err != nil { - return UnsafeUUIDGenerator.NewV5(uuid.Nil, str), nil + return NewUUIDV5(uuid.Nil, str), nil } return u, nil } diff --git a/context/conn.go b/context/conn.go index 35ea788db..c54777801 100644 --- a/context/conn.go +++ b/context/conn.go @@ -17,10 +17,8 @@ type ConnContext struct { } func NewConnContext(conn net.Conn, metadata *C.Metadata) *ConnContext { - id, _ := utils.UnsafeUUIDGenerator.NewV4() - return &ConnContext{ - id: id, + id: utils.NewUUIDV4(), metadata: metadata, conn: N.NewBufferedConn(conn), } diff --git a/context/dns.go b/context/dns.go index 80a4c988b..c41de724c 100644 --- a/context/dns.go +++ b/context/dns.go @@ -23,11 +23,10 @@ type DNSContext struct { } func NewDNSContext(ctx context.Context, msg *dns.Msg) *DNSContext { - id, _ := utils.UnsafeUUIDGenerator.NewV4() return &DNSContext{ Context: ctx, - id: id, + id: utils.NewUUIDV4(), msg: msg, } } diff --git a/context/packetconn.go b/context/packetconn.go index 87a9290a5..b9afef41b 100644 --- a/context/packetconn.go +++ b/context/packetconn.go @@ -16,9 +16,8 @@ type PacketConnContext struct { } func NewPacketConnContext(metadata *C.Metadata) *PacketConnContext { - id, _ := utils.UnsafeUUIDGenerator.NewV4() return &PacketConnContext{ - id: id, + id: utils.NewUUIDV4(), metadata: metadata, } } diff --git a/transport/tuic/server.go b/transport/tuic/server.go index b23f3ae97..5eb6e6116 100644 --- a/transport/tuic/server.go +++ b/transport/tuic/server.go @@ -56,14 +56,10 @@ func (s *Server) Serve() error { return err } SetCongestionController(conn, s.CongestionController) - uuid, err := utils.UnsafeUUIDGenerator.NewV4() - if err != nil { - return err - } h := &serverHandler{ Server: s, quicConn: conn, - uuid: uuid, + uuid: utils.NewUUIDV4(), authCh: make(chan struct{}), } go h.handle() diff --git a/tunnel/statistic/tracker.go b/tunnel/statistic/tracker.go index bb5678b8d..f7e9d9717 100644 --- a/tunnel/statistic/tracker.go +++ b/tunnel/statistic/tracker.go @@ -83,7 +83,6 @@ func (tt *tcpTracker) Upstream() any { } func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64) *tcpTracker { - uuid, _ := utils.UnsafeUUIDGenerator.NewV4() if conn != nil { if tcpAddr, ok := conn.RemoteAddr().(*net.TCPAddr); ok { metadata.RemoteDst = tcpAddr.IP.String() @@ -96,7 +95,7 @@ func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.R Conn: conn, manager: manager, trackerInfo: &trackerInfo{ - UUID: uuid, + UUID: utils.NewUUIDV4(), Start: time.Now(), Metadata: metadata, Chain: conn.Chains(), @@ -149,14 +148,13 @@ func (ut *udpTracker) Close() error { } func NewUDPTracker(conn C.PacketConn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64) *udpTracker { - uuid, _ := utils.UnsafeUUIDGenerator.NewV4() metadata.RemoteDst = conn.RemoteDestination() ut := &udpTracker{ PacketConn: conn, manager: manager, trackerInfo: &trackerInfo{ - UUID: uuid, + UUID: utils.NewUUIDV4(), Start: time.Now(), Metadata: metadata, Chain: conn.Chains(),