From 055cd62dd8c71714a77b5e2a0ea18f6117945607 Mon Sep 17 00:00:00 2001 From: runetfreedom Date: Fri, 18 Oct 2024 15:33:41 +0300 Subject: [PATCH] Sing-box srs updating support (#5855) --- v2rayN/ServiceLib/Services/UpdateService.cs | 82 +++++++++++++++++++-- 1 file changed, 75 insertions(+), 7 deletions(-) diff --git a/v2rayN/ServiceLib/Services/UpdateService.cs b/v2rayN/ServiceLib/Services/UpdateService.cs index 09f103fa..4303d1fc 100644 --- a/v2rayN/ServiceLib/Services/UpdateService.cs +++ b/v2rayN/ServiceLib/Services/UpdateService.cs @@ -257,6 +257,7 @@ namespace ServiceLib.Services { await UpdateGeoFile("geosite", config, updateFunc); await UpdateGeoFile("geoip", config, updateFunc); + await UpdateSrsFileAll(config, updateFunc); _updateFunc?.Invoke(true, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, "geo")); } @@ -454,24 +455,91 @@ namespace ServiceLib.Services var geoUrl = string.IsNullOrEmpty(config?.constItem.geoSourceUrl) ? Global.GeoUrl : config.constItem.geoSourceUrl; + + var fileName = $"{geoName}.dat"; + var targetPath = Utils.GetBinPath($"{fileName}"); var url = string.Format(geoUrl, geoName); - var fileName = Utils.GetTempPath(Utils.GetGuid()); + + await DownloadGeoFile(url, fileName, targetPath, updateFunc); + } + + private async Task UpdateSrsFileAll(Config config, Action updateFunc) + { + _config = config; + _updateFunc = updateFunc; + + var geoipFiles = new List(); + var geoSiteFiles = new List(); + + //Collect used files list + var routingItems = AppHandler.Instance.RoutingItems(); + foreach (var routing in routingItems) + { + var rules = JsonUtils.Deserialize>(routing.ruleSet); + foreach (var item in rules ?? []) + { + foreach (var ip in item.ip ?? []) + { + var prefix = "geoip:"; + if (ip.StartsWith(prefix)) + { + geoipFiles.Add(ip.Substring(prefix.Length)); + } + } + + foreach (var domain in item.domain ?? []) + { + var prefix = "geosite:"; + if (domain.StartsWith(prefix)) + { + geoSiteFiles.Add(domain.Substring(prefix.Length)); + } + } + } + } + + foreach(var item in geoipFiles.Distinct()) + { + await UpdateSrsFile("geoip", item, config, updateFunc); + } + + foreach(var item in geoSiteFiles.Distinct()) + { + await UpdateSrsFile("geosite", item, config, updateFunc); + } + } + + private async Task UpdateSrsFile(string type, string srsName, Config config, Action updateFunc) + { + var srsUrl = string.IsNullOrEmpty(_config.constItem.srsSourceUrl) + ? Global.SingboxRulesetUrl + : _config.constItem.srsSourceUrl; + + var fileName = $"{type}-{srsName}.srs"; + var targetPath = Path.Combine(Utils.GetBinPath("srss"), fileName); + var url = string.Format(srsUrl, type, $"{type}-{srsName}"); + + await DownloadGeoFile(url, fileName, targetPath, updateFunc); + } + + private async Task DownloadGeoFile(string url, string fileName, string targetPath, Action updateFunc) + { + var tmpFileName = Utils.GetTempPath(Utils.GetGuid()); DownloadService downloadHandle = new(); downloadHandle.UpdateCompleted += (sender2, args) => { if (args.Success) { - _updateFunc?.Invoke(false, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, geoName)); + _updateFunc?.Invoke(false, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, fileName)); try { - if (File.Exists(fileName)) + if (File.Exists(tmpFileName)) { - string targetPath = Utils.GetBinPath($"{geoName}.dat"); - File.Copy(fileName, targetPath, true); + File.Copy(tmpFileName, targetPath, true); - File.Delete(fileName); + File.Delete(tmpFileName); //_updateFunc?.Invoke(true, ""); } } @@ -490,7 +558,7 @@ namespace ServiceLib.Services _updateFunc?.Invoke(false, args.GetException().Message); }; - await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout); + await downloadHandle.DownloadFileAsync(url, tmpFileName, true, _timeout); } #endregion private