Add Wireguard uri

pull/4655/head
2dust 2024-01-16 11:05:51 +08:00
parent e2f9aac1c9
commit 8a3eb41314
1 changed files with 81 additions and 17 deletions

View File

@ -1,6 +1,5 @@
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Web;
using v2rayN.Mode; using v2rayN.Mode;
using v2rayN.Resx; using v2rayN.Resx;
@ -30,6 +29,7 @@ namespace v2rayN.Handler
EConfigType.VLESS => ShareVLESS(item), EConfigType.VLESS => ShareVLESS(item),
EConfigType.Hysteria2 => ShareHysteria2(item), EConfigType.Hysteria2 => ShareHysteria2(item),
EConfigType.Tuic => ShareTuic(item), EConfigType.Tuic => ShareTuic(item),
EConfigType.Wireguard => ShareWireguard(item),
_ => null, _ => null,
}; };
@ -221,6 +221,42 @@ namespace v2rayN.Handler
return url; return url;
} }
private static string ShareWireguard(ProfileItem item)
{
string url = string.Empty;
string remark = string.Empty;
if (!Utils.IsNullOrEmpty(item.remarks))
{
remark = "#" + Utils.UrlEncode(item.remarks);
}
var dicQuery = new Dictionary<string, string>();
if (!Utils.IsNullOrEmpty(item.publicKey))
{
dicQuery.Add("publickey", Utils.UrlEncode(item.publicKey));
}
if (!Utils.IsNullOrEmpty(item.path))
{
dicQuery.Add("reserved", Utils.UrlEncode(item.path));
}
if (!Utils.IsNullOrEmpty(item.requestHost))
{
dicQuery.Add("address", Utils.UrlEncode(item.requestHost));
}
if (!Utils.IsNullOrEmpty(item.shortId))
{
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;
}
private static string GetIpv6(string address) private static string GetIpv6(string address)
{ {
return Utils.IsIpv6(address) ? $"[{address}]" : address; return Utils.IsIpv6(address) ? $"[{address}]" : address;
@ -346,7 +382,7 @@ namespace v2rayN.Handler
{ {
msg = string.Empty; msg = string.Empty;
ProfileItem profileItem = new(); ProfileItem? profileItem;
try try
{ {
@ -422,6 +458,10 @@ namespace v2rayN.Handler
{ {
profileItem = ResolveTuic(result); profileItem = ResolveTuic(result);
} }
else if (result.StartsWith(Global.ProtocolShares[EConfigType.Wireguard]))
{
profileItem = ResolveWireguard(result);
}
else else
{ {
msg = ResUI.NonvmessOrssProtocol; msg = ResUI.NonvmessOrssProtocol;
@ -539,7 +579,7 @@ namespace v2rayN.Handler
i.address = u.IdnHost; i.address = u.IdnHost;
i.port = u.Port; i.port = u.Port;
i.remarks = u.GetComponents(UriComponents.Fragment, UriFormat.Unescaped); i.remarks = u.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
var q = HttpUtility.ParseQueryString(u.Query); var query = Utils.ParseQueryString(u.Query);
var m = StdVmessUserInfo.Match(u.UserInfo); var m = StdVmessUserInfo.Match(u.UserInfo);
if (!m.Success) return null; if (!m.Success) return null;
@ -565,17 +605,17 @@ namespace v2rayN.Handler
switch (i.network) switch (i.network)
{ {
case "tcp": case "tcp":
string t1 = q["type"] ?? "none"; string t1 = query["type"] ?? "none";
i.headerType = t1; i.headerType = t1;
break; break;
case "kcp": case "kcp":
i.headerType = q["type"] ?? "none"; i.headerType = query["type"] ?? "none";
break; break;
case "ws": case "ws":
string p1 = q["path"] ?? "/"; string p1 = query["path"] ?? "/";
string h1 = q["host"] ?? ""; string h1 = query["host"] ?? "";
i.requestHost = Utils.UrlDecode(h1); i.requestHost = Utils.UrlDecode(h1);
i.path = p1; i.path = p1;
break; break;
@ -583,16 +623,16 @@ namespace v2rayN.Handler
case "http": case "http":
case "h2": case "h2":
i.network = "h2"; i.network = "h2";
string p2 = q["path"] ?? "/"; string p2 = query["path"] ?? "/";
string h2 = q["host"] ?? ""; string h2 = query["host"] ?? "";
i.requestHost = Utils.UrlDecode(h2); i.requestHost = Utils.UrlDecode(h2);
i.path = p2; i.path = p2;
break; break;
case "quic": case "quic":
string s = q["security"] ?? "none"; string s = query["security"] ?? "none";
string k = q["key"] ?? ""; string k = query["key"] ?? "";
string t3 = q["type"] ?? "none"; string t3 = query["type"] ?? "none";
i.headerType = t3; i.headerType = t3;
i.requestHost = Utils.UrlDecode(s); i.requestHost = Utils.UrlDecode(s);
i.path = k; i.path = k;
@ -647,7 +687,7 @@ namespace v2rayN.Handler
server.id = userInfoParts[1]; server.id = userInfoParts[1];
} }
NameValueCollection queryParameters = HttpUtility.ParseQueryString(parsedUrl.Query); var queryParameters = Utils.ParseQueryString(parsedUrl.Query);
if (queryParameters["plugin"] != null) if (queryParameters["plugin"] != null)
{ {
//obfs-host exists //obfs-host exists
@ -798,7 +838,7 @@ namespace v2rayN.Handler
item.remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped); item.remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
item.id = url.UserInfo; item.id = url.UserInfo;
var query = HttpUtility.ParseQueryString(url.Query); var query = Utils.ParseQueryString(url.Query);
ResolveStdTransport(query, ref item); ResolveStdTransport(query, ref item);
return item; return item;
@ -819,7 +859,7 @@ namespace v2rayN.Handler
item.remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped); item.remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
item.id = url.UserInfo; item.id = url.UserInfo;
var query = HttpUtility.ParseQueryString(url.Query); var query = Utils.ParseQueryString(url.Query);
item.security = query["encryption"] ?? "none"; item.security = query["encryption"] ?? "none";
item.streamSecurity = query["security"] ?? ""; item.streamSecurity = query["security"] ?? "";
ResolveStdTransport(query, ref item); ResolveStdTransport(query, ref item);
@ -841,7 +881,7 @@ namespace v2rayN.Handler
item.remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped); item.remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
item.id = url.UserInfo; item.id = url.UserInfo;
var query = HttpUtility.ParseQueryString(url.Query); var query = Utils.ParseQueryString(url.Query);
ResolveStdTransport(query, ref item); ResolveStdTransport(query, ref item);
item.allowInsecure = (query["insecure"] ?? "") == "1" ? "true" : "false"; item.allowInsecure = (query["insecure"] ?? "") == "1" ? "true" : "false";
@ -867,13 +907,37 @@ namespace v2rayN.Handler
item.security = userInfoParts[1]; item.security = userInfoParts[1];
} }
var query = HttpUtility.ParseQueryString(url.Query); var query = Utils.ParseQueryString(url.Query);
ResolveStdTransport(query, ref item); ResolveStdTransport(query, ref item);
item.headerType = query["congestion_control"] ?? ""; item.headerType = query["congestion_control"] ?? "";
return item; return item;
} }
private static ProfileItem ResolveWireguard(string result)
{
ProfileItem item = new()
{
configType = EConfigType.Wireguard
};
Uri url = new(result);
item.address = url.IdnHost;
item.port = url.Port;
item.remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
item.id = Utils.UrlDecode(url.UserInfo);
var query = Utils.ParseQueryString(url.Query);
item.publicKey = Utils.UrlDecode(query["publickey"] ?? "");
item.path = Utils.UrlDecode(query["reserved"] ?? "");
item.requestHost = Utils.UrlDecode(query["address"] ?? "");
item.shortId = Utils.UrlDecode(query["mtu"] ?? "");
return item;
}
private static int ResolveStdTransport(NameValueCollection query, ref ProfileItem item) private static int ResolveStdTransport(NameValueCollection query, ref ProfileItem item)
{ {
item.flow = query["flow"] ?? ""; item.flow = query["flow"] ?? "";