Skip to content

Commit 40271c0

Browse files
authored
Support for VLESS & VMess UUID v5 mapping standard
#158
1 parent 96adf3f commit 40271c0

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

common/uuid/uuid.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package uuid // import "github.com/xtls/xray-core/common/uuid"
33
import (
44
"bytes"
55
"crypto/rand"
6+
"crypto/sha1"
67
"encoding/hex"
78

89
"github.com/xtls/xray-core/common"
@@ -67,8 +68,18 @@ func ParseString(str string) (UUID, error) {
6768
var uuid UUID
6869

6970
text := []byte(str)
70-
if len(text) < 32 {
71-
return uuid, errors.New("invalid UUID: ", str)
71+
if l := len(text); l < 32 || l > 36 {
72+
if l == 0 || l > 30 {
73+
return uuid, errors.New("invalid UUID: ", str)
74+
}
75+
h := sha1.New()
76+
h.Write(uuid[:])
77+
h.Write(text)
78+
u := h.Sum(nil)[:16]
79+
u[6] = (u[6] & 0x0f) | (5 << 4)
80+
u[8] = (u[8]&(0xff>>2) | (0x02 << 6))
81+
copy(uuid[:], u)
82+
return uuid, nil
7283
}
7384

7485
b := uuid.Bytes()

common/uuid/uuid_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ func TestParseString(t *testing.T) {
3535
t.Fatal(r)
3636
}
3737

38-
_, err = ParseString("2418d087")
39-
if err == nil {
40-
t.Fatal("Expect error but nil")
38+
u0, _ := ParseString("example")
39+
u5, _ := ParseString("feb54431-301b-52bb-a6dd-e1e93e81bb9e")
40+
if r := cmp.Diff(u0, u5); r != "" {
41+
t.Fatal(r)
4142
}
4243

4344
_, err = ParseString("2418d087-648k-4990-86e8-19dca1d006d3")

0 commit comments

Comments
 (0)