Skip to content

Commit

Permalink
Show Providers proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Apr 20, 2022
1 parent 7016f42 commit 463e259
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
33 changes: 30 additions & 3 deletions clashN/clashN/Forms/ProxiesForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static clashN.Mode.ClashProviders;
using static clashN.Mode.ClashProxies;

namespace clashN.Forms
{
public partial class ProxiesForm : BaseForm
{
Dictionary<String, ProxiesItem> proxies;
Dictionary<String, ProvidersItem> providers;
ProxiesItem selectedProxy;

public ProxiesForm()
Expand Down Expand Up @@ -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)
{
Expand All @@ -106,7 +110,7 @@ private void RefreshProxies()

foreach (KeyValuePair<string, ProxiesItem> kv in proxies)
{
if (kv.Value.type != "Selector" && kv.Value.type != "URLTest")
if (!Global.allowSelectType.Contains(kv.Value.type.ToLower()))
{
continue;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, ProvidersItem> 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());
Expand Down
8 changes: 7 additions & 1 deletion clashN/clashN/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Global
/// </summary>
public const string httpsProtocol = "https://";

public const string clashProtocol = "clash://";
public const string clashProtocol = "clash://";

/// <summary>
/// MyRegPath
Expand Down Expand Up @@ -83,6 +83,12 @@ class Global

public static readonly List<string> coreTypes = new List<string> { "clash", "clash_meta" };

public static readonly List<string> allowSelectType = new List<string> { "selector", "urltest" };

public static readonly List<string> notAllowTestType = new List<string> { "selector", "urltest", "direct", "reject", "compatible", "pass" };

public static readonly List<string> proxyVehicleType = new List<string> { "file", "http" };

public const string CheckMark = "√";


Expand Down
6 changes: 6 additions & 0 deletions clashN/clashN/Handler/CoreConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
20 changes: 10 additions & 10 deletions clashN/clashN/Handler/MainFormHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using clashN.Resx;
using static clashN.Mode.ClashProxies;
using clashN.Base;
using static clashN.Mode.ClashProviders;

namespace clashN.Handler
{
Expand Down Expand Up @@ -195,19 +196,22 @@ public void RegisterGlobalHotkey(Config config, EventHandler<HotkeyEventArgs> ha
}
}

public void GetClashProxies(Config config, Action<ClashProxies> update)
public void GetClashProxies(Config config, Action<ClashProxies, ClashProviders> update)
{
Task.Run(() => GetClashProxiesAsync(config, update));
}

private async Task GetClashProxiesAsync(Config config, Action<ClashProxies> update)
private async Task GetClashProxiesAsync(Config config, Action<ClashProxies, ClashProviders> 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<ClashProxies>(result);

update(clashProxies);
var url2 = $"{Global.httpProtocol}{Global.Loopback}:{config.APIPort}/providers/proxies";
var result2 = await HttpClientHelper.GetInstance().GetAsync(url2);
var clashProviders = Utils.FromJson<ClashProviders>(result2);

update(clashProxies, clashProviders);
}

public void ClashProxiesDelayTest(Action<string> update)
Expand All @@ -225,11 +229,7 @@ public void ClashProxiesDelayTest(Action<string> update)
List<Task> tasks = new List<Task>();
foreach (KeyValuePair<string, ProxiesItem> 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;
}
Expand Down
19 changes: 19 additions & 0 deletions clashN/clashN/Mode/ClashProviders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using static clashN.Mode.ClashProxies;

namespace clashN.Mode
{
public class ClashProviders
{
public Dictionary<String, ProvidersItem> 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; }
}
}
}

0 comments on commit 463e259

Please sign in to comment.