AI-optimized code

This commit is contained in:
2dust 2025-03-06 16:36:39 +08:00
parent 7e348c196e
commit 07a3bdc618
4 changed files with 70 additions and 76 deletions

View file

@ -1668,7 +1668,7 @@ namespace ServiceLib.Handler
public static async Task<int> InitRouting(Config config, bool blImportAdvancedRules = false) public static async Task<int> InitRouting(Config config, bool blImportAdvancedRules = false)
{ {
if (string.IsNullOrEmpty(config.ConstItem.RouteRulesTemplateSourceUrl)) if (config.ConstItem.RouteRulesTemplateSourceUrl.IsNullOrEmpty())
{ {
await InitBuiltinRouting(config, blImportAdvancedRules); await InitBuiltinRouting(config, blImportAdvancedRules);
} }
@ -1684,7 +1684,7 @@ namespace ServiceLib.Handler
{ {
var downloadHandle = new DownloadService(); var downloadHandle = new DownloadService();
var templateContent = await downloadHandle.TryDownloadString(config.ConstItem.RouteRulesTemplateSourceUrl, true, ""); var templateContent = await downloadHandle.TryDownloadString(config.ConstItem.RouteRulesTemplateSourceUrl, true, "");
if (string.IsNullOrEmpty(templateContent)) if (templateContent.IsNullOrEmpty())
return await InitBuiltinRouting(config, blImportAdvancedRules); // fallback return await InitBuiltinRouting(config, blImportAdvancedRules); // fallback
var template = JsonUtils.Deserialize<RoutingTemplate>(templateContent); var template = JsonUtils.Deserialize<RoutingTemplate>(templateContent);
@ -1701,14 +1701,14 @@ namespace ServiceLib.Handler
{ {
var item = template.RoutingItems[i]; var item = template.RoutingItems[i];
if (string.IsNullOrEmpty(item.Url) && string.IsNullOrEmpty(item.RuleSet)) if (item.Url.IsNullOrEmpty() && item.RuleSet.IsNullOrEmpty())
continue; continue;
var ruleSetsString = !string.IsNullOrEmpty(item.RuleSet) var ruleSetsString = !item.RuleSet.IsNullOrEmpty()
? item.RuleSet ? item.RuleSet
: await downloadHandle.TryDownloadString(item.Url, true, ""); : await downloadHandle.TryDownloadString(item.Url, true, "");
if (string.IsNullOrEmpty(ruleSetsString)) if (ruleSetsString.IsNullOrEmpty())
continue; continue;
item.Remarks = $"{template.Version}-{item.Remarks}"; item.Remarks = $"{template.Version}-{item.Remarks}";
@ -1841,17 +1841,17 @@ namespace ServiceLib.Handler
var downloadHandle = new DownloadService(); var downloadHandle = new DownloadService();
var templateContent = await downloadHandle.TryDownloadString(url, true, ""); var templateContent = await downloadHandle.TryDownloadString(url, true, "");
if (string.IsNullOrEmpty(templateContent)) if (templateContent.IsNullOrEmpty())
return currentItem; return currentItem;
var template = JsonUtils.Deserialize<DNSItem>(templateContent); var template = JsonUtils.Deserialize<DNSItem>(templateContent);
if (template == null) if (template == null)
return currentItem; return currentItem;
if (!string.IsNullOrEmpty(template.NormalDNS)) if (!template.NormalDNS.IsNullOrEmpty())
template.NormalDNS = await downloadHandle.TryDownloadString(template.NormalDNS, true, ""); template.NormalDNS = await downloadHandle.TryDownloadString(template.NormalDNS, true, "");
if (!string.IsNullOrEmpty(template.TunDNS)) if (!template.TunDNS.IsNullOrEmpty())
template.TunDNS = await downloadHandle.TryDownloadString(template.TunDNS, true, ""); template.TunDNS = await downloadHandle.TryDownloadString(template.TunDNS, true, "");
template.Id = currentItem.Id; template.Id = currentItem.Id;

View file

@ -87,7 +87,7 @@ namespace ServiceLib.Handler.SysProxy
} }
else if (type is 2 or 4) // named proxy or autoproxy script URL else if (type is 2 or 4) // named proxy or autoproxy script URL
{ {
optionCount = string.IsNullOrEmpty(exceptions) ? 2 : 3; optionCount = exceptions.IsNullOrEmpty() ? 2 : 3;
} }
var m_Int = (int)PerConnFlags.PROXY_TYPE_DIRECT; var m_Int = (int)PerConnFlags.PROXY_TYPE_DIRECT;

View file

@ -53,32 +53,7 @@ namespace ServiceLib.ViewModels
private async Task Init() private async Task Init()
{ {
_ = Task.Run(async () => _ = DelayTestTask();
{
var numOfExecuted = 1;
while (true)
{
await Task.Delay(1000 * 5);
numOfExecuted++;
if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box)))
{
continue;
}
if (_config.ClashUIItem.ConnectionsRefreshInterval <= 0)
{
continue;
}
if (numOfExecuted % _config.ClashUIItem.ConnectionsRefreshInterval != 0)
{
continue;
}
await GetClashConnections();
}
});
await Task.CompletedTask;
} }
private async Task GetClashConnections() private async Task GetClashConnections()
@ -146,5 +121,35 @@ namespace ServiceLib.ViewModels
await ClashApiHandler.Instance.ClashConnectionClose(id); await ClashApiHandler.Instance.ClashConnectionClose(id);
await GetClashConnections(); await GetClashConnections();
} }
public async Task DelayTestTask()
{
_ = Task.Run(async () =>
{
var numOfExecuted = 1;
while (true)
{
await Task.Delay(1000 * 5);
numOfExecuted++;
if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box)))
{
continue;
}
if (_config.ClashUIItem.ConnectionsRefreshInterval <= 0)
{
continue;
}
if (numOfExecuted % _config.ClashUIItem.ConnectionsRefreshInterval != 0)
{
continue;
}
await GetClashConnections();
}
});
await Task.CompletedTask;
}
} }
} }

