diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index 8f5d0b15..c689e6d2 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -20,6 +20,7 @@ public const string tuicCoreUrl = "https://github.com/EAimTY/tuic/releases"; public const string singboxCoreUrl = "https://github.com/SagerNet/sing-box/releases"; public const string geoUrl = "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/{0}.dat"; + public const string singboxGeoUrl = "https://github.com/soffchen/sing-{0}/releases/latest/download/{0}.db"; public const string SpeedPingTestUrl = @"https://www.google.com/generate_204"; public const string CustomRoutingListUrl = @"https://raw.githubusercontent.com/2dust/v2rayCustomRoutingList/master/"; diff --git a/v2rayN/v2rayN/Handler/DownloadHandle.cs b/v2rayN/v2rayN/Handler/DownloadHandle.cs index 66ef31ba..43c54589 100644 --- a/v2rayN/v2rayN/Handler/DownloadHandle.cs +++ b/v2rayN/v2rayN/Handler/DownloadHandle.cs @@ -62,12 +62,12 @@ namespace v2rayN.Handler return 0; } - public void DownloadFileAsync(string url, bool blProxy, int downloadTimeout) + public async Task DownloadFileAsync(string url, bool blProxy, int downloadTimeout) { try { Utils.SetSecurityProtocol(LazyConfig.Instance.GetConfig().guiItem.enableSecurityProtocolTls13); - UpdateCompleted?.Invoke(this, new ResultEventArgs(false, ResUI.Downloading)); + UpdateCompleted?.Invoke(this, new ResultEventArgs(false, $"{ResUI.Downloading} {url}")); var progress = new Progress(); progress.ProgressChanged += (sender, value) => @@ -76,7 +76,7 @@ namespace v2rayN.Handler }; var webProxy = GetWebProxy(blProxy); - _ = DownloaderHelper.Instance.DownloadFileAsync(webProxy, + await DownloaderHelper.Instance.DownloadFileAsync(webProxy, url, Utils.GetTempPath(Utils.GetDownloadFileName(url)), progress, diff --git a/v2rayN/v2rayN/Handler/MainFormHandler.cs b/v2rayN/v2rayN/Handler/MainFormHandler.cs index ddb4612a..bae28515 100644 --- a/v2rayN/v2rayN/Handler/MainFormHandler.cs +++ b/v2rayN/v2rayN/Handler/MainFormHandler.cs @@ -206,19 +206,10 @@ namespace v2rayN.Handler { if ((dtNow - autoUpdateGeoTime).Hours % config.guiItem.autoUpdateInterval == 0) { - updateHandle.UpdateGeoFile("geosite", config, (bool success, string msg) => + updateHandle.UpdateGeoFileAll(config, (bool success, string msg) => { - update(false, msg); - if (success) - Utils.SaveLog("geosite" + msg); - }); - - updateHandle.UpdateGeoFile("geoip", config, (bool success, string msg) => - { - update(false, msg); - if (success) - Utils.SaveLog("geoip" + msg); - }); + update(false, msg); + }); autoUpdateGeoTime = dtNow; } } diff --git a/v2rayN/v2rayN/Handler/UpdateHandle.cs b/v2rayN/v2rayN/Handler/UpdateHandle.cs index 01b282fc..02427a48 100644 --- a/v2rayN/v2rayN/Handler/UpdateHandle.cs +++ b/v2rayN/v2rayN/Handler/UpdateHandle.cs @@ -84,7 +84,7 @@ namespace v2rayN.Handler _updateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, "v2rayN")); url = args.Msg; - askToDownload(downloadHandle, url, true); + _ = askToDownload(downloadHandle, url, true); } else { @@ -135,7 +135,7 @@ namespace v2rayN.Handler { _updateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, "Core")); url = args.Msg; - askToDownload(downloadHandle, url, true); + _ = askToDownload(downloadHandle, url, true); } else { @@ -279,51 +279,16 @@ namespace v2rayN.Handler }); } - public void UpdateGeoFile(string geoName, Config config, Action update) + public void UpdateGeoFileAll(Config config, Action update) { - _config = config; - _updateFunc = update; - var url = string.Format(Global.geoUrl, geoName); - - DownloadHandle downloadHandle = new(); - downloadHandle.UpdateCompleted += (sender2, args) => + Task.Run(async () => { - if (args.Success) - { - _updateFunc(false, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, geoName)); + await UpdateGeoFile("geosite", _config, update); + await UpdateGeoFile("geoip", _config, update); - try - { - string fileName = Utils.GetTempPath(Utils.GetDownloadFileName(url)); - if (File.Exists(fileName)) - { - //Global.coreTypes.ForEach(it => - //{ - // string targetPath = Utils.GetBinPath($"{geoName}.dat", (ECoreType)Enum.Parse(typeof(ECoreType), it)); - // File.Copy(fileName, targetPath, true); - //}); - string targetPath = Utils.GetBinPath($"{geoName}.dat"); - File.Copy(fileName, targetPath, true); - - File.Delete(fileName); - //_updateFunc(true, ""); - } - } - catch (Exception ex) - { - _updateFunc(false, ex.Message); - } - } - else - { - _updateFunc(false, args.Msg); - } - }; - downloadHandle.Error += (sender2, args) => - { - _updateFunc(false, args.GetException().Message); - }; - askToDownload(downloadHandle, url, false); + await UpdateGeoFile4Singbox("geosite", _config, update); + await UpdateGeoFile4Singbox("geoip", _config, update); + }); } public void RunAvailabilityCheck(Action update) @@ -570,7 +535,7 @@ namespace v2rayN.Handler } } - private void askToDownload(DownloadHandle downloadHandle, string url, bool blAsk) + private async Task askToDownload(DownloadHandle downloadHandle, string url, bool blAsk) { bool blDownload = false; if (blAsk) @@ -586,10 +551,98 @@ namespace v2rayN.Handler } if (blDownload) { - downloadHandle.DownloadFileAsync(url, true, 600); + await downloadHandle.DownloadFileAsync(url, true, 600); } } + private async Task UpdateGeoFile(string geoName, Config config, Action update) + { + _config = config; + _updateFunc = update; + var url = string.Format(Global.geoUrl, geoName); + + DownloadHandle downloadHandle = new(); + downloadHandle.UpdateCompleted += (sender2, args) => + { + if (args.Success) + { + _updateFunc(false, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, geoName)); + + try + { + string fileName = Utils.GetTempPath(Utils.GetDownloadFileName(url)); + if (File.Exists(fileName)) + { + //Global.coreTypes.ForEach(it => + //{ + // string targetPath = Utils.GetBinPath($"{geoName}.dat", (ECoreType)Enum.Parse(typeof(ECoreType), it)); + // File.Copy(fileName, targetPath, true); + //}); + string targetPath = Utils.GetBinPath($"{geoName}.dat"); + File.Copy(fileName, targetPath, true); + + File.Delete(fileName); + //_updateFunc(true, ""); + } + } + catch (Exception ex) + { + _updateFunc(false, ex.Message); + } + } + else + { + _updateFunc(false, args.Msg); + } + }; + downloadHandle.Error += (sender2, args) => + { + _updateFunc(false, args.GetException().Message); + }; + await askToDownload(downloadHandle, url, false); + } + + private async Task UpdateGeoFile4Singbox(string geoName, Config config, Action update) + { + _config = config; + _updateFunc = update; + var url = string.Format(Global.singboxGeoUrl, geoName); + + DownloadHandle downloadHandle = new(); + downloadHandle.UpdateCompleted += (sender2, args) => + { + if (args.Success) + { + _updateFunc(false, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, geoName)); + + try + { + string fileName = Utils.GetTempPath(Utils.GetDownloadFileName(url)); + if (File.Exists(fileName)) + { + string targetPath = Utils.GetConfigPath($"{geoName}.db"); + File.Copy(fileName, targetPath, true); + + File.Delete(fileName); + } + } + catch (Exception ex) + { + _updateFunc(false, ex.Message); + } + } + else + { + _updateFunc(false, args.Msg); + } + }; + downloadHandle.Error += (sender2, args) => + { + _updateFunc(false, args.GetException().Message); + }; + await askToDownload(downloadHandle, url, false); + } + #endregion private } } \ No newline at end of file diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index a02b67d0..16c517d6 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -1460,12 +1460,7 @@ namespace v2rayN.ViewModels private void CheckUpdateGeo() { - Task.Run(() => - { - var updateHandle = new UpdateHandle(); - updateHandle.UpdateGeoFile("geosite", _config, UpdateTaskHandler); - updateHandle.UpdateGeoFile("geoip", _config, UpdateTaskHandler); - }); + (new UpdateHandle()).UpdateGeoFileAll(_config, UpdateTaskHandler); } #endregion CheckUpdate