Adjust some code

https://github.com/2dust/v2rayN/pull/5840
This commit is contained in:
2dust
2024-10-16 15:42:44 +08:00
parent 2edbbc523a
commit 9c4dc185be
16 changed files with 232 additions and 188 deletions
+1 -3
View File
@@ -116,18 +116,16 @@
public static readonly List<string> GeoFilesSources = new() {
"",
GeoUrl,
@"https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/{0}.dat",
};
public static readonly List<string> SingboxRulesetSources = new() {
"",
SingboxRulesetUrl,
@"https://raw.githubusercontent.com/runetfreedom/russia-v2ray-rules-dat/refs/heads/release/sing-box/rule-set-{0}/{1}.srs",
};
public static readonly List<string> RoutingRulesSources = new() {
"", //Default
"",
@"https://raw.githubusercontent.com/runetfreedom/russia-v2ray-custom-routing-list/refs/heads/main/template.json",
};
+69 -80
View File
@@ -1,6 +1,4 @@
using ServiceLib.Common;
using System.Data;
using System.Text.Json.Nodes;
using System.Data;
using System.Text.RegularExpressions;
namespace ServiceLib.Handler
@@ -1620,24 +1618,13 @@ namespace ServiceLib.Handler
public static int InitRouting(Config config, bool blImportAdvancedRules = false)
{
if (!String.IsNullOrEmpty(config.constItem.routeRulesTemplateSourceUrl))
{
InitExternalRouting(config, blImportAdvancedRules);
}
else
if (string.IsNullOrEmpty(config.constItem.routeRulesTemplateSourceUrl))
{
InitBuiltinRouting(config, blImportAdvancedRules);
}
if (GetLockedRoutingItem(config) == null)
else
{
var item1 = new RoutingItem()
{
remarks = "locked",
url = string.Empty,
locked = true,
};
AddBatchRoutingRules(ref item1, Utils.GetEmbedText(Global.CustomRoutingFileName + "locked"));
InitExternalRouting(config, blImportAdvancedRules);
}
return 0;
@@ -1645,10 +1632,10 @@ namespace ServiceLib.Handler
public static int InitExternalRouting(Config config, bool blImportAdvancedRules = false)
{
DownloadService downloadHandle = new DownloadService();
var downloadHandle = new DownloadService();
var templateContent = Task.Run(() => downloadHandle.TryDownloadString(config.constItem.routeRulesTemplateSourceUrl, false, "")).Result;
if (String.IsNullOrEmpty(templateContent))
return InitBuiltinRouting(config, blImportAdvancedRules); // fallback
return InitBuiltinRouting(config, blImportAdvancedRules); // fallback
var template = JsonUtils.Deserialize<RoutingTemplate>(templateContent);
if (template == null)
@@ -1656,35 +1643,35 @@ namespace ServiceLib.Handler
var items = AppHandler.Instance.RoutingItems();
var maxSort = items.Count;
if (blImportAdvancedRules || items.Where(t => t.remarks.StartsWith(template.version)).ToList().Count <= 0)
if (!blImportAdvancedRules && items.Where(t => t.remarks.StartsWith(template.version)).ToList().Count > 0)
{
for (var i = 0; i < template.routingItems.Length; i++)
return 0;
}
for (var i = 0; i < template.routingItems.Length; i++)
{
var item = template.routingItems[i];
if (String.IsNullOrEmpty(item.url) && String.IsNullOrEmpty(item.ruleSet))
continue;
var ruleSetsString = !String.IsNullOrEmpty(item.ruleSet)
? item.ruleSet
: Task.Run(() => downloadHandle.TryDownloadString(item.url, false, "")).Result;
if (String.IsNullOrEmpty(ruleSetsString))
continue;
item.remarks = $"{template.version}-{item.remarks}";
item.enabled = true;
item.sort = ++maxSort;
item.url = string.Empty;
AddBatchRoutingRules(ref item, ruleSetsString);
//first rule as default at first startup
if (!blImportAdvancedRules && i == 0)
{
var item = template.routingItems[i];
if (String.IsNullOrEmpty(item.url) && String.IsNullOrEmpty(item.ruleSet))
continue;
var ruleSetsString = !String.IsNullOrEmpty(item.ruleSet)
? item.ruleSet
: Task.Run(() => downloadHandle.TryDownloadString(item.url, false, "")).Result;
if (String.IsNullOrEmpty(ruleSetsString))
continue;
item.remarks = $"{template.version}-{item.remarks}";
item.enabled = true;
item.sort = ++maxSort;
item.url = string.Empty;
AddBatchRoutingRules(ref item, ruleSetsString);
//first rule as default at first startup
if (!blImportAdvancedRules && i == 0)
{
SetDefaultRouting(config, item);
}
SetDefaultRouting(config, item);
}
}
@@ -1695,45 +1682,47 @@ namespace ServiceLib.Handler
{
var ver = "V3-";
var items = AppHandler.Instance.RoutingItems();
if (blImportAdvancedRules || items.Where(t => t.remarks.StartsWith(ver)).ToList().Count <= 0)
if (!blImportAdvancedRules && items.Where(t => t.remarks.StartsWith(ver)).ToList().Count > 0)
{
var maxSort = items.Count;
//Bypass the mainland
var item2 = new RoutingItem()
{
remarks = $"{ver}绕过大陆(Whitelist)",
url = string.Empty,
sort = maxSort + 1,
};
AddBatchRoutingRules(ref item2, Utils.GetEmbedText(Global.CustomRoutingFileName + "white"));
return 0;
}
//Blacklist
var item3 = new RoutingItem()
{
remarks = $"{ver}黑名单(Blacklist)",
url = string.Empty,
sort = maxSort + 2,
};
AddBatchRoutingRules(ref item3, Utils.GetEmbedText(Global.CustomRoutingFileName + "black"));
var maxSort = items.Count;
//Bypass the mainland
var item2 = new RoutingItem()
{
remarks = $"{ver}绕过大陆(Whitelist)",
url = string.Empty,
sort = maxSort + 1,
};
AddBatchRoutingRules(ref item2, Utils.GetEmbedText(Global.CustomRoutingFileName + "white"));
//Global
var item1 = new RoutingItem()
{
remarks = $"{ver}全局(Global)",
url = string.Empty,
sort = maxSort + 3,
};
AddBatchRoutingRules(ref item1, Utils.GetEmbedText(Global.CustomRoutingFileName + "global"));
//Blacklist
var item3 = new RoutingItem()
{
remarks = $"{ver}黑名单(Blacklist)",
url = string.Empty,
sort = maxSort + 2,
};
AddBatchRoutingRules(ref item3, Utils.GetEmbedText(Global.CustomRoutingFileName + "black"));
if (!blImportAdvancedRules)
{
SetDefaultRouting(config, item2);
}
//Global
var item1 = new RoutingItem()
{
remarks = $"{ver}全局(Global)",
url = string.Empty,
sort = maxSort + 3,
};
AddBatchRoutingRules(ref item1, Utils.GetEmbedText(Global.CustomRoutingFileName + "global"));
if (!blImportAdvancedRules)
{
SetDefaultRouting(config, item2);
}
return 0;
}
public static RoutingItem GetLockedRoutingItem(Config config)
public static RoutingItem? GetLockedRoutingItem(Config config)
{
return SQLiteHelper.Instance.Table<RoutingItem>().FirstOrDefault(it => it.locked == true);
}
@@ -1789,9 +1778,9 @@ namespace ServiceLib.Handler
#endregion DNS
#region Presets
#region Regional Presets
public static bool ApplyPreset(Config config, EPresetType type)
public static bool ApplyRegionalPreset(Config config, EPresetType type)
{
switch (type)
{
@@ -1813,6 +1802,6 @@ namespace ServiceLib.Handler
return false;
}
#endregion
#endregion Regional Presets
}
}
+24 -17
View File
@@ -1116,27 +1116,27 @@ namespace ServiceLib.Resx {
/// <summary>
/// 查找类似 Regional presets 的本地化字符串。
/// </summary>
public static string menuPresets {
public static string menuRegionalPresets {
get {
return ResourceManager.GetString("menuPresets", resourceCulture);
return ResourceManager.GetString("menuRegionalPresets", resourceCulture);
}
}
/// <summary>
/// 查找类似 Default 的本地化字符串。
/// </summary>
public static string menuPresetsDefault {
public static string menuRegionalPresetsDefault {
get {
return ResourceManager.GetString("menuPresetsDefault", resourceCulture);
return ResourceManager.GetString("menuRegionalPresetsDefault", resourceCulture);
}
}
/// <summary>
/// 查找类似 Russia 的本地化字符串。
/// </summary>
public static string menuPresetsRussia {
public static string menuRegionalPresetsRussia {
get {
return ResourceManager.GetString("menuPresetsRussia", resourceCulture);
return ResourceManager.GetString("menuRegionalPresetsRussia", resourceCulture);
}
}
@@ -2752,6 +2752,15 @@ namespace ServiceLib.Resx {
}
}
/// <summary>
/// 查找类似 Users in China region can ignore this item 的本地化字符串。
/// </summary>
public static string TbSettingsChinaUserTip {
get {
return ResourceManager.GetString("TbSettingsChinaUserTip", resourceCulture);
}
}
/// <summary>
/// 查找类似 Color 的本地化字符串。
/// </summary>
@@ -3040,17 +3049,6 @@ namespace ServiceLib.Resx {
}
}
/// <summary>
/// 查找类似 Routing rules source (optional) 的本地化字符串。
/// </summary>
public static string TbSettingsRoutingRulesSource
{
get
{
return ResourceManager.GetString("TbSettingsRoutingRulesSource", resourceCulture);
}
}
/// <summary>
/// 查找类似 HTTP Port 的本地化字符串。
/// </summary>
@@ -3204,6 +3202,15 @@ namespace ServiceLib.Resx {
}
}
/// <summary>
/// 查找类似 Routing rules source (optional) 的本地化字符串。
/// </summary>
public static string TbSettingsRoutingRulesSource {
get {
return ResourceManager.GetString("TbSettingsRoutingRulesSource", resourceCulture);
}
}
/// <summary>
/// 查找类似 Set Win10 UWP Loopback 的本地化字符串。
/// </summary>
+7 -4
View File
@@ -1339,13 +1339,16 @@
<data name="TbSettingsRoutingRulesSource" xml:space="preserve">
<value>Routing rules source (optional)</value>
</data>
<data name="menuPresets" xml:space="preserve">
<value>Regional presets</value>
<data name="menuRegionalPresets" xml:space="preserve">
<value>Regional presets setting</value>
</data>
<data name="menuPresetsDefault" xml:space="preserve">
<data name="menuRegionalPresetsDefault" xml:space="preserve">
<value>Default</value>
</data>
<data name="menuPresetsRussia" xml:space="preserve">
<data name="menuRegionalPresetsRussia" xml:space="preserve">
<value>Russia</value>
</data>
<data name="TbSettingsChinaUserTip" xml:space="preserve">
<value>Users in China region can ignore this item</value>
</data>
</root>
+15
View File
@@ -1333,4 +1333,19 @@
<data name="TbSettingsSrsFilesSource" xml:space="preserve">
<value>sing-box ruleset文件来源(可选)</value>
</data>
<data name="TbSettingsRoutingRulesSource" xml:space="preserve">
<value>路由规则集来源(可选)</value>
</data>
<data name="TbSettingsChinaUserTip" xml:space="preserve">
<value>中国区域用户可忽略此项</value>
</data>
<data name="menuRegionalPresets" xml:space="preserve">
<value>区域预置设置</value>
</data>
<data name="menuRegionalPresetsDefault" xml:space="preserve">
<value>默认区域</value>
</data>
<data name="menuRegionalPresetsRussia" xml:space="preserve">
<value>俄罗斯</value>
</data>
</root>
+15
View File
@@ -1213,4 +1213,19 @@
<data name="TbSettingsSrsFilesSource" xml:space="preserve">
<value>sing-box ruleset文件來源(可選)</value>
</data>
<data name="TbSettingsRoutingRulesSource" xml:space="preserve">
<value>路由规则集来源(可选)</value>
</data>
<data name="TbSettingsChinaUserTip" xml:space="preserve">
<value>中国区域用户可忽略此项</value>
</data>
<data name="menuRegionalPresets" xml:space="preserve">
<value>区域预置设置</value>
</data>
<data name="menuRegionalPresetsDefault" xml:space="preserve">
<value>默认区域</value>
</data>
<data name="menuRegionalPresetsRussia" xml:space="preserve">
<value>俄羅斯</value>
</data>
</root>
@@ -45,8 +45,8 @@ namespace ServiceLib.ViewModels
public ReactiveCommand<Unit, Unit> OpenTheFileLocationCmd { get; }
//Presets
public ReactiveCommand<Unit, Unit> PresetDefaultCmd { get; }
public ReactiveCommand<Unit, Unit> PresetRussiaCmd { get; }
public ReactiveCommand<Unit, Unit> RegionalPresetDefaultCmd { get; }
public ReactiveCommand<Unit, Unit> RegionalPresetRussiaCmd { get; }
public ReactiveCommand<Unit, Unit> ReloadCmd { get; }
@@ -185,14 +185,14 @@ namespace ServiceLib.ViewModels
await Reload();
});
PresetDefaultCmd = ReactiveCommand.CreateFromTask(async () =>
RegionalPresetDefaultCmd = ReactiveCommand.CreateFromTask(async () =>
{
await ApplyPreset(EPresetType.Default);
await ApplyRegionalPreset(EPresetType.Default);
});
PresetRussiaCmd = ReactiveCommand.CreateFromTask(async () =>
RegionalPresetRussiaCmd = ReactiveCommand.CreateFromTask(async () =>
{
await ApplyPreset(EPresetType.Russia);
await ApplyRegionalPreset(EPresetType.Russia);
});
#endregion WhenAnyValue && ReactiveCommand
@@ -202,7 +202,7 @@ namespace ServiceLib.ViewModels
private void Init()
{
ConfigHandler.InitRouting(_config);
ConfigHandler.InitBuiltinRouting(_config);
ConfigHandler.InitBuiltinDNS(_config);
CoreHandler.Instance.Init(_config, UpdateHandler);
TaskHandler.Instance.RegUpdateTask(_config, UpdateTaskHandler);
@@ -445,7 +445,7 @@ namespace ServiceLib.ViewModels
var ret = await _updateView?.Invoke(EViewAction.RoutingSettingWindow, null);
if (ret == true)
{
ConfigHandler.InitRouting(_config);
ConfigHandler.InitBuiltinRouting(_config);
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
Reload();
}
@@ -560,16 +560,14 @@ namespace ServiceLib.ViewModels
#region Presets
public async Task ApplyPreset(EPresetType type)
public async Task ApplyRegionalPreset(EPresetType type)
{
ConfigHandler.ApplyPreset(_config, type);
await new UpdateService().UpdateGeoFileAll(_config, UpdateHandler);
ConfigHandler.ApplyRegionalPreset(_config, type);
ConfigHandler.InitRouting(_config);
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
ConfigHandler.SaveConfig(_config, false);
await new UpdateService().UpdateGeoFileAll(_config, UpdateHandler);
Reload();
}
@@ -71,7 +71,7 @@ namespace ServiceLib.ViewModels
_updateView = updateView;
SelectedSource = new();
ConfigHandler.InitRouting(_config);
ConfigHandler.InitBuiltinRouting(_config);
enableRoutingAdvanced = _config.routingBasicItem.enableRoutingAdvanced;
domainStrategy = _config.routingBasicItem.domainStrategy;
@@ -123,6 +123,17 @@ namespace ServiceLib.ViewModels
private void BindingLockedData()
{
_lockedItem = ConfigHandler.GetLockedRoutingItem(_config);
if (_lockedItem == null)
{
_lockedItem = new RoutingItem()
{
remarks = "locked",
url = string.Empty,
locked = true,
};
ConfigHandler.AddBatchRoutingRules(ref _lockedItem, Utils.GetEmbedText(Global.CustomRoutingFileName + "locked"));
}
if (_lockedItem != null)
{
_lockedRules = JsonUtils.Deserialize<List<RulesItem>>(_lockedItem.ruleSet);
@@ -33,7 +33,6 @@ namespace ServiceLib.ViewModels
public ReactiveCommand<Unit, Unit> SubUpdateCmd { get; }
public ReactiveCommand<Unit, Unit> SubUpdateViaProxyCmd { get; }
public ReactiveCommand<Unit, Unit> NotifyLeftClickCmd { get; }
public ReactiveCommand<Unit, Unit> ExitCmd { get; }
#region System Proxy