Update RetResult

This commit is contained in:
2dust 2024-10-21 18:18:41 +08:00
parent 3dd54312e7
commit 5ea6b1e08a
10 changed files with 64 additions and 77 deletions

View file

@ -1009,7 +1009,7 @@ namespace ServiceLib.Handler
var configPath = Utils.GetConfigPath(Global.CoreMultipleLoadConfigFileName);
var result = await CoreConfigHandler.GenerateClientMultipleLoadConfig(config, configPath, selecteds, coreType);
if (result.Code != 0)
if (result.Success != true)
{
return result;
}
@ -1642,7 +1642,7 @@ namespace ServiceLib.Handler
public static async Task<int> InitExternalRouting(Config config, bool blImportAdvancedRules = false)
{
var downloadHandle = new DownloadService();
var templateContent = Task.Run(() => downloadHandle.TryDownloadString(config.constItem.routeRulesTemplateSourceUrl, false, "")).Result;
var templateContent = await downloadHandle.TryDownloadString(config.constItem.routeRulesTemplateSourceUrl, true, "");
if (string.IsNullOrEmpty(templateContent))
return await InitBuiltinRouting(config, blImportAdvancedRules); // fallback
@ -1665,7 +1665,7 @@ namespace ServiceLib.Handler
var ruleSetsString = !string.IsNullOrEmpty(item.ruleSet)
? item.ruleSet
: Task.Run(() => downloadHandle.TryDownloadString(item.url, false, "")).Result;
: await downloadHandle.TryDownloadString(item.url, true, "");
if (string.IsNullOrEmpty(ruleSetsString))
continue;
@ -1795,7 +1795,7 @@ namespace ServiceLib.Handler
var currentItem = await AppHandler.Instance.GetDNSItem(type);
var downloadHandle = new DownloadService();
var templateContent = Task.Run(() => downloadHandle.TryDownloadString(url, true, "")).Result;
var templateContent = await downloadHandle.TryDownloadString(url, true, "");
if (string.IsNullOrEmpty(templateContent))
return currentItem;
@ -1804,10 +1804,10 @@ namespace ServiceLib.Handler
return currentItem;
if (!string.IsNullOrEmpty(template.normalDNS))
template.normalDNS = Task.Run(() => downloadHandle.TryDownloadString(template.normalDNS, true, "")).Result;
template.normalDNS = await downloadHandle.TryDownloadString(template.normalDNS, true, "");
if (!string.IsNullOrEmpty(template.tunDNS))
template.tunDNS = Task.Run(() => downloadHandle.TryDownloadString(template.tunDNS, true, "")).Result;
template.tunDNS = await downloadHandle.TryDownloadString(template.tunDNS, true, "");
template.id = currentItem.id;
template.enabled = currentItem.enabled;

View file

@ -8,7 +8,7 @@
public static async Task<RetResult> GenerateClientConfig(ProfileItem node, string? fileName)
{
var config = AppHandler.Instance.Config;
var result = new RetResult(-1);
var result = new RetResult();
if (node.configType == EConfigType.Custom)
{
@ -33,7 +33,7 @@
{
result = await new CoreConfigV2rayService(config).GenerateClientConfigContent(node);
}
if (result.Code != 0)
if (result.Success != true)
{
return result;
}
@ -47,7 +47,7 @@
private static async Task<RetResult> GenerateClientCustomConfig(ProfileItem node, string? fileName)
{
var ret = new RetResult(-1);
var ret = new RetResult();
try
{
if (node == null || fileName is null)
@ -83,7 +83,7 @@
}
ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
ret.Code = 0;
ret.Success = true;
return ret;
}
catch (Exception ex)
@ -96,7 +96,7 @@
public static async Task<RetResult> GenerateClientSpeedtestConfig(Config config, string fileName, List<ServerTestItem> selecteds, ECoreType coreType)
{
var result = new RetResult(-1);
var result = new RetResult();
if (coreType == ECoreType.sing_box)
{
result = await new CoreConfigSingboxService(config).GenerateClientSpeedtestConfig(selecteds);
@ -105,7 +105,7 @@
{
result = await new CoreConfigV2rayService(config).GenerateClientSpeedtestConfig(selecteds);
}
if (result.Code != 0)
if (result.Success != true)
{
return result;
}
@ -115,7 +115,7 @@
public static async Task<RetResult> GenerateClientMultipleLoadConfig(Config config, string fileName, List<ProfileItem> selecteds, ECoreType coreType)
{
var result = new RetResult(-1);
var result = new RetResult();
if (coreType == ECoreType.sing_box)
{
result = await new CoreConfigSingboxService(config).GenerateClientMultipleLoadConfig(selecteds);
@ -125,7 +125,7 @@
result = await new CoreConfigV2rayService(config).GenerateClientMultipleLoadConfig(selecteds);
}
if (result.Code != 0)
if (result.Success != true)
{
return result;
}

View file

@ -35,7 +35,7 @@ namespace ServiceLib.Handler
var fileName = Utils.GetConfigPath(Global.CoreConfigFileName);
var result = await CoreConfigHandler.GenerateClientConfig(node, fileName);
ShowMsg(false, result.Msg);
if (result.Code != 0)
if (result.Success != true)
{
return;
}
@ -72,7 +72,7 @@ namespace ServiceLib.Handler
var configPath = Utils.GetConfigPath(Global.CoreSpeedtestConfigFileName);
var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType);
ShowMsg(false, result.Msg);
if (result.Code == 0)
if (result.Success)
{
pid = CoreStartSpeedtest(configPath, coreType);
}
@ -225,7 +225,7 @@ namespace ServiceLib.Handler
{
string fileName2 = Utils.GetConfigPath(Global.CorePreConfigFileName);
var result = await CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2);
if (result.Code == 0)
if (result.Success)
{
var coreInfo2 = CoreInfoHandler.Instance.GetCoreInfo(preCoreType);
var proc2 = RunProcess(node, coreInfo2, $" -c {Global.CorePreConfigFileName}", true);

View file

@ -2,13 +2,26 @@
{
public class RetResult
{
public int Code { get; set; }
public bool Success { get; set; }
public string? Msg { get; set; }
public object? Data { get; set; }
public RetResult(int code)
public RetResult(bool success = false)
{
Code = code;
Success = success;
}
public RetResult(bool success, string? msg)
{
Success = success;
Msg = msg;
}
public RetResult(bool success, string? msg, object? data)
{
Success = success;
Msg = msg;
Data = data;
}
}
}

View file

@ -21,7 +21,7 @@
/// <returns></returns>
public async Task<RetResult> GenerateClientCustomConfig(ProfileItem node, string? fileName)
{
var ret = new RetResult(-1);
var ret = new RetResult();
if (node == null || fileName is null)
{
ret.Msg = ResUI.CheckServerSettings;
@ -148,7 +148,7 @@
ClashApiHandler.Instance.ProfileContent = fileContent;
ret.Msg = string.Format(ResUI.SuccessfulConfiguration, $"{node.GetSummary()}");
ret.Code = 0;
ret.Success = true;
return ret;
}
catch (Exception ex)

View file

@ -17,7 +17,7 @@ namespace ServiceLib.Services.CoreConfig
public async Task<RetResult> GenerateClientConfigContent(ProfileItem node)
{
var ret = new RetResult(-1);
var ret = new RetResult();
try
{
if (node == null
@ -65,7 +65,7 @@ namespace ServiceLib.Services.CoreConfig
await ConvertGeo2Ruleset(singboxConfig);
ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
ret.Code = 0;
ret.Success = true;
ret.Data = JsonUtils.Serialize(singboxConfig);
return ret;
}
@ -79,7 +79,7 @@ namespace ServiceLib.Services.CoreConfig
public async Task<RetResult> GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds)
{
var ret = new RetResult(-1);
var ret = new RetResult();
try
{
if (_config == null)
@ -229,7 +229,7 @@ namespace ServiceLib.Services.CoreConfig
//}
//ret.Msg =string.Format(ResUI.SuccessfulConfiguration"), node.getSummary());
ret.Code = 0;
ret.Success = true;
ret.Data = JsonUtils.Serialize(singboxConfig);
return ret;
}
@ -243,7 +243,7 @@ namespace ServiceLib.Services.CoreConfig
public async Task<RetResult> GenerateClientMultipleLoadConfig(List<ProfileItem> selecteds)
{
var ret = new RetResult(-1);
var ret = new RetResult();
try
{
if (_config == null)
@ -345,7 +345,7 @@ namespace ServiceLib.Services.CoreConfig
outSelector.outbounds.Insert(0, outUrltest.tag);
singboxConfig.outbounds.Add(outSelector);
ret.Code = 0;
ret.Success = true;
ret.Data = JsonUtils.Serialize(singboxConfig);
return ret;
}
@ -359,7 +359,7 @@ namespace ServiceLib.Services.CoreConfig
public async Task<RetResult> GenerateClientCustomConfig(ProfileItem node, string? fileName)
{
var ret = new RetResult(-1);
var ret = new RetResult();
if (node == null || fileName is null)
{
ret.Msg = ResUI.CheckServerSettings;
@ -425,7 +425,7 @@ namespace ServiceLib.Services.CoreConfig
}
ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
ret.Code = 0;
ret.Success = true;
return ret;
}
catch (Exception ex)

View file

@ -17,7 +17,7 @@ namespace ServiceLib.Services.CoreConfig
public async Task<RetResult> GenerateClientConfigContent(ProfileItem node)
{
var ret = new RetResult(-1);
var ret = new RetResult();
try
{
if (node == null
@ -58,7 +58,7 @@ namespace ServiceLib.Services.CoreConfig
await GenStatistic(v2rayConfig);
ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
ret.Code = 0;
ret.Success = true;
ret.Data = JsonUtils.Serialize(v2rayConfig);
return ret;
}
@ -72,7 +72,7 @@ namespace ServiceLib.Services.CoreConfig
public async Task<RetResult> GenerateClientMultipleLoadConfig(List<ProfileItem> selecteds)
{
var ret = new RetResult(-1);
var ret = new RetResult();
try
{
@ -185,7 +185,7 @@ namespace ServiceLib.Services.CoreConfig
});
}
ret.Code = 0;
ret.Success = true;
ret.Data = JsonUtils.Serialize(v2rayConfig);
return ret;
}
@ -199,7 +199,7 @@ namespace ServiceLib.Services.CoreConfig
public async Task<RetResult> GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds)
{
var ret = new RetResult(-1);
var ret = new RetResult();
try
{
if (_config == null)
@ -336,7 +336,7 @@ namespace ServiceLib.Services.CoreConfig
}
//ret.Msg =string.Format(ResUI.SuccessfulConfiguration"), node.getSummary());
ret.Code = 0;
ret.Success = true;
ret.Data = JsonUtils.Serialize(v2rayConfig);
return ret;
}

View file

@ -10,22 +10,10 @@ namespace ServiceLib.Services
/// </summary>
public class DownloadService
{
public event EventHandler<ResultEventArgs>? UpdateCompleted;
public event EventHandler<RetResult>? UpdateCompleted;
public event ErrorEventHandler? Error;
public class ResultEventArgs : EventArgs
{
public bool Success;
public string Msg;
public ResultEventArgs(bool success, string msg)
{
Success = success;
Msg = msg;
}
}
public async Task<int> DownloadDataAsync(string url, WebProxy webProxy, int downloadTimeout, Action<bool, string> updateFunc)
{
try
@ -63,12 +51,12 @@ namespace ServiceLib.Services
try
{
SetSecurityProtocol(AppHandler.Instance.Config.guiItem.enableSecurityProtocolTls13);
UpdateCompleted?.Invoke(this, new ResultEventArgs(false, $"{ResUI.Downloading} {url}"));
UpdateCompleted?.Invoke(this, new RetResult(false, $"{ResUI.Downloading} {url}"));
var progress = new Progress<double>();
progress.ProgressChanged += (sender, value) =>
{
UpdateCompleted?.Invoke(this, new ResultEventArgs(value > 100, $"...{value}%"));
UpdateCompleted?.Invoke(this, new RetResult(value > 100, $"...{value}%"));
};
var webProxy = GetWebProxy(blProxy);

View file

@ -9,20 +9,6 @@ namespace ServiceLib.Services
private Config _config;
private int _timeout = 30;
private class ResultEventArgs
{
public bool Success;
public string Msg;
public string Url;
public ResultEventArgs(bool success, string msg, string url = "")
{
Success = success;
Msg = msg;
Url = url;
}
}
public async Task CheckUpdateGuiN(Config config, Action<bool, string> updateFunc, bool preRelease)
{
_config = config;
@ -55,7 +41,7 @@ namespace ServiceLib.Services
_updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN));
_updateFunc?.Invoke(false, args.Msg);
url = args.Url;
url = args.Data?.ToString();
fileName = Utils.GetTempPath(Utils.GetGuid());
await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout);
}
@ -106,7 +92,7 @@ namespace ServiceLib.Services
_updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, type));
_updateFunc?.Invoke(false, args.Msg);
url = args.Url;
url = args.Data?.ToString();
var ext = url.Contains(".tar.gz") ? ".tar.gz" : Path.GetExtension(url);
fileName = Utils.GetTempPath(Utils.GetGuid() + ext);
await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout);
@ -266,7 +252,7 @@ namespace ServiceLib.Services
#region private
private async Task<ResultEventArgs> CheckUpdateAsync(DownloadService downloadHandle, ECoreType type, bool preRelease)
private async Task<RetResult> CheckUpdateAsync(DownloadService downloadHandle, ECoreType type, bool preRelease)
{
try
{
@ -280,14 +266,14 @@ namespace ServiceLib.Services
}
else
{
return new ResultEventArgs(false, "");
return new RetResult(false, "");
}
}
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
_updateFunc?.Invoke(false, ex.Message);
return new ResultEventArgs(false, ex.Message);
return new RetResult(false, ex.Message);
}
}
@ -347,7 +333,7 @@ namespace ServiceLib.Services
}
}
private async Task<ResultEventArgs> ParseDownloadUrl(ECoreType type, string gitHubReleaseApi, bool preRelease)
private async Task<RetResult> ParseDownloadUrl(ECoreType type, string gitHubReleaseApi, bool preRelease)
{
try
{
@ -398,16 +384,16 @@ namespace ServiceLib.Services
if (curVersion >= version && version != new SemanticVersion(0, 0, 0))
{
return new ResultEventArgs(false, message);
return new RetResult(false, message);
}
return new ResultEventArgs(true, body, url);
return new RetResult(true, body, url);
}
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
_updateFunc?.Invoke(false, ex.Message);
return new ResultEventArgs(false, ex.Message);
return new RetResult(false, ex.Message);
}
}

View file

@ -586,7 +586,7 @@ namespace ServiceLib.ViewModels
}
var ret = await ConfigHandler.AddCustomServer4Multiple(_config, lstSelecteds, coreType);
if (ret.Code != 0)
if (ret.Success != true)
{
NoticeHandler.Instance.Enqueue(ResUI.OperationFailed);
return;
@ -705,7 +705,7 @@ namespace ServiceLib.ViewModels
if (blClipboard)
{
var result = await CoreConfigHandler.GenerateClientConfig(item, null);
if (result.Code != 0)
if (result.Success != true)
{
NoticeHandler.Instance.Enqueue(result.Msg);
}
@ -728,7 +728,7 @@ namespace ServiceLib.ViewModels
return;
}
var result = await CoreConfigHandler.GenerateClientConfig(item, null);
if (result.Code != 0)
if (result.Success != true)
{
NoticeHandler.Instance.Enqueue(result.Msg);
}