Add support for parsing Trojan subscriptions

This commit is contained in:
Tindy X
2020-05-10 19:27:37 +08:00
committed by GitHub
parent a711ee4f3f
commit f02c9151de

View File

@@ -516,6 +516,56 @@ namespace Netch.Utils
}
list.Add(NetchLink);
}
else if(text.StartsWith("trojan://"))
{
var data = new Server();
data.Type = "Trojan";
text = text.Replace("/?", "?");
try
{
if (text.Contains("#"))
{
data.Remark = HttpUtility.UrlDecode(text.Split('#')[1]);
text = text.Split('#')[0];
}
if (text.Contains("?"))
{
var reg = new Regex(@"^(?<data>.+?)\?(.+)$");
var regmatch = reg.Match(text);
if (regmatch.Success)
{
var peer = HttpUtility.UrlDecode(HttpUtility.ParseQueryString(new Uri(text).Query).Get("peer"));
if (peer != null)
data.Host = peer;
text = regmatch.Groups["data"].Value;
}
else
{
throw new FormatException();
}
}
var finder = new Regex(@"^trojan://(?<psk>.+?)@(?<server>.+):(?<port>\d+)");
var match = finder.Match(text);
if (!match.Success)
{
throw new FormatException();
}
data.Password = match.Groups["psk"].Value;
data.Hostname = match.Groups["server"].Value;
data.Port = int.Parse(match.Groups["port"].Value);
list.Add(data);
}
catch (FormatException)
{
return null;
}
}
}
catch (Exception e)
{