chore: better uuid using

This commit is contained in:
wwqgtxx 2023-03-15 10:10:03 +08:00
parent 516c219580
commit f7610ce2ad
9 changed files with 39 additions and 31 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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)

View File

@ -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
}

View File

@ -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),
}

View File

@ -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,
}
}

View File

@ -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,
}
}

View File

@ -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()

View File

@ -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(),