refactor: 优化代码格式.
This commit is contained in:
@@ -12,7 +12,6 @@ namespace Proxd_man.Monitor;
|
||||
|
||||
public class OutboundMonitor
|
||||
{
|
||||
|
||||
private static readonly Uri HttpLatencyTestUrl = new("https://cp.cloudflare.com/generate_204");
|
||||
private static readonly Uri HttpBandwidthTestUrl = new("https://speed.cloudflare.com/__down?bytes=51200000");
|
||||
|
||||
@@ -23,7 +22,8 @@ public class OutboundMonitor
|
||||
|
||||
private ProxdConfig Config => _configProvider.CurrentValue;
|
||||
|
||||
public OutboundMonitor(ILogger<OutboundMonitor> logger, IOptionsMonitor<ProxdConfig> configProvider, SubscribeManager subscribeManager, XrayCoreManager xrayCoreManager)
|
||||
public OutboundMonitor(ILogger<OutboundMonitor> logger, IOptionsMonitor<ProxdConfig> configProvider,
|
||||
SubscribeManager subscribeManager, XrayCoreManager xrayCoreManager)
|
||||
{
|
||||
_logger = logger;
|
||||
_configProvider = configProvider;
|
||||
@@ -38,8 +38,6 @@ public class OutboundMonitor
|
||||
}
|
||||
|
||||
public async Task ReselectOutbound(IDictionary<SubscribeSource, List<BaseOutboundSetting>> subscribeOutbounds)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("开始重新选择代理...");
|
||||
var outboundLatencies = new ConcurrentDictionary<BaseOutboundSetting, double>();
|
||||
@@ -87,9 +85,9 @@ public class OutboundMonitor
|
||||
|
||||
// 随机选择一个端口进行测试
|
||||
var port = (ushort)new Random().Next(50000, 60000);
|
||||
var outboundTestProcess =
|
||||
await _xrayCoreManager.CreateOutboundTestProcess(port, outboundSetting);
|
||||
|
||||
using var outboundTestProcess =
|
||||
await _xrayCoreManager.CreateOutboundTestProcess(port, outboundSetting);
|
||||
if (!outboundTestProcess.Start())
|
||||
{
|
||||
_logger.LogError("无法启动 Xray-Core 进程, 地址: {OutboundSettingAddress}",
|
||||
@@ -102,8 +100,6 @@ public class OutboundMonitor
|
||||
_xrayCoreManager.RedirectXrayCoreProcessOutput(outboundTestProcess);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 等 Xray-Core 就绪.
|
||||
await Task.Delay(200);
|
||||
_logger.LogDebug("Xray-Core 进程已就绪, 开始测试延迟...");
|
||||
@@ -126,14 +122,10 @@ public class OutboundMonitor
|
||||
outboundSetting.Name, outboundSetting.GetTag(), avgLatency,
|
||||
outboundSetting.Address);
|
||||
}
|
||||
finally
|
||||
{
|
||||
outboundTestProcess.Kill();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "测试代理 {OutboundSettingName} ({OutboundSettingTag}) 时发生错误, 跳过代理, 地址: {OutboundSettingAddress}",
|
||||
_logger.LogError(e,
|
||||
"测试代理 {OutboundSettingName} ({OutboundSettingTag}) 时发生错误, 跳过代理, 地址: {OutboundSettingAddress}",
|
||||
outboundSetting.Name, outboundSetting.GetTag(), outboundSetting.Address);
|
||||
}
|
||||
finally
|
||||
@@ -155,12 +147,13 @@ public class OutboundMonitor
|
||||
|
||||
// 展示延迟前10的节点名称和延迟
|
||||
var index = 1;
|
||||
var latenciesRanking = "---------------- 延迟排名 ----------------\n";
|
||||
var latenciesRanking = "";
|
||||
sortedOutboundSettings
|
||||
.Take(10)
|
||||
.ToList()
|
||||
.ForEach(kv => latenciesRanking += $"{index++}. {kv.Key.Name} ({kv.Key.GetTag()}) [{kv.Key.Address}]: {kv.Value}ms\n");
|
||||
_logger.LogInformation(latenciesRanking);
|
||||
.ForEach(kv =>
|
||||
latenciesRanking += $"{index++}. {kv.Key.Name} ({kv.Key.GetTag()}) [{kv.Key.Address}]: {kv.Value}ms\n");
|
||||
_logger.LogInformation("---------------- 延迟排名 ----------------\n{latenciesRanking}", latenciesRanking);
|
||||
|
||||
var bestOutbound = sortedOutboundSettings
|
||||
.FirstOrDefault().Key;
|
||||
@@ -177,11 +170,6 @@ public class OutboundMonitor
|
||||
_logger.LogWarning("没有可用的代理.");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "重新选择代理时发生错误.");
|
||||
}
|
||||
}
|
||||
|
||||
private WebProxy GetWebProxyFromConfig()
|
||||
{
|
||||
@@ -248,7 +236,8 @@ public class OutboundMonitor
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<(long[] latencies, long[] bandwidths)> MeasureOutbound(ushort port, bool withBandwidth = false, CancellationToken cancellationToken = default)
|
||||
private static async Task<(long[] latencies, long[] bandwidths)> MeasureOutbound(ushort port,
|
||||
bool withBandwidth = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var proxy = new WebProxy(new Uri($"http://localhost:{port}"));
|
||||
|
||||
@@ -299,7 +288,8 @@ public class OutboundMonitor
|
||||
return latencies;
|
||||
}
|
||||
|
||||
private static async Task<long[]> MeasureHttpBandwidth(IWebProxy proxy, CancellationToken cancellationToken = default)
|
||||
private static async Task<long[]> MeasureHttpBandwidth(IWebProxy proxy,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var httpClient = new HttpClient(new HttpClientHandler
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user