mirror of
https://github.com/2dust/v2rayN.git
synced 2025-08-30 06:46:19 +00:00
External routing rules templates + auto download geo file if repo changed
This commit is contained in:
parent
35e5475255
commit
55d2625447
14 changed files with 180 additions and 16 deletions
|
@ -124,6 +124,11 @@
|
|||
@"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",
|
||||
};
|
||||
|
||||
public static readonly Dictionary<string, string> UserAgentTexts = new()
|
||||
{
|
||||
{"chrome","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36" },
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
using System.Data;
|
||||
using ServiceLib.Common;
|
||||
using System.Data;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace ServiceLib.Handler
|
||||
|
@ -1616,6 +1618,79 @@ namespace ServiceLib.Handler
|
|||
return item;
|
||||
}
|
||||
|
||||
public static int InitRouting(Config config, bool blImportAdvancedRules = false)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(config.constItem.routeRulesTemplateSourceUrl))
|
||||
{
|
||||
InitExternalRouting(config, blImportAdvancedRules);
|
||||
}
|
||||
else
|
||||
{
|
||||
InitBuiltinRouting(config, blImportAdvancedRules);
|
||||
}
|
||||
|
||||
if (GetLockedRoutingItem(config) == null)
|
||||
{
|
||||
var item1 = new RoutingItem()
|
||||
{
|
||||
remarks = "locked",
|
||||
url = string.Empty,
|
||||
locked = true,
|
||||
};
|
||||
AddBatchRoutingRules(ref item1, Utils.GetEmbedText(Global.CustomRoutingFileName + "locked"));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int InitExternalRouting(Config config, bool blImportAdvancedRules = false)
|
||||
{
|
||||
DownloadService downloadHandle = new DownloadService();
|
||||
var templateContent = Task.Run(() => downloadHandle.TryDownloadString(config.constItem.routeRulesTemplateSourceUrl, false, "")).Result;
|
||||
if (String.IsNullOrEmpty(templateContent))
|
||||
return InitBuiltinRouting(config, blImportAdvancedRules); // fallback
|
||||
|
||||
var template = JsonUtils.Deserialize<RoutingTemplate>(templateContent);
|
||||
if (template == null)
|
||||
return InitBuiltinRouting(config, blImportAdvancedRules); // fallback
|
||||
|
||||
var items = AppHandler.Instance.RoutingItems();
|
||||
var maxSort = items.Count;
|
||||
|
||||
if (blImportAdvancedRules || items.Where(t => t.remarks.StartsWith(template.version)).ToList().Count <= 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)
|
||||
{
|
||||
SetDefaultRouting(config, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int InitBuiltinRouting(Config config, bool blImportAdvancedRules = false)
|
||||
{
|
||||
var ver = "V3-";
|
||||
|
@ -1655,17 +1730,6 @@ namespace ServiceLib.Handler
|
|||
SetDefaultRouting(config, item2);
|
||||
}
|
||||
}
|
||||
|
||||
if (GetLockedRoutingItem(config) == null)
|
||||
{
|
||||
var item1 = new RoutingItem()
|
||||
{
|
||||
remarks = "locked",
|
||||
url = string.Empty,
|
||||
locked = true,
|
||||
};
|
||||
AddBatchRoutingRules(ref item1, Utils.GetEmbedText(Global.CustomRoutingFileName + "locked"));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -142,6 +142,7 @@
|
|||
public string subConvertUrl { get; set; } = string.Empty;
|
||||
public string? geoSourceUrl { get; set; }
|
||||
public string? srsSourceUrl { get; set; }
|
||||
public string? routeRulesTemplateSourceUrl { get; set; }
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
|
|
9
v2rayN/ServiceLib/Models/RoutingTemplate.cs
Normal file
9
v2rayN/ServiceLib/Models/RoutingTemplate.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
namespace ServiceLib.Models
|
||||
{
|
||||
[Serializable]
|
||||
public class RoutingTemplate
|
||||
{
|
||||
public string version { get; set; }
|
||||
public RoutingItem[] routingItems { get; set; }
|
||||
}
|
||||
}
|
11
v2rayN/ServiceLib/Resx/ResUI.Designer.cs
generated
11
v2rayN/ServiceLib/Resx/ResUI.Designer.cs
generated
|
@ -3013,6 +3013,17 @@ namespace ServiceLib.Resx {
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找类似 Routing rules source (optional) 的本地化字符串。
|
||||
/// </summary>
|
||||
public static string TbSettingsRoutingRulesSource
|
||||
{
|
||||
get
|
||||
{
|
||||
return ResourceManager.GetString("TbSettingsRoutingRulesSource", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找类似 HTTP Port 的本地化字符串。
|
||||
/// </summary>
|
||||
|
|
|
@ -1336,4 +1336,7 @@
|
|||
<data name="UpgradeAppNotExistTip" xml:space="preserve">
|
||||
<value>UpgradeApp does not exist</value>
|
||||
</data>
|
||||
<data name="TbSettingsRoutingRulesSource" xml:space="preserve">
|
||||
<value>Routing rules source (optional)</value>
|
||||
</data>
|
||||
</root>
|
|
@ -253,6 +253,23 @@ namespace ServiceLib.Services
|
|||
});
|
||||
}
|
||||
|
||||
public async Task<bool> VerifyGeoFilesRepo(Config config, Action<bool, string> updateFunc)
|
||||
{
|
||||
var repoPath = Utils.GetBinPath("geo.repo");
|
||||
var repo = File.Exists(repoPath) ? File.ReadAllText(repoPath) : "";
|
||||
|
||||
if (repo != (config.constItem.geoSourceUrl ?? ""))
|
||||
{
|
||||
await UpdateGeoFileAll(config, updateFunc);
|
||||
|
||||
File.WriteAllText(repoPath, repo);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task UpdateGeoFileAll(Config config, Action<bool, string> updateFunc)
|
||||
{
|
||||
await UpdateGeoFile("geosite", config, updateFunc);
|
||||
|
|
|
@ -188,9 +188,10 @@ namespace ServiceLib.ViewModels
|
|||
|
||||
private void Init()
|
||||
{
|
||||
ConfigHandler.InitBuiltinRouting(_config);
|
||||
ConfigHandler.InitRouting(_config);
|
||||
ConfigHandler.InitBuiltinDNS(_config);
|
||||
CoreHandler.Instance.Init(_config, UpdateHandler);
|
||||
Task.Run(() => VerifyGeoFiles(true));
|
||||
TaskHandler.Instance.RegUpdateTask(_config, UpdateTaskHandler);
|
||||
|
||||
if (_config.guiItem.enableStatistics)
|
||||
|
@ -421,6 +422,7 @@ namespace ServiceLib.ViewModels
|
|||
var ret = await _updateView?.Invoke(EViewAction.OptionSettingWindow, null);
|
||||
if (ret == true)
|
||||
{
|
||||
await VerifyGeoFiles();
|
||||
Locator.Current.GetService<StatusBarViewModel>()?.InboundDisplayStatus();
|
||||
Reload();
|
||||
}
|
||||
|
@ -431,7 +433,7 @@ namespace ServiceLib.ViewModels
|
|||
var ret = await _updateView?.Invoke(EViewAction.RoutingSettingWindow, null);
|
||||
if (ret == true)
|
||||
{
|
||||
ConfigHandler.InitBuiltinRouting(_config);
|
||||
ConfigHandler.InitRouting(_config);
|
||||
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
|
||||
Reload();
|
||||
}
|
||||
|
@ -541,6 +543,14 @@ namespace ServiceLib.ViewModels
|
|||
}
|
||||
}
|
||||
|
||||
public async Task VerifyGeoFiles(bool needReload = false)
|
||||
{
|
||||
var result = await new UpdateService().VerifyGeoFilesRepo(_config, UpdateHandler);
|
||||
|
||||
if (needReload && !result)
|
||||
Reload();
|
||||
}
|
||||
|
||||
#endregion core job
|
||||
}
|
||||
}
|
|
@ -67,6 +67,7 @@ namespace ServiceLib.ViewModels
|
|||
[Reactive] public int MainGirdOrientation { get; set; }
|
||||
[Reactive] public string GeoFileSourceUrl { get; set; }
|
||||
[Reactive] public string SrsFileSourceUrl { get; set; }
|
||||
[Reactive] public string RoutingRulesSourceUrl { get; set; }
|
||||
|
||||
#endregion UI
|
||||
|
||||
|
@ -168,6 +169,7 @@ namespace ServiceLib.ViewModels
|
|||
MainGirdOrientation = (int)_config.uiItem.mainGirdOrientation;
|
||||
GeoFileSourceUrl = _config.constItem.geoSourceUrl;
|
||||
SrsFileSourceUrl = _config.constItem.srsSourceUrl;
|
||||
RoutingRulesSourceUrl = _config.constItem.routeRulesTemplateSourceUrl;
|
||||
|
||||
#endregion UI
|
||||
|
||||
|
@ -322,6 +324,7 @@ namespace ServiceLib.ViewModels
|
|||
_config.uiItem.mainGirdOrientation = (EGirdOrientation)MainGirdOrientation;
|
||||
_config.constItem.geoSourceUrl = GeoFileSourceUrl;
|
||||
_config.constItem.srsSourceUrl = SrsFileSourceUrl;
|
||||
_config.constItem.routeRulesTemplateSourceUrl = RoutingRulesSourceUrl;
|
||||
|
||||
//systemProxy
|
||||
_config.systemProxyItem.systemProxyExceptions = systemProxyExceptions;
|
||||
|
|
|
@ -71,7 +71,7 @@ namespace ServiceLib.ViewModels
|
|||
_updateView = updateView;
|
||||
SelectedSource = new();
|
||||
|
||||
ConfigHandler.InitBuiltinRouting(_config);
|
||||
ConfigHandler.InitRouting(_config);
|
||||
|
||||
enableRoutingAdvanced = _config.routingBasicItem.enableRoutingAdvanced;
|
||||
domainStrategy = _config.routingBasicItem.domainStrategy;
|
||||
|
@ -286,7 +286,7 @@ namespace ServiceLib.ViewModels
|
|||
|
||||
private async Task RoutingAdvancedImportRules()
|
||||
{
|
||||
if (ConfigHandler.InitBuiltinRouting(_config, true) == 0)
|
||||
if (ConfigHandler.InitRouting(_config, true) == 0)
|
||||
{
|
||||
RefreshRoutingItems();
|
||||
IsModified = true;
|
||||
|
|
|
@ -366,6 +366,7 @@
|
|||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
|
@ -638,6 +639,19 @@
|
|||
Grid.Column="1"
|
||||
Width="300"
|
||||
Classes="Margin8" />
|
||||
|
||||
<TextBlock
|
||||
Grid.Row="24"
|
||||
Grid.Column="0"
|
||||
VerticalAlignment="Center"
|
||||
Classes="Margin8"
|
||||
Text="{x:Static resx:ResUI.TbSettingsRoutingRulesSource}" />
|
||||
<ComboBox
|
||||
x:Name="cmbRoutingRulesSourceUrl"
|
||||
Grid.Row="24"
|
||||
Grid.Column="1"
|
||||
Width="300"
|
||||
Classes="Margin8" />
|
||||
</Grid>
|
||||
</ScrollViewer>
|
||||
</TabItem>
|
||||
|
|
|
@ -91,6 +91,10 @@ namespace v2rayN.Desktop.Views
|
|||
{
|
||||
cmbSrsFilesSourceUrl.Items.Add(it);
|
||||
});
|
||||
Global.RoutingRulesSources.ForEach(it =>
|
||||
{
|
||||
cmbRoutingRulesSourceUrl.Items.Add(it);
|
||||
});
|
||||
foreach (EGirdOrientation it in Enum.GetValues(typeof(EGirdOrientation)))
|
||||
{
|
||||
cmbMainGirdOrientation.Items.Add(it.ToString());
|
||||
|
@ -142,6 +146,7 @@ namespace v2rayN.Desktop.Views
|
|||
this.Bind(ViewModel, vm => vm.MainGirdOrientation, v => v.cmbMainGirdOrientation.SelectedIndex).DisposeWith(disposables);
|
||||
this.Bind(ViewModel, vm => vm.GeoFileSourceUrl, v => v.cmbGetFilesSourceUrl.SelectedValue).DisposeWith(disposables);
|
||||
this.Bind(ViewModel, vm => vm.SrsFileSourceUrl, v => v.cmbSrsFilesSourceUrl.SelectedValue).DisposeWith(disposables);
|
||||
this.Bind(ViewModel, vm => vm.RoutingRulesSourceUrl, v => v.cmbRoutingRulesSourceUrl.SelectedValue).DisposeWith(disposables);
|
||||
|
||||
this.Bind(ViewModel, vm => vm.notProxyLocalAddress, v => v.tognotProxyLocalAddress.IsChecked).DisposeWith(disposables);
|
||||
this.Bind(ViewModel, vm => vm.systemProxyAdvancedProtocol, v => v.cmbsystemProxyAdvancedProtocol.SelectedValue).DisposeWith(disposables);
|
||||
|
|
|
@ -530,6 +530,7 @@
|
|||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
|
@ -879,6 +880,22 @@
|
|||
Margin="{StaticResource Margin8}"
|
||||
IsEditable="True"
|
||||
Style="{StaticResource DefComboBox}" />
|
||||
|
||||
<TextBlock
|
||||
Grid.Row="24"
|
||||
Grid.Column="0"
|
||||
Margin="{StaticResource Margin8}"
|
||||
VerticalAlignment="Center"
|
||||
Style="{StaticResource ToolbarTextBlock}"
|
||||
Text="{x:Static resx:ResUI.TbSettingsRoutingRulesSource}" />
|
||||
<ComboBox
|
||||
x:Name="cmbRoutingRulesSourceUrl"
|
||||
Grid.Row="24"
|
||||
Grid.Column="1"
|
||||
Width="300"
|
||||
Margin="{StaticResource Margin8}"
|
||||
IsEditable="True"
|
||||
Style="{StaticResource DefComboBox}" />
|
||||
</Grid>
|
||||
</ScrollViewer>
|
||||
</TabItem>
|
||||
|
|
|
@ -93,6 +93,10 @@ namespace v2rayN.Views
|
|||
{
|
||||
cmbSrsFilesSourceUrl.Items.Add(it);
|
||||
});
|
||||
Global.RoutingRulesSources.ForEach(it =>
|
||||
{
|
||||
cmbRoutingRulesSourceUrl.Items.Add(it);
|
||||
});
|
||||
foreach (EGirdOrientation it in Enum.GetValues(typeof(EGirdOrientation)))
|
||||
{
|
||||
cmbMainGirdOrientation.Items.Add(it.ToString());
|
||||
|
@ -155,6 +159,7 @@ namespace v2rayN.Views
|
|||
this.Bind(ViewModel, vm => vm.MainGirdOrientation, v => v.cmbMainGirdOrientation.SelectedIndex).DisposeWith(disposables);
|
||||
this.Bind(ViewModel, vm => vm.GeoFileSourceUrl, v => v.cmbGetFilesSourceUrl.Text).DisposeWith(disposables);
|
||||
this.Bind(ViewModel, vm => vm.SrsFileSourceUrl, v => v.cmbSrsFilesSourceUrl.Text).DisposeWith(disposables);
|
||||
this.Bind(ViewModel, vm => vm.RoutingRulesSourceUrl, v => v.cmbRoutingRulesSourceUrl.Text).DisposeWith(disposables);
|
||||
|
||||
this.Bind(ViewModel, vm => vm.notProxyLocalAddress, v => v.tognotProxyLocalAddress.IsChecked).DisposeWith(disposables);
|
||||
this.Bind(ViewModel, vm => vm.systemProxyAdvancedProtocol, v => v.cmbsystemProxyAdvancedProtocol.Text).DisposeWith(disposables);
|
||||
|
|
Loading…
Reference in a new issue