From d3582340b0842f45f4567e227207f72fddc6e8fb Mon Sep 17 00:00:00 2001 From: Hellojack <106379370+H1JK@users.noreply.github.com> Date: Fri, 10 Jun 2022 14:21:53 +0800 Subject: [PATCH] V2Ray : Support Xray UUIDv5 mapping standard --- Netch/Servers/V2ray/V2rayConfigUtils.cs | 13 +++++++++++-- Netch/Utils/StringExtension.cs | 26 +++++++++++++++++++++++++ Tests/Global.cs | 2 +- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Netch/Servers/V2ray/V2rayConfigUtils.cs b/Netch/Servers/V2ray/V2rayConfigUtils.cs index 5f98c7f2..05d86073 100644 --- a/Netch/Servers/V2ray/V2rayConfigUtils.cs +++ b/Netch/Servers/V2ray/V2rayConfigUtils.cs @@ -82,7 +82,7 @@ public static class V2rayConfigUtils { new User { - id = vless.UserID, + id = getUUID(vless.UserID), flow = vless.Flow.ValueOrDefault(), encryption = vless.EncryptMethod } @@ -125,7 +125,7 @@ public static class V2rayConfigUtils { new User { - id = vmess.UserID, + id = getUUID(vmess.UserID), alterId = vmess.AlterID, security = vmess.EncryptMethod } @@ -366,4 +366,13 @@ public static class V2rayConfigUtils return streamSettings; } + + public static string getUUID(string uuid) + { + if (uuid.Length == 36 || uuid.Length == 32) + { + return uuid; + } + return uuid.GenerateUUIDv5(); + } } \ No newline at end of file diff --git a/Netch/Utils/StringExtension.cs b/Netch/Utils/StringExtension.cs index 8ee4211d..5c38588d 100644 --- a/Netch/Utils/StringExtension.cs +++ b/Netch/Utils/StringExtension.cs @@ -78,4 +78,30 @@ public static class StringExtension { return !string.IsNullOrWhiteSpace(value) ? value.Split(',') : default; } + + public static string GenerateUUIDv5(this string str) + { + // https://github.com/XTLS/Xray-core/discussions/715 + // https://xray-uuid.ducksoft.site/ + + SHA1 sha1 = new SHA1CryptoServiceProvider(); + + // example string: "example" + List byteSource = new List(); + byteSource.AddRange(new byte[16]); + byteSource.AddRange(Encoding.UTF8.GetBytes(str)); + + byte[] Sha1Bytes = sha1.ComputeHash(byteSource.ToArray()).Skip(0).Take(16).ToArray(); + sha1.Dispose(); + + //UUIDv5: [254 181 68 49 48 27 82 187 166 221 225 233 62 129 187 158] + + Sha1Bytes[6] = (byte)((Sha1Bytes[6] & 0x0f) | (5 << 4)); + Sha1Bytes[8] = (byte)(Sha1Bytes[8] & (0xff >> 2) | (0x02 << 6)); + + return BitConverter.ToString(Sha1Bytes).Replace("-", "") + .Insert(8, "-").Insert(13, "-").Insert(18, "-").Insert(23, "-") + .ToLower(); + //UUIDv5: feb54431-301b-52bb-a6dd-e1e93e81bb9e + } } \ No newline at end of file diff --git a/Tests/Global.cs b/Tests/Global.cs index 58029899..63d798b7 100644 --- a/Tests/Global.cs +++ b/Tests/Global.cs @@ -24,7 +24,7 @@ namespace Tests var str = "example"; SHA1 sha1 = new SHA1CryptoServiceProvider(); - byte[] StrBytes = Encoding.Default.GetBytes(str); + byte[] StrBytes = Encoding.UTF8.GetBytes(str); List byteSource = new List(); byteSource.AddRange(bytes);