mirror of
https://github.com/netchx/netch.git
synced 2026-05-07 22:44:03 +08:00
Fix a bug
Clean up codes.
This commit is contained in:
@@ -47,7 +47,7 @@ namespace Netch.Utils
|
||||
foreach (var line in text.GetLines())
|
||||
{
|
||||
var servers = ParseLine(line);
|
||||
if (line != null)
|
||||
if (servers != null)
|
||||
{
|
||||
list.AddRange(servers);
|
||||
}
|
||||
@@ -109,110 +109,7 @@ namespace Netch.Utils
|
||||
{
|
||||
var data = new Server();
|
||||
data.Type = "SS";
|
||||
/*
|
||||
try
|
||||
{
|
||||
if(!text.Contains("/?"))
|
||||
{
|
||||
var finder = new Regex(@"ss://(?<base64>[A-Za-z0-9+-/=_]+)(?:#(?<tag>\S+))?", RegexOptions.IgnoreCase);
|
||||
var parser = new Regex(@"^((?<method>.+?):(?<password>.*)@(?<hostname>.+?):(?<port>\d+?))$", RegexOptions.IgnoreCase);
|
||||
var match = finder.Match(text);
|
||||
if (!match.Success)
|
||||
{
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
var base64 = match.Groups["base64"].Value.TrimEnd('/');
|
||||
var tag = match.Groups["tag"].Value;
|
||||
if (!String.IsNullOrEmpty(tag))
|
||||
{
|
||||
data.Remark = HttpUtility.UrlDecode(tag);
|
||||
}
|
||||
|
||||
match = parser.Match(URLSafeBase64Decode(base64));
|
||||
if (!match.Success)
|
||||
{
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
data.Address = match.Groups["hostname"].Value;
|
||||
data.Port = int.Parse(match.Groups["port"].Value);
|
||||
data.Password = match.Groups["password"].Value;
|
||||
data.EncryptMethod = match.Groups["method"].Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (text.Contains("#"))
|
||||
{
|
||||
data.Remark = HttpUtility.UrlDecode(text.Split('#')[1]);
|
||||
text = text.Split('#')[0];
|
||||
}
|
||||
var finder = new Regex(@"ss://(?<base64>.+?)@(?<server>.+?):(?<port>\d+?)/\?plugin=(?<plugin>.+)");
|
||||
var parser = new Regex(@"^(?<method>.+?):(?<password>.+)$");
|
||||
var match = finder.Match(text);
|
||||
if (!match.Success)
|
||||
{
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
data.Address = match.Groups["server"].Value;
|
||||
data.Port = int.Parse(match.Groups["port"].Value);
|
||||
var plugins = HttpUtility.UrlDecode(match.Groups["plugin"].Value).Split(';');
|
||||
if (plugins[0] == "obfs-local")
|
||||
plugins[0] = "simple-obfs";
|
||||
|
||||
var base64 = URLSafeBase64Decode(match.Groups["base64"].Value);
|
||||
match = parser.Match(base64);
|
||||
if (!match.Success)
|
||||
{
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
data.EncryptMethod = match.Groups["method"].Value;
|
||||
data.Password = match.Groups["password"].Value;
|
||||
data.Plugin = plugins[0];
|
||||
data.PluginOption = plugins[1];
|
||||
}
|
||||
|
||||
if (!Global.EncryptMethods.SS.Contains(data.EncryptMethod))
|
||||
{
|
||||
Logging.Info(String.Format("不支持的 SS 加密方式:{0}", data.EncryptMethod));
|
||||
return null;
|
||||
}
|
||||
|
||||
list.Add(data);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
try
|
||||
{
|
||||
var uri = new Uri(text);
|
||||
var userinfo = URLSafeBase64Decode(uri.UserInfo).Split(new char[] { ':' }, 2);
|
||||
if (userinfo.Length != 2)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
data.Remark = uri.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
|
||||
data.Address = uri.IdnHost;
|
||||
data.Port = uri.Port;
|
||||
data.Password = userinfo[1];
|
||||
data.EncryptMethod = userinfo[0];
|
||||
|
||||
if (!Global.EncryptMethods.SS.Contains(data.EncryptMethod))
|
||||
{
|
||||
Logging.Info(String.Format("不支持的 SS 加密方式:{0}", data.EncryptMethod));
|
||||
return null;
|
||||
}
|
||||
|
||||
list.Add(data);
|
||||
}
|
||||
catch (UriFormatException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
text = text.Replace("/?", "?");
|
||||
try
|
||||
{
|
||||
@@ -337,81 +234,7 @@ namespace Netch.Utils
|
||||
data.Type = "SSR";
|
||||
|
||||
text = text.Substring(6);
|
||||
/*
|
||||
var shadowsocksr = URLSafeBase64Decode(text).Split(':');
|
||||
|
||||
if (shadowsocksr.Length > 6)
|
||||
{
|
||||
var buff = "";
|
||||
|
||||
for (int i = 0; i < shadowsocksr.Length - 5; i++)
|
||||
{
|
||||
buff += shadowsocksr[i];
|
||||
buff += ":";
|
||||
}
|
||||
|
||||
data.Address = buff.Substring(0, buff.Length - 1).Trim();
|
||||
}
|
||||
else
|
||||
{
|
||||
data.Address = shadowsocksr[0];
|
||||
}
|
||||
data.Port = int.Parse(shadowsocksr[shadowsocksr.Length - 5]);
|
||||
data.Protocol = shadowsocksr[shadowsocksr.Length - 4];
|
||||
if (!Global.Protocols.Contains(data.Protocol))
|
||||
{
|
||||
Logging.Info(String.Format("不支持的 SSR 协议:{0}", data.Protocol));
|
||||
return null;
|
||||
}
|
||||
|
||||
data.EncryptMethod = shadowsocksr[shadowsocksr.Length - 3];
|
||||
if (!Global.EncryptMethods.SSR.Contains(data.EncryptMethod))
|
||||
{
|
||||
Logging.Info(String.Format("不支持的 SSR 加密方式:{0}", data.EncryptMethod));
|
||||
return null;
|
||||
}
|
||||
|
||||
data.OBFS = shadowsocksr[shadowsocksr.Length - 2];
|
||||
if (!Global.OBFSs.Contains(data.OBFS))
|
||||
{
|
||||
Logging.Info(String.Format("不支持的 SSR 混淆:{0}", data.OBFS));
|
||||
return null;
|
||||
}
|
||||
|
||||
var info = shadowsocksr[shadowsocksr.Length - 1].Split('/');
|
||||
data.Password = URLSafeBase64Decode(info[0]);
|
||||
|
||||
var dict = new Dictionary<string, string>();
|
||||
if (info.Length > 1 && info[1].Length > 1)
|
||||
{
|
||||
foreach (var str in info[1].Substring(1).Split('&'))
|
||||
{
|
||||
var splited = str.Split('=');
|
||||
|
||||
dict.Add(splited[0], splited[1]);
|
||||
}
|
||||
}
|
||||
|
||||
if (dict.ContainsKey("remarks"))
|
||||
{
|
||||
data.Remark = URLSafeBase64Decode(dict["remarks"]);
|
||||
}
|
||||
|
||||
if (dict.ContainsKey("protoparam"))
|
||||
{
|
||||
data.ProtocolParam = URLSafeBase64Decode(dict["protoparam"]);
|
||||
}
|
||||
|
||||
if (dict.ContainsKey("obfsparam"))
|
||||
{
|
||||
data.OBFSParam = URLSafeBase64Decode(dict["obfsparam"]);
|
||||
}
|
||||
|
||||
if (data.EncryptMethod != "none" && data.Protocol == "origin" && data.OBFS == "plain")
|
||||
{
|
||||
data.Type = "SS";
|
||||
}
|
||||
*/
|
||||
var parser = new Regex(@"^(?<server>.+):(?<port>(-?\d+?)):(?<protocol>.+?):(?<method>.+?):(?<obfs>.+?):(?<password>.+?)/\?(?<info>.*)$");
|
||||
var match = parser.Match(URLSafeBase64Decode(text));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user