Skip to content

Commit

Permalink
regex vmess url
Browse files Browse the repository at this point in the history
  • Loading branch information
Student Main committed Jul 25, 2020
1 parent e313e00 commit 4e8ea37
Showing 1 changed file with 40 additions and 27 deletions.
67 changes: 40 additions & 27 deletions v2rayN/v2rayN/Handler/V2rayConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ public static VmessItem ImportFromClipboardConfig(string clipboardData, out stri
int indexSplit = result.IndexOf("?");
if (indexSplit > 0)
{
vmessItem = ResolveVmess4Vmess(result) ?? ResolveVmess4Kitsunebi(result);
vmessItem = ResolveStdVmess(result) ?? ResolveVmess4Kitsunebi(result);
}
else
{
Expand Down Expand Up @@ -1469,52 +1469,63 @@ private static VmessItem ResolveSSLegacy(string result)
}


private static VmessItem ResolveVmess4Vmess(string result)
private static readonly Regex StdVmessUserInfo = new Regex(
@"^(?<network>[a-z]+)(\+(?<streamSecurity>[a-z]+))?:(?<id>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})-(?<alterId>[0-9]+)$");

private static VmessItem ResolveStdVmess(string result)
{
VmessItem i = new VmessItem();
VmessItem i = new VmessItem
{
configType = (int)EConfigType.Vmess,
security = "auto"
};

Uri u = new Uri(result);

var uinfo = u.UserInfo;
var uinfo12 = uinfo.Split(':');
if (uinfo12.Length != 2) return null;
var user = uinfo12[0];
var pass = uinfo12[1];
var passsp = pass.LastIndexOf('-');
var id = pass.Substring(0, passsp);
var aid = pass.Substring(passsp + 1);
i.address = u.IdnHost;
i.port = u.Port;
i.id = id;
i.alterId = int.Parse(aid);
i.remarks = u.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
var q = HttpUtility.ParseQueryString(u.Query);

var m = StdVmessUserInfo.Match(u.UserInfo);
if (!m.Success) return null;

var query = u.Query;

var q = HttpUtility.ParseQueryString(u.Query);
i.id = m.Groups["id"].Value;
if (!int.TryParse(m.Groups["alterId"].Value, out int aid))
{
return null;
}
i.alterId = aid;

if (user.EndsWith("+tls"))
if (m.Groups["streamSecurity"].Success)
{
user = user.Split('+')[0];
i.streamSecurity = "tls";
// TODO tlsServerName
i.streamSecurity = m.Groups["streamSecurity"].Value;
}
i.network = user;
switch (user)
switch (i.streamSecurity)
{
case "tls":
// TODO tls config
break;
default:
if (!string.IsNullOrWhiteSpace(i.streamSecurity))
return null;
break;
}

i.network = m.Groups["network"].Value;
switch (i.network)
{
case "tcp":
string t1 = q["type"] ?? "none";
i.headerType = t1;
// TODO t = http, parse http option

// TODO http option

break;
case "kcp":
string t2 = q["type"] ?? "none";
i.headerType = t2;
// TODO seed
i.headerType = q["type"] ?? "none";
// TODO kcp seed
break;

case "ws":
string p1 = q["path"] ?? "/";
string h1 = q["host"] ?? "";
Expand All @@ -1539,6 +1550,8 @@ private static VmessItem ResolveVmess4Vmess(string result)
i.path = k;
break;

default:
return null;
}

return i;
Expand Down

0 comments on commit 4e8ea37

Please sign in to comment.