From 142b4500943f116c3584cd5ba0cd1fd1868f39ba Mon Sep 17 00:00:00 2001 From: runetfreedom Date: Fri, 18 Oct 2024 10:06:33 +0300 Subject: [PATCH] External dns for presets --- v2rayN/ServiceLib/Common/SqliteHelper.cs | 5 +++ v2rayN/ServiceLib/Global.cs | 9 +++-- v2rayN/ServiceLib/Handler/ConfigHandler.cs | 38 ++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/v2rayN/ServiceLib/Common/SqliteHelper.cs b/v2rayN/ServiceLib/Common/SqliteHelper.cs index ca34319ef1..36447e9a5a 100644 --- a/v2rayN/ServiceLib/Common/SqliteHelper.cs +++ b/v2rayN/ServiceLib/Common/SqliteHelper.cs @@ -105,6 +105,11 @@ public int Execute(string sql) return _db.Execute(sql); } + public int DeleteAll() + { + return _db.DeleteAll(); + } + public async Task ExecuteAsync(string sql) { return await _dbAsync.ExecuteAsync(sql); diff --git a/v2rayN/ServiceLib/Global.cs b/v2rayN/ServiceLib/Global.cs index 48d39d6fad..f9e955dae2 100644 --- a/v2rayN/ServiceLib/Global.cs +++ b/v2rayN/ServiceLib/Global.cs @@ -121,12 +121,17 @@ public class Global public static readonly List SingboxRulesetSources = new() { "", - @"https://raw.githubusercontent.com/runetfreedom/russia-v2ray-rules-dat/refs/heads/release/sing-box/rule-set-{0}/{1}.srs", + @"https://cdn.jsdelivr.net/gh/runetfreedom/russia-v2ray-rules-dat@release/sing-box/rule-set-{0}/{1}.srs", }; public static readonly List RoutingRulesSources = new() { "", - @"https://raw.githubusercontent.com/runetfreedom/russia-v2ray-custom-routing-list/refs/heads/main/template.json", + @"https://cdn.jsdelivr.net/gh/runetfreedom/russia-v2ray-custom-routing-list@main/template.json", + }; + + public static readonly List DNSTemplateSources = new() { + "", + @"https://cdn.jsdelivr.net/gh/runetfreedom/russia-v2ray-custom-routing-list@main/", }; public static readonly Dictionary UserAgentTexts = new() diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs index 71f7d47e93..c227b9429a 100644 --- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs @@ -1761,6 +1761,11 @@ public static int InitBuiltinDNS(Config config) public static int SaveDNSItems(Config config, DNSItem item) { + if (item == null) + { + return -1; + } + if (Utils.IsNullOrEmpty(item.id)) { item.id = Utils.GetGuid(false); @@ -1776,6 +1781,33 @@ public static int SaveDNSItems(Config config, DNSItem item) } } + public static DNSItem GetExternalDNSItem(ECoreType type, string url) + { + var currentItem = AppHandler.Instance.GetDNSItem(type); + + var downloadHandle = new DownloadService(); + var templateContent = Task.Run(() => downloadHandle.TryDownloadString(url, true, "")).Result; + if (String.IsNullOrEmpty(templateContent)) + return currentItem; + + var template = JsonUtils.Deserialize(templateContent); + if (template == null) + return currentItem; + + if (!String.IsNullOrEmpty(template.normalDNS)) + template.normalDNS = Task.Run(() => downloadHandle.TryDownloadString(template.normalDNS, true, "")).Result; + + if (!String.IsNullOrEmpty(template.tunDNS)) + template.tunDNS = Task.Run(() => downloadHandle.TryDownloadString(template.tunDNS, true, "")).Result; + + template.id = currentItem.id; + template.enabled = currentItem.enabled; + template.remarks = currentItem.remarks; + template.coreType = type; + + return template; + } + #endregion DNS #region Regional Presets @@ -1789,6 +1821,9 @@ public static bool ApplyRegionalPreset(Config config, EPresetType type) config.constItem.srsSourceUrl = ""; config.constItem.routeRulesTemplateSourceUrl = ""; + SQLiteHelper.Instance.DeleteAll(); + InitBuiltinDNS(config); + return true; case EPresetType.Russia: @@ -1796,6 +1831,9 @@ public static bool ApplyRegionalPreset(Config config, EPresetType type) config.constItem.srsSourceUrl = Global.SingboxRulesetSources[1]; config.constItem.routeRulesTemplateSourceUrl = Global.RoutingRulesSources[1]; + SaveDNSItems(config, GetExternalDNSItem(ECoreType.Xray, Global.DNSTemplateSources[1] + "v2ray.json")); + SaveDNSItems(config, GetExternalDNSItem(ECoreType.sing_box, Global.DNSTemplateSources[1] + "sing_box.json")); + return true; }