From 463e259523a112b42bc12033a5ca297bac426055 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Wed, 20 Apr 2022 19:45:59 +0800 Subject: [PATCH] Show Providers proxy --- clashN/clashN/Forms/ProxiesForm.cs | 33 ++++++++++++++++++++-- clashN/clashN/Global.cs | 8 +++++- clashN/clashN/Handler/CoreConfigHandler.cs | 6 ++++ clashN/clashN/Handler/MainFormHandler.cs | 20 ++++++------- clashN/clashN/Mode/ClashProviders.cs | 19 +++++++++++++ 5 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 clashN/clashN/Mode/ClashProviders.cs diff --git a/clashN/clashN/Forms/ProxiesForm.cs b/clashN/clashN/Forms/ProxiesForm.cs index cc6bba3..43ec252 100644 --- a/clashN/clashN/Forms/ProxiesForm.cs +++ b/clashN/clashN/Forms/ProxiesForm.cs @@ -11,6 +11,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using static clashN.Mode.ClashProviders; using static clashN.Mode.ClashProxies; namespace clashN.Forms @@ -18,6 +19,7 @@ namespace clashN.Forms public partial class ProxiesForm : BaseForm { Dictionary proxies; + Dictionary providers; ProxiesItem selectedProxy; public ProxiesForm() @@ -78,9 +80,11 @@ private void InitDetailView() private void GetClashProxies(bool refreshUI) { - MainFormHandler.Instance.GetClashProxies(config, it => + MainFormHandler.Instance.GetClashProxies(config, (it, it2) => { proxies = it.proxies; + providers = it2.providers; + LazyConfig.Instance.SetProxies(proxies); if (refreshUI) { @@ -106,7 +110,7 @@ private void RefreshProxies() foreach (KeyValuePair kv in proxies) { - if (kv.Value.type != "Selector" && kv.Value.type != "URLTest") + if (!Global.allowSelectType.Contains(kv.Value.type.ToLower())) { continue; } @@ -159,7 +163,8 @@ private void RefreshDetail(int index) foreach (var item in proxy.all) { var isActive = item == proxy.now; - proxies.TryGetValue(item, out ProxiesItem proxy2); + + var proxy2 = TryGetProxy(item); if (proxy2 == null) { continue; @@ -197,6 +202,28 @@ private void RefreshDetail(int index) lvDetail.EndUpdate(); } + private ProxiesItem TryGetProxy(string name) + { + proxies.TryGetValue(name, out ProxiesItem proxy2); + if (proxy2 != null) + { + return proxy2; + } + //from providers + foreach (KeyValuePair kv in providers) + { + if (Global.proxyVehicleType.Contains(kv.Value.vehicleType.ToLower())) + { + var proxy3 = kv.Value.proxies.FirstOrDefault(t => t.name == name); + if (proxy3 != null) + { + return proxy3; + } + } + } + return null; + } + private void lvProxies_SelectedIndexChanged(object sender, EventArgs e) { RefreshDetail(GetLvSelectedIndex()); diff --git a/clashN/clashN/Global.cs b/clashN/clashN/Global.cs index 25a2fe6..a576ac5 100644 --- a/clashN/clashN/Global.cs +++ b/clashN/clashN/Global.cs @@ -48,7 +48,7 @@ class Global /// public const string httpsProtocol = "https://"; - public const string clashProtocol = "clash://"; + public const string clashProtocol = "clash://"; /// /// MyRegPath @@ -83,6 +83,12 @@ class Global public static readonly List coreTypes = new List { "clash", "clash_meta" }; + public static readonly List allowSelectType = new List { "selector", "urltest" }; + + public static readonly List notAllowTestType = new List { "selector", "urltest", "direct", "reject", "compatible", "pass" }; + + public static readonly List proxyVehicleType = new List { "file", "http" }; + public const string CheckMark = "√"; diff --git a/clashN/clashN/Handler/CoreConfigHandler.cs b/clashN/clashN/Handler/CoreConfigHandler.cs index ab46ac2..85e783a 100644 --- a/clashN/clashN/Handler/CoreConfigHandler.cs +++ b/clashN/clashN/Handler/CoreConfigHandler.cs @@ -100,6 +100,12 @@ public static int GenerateClientConfig(ProfileItem node, string fileName, bool b ModifyContent(fileContent, "allow-lan", "false"); } + //mode + if (!fileContent.ContainsKey("mode")) + { + ModifyContent(fileContent, "mode", "Rule"); + } + //enable tun mode if (node.enableTun) { diff --git a/clashN/clashN/Handler/MainFormHandler.cs b/clashN/clashN/Handler/MainFormHandler.cs index 0e5bbc7..1d753b1 100644 --- a/clashN/clashN/Handler/MainFormHandler.cs +++ b/clashN/clashN/Handler/MainFormHandler.cs @@ -12,6 +12,7 @@ using clashN.Resx; using static clashN.Mode.ClashProxies; using clashN.Base; +using static clashN.Mode.ClashProviders; namespace clashN.Handler { @@ -195,19 +196,22 @@ public void RegisterGlobalHotkey(Config config, EventHandler ha } } - public void GetClashProxies(Config config, Action update) + public void GetClashProxies(Config config, Action update) { Task.Run(() => GetClashProxiesAsync(config, update)); } - private async Task GetClashProxiesAsync(Config config, Action update) + private async Task GetClashProxiesAsync(Config config, Action update) { var url = $"{Global.httpProtocol}{Global.Loopback}:{config.APIPort}/proxies"; - var result = await Base.HttpClientHelper.GetInstance().GetAsync(url); - + var result = await HttpClientHelper.GetInstance().GetAsync(url); var clashProxies = Utils.FromJson(result); - update(clashProxies); + var url2 = $"{Global.httpProtocol}{Global.Loopback}:{config.APIPort}/providers/proxies"; + var result2 = await HttpClientHelper.GetInstance().GetAsync(url2); + var clashProviders = Utils.FromJson(result2); + + update(clashProxies, clashProviders); } public void ClashProxiesDelayTest(Action update) @@ -225,11 +229,7 @@ public void ClashProxiesDelayTest(Action update) List tasks = new List(); foreach (KeyValuePair kv in proxies) { - if (kv.Value.type == "Selector" - || kv.Value.type == "URLTest" - || kv.Value.type == "Direct" - || kv.Value.type == "Reject" - ) + if (Global.notAllowTestType.Contains(kv.Value.type.ToLower())) { continue; } diff --git a/clashN/clashN/Mode/ClashProviders.cs b/clashN/clashN/Mode/ClashProviders.cs new file mode 100644 index 0000000..26633d7 --- /dev/null +++ b/clashN/clashN/Mode/ClashProviders.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using static clashN.Mode.ClashProxies; + +namespace clashN.Mode +{ + public class ClashProviders + { + public Dictionary providers { get; set; } + + public class ProvidersItem + { + public string name { get; set; } + public ProxiesItem[] proxies { get; set; } + public string type { get; set; } + public string vehicleType { get; set; } + } + } +} \ No newline at end of file