diff --git a/v2rayN/ServiceLib/Handler/Fmt/BaseFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/BaseFmt.cs index fc1bcda6..8f7c319f 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/BaseFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/BaseFmt.cs @@ -201,5 +201,14 @@ namespace ServiceLib.Handler.Fmt File.WriteAllText(fileName, strData); return fileName; } + protected static string ToUri(EConfigType eConfigType, string address, object port, string userInfo, Dictionary? dicQuery, string? remark) + { + var query = dicQuery != null + ? ("?" + string.Join("&", dicQuery.Select(x => x.Key + "=" + x.Value).ToArray())) + : string.Empty; + + var url = $"{Utils.UrlEncode(userInfo)}@{GetIpv6(address)}:{port}"; + return $"{Global.ProtocolShares[eConfigType]}{url}{query}{remark}"; + } } } \ No newline at end of file diff --git a/v2rayN/ServiceLib/Handler/Fmt/Hysteria2Fmt.cs b/v2rayN/ServiceLib/Handler/Fmt/Hysteria2Fmt.cs index 5f877858..146d2d74 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/Hysteria2Fmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/Hysteria2Fmt.cs @@ -51,14 +51,7 @@ } dicQuery.Add("insecure", item.allowInsecure.ToLower() == "true" ? "1" : "0"); - string query = "?" + string.Join("&", dicQuery.Select(x => x.Key + "=" + x.Value).ToArray()); - - url = string.Format("{0}@{1}:{2}", - item.id, - GetIpv6(item.address), - item.port); - url = $"{Global.ProtocolShares[EConfigType.Hysteria2]}{url}/{query}{remark}"; - return url; + return ToUri(EConfigType.Hysteria2, item.address, item.port, item.id, dicQuery, remark); } public static ProfileItem? ResolveFull(string strData, string? subRemarks) diff --git a/v2rayN/ServiceLib/Handler/Fmt/ShadowsocksFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/ShadowsocksFmt.cs index 93c387f6..57f8e1bc 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/ShadowsocksFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/ShadowsocksFmt.cs @@ -42,9 +42,7 @@ namespace ServiceLib.Handler.Fmt //url = Utile.Base64Encode(url); //new Sip002 var pw = Utils.Base64Encode($"{item.security}:{item.id}"); - url = $"{pw}@{GetIpv6(item.address)}:{item.port}"; - url = $"{Global.ProtocolShares[EConfigType.Shadowsocks]}{url}{remark}"; - return url; + return ToUri(EConfigType.Shadowsocks, item.address, item.port, pw, null, remark); } private static readonly Regex UrlFinder = new(@"ss://(?[A-Za-z0-9+-/=_]+)(?:#(?\S+))?", RegexOptions.IgnoreCase | RegexOptions.Compiled); @@ -98,7 +96,7 @@ namespace ServiceLib.Handler.Fmt address = parsedUrl.IdnHost, port = parsedUrl.Port, }; - string rawUserInfo = parsedUrl.GetComponents(UriComponents.UserInfo, UriFormat.UriEscaped); + var rawUserInfo = Utils.UrlDecode(parsedUrl.UserInfo); //2022-blake3 if (rawUserInfo.Contains(':')) { diff --git a/v2rayN/ServiceLib/Handler/Fmt/SocksFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/SocksFmt.cs index 0f0df5fb..cbf24fcc 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/SocksFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/SocksFmt.cs @@ -40,9 +40,7 @@ //url = Utile.Base64Encode(url); //new var pw = Utils.Base64Encode($"{item.security}:{item.id}"); - url = $"{pw}@{GetIpv6(item.address)}:{item.port}"; - url = $"{Global.ProtocolShares[EConfigType.SOCKS]}{url}{remark}"; - return url; + return ToUri(EConfigType.SOCKS, item.address, item.port, pw, null, remark); } private static ProfileItem? ResolveSocks(string result) @@ -112,9 +110,9 @@ }; // parse base64 UserInfo - string rawUserInfo = parsedUrl.GetComponents(UriComponents.UserInfo, UriFormat.Unescaped); - string userInfo = Utils.Base64Decode(rawUserInfo); - string[] userInfoParts = userInfo.Split(new[] { ':' }, 2); + var rawUserInfo = Utils.UrlDecode(parsedUrl.UserInfo); + var userInfo = Utils.Base64Decode(rawUserInfo); + var userInfoParts = userInfo.Split(new[] { ':' }, 2); if (userInfoParts.Length == 2) { item.security = userInfoParts[0]; diff --git a/v2rayN/ServiceLib/Handler/Fmt/TrojanFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/TrojanFmt.cs index 51f08524..129ba9f3 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/TrojanFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/TrojanFmt.cs @@ -36,14 +36,8 @@ } var dicQuery = new Dictionary(); GetStdTransport(item, null, ref dicQuery); - string query = "?" + string.Join("&", dicQuery.Select(x => x.Key + "=" + x.Value).ToArray()); - - url = string.Format("{0}@{1}:{2}", - item.id, - GetIpv6(item.address), - item.port); - url = $"{Global.ProtocolShares[EConfigType.Trojan]}{url}{query}{remark}"; - return url; + + return ToUri(EConfigType.Trojan, item.address, item.port, item.id, dicQuery, remark); } } } \ No newline at end of file diff --git a/v2rayN/ServiceLib/Handler/Fmt/TuicFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/TuicFmt.cs index bdfdce91..e8da3ada 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/TuicFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/TuicFmt.cs @@ -16,7 +16,8 @@ item.address = url.IdnHost; item.port = url.Port; item.remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped); - var userInfoParts = url.UserInfo.Split(new[] { ':' }, 2); + var rawUserInfo = Utils.UrlDecode(url.UserInfo); + var userInfoParts = rawUserInfo.Split(new[] { ':' }, 2); if (userInfoParts.Length == 2) { item.id = userInfoParts[0]; @@ -51,14 +52,7 @@ } dicQuery.Add("congestion_control", item.headerType); - string query = "?" + string.Join("&", dicQuery.Select(x => x.Key + "=" + x.Value).ToArray()); - - url = string.Format("{0}@{1}:{2}", - $"{item.id}:{item.security}", - GetIpv6(item.address), - item.port); - url = $"{Global.ProtocolShares[EConfigType.TUIC]}{url}{query}{remark}"; - return url; + return ToUri(EConfigType.TUIC, item.address, item.port, $"{item.id}:{item.security}", dicQuery, remark); } } } \ No newline at end of file diff --git a/v2rayN/ServiceLib/Handler/Fmt/VLESSFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/VLESSFmt.cs index fdcc34d3..aa26af15 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/VLESSFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/VLESSFmt.cs @@ -47,14 +47,8 @@ dicQuery.Add("encryption", Global.None); } GetStdTransport(item, Global.None, ref dicQuery); - string query = "?" + string.Join("&", dicQuery.Select(x => x.Key + "=" + x.Value).ToArray()); - - url = string.Format("{0}@{1}:{2}", - item.id, - GetIpv6(item.address), - item.port); - url = $"{Global.ProtocolShares[EConfigType.VLESS]}{url}{query}{remark}"; - return url; + + return ToUri(EConfigType.VLESS, item.address, item.port, item.id, dicQuery, remark); } } } \ No newline at end of file diff --git a/v2rayN/ServiceLib/Handler/Fmt/WireguardFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/WireguardFmt.cs index 743a82be..b6c980a0 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/WireguardFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/WireguardFmt.cs @@ -56,14 +56,7 @@ { dicQuery.Add("mtu", Utils.UrlEncode(item.shortId)); } - string query = "?" + string.Join("&", dicQuery.Select(x => x.Key + "=" + x.Value).ToArray()); - - url = string.Format("{0}@{1}:{2}", - Utils.UrlEncode(item.id), - GetIpv6(item.address), - item.port); - url = $"{Global.ProtocolShares[EConfigType.WireGuard]}{url}/{query}{remark}"; - return url; + return ToUri(EConfigType.WireGuard, item.address, item.port, item.id, dicQuery, remark); } } } \ No newline at end of file