From 1fcc1d208e8bad27a52dee5a75ca9d115313f36e Mon Sep 17 00:00:00 2001 From: Bruce Wayne Date: Wed, 22 Jan 2020 22:06:49 +0800 Subject: [PATCH] Refine parse links --- Netch/Forms/MainForm.cs | 48 ++++----------- Netch/Netch.csproj | 1 + Netch/Utils/Extensions.cs | 23 ++++++++ Netch/Utils/ShareLink.cs | 119 +++++++++++++++++++++++--------------- 4 files changed, 108 insertions(+), 83 deletions(-) create mode 100644 Netch/Utils/Extensions.cs diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index 9f0503a9..5b2a13dd 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -391,19 +391,11 @@ namespace Netch.Forms var texts = Clipboard.GetText(); if (!String.IsNullOrWhiteSpace(texts)) { - using (var sr = new StringReader(texts)) + var result = Utils.ShareLink.Parse(texts); + + if (result != null) { - string text; - - while ((text = sr.ReadLine()) != null) - { - var result = Utils.ShareLink.Parse(text); - - if (result != null) - { - Global.Settings.Server.AddRange(result); - } - } + Global.Settings.Server.AddRange(result); } InitServer(); @@ -508,34 +500,16 @@ namespace Netch.Forms // 跳过 } - var list = new List(); - foreach (var server in Global.Settings.Server) + Global.Settings.Server = Global.Settings.Server.Where(server => server.Group != item.Remark).ToList(); + var result = Utils.ShareLink.Parse(response); + + if (result != null) { - if (server.Group != item.Remark) + foreach (var x in result) { - list.Add(server); - } - } - Global.Settings.Server = list; - - using (var sr = new StringReader(response)) - { - string text; - - while ((text = sr.ReadLine()) != null) - { - var result = Utils.ShareLink.Parse(text); - - if (result != null) - { - foreach (var x in result) - { - x.Group = item.Remark; - } - - Global.Settings.Server.AddRange(result); - } + x.Group = item.Remark; } + Global.Settings.Server.AddRange(result); } } catch (Exception) diff --git a/Netch/Netch.csproj b/Netch/Netch.csproj index fe5354b7..56e226ed 100644 --- a/Netch/Netch.csproj +++ b/Netch/Netch.csproj @@ -10,6 +10,7 @@ Netch.ico x86;x64 false + latest diff --git a/Netch/Utils/Extensions.cs b/Netch/Utils/Extensions.cs new file mode 100644 index 00000000..933b9021 --- /dev/null +++ b/Netch/Utils/Extensions.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using System.IO; + +namespace Netch.Utils +{ + public static class Extensions + { + public static IEnumerable GetLines(this string str, bool removeEmptyLines = true) + { + using var sr = new StringReader(str); + string line; + while ((line = sr.ReadLine()) != null) + { + if (removeEmptyLines && string.IsNullOrWhiteSpace(line)) + { + continue; + } + + yield return line; + } + } + } +} diff --git a/Netch/Utils/ShareLink.cs b/Netch/Utils/ShareLink.cs index 0b265b24..a8c5e298 100644 --- a/Netch/Utils/ShareLink.cs +++ b/Netch/Utils/ShareLink.cs @@ -1,4 +1,5 @@ -using System; +using Netch.Models; +using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; @@ -18,15 +19,43 @@ namespace Netch.Utils return Encoding.UTF8.GetString(Convert.FromBase64String(text.Replace("-", "+").Replace("_", "/").PadRight(text.Length + (4 - text.Length % 4) % 4, '='))); } - public static List Parse(string text) + public static List Parse(string text) { - var list = new List(); + var list = new List(); + try + { + foreach (var line in text.GetLines()) + { + var servers = ParseLine(line); + if (line != null) + { + list.AddRange(servers); + } + } + if (list.Count == 0) + { + System.Windows.Forms.MessageBox.Show(@"未找到可导入的链接!", @"错误", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); + return null; + } + } + catch (Exception e) + { + Logging.Info(e.ToString()); + return null; + } + + return list; + } + + private static IEnumerable ParseLine(string text) + { + var list = new List(); try { if (text.StartsWith("tg://socks?") || text.StartsWith("https://t.me/socks?")) { - var data = new Models.Server(); + var data = new Server(); data.Type = "Socks5"; var dict = new Dictionary(); @@ -44,11 +73,11 @@ namespace Netch.Utils data.Hostname = dict["server"]; data.Port = int.Parse(dict["port"]); - if (dict.ContainsKey("user") && !String.IsNullOrWhiteSpace(dict["user"])) + if (dict.ContainsKey("user") && !string.IsNullOrWhiteSpace(dict["user"])) { data.Username = dict["user"]; } - if (dict.ContainsKey("pass") && !String.IsNullOrWhiteSpace(dict["pass"])) + if (dict.ContainsKey("pass") && !string.IsNullOrWhiteSpace(dict["pass"])) { data.Password = dict["pass"]; } @@ -57,7 +86,7 @@ namespace Netch.Utils } else if (text.StartsWith("ss://")) { - var data = new Models.Server(); + var data = new Server(); data.Type = "SS"; /* try @@ -179,23 +208,27 @@ namespace Netch.Utils if (match.Success) { var plugins = HttpUtility.UrlDecode(HttpUtility.ParseQueryString(new Uri(text).Query).Get("plugin")); - var plugin = plugins.Substring(0, plugins.IndexOf(";")); - var pluginopts = plugins.Substring(plugins.IndexOf(";") + 1); - if (plugin == "obfs-local" || plugin == "simple-obfs") + if (plugins != null) { - plugin = "simple-obfs"; - if (!pluginopts.Contains("obfs=")) - pluginopts = "obfs=http;obfs-host=" + pluginopts; - } - else if (plugin == "simple-obfs-tls") - { - plugin = "simple-obfs"; - if (!pluginopts.Contains("obfs=")) - pluginopts = "obfs=tls;obfs-host=" + pluginopts; + var plugin = plugins.Substring(0, plugins.IndexOf(";", StringComparison.Ordinal)); + var pluginopts = plugins.Substring(plugins.IndexOf(";", StringComparison.Ordinal) + 1); + if (plugin == "obfs-local" || plugin == "simple-obfs") + { + plugin = "simple-obfs"; + if (!pluginopts.Contains("obfs=")) + pluginopts = "obfs=http;obfs-host=" + pluginopts; + } + else if (plugin == "simple-obfs-tls") + { + plugin = "simple-obfs"; + if (!pluginopts.Contains("obfs=")) + pluginopts = "obfs=tls;obfs-host=" + pluginopts; + } + + data.Plugin = plugin; + data.PluginOption = pluginopts; } - data.Plugin = plugin; - data.PluginOption = pluginopts; text = match.Groups["data"].Value; } else @@ -243,7 +276,7 @@ namespace Netch.Utils if (!Global.EncryptMethods.SS.Contains(data.EncryptMethod)) { - Logging.Info(String.Format("不支持的 SS 加密方式:{0}", data.EncryptMethod)); + Logging.Info(string.Format("不支持的 SS 加密方式:{0}", data.EncryptMethod)); return null; } @@ -260,7 +293,7 @@ namespace Netch.Utils foreach (var server in json.servers) { - var data = new Models.Server(); + var data = new Server(); data.Type = "SS"; data.Remark = server.remarks; @@ -268,8 +301,8 @@ namespace Netch.Utils data.Port = (server.port != 0) ? server.port : json.port; data.Password = (server.password != null) ? server.password : json.password; data.EncryptMethod = (server.encryption != null) ? server.encryption : json.encryption; - data.Plugin = (String.IsNullOrEmpty(json.plugin)) ? (String.IsNullOrEmpty(server.plugin) ? null : server.plugin) : json.plugin; - data.PluginOption = (String.IsNullOrEmpty(json.plugin_options)) ? (String.IsNullOrEmpty(server.plugin_options) ? null : server.plugin_options) : json.plugin_options; + data.Plugin = (string.IsNullOrEmpty(json.plugin)) ? (string.IsNullOrEmpty(server.plugin) ? null : server.plugin) : json.plugin; + data.PluginOption = (string.IsNullOrEmpty(json.plugin_options)) ? (string.IsNullOrEmpty(server.plugin_options) ? null : server.plugin_options) : json.plugin_options; if (Global.EncryptMethods.SS.Contains(data.EncryptMethod)) { @@ -279,7 +312,7 @@ namespace Netch.Utils } else if (text.StartsWith("ssr://")) { - var data = new Models.Server(); + var data = new Server(); data.Type = "SSR"; text = text.Substring(6); @@ -374,14 +407,14 @@ namespace Netch.Utils data.EncryptMethod = match.Groups["method"].Value; if (!Global.EncryptMethods.SSR.Contains(data.EncryptMethod)) { - Logging.Info(String.Format("不支持的 SSR 加密方式:{0}", data.EncryptMethod)); + Logging.Info(string.Format("不支持的 SSR 加密方式:{0}", data.EncryptMethod)); return null; } data.Protocol = match.Groups["protocol"].Value; if (!Global.Protocols.Contains(data.Protocol)) { - Logging.Info(String.Format("不支持的 SSR 协议:{0}", data.Protocol)); + Logging.Info(string.Format("不支持的 SSR 协议:{0}", data.Protocol)); return null; } @@ -392,7 +425,7 @@ namespace Netch.Utils } if (!Global.OBFSs.Contains(data.OBFS)) { - Logging.Info(String.Format("不支持的 SSR 混淆:{0}", data.OBFS)); + Logging.Info(string.Format("不支持的 SSR 混淆:{0}", data.OBFS)); return null; } @@ -430,11 +463,11 @@ namespace Netch.Utils } else if (text.StartsWith("vmess://")) { - var data = new Models.Server(); + var data = new Server(); data.Type = "VMess"; text = text.Substring(8); - var vmess = Newtonsoft.Json.JsonConvert.DeserializeObject(URLSafeBase64Decode(text)); + var vmess = Newtonsoft.Json.JsonConvert.DeserializeObject(URLSafeBase64Decode(text)); data.Remark = vmess.ps; data.Hostname = vmess.add; @@ -445,14 +478,14 @@ namespace Netch.Utils data.TransferProtocol = vmess.net; if (!Global.TransferProtocols.Contains(data.TransferProtocol)) { - Logging.Info(String.Format("不支持的 VMess 传输协议:{0}", data.TransferProtocol)); + Logging.Info(string.Format("不支持的 VMess 传输协议:{0}", data.TransferProtocol)); return null; } data.FakeType = vmess.type; if (!Global.FakeTypes.Contains(data.FakeType)) { - Logging.Info(String.Format("不支持的 VMess 伪装类型:{0}", data.FakeType)); + Logging.Info(string.Format("不支持的 VMess 伪装类型:{0}", data.FakeType)); return null; } @@ -469,7 +502,7 @@ namespace Netch.Utils { if (!Global.EncryptMethods.VMessQUIC.Contains(vmess.host)) { - Logging.Info(String.Format("不支持的 VMess QUIC 加密方式:{0}", vmess.host)); + Logging.Info(string.Format("不支持的 VMess QUIC 加密方式:{0}", vmess.host)); return null; } else @@ -492,13 +525,13 @@ namespace Netch.Utils } else { - if (vmess.mux.enabled is Boolean) + if (vmess.mux.enabled is bool enabled) { - data.UseMux = (bool)vmess.mux.enabled; + data.UseMux = enabled; } - else if (vmess.mux.enabled is String) + else if (vmess.mux.enabled is string muxEnabled) { - data.UseMux = (string)vmess.mux.enabled == "true"; // 针对使用字符串当作布尔值的情况 + data.UseMux = muxEnabled == "true"; // 针对使用字符串当作布尔值的情况 } else { @@ -513,8 +546,8 @@ namespace Netch.Utils else if (text.StartsWith("Netch://")) { text = text.Substring(8); - var NetchLink = Newtonsoft.Json.JsonConvert.DeserializeObject(URLSafeBase64Decode(text)); - if (!String.IsNullOrEmpty(NetchLink.Hostname) || NetchLink.Port > 65536 || NetchLink.Port > 0) + var NetchLink = Newtonsoft.Json.JsonConvert.DeserializeObject(URLSafeBase64Decode(text)); + if (!string.IsNullOrEmpty(NetchLink.Hostname) || NetchLink.Port > 65536 || NetchLink.Port > 0) { return null; } @@ -573,12 +606,6 @@ namespace Netch.Utils } list.Add(NetchLink); } - - else - { - System.Windows.Forms.MessageBox.Show("未找到可导入的链接!", "错误", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); - return null; - } } catch (Exception e) {