Improved proxies

This commit is contained in:
2dust
2022-07-28 20:16:01 +08:00
parent bce0b7d66a
commit 96ef70592e
4 changed files with 63 additions and 24 deletions

View File

@@ -93,5 +93,21 @@ namespace clashN.Base
this.DoDragDrop(e.Item, DragDropEffects.Move);
this.InsertionMark.Index = -1;
}
public void SetScrollPosition(int pos)
{
pos = Math.Min(Items.Count - 1, pos);
if (pos < 0 || pos >= Items.Count)
return;
EnsureVisible(pos);
for (int i = 0; i < 10; i++)
{
if (TopItem != null && TopItem.Index != pos)
TopItem = Items[pos];
}
}
}
}

View File

@@ -100,7 +100,7 @@ namespace clashN.Forms
_ = LoadCore();
proxiesControl.Init(config);
proxiesControl.Init(config, UpdateTaskHandler);
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
@@ -332,7 +332,7 @@ namespace clashN.Forms
if (index >= 0 && index < lvProfiles.Items.Count && lvProfiles.Items.Count > 0)
{
lvProfiles.Items[index].Selected = true;
lvProfiles.EnsureVisible(index); // workaround
lvProfiles.SetScrollPosition(index); // workaround
}
}
@@ -456,6 +456,7 @@ namespace clashN.Forms
SwitchUI(true);
proxiesControl.ProxiesReload();
proxiesControl.ProxiesDelayTest();
}
@@ -847,7 +848,7 @@ namespace clashN.Forms
if (index >= 0 && index < lvProfiles.Items.Count && lvProfiles.Items.Count > 0)
{
lvProfiles.Items[index].Selected = true;
lvProfiles.EnsureVisible(index); // workaround
lvProfiles.SetScrollPosition(index); // workaround
}
SetVisibleCore(true);
@@ -1225,11 +1226,13 @@ namespace clashN.Forms
{
tsbCurrentProxies.Enabled = true;
tsbProfile.Enabled = false;
proxiesControl.Focus();
}
else
{
tsbCurrentProxies.Enabled = false;
tsbProfile.Enabled = true;
lvProfiles.Focus();
}
tsbReload.Enabled = true;

View File

@@ -17,7 +17,8 @@ namespace clashN.Forms
{
public partial class ProxiesControl : UserControl
{
private Config config;
private Config _config;
Action<bool, string> _updateFunc;
private Dictionary<String, ProxiesItem> proxies;
private Dictionary<String, ProvidersItem> providers;
private List<ProxiesItem> lstProxies;
@@ -37,9 +38,10 @@ namespace clashN.Forms
}
public void Init(Config _config)
public void Init(Config config, Action<bool, string> update)
{
config = _config;
_config = config;
_updateFunc = update;
InitProxiesView();
InitDetailView();
@@ -137,8 +139,9 @@ namespace clashN.Forms
private void GetClashProxies(bool refreshUI)
{
MainFormHandler.Instance.GetClashProxies(config, (it, it2) =>
MainFormHandler.Instance.GetClashProxies(_config, (it, it2) =>
{
_updateFunc(false, "Refresh proxies");
proxies = it?.proxies;
providers = it2?.providers;
@@ -224,7 +227,8 @@ namespace clashN.Forms
if (index >= 0 && index < lvProxies.Items.Count && lvProxies.Items.Count > 0)
{
lvProxies.Items[index].Selected = true;
lvProxies.EnsureVisible(index);
lvProxies.SetScrollPosition(index);
}
}));
@@ -393,11 +397,11 @@ namespace clashN.Forms
var selectedProxy = TryGetProxy(name);
if (selectedProxy == null || selectedProxy.type != "Selector")
{
UI.Show(ResUI.OperationFailed);
_updateFunc(false, ResUI.OperationFailed);
return;
}
var url = $"{Global.httpProtocol}{Global.Loopback}:{config.APIPort}/proxies/{name}";
var url = $"{Global.httpProtocol}{Global.Loopback}:{_config.APIPort}/proxies/{name}";
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("name", nameNode);
_ = HttpClientHelper.GetInstance().PutAsync(url, headers);
@@ -405,6 +409,8 @@ namespace clashN.Forms
selectedProxy.now = nameNode;
RefreshDetail(GetLvSelectedIndex());
_updateFunc(false, ResUI.OperationSuccess);
GetClashProxies(true);
}
@@ -433,17 +439,14 @@ namespace clashN.Forms
public void ProxiesReload()
{
GetClashProxies(true);
lvProxies.Focus();
}
public void ProxiesDelayTest()
{
if (proxies == null)
{
return;
}
MainFormHandler.Instance.ClashProxiesDelayTest(it =>
{
_updateFunc(false, "Clash Proxies Delay Test");
GetClashProxies(true);
});
}
@@ -503,9 +506,9 @@ namespace clashN.Forms
{
var dtNow = DateTime.Now;
if (config.autoDelayTestInterval > 0)
if (_config.autoDelayTestInterval > 0)
{
if ((dtNow - autoDelayTestTime).Minutes % config.autoDelayTestInterval == 0)
if ((dtNow - autoDelayTestTime).Minutes % _config.autoDelayTestInterval == 0)
{
ProxiesDelayTest();
autoDelayTestTime = dtNow;

View File

@@ -203,21 +203,38 @@ namespace clashN.Handler
private async Task GetClashProxiesAsync(Config config, Action<ClashProxies, ClashProviders> update)
{
var url = $"{Global.httpProtocol}{Global.Loopback}:{config.APIPort}/proxies";
var result = await HttpClientHelper.GetInstance().GetAsync(url);
var clashProxies = Utils.FromJson<ClashProxies>(result);
for (var i = 0; i < 5; i++)
{
var url = $"{Global.httpProtocol}{Global.Loopback}:{config.APIPort}/proxies";
var result = await HttpClientHelper.GetInstance().GetAsync(url);
var clashProxies = Utils.FromJson<ClashProxies>(result);
var url2 = $"{Global.httpProtocol}{Global.Loopback}:{config.APIPort}/providers/proxies";
var result2 = await HttpClientHelper.GetInstance().GetAsync(url2);
var clashProviders = Utils.FromJson<ClashProviders>(result2);
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);
if (clashProxies != null || clashProviders != null)
{
update(clashProxies, clashProviders);
return;
}
Thread.Sleep(5000);
}
update(null, null);
}
public void ClashProxiesDelayTest(Action<string> update)
{
Task.Run(() =>
{
for (int i = 0; i < 5; i++)
{
if (LazyConfig.Instance.GetProxies() == null)
{
Thread.Sleep(5000);
continue;
}
}
var proxies = LazyConfig.Instance.GetProxies();
if (proxies == null)
{