Compare commits

..

1 commit

Author SHA1 Message Date
fonaix
f6dd2178e7
Merge e45c1ddb36 into 4d2eb324f1 2024-12-05 07:09:33 +03:00
4 changed files with 78 additions and 85 deletions

View file

@ -1029,11 +1029,12 @@ namespace ServiceLib.Handler
public static async Task<ProfileItem?> GetPreSocksItem(Config config, ProfileItem node, ECoreType coreType) public static async Task<ProfileItem?> GetPreSocksItem(Config config, ProfileItem node, ECoreType coreType)
{ {
ProfileItem? itemSocks = null; ProfileItem? itemSocks = null;
var preCoreType = ECoreType.sing_box;
if (node.ConfigType != EConfigType.Custom && coreType != ECoreType.sing_box && config.TunModeItem.EnableTun) if (node.ConfigType != EConfigType.Custom && coreType != ECoreType.sing_box && config.TunModeItem.EnableTun)
{ {
itemSocks = new ProfileItem() itemSocks = new ProfileItem()
{ {
CoreType = ECoreType.sing_box, CoreType = preCoreType,
ConfigType = EConfigType.SOCKS, ConfigType = EConfigType.SOCKS,
Address = Global.Loopback, Address = Global.Loopback,
Sni = node.Address, //Tun2SocksAddress Sni = node.Address, //Tun2SocksAddress
@ -1042,7 +1043,7 @@ namespace ServiceLib.Handler
} }
else if ((node.ConfigType == EConfigType.Custom && node.PreSocksPort > 0)) else if ((node.ConfigType == EConfigType.Custom && node.PreSocksPort > 0))
{ {
var preCoreType = config.RunningCoreType = config.TunModeItem.EnableTun ? ECoreType.sing_box : ECoreType.Xray; preCoreType = config.TunModeItem.EnableTun ? ECoreType.sing_box : ECoreType.Xray;
itemSocks = new ProfileItem() itemSocks = new ProfileItem()
{ {
CoreType = preCoreType, CoreType = preCoreType,

View file

@ -174,7 +174,7 @@ namespace ServiceLib.Handler
var itemSocks = await ConfigHandler.GetPreSocksItem(_config, node, coreType); var itemSocks = await ConfigHandler.GetPreSocksItem(_config, node, coreType);
if (itemSocks != null) if (itemSocks != null)
{ {
var preCoreType = itemSocks.CoreType ?? ECoreType.sing_box; var preCoreType = _config.RunningCoreType = itemSocks.CoreType ?? ECoreType.sing_box;
var fileName = Utils.GetConfigPath(Global.CorePreConfigFileName); var fileName = Utils.GetConfigPath(Global.CorePreConfigFileName);
var result = await CoreConfigHandler.GenerateClientConfig(itemSocks, fileName); var result = await CoreConfigHandler.GenerateClientConfig(itemSocks, fileName);
if (result.Success) if (result.Success)

View file

@ -349,20 +349,20 @@ namespace ServiceLib.Models
public string? path { get; set; } public string? path { get; set; }
public string? host { get; set; } public string? host { get; set; }
public string? mode { get; set; } public string? mode { get; set; }
public object? scMaxEachPostBytes { get; set; } public string? scMaxEachPostBytes { get; set; }
public object? scMaxConcurrentPosts { get; set; } public string? scMaxConcurrentPosts { get; set; }
public object? scMinPostsIntervalMs { get; set; } public string? scMinPostsIntervalMs { get; set; }
//public Xmux4Ray? xmux { get; set; } public Xmux4Ray? xmux { get; set; }
public object? extra { get; set; } public object? extra { get; set; }
} }
//public class Xmux4Ray public class Xmux4Ray
//{ {
// public object? maxConcurrency { get; set; } public int? maxConcurrency { get; set; }
// public object? maxConnections { get; set; } public int? maxConnections { get; set; }
// public object? cMaxReuseTimes { get; set; } public int? cMaxReuseTimes { get; set; }
// public object? cMaxLifetimeMs { get; set; } public int? cMaxLifetimeMs { get; set; }
//} }
public class HttpSettings4Ray public class HttpSettings4Ray
{ {

View file

@ -8,16 +8,19 @@ namespace ServiceLib.Services
public class SpeedtestService public class SpeedtestService
{ {
private Config? _config; private Config? _config;
private List<ServerTestItem> _selecteds;
private ESpeedActionType _actionType;
private Action<SpeedTestResult>? _updateFunc; private Action<SpeedTestResult>? _updateFunc;
private bool _exitLoop = false; private bool _exitLoop = false;
public SpeedtestService(Config config, List<ProfileItem> selecteds, ESpeedActionType actionType, Action<SpeedTestResult> updateFunc) public SpeedtestService(Config config, List<ProfileItem> selecteds, ESpeedActionType actionType, Action<SpeedTestResult> updateFunc)
{ {
_config = config; _config = config;
_actionType = actionType;
_updateFunc = updateFunc; _updateFunc = updateFunc;
var lstSelected = new List<ServerTestItem>(); _selecteds = new List<ServerTestItem>();
foreach (var it in selecteds) foreach (var it in selecteds)
{ {
if (it.ConfigType == EConfigType.Custom) if (it.ConfigType == EConfigType.Custom)
@ -28,7 +31,7 @@ namespace ServiceLib.Services
{ {
continue; continue;
} }
lstSelected.Add(new ServerTestItem() _selecteds.Add(new ServerTestItem()
{ {
IndexId = it.IndexId, IndexId = it.IndexId,
Address = it.Address, Address = it.Address,
@ -36,9 +39,8 @@ namespace ServiceLib.Services
ConfigType = it.ConfigType ConfigType = it.ConfigType
}); });
} }
//clear test result //clear test result
foreach (var it in lstSelected) foreach (var it in _selecteds)
{ {
switch (actionType) switch (actionType)
{ {
@ -61,44 +63,25 @@ namespace ServiceLib.Services
} }
} }
switch (actionType)
{
case ESpeedActionType.Tcping:
Task.Run(RunTcping);
break;
case ESpeedActionType.Realping:
Task.Run(RunRealPing);
break;
case ESpeedActionType.Speedtest:
Task.Run(RunSpeedTestAsync);
break;
case ESpeedActionType.Mixedtest:
Task.Run(RunMixedtestAsync);
break;
}
MessageBus.Current.Listen<string>(EMsgCommand.StopSpeedtest.ToString()).Subscribe(ExitLoop); MessageBus.Current.Listen<string>(EMsgCommand.StopSpeedtest.ToString()).Subscribe(ExitLoop);
Task.Run(async () => { await RunAsync(actionType, lstSelected); });
}
private async Task RunAsync(ESpeedActionType actionType, List<ServerTestItem> lstSelected)
{
if (actionType == ESpeedActionType.Tcping)
{
await RunTcpingAsync(lstSelected);
return;
}
List<List<ServerTestItem>> lstTest = new();
lstTest.Add(lstSelected.Where(t => t.ConfigType is not (EConfigType.Hysteria2 or EConfigType.TUIC or EConfigType.WireGuard)).ToList());
lstTest.Add(lstSelected.Where(t => t.ConfigType is EConfigType.Hysteria2 or EConfigType.TUIC or EConfigType.WireGuard).ToList());
foreach (var lst in lstTest)
{
switch (actionType)
{
case ESpeedActionType.Realping:
await RunRealPingAsync(lst);
break;
case ESpeedActionType.Speedtest:
await RunSpeedTestAsync(lst);
break;
case ESpeedActionType.Mixedtest:
await RunMixedTestAsync(lst);
break;
}
await Task.Delay(1000);
}
UpdateFunc("", ResUI.SpeedtestingCompleted);
} }
private void ExitLoop(string x) private void ExitLoop(string x)
@ -108,12 +91,12 @@ namespace ServiceLib.Services
UpdateFunc("", ResUI.SpeedtestingStop); UpdateFunc("", ResUI.SpeedtestingStop);
} }
private async Task RunTcpingAsync(List<ServerTestItem> selecteds) private async Task RunTcping()
{ {
try try
{ {
List<Task> tasks = []; List<Task> tasks = [];
foreach (var it in selecteds) foreach (var it in _selecteds)
{ {
if (it.ConfigType == EConfigType.Custom) if (it.ConfigType == EConfigType.Custom)
{ {
@ -123,7 +106,7 @@ namespace ServiceLib.Services
{ {
try try
{ {
var time = await GetTcpingTime(it.Address, it.Port); int time = await GetTcpingTime(it.Address, it.Port);
var output = FormatOut(time, Global.DelayUnit); var output = FormatOut(time, Global.DelayUnit);
ProfileExHandler.Instance.SetTestDelay(it.IndexId, output); ProfileExHandler.Instance.SetTestDelay(it.IndexId, output);
@ -147,22 +130,24 @@ namespace ServiceLib.Services
} }
} }
private async Task RunRealPingAsync(List<ServerTestItem> selecteds) private async Task RunRealPing()
{ {
var pid = -1; int pid = -1;
try try
{ {
pid = await CoreHandler.Instance.LoadCoreConfigSpeedtest(selecteds); string msg = string.Empty;
pid = await CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds);
if (pid < 0) if (pid < 0)
{ {
UpdateFunc("", ResUI.FailedToRunCore); UpdateFunc("", ResUI.FailedToRunCore);
return; return;
} }
var downloadHandle = new DownloadService(); DownloadService downloadHandle = new DownloadService();
List<Task> tasks = new(); List<Task> tasks = new();
foreach (var it in selecteds) foreach (var it in _selecteds)
{ {
if (!it.AllowTest) if (!it.AllowTest)
{ {
@ -177,11 +162,11 @@ namespace ServiceLib.Services
try try
{ {
WebProxy webProxy = new(Global.Loopback, it.Port); WebProxy webProxy = new(Global.Loopback, it.Port);
var output = await GetRealPingTime(downloadHandle, webProxy); string output = await GetRealPingTime(downloadHandle, webProxy);
ProfileExHandler.Instance.SetTestDelay(it.IndexId, output); ProfileExHandler.Instance.SetTestDelay(it.IndexId, output);
UpdateFunc(it.IndexId, output); UpdateFunc(it.IndexId, output);
int.TryParse(output, out var delay); int.TryParse(output, out int delay);
it.Delay = delay; it.Delay = delay;
} }
catch (Exception ex) catch (Exception ex)
@ -206,22 +191,27 @@ namespace ServiceLib.Services
} }
} }
private async Task RunSpeedTestAsync(List<ServerTestItem> selecteds) private async Task RunSpeedTestAsync()
{ {
var pid = -1; int pid = -1;
pid = await CoreHandler.Instance.LoadCoreConfigSpeedtest(selecteds); //if (_actionType == ESpeedActionType.Mixedtest)
//{
// _selecteds = _selecteds.OrderBy(t => t.delay).ToList();
//}
pid = await CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds);
if (pid < 0) if (pid < 0)
{ {
UpdateFunc("", ResUI.FailedToRunCore); UpdateFunc("", ResUI.FailedToRunCore);
return; return;
} }
var url = _config.SpeedTestItem.SpeedTestUrl; string url = _config.SpeedTestItem.SpeedTestUrl;
var timeout = _config.SpeedTestItem.SpeedTestTimeout; var timeout = _config.SpeedTestItem.SpeedTestTimeout;
DownloadService downloadHandle = new(); DownloadService downloadHandle = new();
foreach (var it in selecteds) foreach (var it in _selecteds)
{ {
if (_exitLoop) if (_exitLoop)
{ {
@ -251,7 +241,7 @@ namespace ServiceLib.Services
await downloadHandle.DownloadDataAsync(url, webProxy, timeout, (success, msg) => await downloadHandle.DownloadDataAsync(url, webProxy, timeout, (success, msg) =>
{ {
decimal.TryParse(msg, out var dec); decimal.TryParse(msg, out decimal dec);
if (dec > 0) if (dec > 0)
{ {
ProfileExHandler.Instance.SetTestSpeed(it.IndexId, msg); ProfileExHandler.Instance.SetTestSpeed(it.IndexId, msg);
@ -264,25 +254,26 @@ namespace ServiceLib.Services
{ {
await CoreHandler.Instance.CoreStopPid(pid); await CoreHandler.Instance.CoreStopPid(pid);
} }
UpdateFunc("", ResUI.SpeedtestingCompleted);
await ProfileExHandler.Instance.SaveTo(); await ProfileExHandler.Instance.SaveTo();
} }
private async Task RunSpeedTestMulti(List<ServerTestItem> selecteds) private async Task RunSpeedTestMulti()
{ {
var pid = -1; int pid = -1;
pid = await CoreHandler.Instance.LoadCoreConfigSpeedtest(selecteds); pid = await CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds);
if (pid < 0) if (pid < 0)
{ {
UpdateFunc("", ResUI.FailedToRunCore); UpdateFunc("", ResUI.FailedToRunCore);
return; return;
} }
var url = _config.SpeedTestItem.SpeedTestUrl; string url = _config.SpeedTestItem.SpeedTestUrl;
var timeout = _config.SpeedTestItem.SpeedTestTimeout; var timeout = _config.SpeedTestItem.SpeedTestTimeout;
DownloadService downloadHandle = new(); DownloadService downloadHandle = new();
foreach (var it in selecteds) foreach (var it in _selecteds)
{ {
if (_exitLoop) if (_exitLoop)
{ {
@ -312,7 +303,7 @@ namespace ServiceLib.Services
WebProxy webProxy = new(Global.Loopback, it.Port); WebProxy webProxy = new(Global.Loopback, it.Port);
_ = downloadHandle.DownloadDataAsync(url, webProxy, timeout, (success, msg) => _ = downloadHandle.DownloadDataAsync(url, webProxy, timeout, (success, msg) =>
{ {
decimal.TryParse(msg, out var dec); decimal.TryParse(msg, out decimal dec);
if (dec > 0) if (dec > 0)
{ {
ProfileExHandler.Instance.SetTestSpeed(it.IndexId, msg); ProfileExHandler.Instance.SetTestSpeed(it.IndexId, msg);
@ -328,34 +319,35 @@ namespace ServiceLib.Services
{ {
await CoreHandler.Instance.CoreStopPid(pid); await CoreHandler.Instance.CoreStopPid(pid);
} }
UpdateFunc("", ResUI.SpeedtestingCompleted);
await ProfileExHandler.Instance.SaveTo(); await ProfileExHandler.Instance.SaveTo();
} }
private async Task RunMixedTestAsync(List<ServerTestItem> selecteds) private async Task RunMixedtestAsync()
{ {
await RunRealPingAsync(selecteds); await RunRealPing();
await Task.Delay(1000); await Task.Delay(1000);
await RunSpeedTestMulti(selecteds); await RunSpeedTestMulti();
} }
private async Task<string> GetRealPingTime(DownloadService downloadHandle, IWebProxy webProxy) private async Task<string> GetRealPingTime(DownloadService downloadHandle, IWebProxy webProxy)
{ {
var responseTime = await downloadHandle.GetRealPingTime(_config.SpeedTestItem.SpeedPingTestUrl, webProxy, 10); int responseTime = await downloadHandle.GetRealPingTime(_config.SpeedTestItem.SpeedPingTestUrl, webProxy, 10);
//string output = Utile.IsNullOrEmpty(status) ? FormatOut(responseTime, "ms") : status; //string output = Utile.IsNullOrEmpty(status) ? FormatOut(responseTime, "ms") : status;
return FormatOut(responseTime, Global.DelayUnit); return FormatOut(responseTime, Global.DelayUnit);
} }
private async Task<int> GetTcpingTime(string url, int port) private async Task<int> GetTcpingTime(string url, int port)
{ {
var responseTime = -1; int responseTime = -1;
try try
{ {
if (!IPAddress.TryParse(url, out var ipAddress)) if (!IPAddress.TryParse(url, out IPAddress? ipAddress))
{ {
var ipHostInfo = await Dns.GetHostEntryAsync(url); IPHostEntry ipHostInfo = Dns.GetHostEntry(url);
ipAddress = ipHostInfo.AddressList.First(); ipAddress = ipHostInfo.AddressList.First();
} }