View file

@ -96,7 +96,6 @@ namespace ServiceLib.ViewModels
private async Task Init() private async Task Init()
{ {
_ = DelayTestTask(); _ = DelayTestTask();
await ProxiesReload();
} }
private async Task DoRulemodeSelected(bool c) private async Task DoRulemodeSelected(bool c)
@ -135,22 +134,12 @@ namespace ServiceLib.ViewModels
RefreshProxyDetails(c); RefreshProxyDetails(c);
} }
private void UpdateHandler(bool notify, string msg)
{
NoticeHandler.Instance.SendMessageEx(msg);
}
public async Task ProxiesReload() public async Task ProxiesReload()
{ {
await GetClashProxies(true); await GetClashProxies(true);
await ProxiesDelayTest(); await ProxiesDelayTest();
} }
public async Task ProxiesDelayTest()
{
await ProxiesDelayTest(true);
}
#region proxy function #region proxy function
private async Task SetRuleMode(ERuleMode mode) private async Task SetRuleMode(ERuleMode mode)
@ -218,7 +207,7 @@ namespace ServiceLib.ViewModels
{ {
continue; continue;
} }
var item = _proxyGroups.Where(t => t.Name == kv.Key).FirstOrDefault(); var item = _proxyGroups.FirstOrDefault(t => t.Name == kv.Key);
if (item != null && item.Name.IsNotEmpty()) if (item != null && item.Name.IsNotEmpty())
{ {
continue; continue;
@ -369,7 +358,7 @@ namespace ServiceLib.ViewModels
await ClashApiHandler.Instance.ClashSetActiveProxy(name, nameNode); await ClashApiHandler.Instance.ClashSetActiveProxy(name, nameNode);
selectedProxy.now = nameNode; selectedProxy.now = nameNode;
var group = _proxyGroups.Where(it => it.Name == SelectedGroup.Name).FirstOrDefault(); var group = _proxyGroups.FirstOrDefault(it => it.Name == SelectedGroup.Name);
if (group != null) if (group != null)
{ {
group.Now = nameNode; group.Now = nameNode;
@ -381,7 +370,7 @@ namespace ServiceLib.ViewModels
NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess); NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess);
} }
private async Task ProxiesDelayTest(bool blAll) private async Task ProxiesDelayTest(bool blAll = true)
{ {
ClashApiHandler.Instance.ClashProxiesDelayTest(blAll, _proxyDetails.ToList(), async (item, result) => ClashApiHandler.Instance.ClashProxiesDelayTest(blAll, _proxyDetails.ToList(), async (item, result) =>
{ {
@ -403,19 +392,19 @@ namespace ServiceLib.ViewModels
public void ProxiesDelayTestResult(SpeedTestResult result) public void ProxiesDelayTestResult(SpeedTestResult result)
{ {
//UpdateHandler(false, $"{item.name}={result}"); //UpdateHandler(false, $"{item.name}={result}");
var detail = _proxyDetails.Where(it => it.Name == result.IndexId).FirstOrDefault(); var detail = _proxyDetails.FirstOrDefault(it => it.Name == result.IndexId);
if (detail != null) if (detail != null)
{ {
var dicResult = JsonUtils.Deserialize<Dictionary<string, object>>(result.Delay); var dicResult = JsonUtils.Deserialize<Dictionary<string, object>>(result.Delay);
if (dicResult != null && dicResult.ContainsKey("delay")) if (dicResult != null && dicResult.TryGetValue("delay", out var value))
{ {
detail.Delay = Convert.ToInt32(dicResult["delay"].ToString()); detail.Delay = Convert.ToInt32(value.ToString());
detail.DelayName = $"{detail.Delay}ms"; detail.DelayName = $"{detail.Delay}ms";
} }
else if (dicResult != null && dicResult.ContainsKey("message")) else if (dicResult != null && dicResult.TryGetValue("message", out var value1))
{ {
detail.Delay = _delayTimeout; detail.Delay = _delayTimeout;
detail.DelayName = $"{dicResult["message"]}"; detail.DelayName = $"{value1}";
} }
else else
{ {
@ -432,28 +421,28 @@ namespace ServiceLib.ViewModels
public async Task DelayTestTask() public async Task DelayTestTask()
{ {
Task.Run(async () => _ = Task.Run(async () =>
{ {
var numOfExecuted = 1; var numOfExecuted = 1;
while (true) while (true)
{ {
await Task.Delay(1000 * 60); await Task.Delay(1000 * 60);
numOfExecuted++; numOfExecuted++;
if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box))) if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box)))
{ {
continue; continue;
} }
if (_config.ClashUIItem.ProxiesAutoDelayTestInterval <= 0) if (_config.ClashUIItem.ProxiesAutoDelayTestInterval <= 0)
{ {
continue; continue;
} }
if (numOfExecuted % _config.ClashUIItem.ProxiesAutoDelayTestInterval != 0) if (numOfExecuted % _config.ClashUIItem.ProxiesAutoDelayTestInterval != 0)
{ {
continue; continue;
} }
await ProxiesDelayTest(); await ProxiesDelayTest();
} }
}); });
await Task.CompletedTask; await Task.CompletedTask;
} }