diff --git a/adapter/outbound/base.go b/adapter/outbound/base.go index 61843a89..ab7fd24b 100644 --- a/adapter/outbound/base.go +++ b/adapter/outbound/base.go @@ -2,16 +2,12 @@ package outbound import ( "context" - "crypto/sha1" - "encoding/hex" "encoding/json" "errors" "net" "github.com/Dreamacro/clash/component/dialer" C "github.com/Dreamacro/clash/constant" - - "github.com/gofrs/uuid" ) type Base struct { @@ -150,28 +146,3 @@ func (c *packetConn) AppendToChains(a C.ProxyAdapter) { func newPacketConn(pc net.PacketConn, a C.ProxyAdapter) C.PacketConn { return &packetConn{pc, []string{a.Name()}} } - -func uuidMap(str string) string { - _, err := uuid.FromString(str) - if err != nil { - var Nil [16]byte - h := sha1.New() - h.Write(Nil[:]) - h.Write([]byte(str)) - u := h.Sum(nil)[:16] - u[6] = (u[6] & 0x0f) | (5 << 4) - u[8] = u[8]&(0xff>>2) | (0x02 << 6) - buf := make([]byte, 36) - hex.Encode(buf[0:8], u[0:4]) - buf[8] = '-' - hex.Encode(buf[9:13], u[4:6]) - buf[13] = '-' - hex.Encode(buf[14:18], u[6:8]) - buf[18] = '-' - hex.Encode(buf[19:23], u[8:10]) - buf[23] = '-' - hex.Encode(buf[24:], u[10:]) - return string(buf) - } - return str -} diff --git a/adapter/outbound/vless.go b/adapter/outbound/vless.go index 72ceb84c..aa06c26b 100644 --- a/adapter/outbound/vless.go +++ b/adapter/outbound/vless.go @@ -394,7 +394,7 @@ func NewVless(option VlessOption) (*Vless, error) { } } - client, err := vless.NewClient(uuidMap(option.UUID), addons, option.FlowShow) + client, err := vless.NewClient(option.UUID, addons, option.FlowShow) if err != nil { return nil, err } diff --git a/adapter/outbound/vmess.go b/adapter/outbound/vmess.go index 7ef00c59..b17bb5d0 100644 --- a/adapter/outbound/vmess.go +++ b/adapter/outbound/vmess.go @@ -274,7 +274,7 @@ func (v *Vmess) SupportUOT() bool { func NewVmess(option VmessOption) (*Vmess, error) { security := strings.ToLower(option.Cipher) client, err := vmess.NewClient(vmess.Config{ - UUID: uuidMap(option.UUID), + UUID: option.UUID, AlterID: uint16(option.AlterID), Security: security, HostName: option.Server, diff --git a/common/utils/uuid.go b/common/utils/uuid.go new file mode 100644 index 00000000..1e63e324 --- /dev/null +++ b/common/utils/uuid.go @@ -0,0 +1,33 @@ +package utils + +import ( + "crypto/sha1" + "encoding/hex" + "github.com/gofrs/uuid" +) + +// 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 { + var Nil [16]byte + h := sha1.New() + h.Write(Nil[:]) + h.Write([]byte(str)) + u := h.Sum(nil)[:16] + u[6] = (u[6] & 0x0f) | (5 << 4) + u[8] = u[8]&(0xff>>2) | (0x02 << 6) + buf := make([]byte, 36) + hex.Encode(buf[0:8], u[0:4]) + buf[8] = '-' + hex.Encode(buf[9:13], u[4:6]) + buf[13] = '-' + hex.Encode(buf[14:18], u[6:8]) + buf[18] = '-' + hex.Encode(buf[19:23], u[8:10]) + buf[23] = '-' + hex.Encode(buf[24:], u[10:]) + return uuid.FromString(string(buf)) + } + return u, nil +} diff --git a/common/utils/uuid_test.go b/common/utils/uuid_test.go new file mode 100644 index 00000000..748d72c9 --- /dev/null +++ b/common/utils/uuid_test.go @@ -0,0 +1,72 @@ +package utils + +import ( + "github.com/gofrs/uuid" + "reflect" + "testing" +) + +func TestUUIDMap(t *testing.T) { + type args struct { + str string + } + tests := []struct { + name string + args args + want uuid.UUID + wantErr bool + }{ + { + name: "uuid-test-1", + args: args{ + str: "82410302-039e-41b6-98b0-d964084b4170", + }, + want: uuid.FromStringOrNil("82410302-039e-41b6-98b0-d964084b4170"), + wantErr: false, + }, + { + name: "uuid-test-2", + args: args{ + str: "88c502e6-d7eb-4c8e-8259-94cb13d83c77", + }, + want: uuid.FromStringOrNil("88c502e6-d7eb-4c8e-8259-94cb13d83c77"), + wantErr: false, + }, + { + name: "uuid-map-1", + args: args{ + str: "123456", + }, + want: uuid.FromStringOrNil("f8598425-92f2-5508-a071-4fc67f9040ac"), + wantErr: false, + }, + { + name: "uuid-map-2", + args: args{ + str: "a9dk23bz0", + }, + want: uuid.FromStringOrNil("c91481b6-fc0f-5d9e-b166-5ddf07b9c3c5"), + wantErr: false, + }, + { + name: "uuid-map-2", + args: args{ + str: "中文123", + }, + want: uuid.FromStringOrNil("145c544c-2229-59e5-8dbb-3f33b7610d26"), + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := UUIDMap(tt.args.str) + if (err != nil) != tt.wantErr { + t.Errorf("UUIDMap() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("UUIDMap() got = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/transport/vless/vless.go b/transport/vless/vless.go index bb0ce881..5afe2c3e 100644 --- a/transport/vless/vless.go +++ b/transport/vless/vless.go @@ -1,6 +1,7 @@ package vless import ( + "github.com/Dreamacro/clash/common/utils" "net" "github.com/gofrs/uuid" @@ -49,7 +50,7 @@ func (c *Client) StreamConn(conn net.Conn, dst *DstAddr) (net.Conn, error) { // NewClient return Client instance func NewClient(uuidStr string, addons *Addons, xtlsShow bool) (*Client, error) { - uid, err := uuid.FromString(uuidStr) + uid, err := utils.UUIDMap(uuidStr) if err != nil { return nil, err } diff --git a/transport/vmess/vmess.go b/transport/vmess/vmess.go index cb0731d7..d7c8edb4 100644 --- a/transport/vmess/vmess.go +++ b/transport/vmess/vmess.go @@ -2,6 +2,7 @@ package vmess import ( "fmt" + "github.com/Dreamacro/clash/common/utils" "math/rand" "net" "runtime" @@ -82,7 +83,7 @@ func (c *Client) StreamConn(conn net.Conn, dst *DstAddr) (net.Conn, error) { // NewClient return Client instance func NewClient(config Config) (*Client, error) { - uid, err := uuid.FromString(config.UUID) + uid, err := utils.UUIDMap(config.UUID) if err != nil { return nil, err }