Skip to content

Commit

Permalink
Add Wireguard uri
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Jan 16, 2024
1 parent e2f9aac commit 8a3eb41
Showing 1 changed file with 81 additions and 17 deletions.
98 changes: 81 additions & 17 deletions v2rayN/v2rayN/Handler/ShareHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Web;
using v2rayN.Mode;
using v2rayN.Resx;

Expand Down Expand Up @@ -30,6 +29,7 @@ internal class ShareHandler
EConfigType.VLESS => ShareVLESS(item),
EConfigType.Hysteria2 => ShareHysteria2(item),
EConfigType.Tuic => ShareTuic(item),
EConfigType.Wireguard => ShareWireguard(item),
_ => null,
};

Expand Down Expand Up @@ -221,6 +221,42 @@ private static string ShareTuic(ProfileItem item)
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)
{
return Utils.IsIpv6(address) ? $"[{address}]" : address;
Expand Down Expand Up @@ -346,7 +382,7 @@ private static int GetStdTransport(ProfileItem item, string? securityDef, ref Di
{
msg = string.Empty;

ProfileItem profileItem = new();
ProfileItem? profileItem;

try
{
Expand Down Expand Up @@ -422,6 +458,10 @@ private static int GetStdTransport(ProfileItem item, string? securityDef, ref Di
{
profileItem = ResolveTuic(result);
}
else if (result.StartsWith(Global.ProtocolShares[EConfigType.Wireguard]))
{
profileItem = ResolveWireguard(result);
}
else
{
msg = ResUI.NonvmessOrssProtocol;
Expand Down Expand Up @@ -539,7 +579,7 @@ private static int GetStdTransport(ProfileItem item, string? securityDef, ref Di
i.address = u.IdnHost;
i.port = u.Port;
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);
if (!m.Success) return null;
Expand All @@ -565,34 +605,34 @@ private static int GetStdTransport(ProfileItem item, string? securityDef, ref Di
switch (i.network)
{
case "tcp":
string t1 = q["type"] ?? "none";
string t1 = query["type"] ?? "none";
i.headerType = t1;
break;

case "kcp":
i.headerType = q["type"] ?? "none";
i.headerType = query["type"] ?? "none";
break;

case "ws":
string p1 = q["path"] ?? "/";
string h1 = q["host"] ?? "";
string p1 = query["path"] ?? "/";
string h1 = query["host"] ?? "";
i.requestHost = Utils.UrlDecode(h1);
i.path = p1;
break;

case "http":
case "h2":
i.network = "h2";
string p2 = q["path"] ?? "/";
string h2 = q["host"] ?? "";
string p2 = query["path"] ?? "/";
string h2 = query["host"] ?? "";
i.requestHost = Utils.UrlDecode(h2);
i.path = p2;
break;

case "quic":
string s = q["security"] ?? "none";
string k = q["key"] ?? "";
string t3 = q["type"] ?? "none";
string s = query["security"] ?? "none";
string k = query["key"] ?? "";
string t3 = query["type"] ?? "none";
i.headerType = t3;
i.requestHost = Utils.UrlDecode(s);
i.path = k;
Expand Down Expand Up @@ -647,7 +687,7 @@ private static int GetStdTransport(ProfileItem item, string? securityDef, ref Di
server.id = userInfoParts[1];
}

NameValueCollection queryParameters = HttpUtility.ParseQueryString(parsedUrl.Query);
var queryParameters = Utils.ParseQueryString(parsedUrl.Query);
if (queryParameters["plugin"] != null)
{
//obfs-host exists
Expand Down Expand Up @@ -798,7 +838,7 @@ private static ProfileItem ResolveTrojan(string result)
item.remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
item.id = url.UserInfo;

var query = HttpUtility.ParseQueryString(url.Query);
var query = Utils.ParseQueryString(url.Query);
ResolveStdTransport(query, ref item);

return item;
Expand All @@ -819,7 +859,7 @@ private static ProfileItem ResolveStdVLESS(string result)
item.remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
item.id = url.UserInfo;

var query = HttpUtility.ParseQueryString(url.Query);
var query = Utils.ParseQueryString(url.Query);
item.security = query["encryption"] ?? "none";
item.streamSecurity = query["security"] ?? "";
ResolveStdTransport(query, ref item);
Expand All @@ -841,7 +881,7 @@ private static ProfileItem ResolveHysteria2(string result)
item.remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
item.id = url.UserInfo;

var query = HttpUtility.ParseQueryString(url.Query);
var query = Utils.ParseQueryString(url.Query);
ResolveStdTransport(query, ref item);
item.allowInsecure = (query["insecure"] ?? "") == "1" ? "true" : "false";

Expand All @@ -867,13 +907,37 @@ private static ProfileItem ResolveTuic(string result)
item.security = userInfoParts[1];
}

var query = HttpUtility.ParseQueryString(url.Query);
var query = Utils.ParseQueryString(url.Query);
ResolveStdTransport(query, ref item);
item.headerType = query["congestion_control"] ?? "";

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)
{
item.flow = query["flow"] ?? "";
Expand Down

0 comments on commit 8a3eb41

Please sign in to comment.