From 73aa8c7be7d06e16a86b60c8eda2eee476431231 Mon Sep 17 00:00:00 2001 From: Meta Date: Wed, 27 Apr 2022 18:02:29 +0800 Subject: [PATCH] feat: support uuid with custom string --- adapter/outbound/base.go | 28 ++++++++++++++++++++++++++++ adapter/outbound/vless.go | 2 +- adapter/outbound/vmess.go | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/adapter/outbound/base.go b/adapter/outbound/base.go index e9119415..6b5c61f6 100644 --- a/adapter/outbound/base.go +++ b/adapter/outbound/base.go @@ -2,9 +2,12 @@ package outbound import ( "context" + "crypto/sha1" + "encoding/hex" "encoding/json" "errors" "net" + "regexp" "github.com/Dreamacro/clash/component/dialer" C "github.com/Dreamacro/clash/constant" @@ -136,3 +139,28 @@ 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 { + match, _ := regexp.MatchString(`[\da-f]{8}(-[\da-f]{4}){3}-[\da-f]{12}$`, str) + if !match { + 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 d8e8e18f..1cea9ab5 100644 --- a/adapter/outbound/vless.go +++ b/adapter/outbound/vless.go @@ -386,7 +386,7 @@ func NewVless(option VlessOption) (*Vless, error) { } } - client, err := vless.NewClient(option.UUID, addons, option.FlowShow) + client, err := vless.NewClient(uuidMap(option.UUID), addons, option.FlowShow) if err != nil { return nil, err } diff --git a/adapter/outbound/vmess.go b/adapter/outbound/vmess.go index b32349d0..be3def96 100644 --- a/adapter/outbound/vmess.go +++ b/adapter/outbound/vmess.go @@ -266,7 +266,7 @@ func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata, o func NewVmess(option VmessOption) (*Vmess, error) { security := strings.ToLower(option.Cipher) client, err := vmess.NewClient(vmess.Config{ - UUID: option.UUID, + UUID: uuidMap(option.UUID), AlterID: uint16(option.AlterID), Security: security, HostName: option.Server,