Skip to content

Commit

Permalink
External dns templates support + move ru sources to jsdelivr
Browse files Browse the repository at this point in the history
  • Loading branch information
runetfreedom committed Oct 17, 2024
1 parent 85576c0 commit 587f410
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 13 deletions.
16 changes: 11 additions & 5 deletions v2rayN/ServiceLib/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,21 @@ public class Global
public const string CorePreConfigFileName = "configPre.json";
public const string CoreSpeedtestConfigFileName = "configSpeedtest.json";
public const string CoreMultipleLoadConfigFileName = "configMultipleLoad.json";
public const string TunSingboxDNSFileName = "tun_singbox_dns";
public const string DNSSingboxNormalFileName = "dns_singbox_normal";
public const string DNSV2rayNormalFileName = "dns_v2ray_normal";
public const string ClashMixinConfigFileName = "Mixin.yaml";
public const string V2raySampleClient = "ServiceLib.Sample.SampleClientConfig";
public const string ServiceLibSamplePath = "ServiceLib.Sample.";
public const string SingboxSampleClient = "ServiceLib.Sample.SingboxSampleClientConfig";
public const string V2raySampleHttpRequestFileName = "ServiceLib.Sample.SampleHttpRequest";
public const string V2raySampleHttpResponseFileName = "ServiceLib.Sample.SampleHttpResponse";
public const string V2raySampleInbound = "ServiceLib.Sample.SampleInbound";
public const string V2raySampleOutbound = "ServiceLib.Sample.SampleOutbound";
public const string SingboxSampleOutbound = "ServiceLib.Sample.SingboxSampleOutbound";
public const string CustomRoutingFileName = "ServiceLib.Sample.custom_routing_";
public const string TunSingboxDNSFileName = "ServiceLib.Sample.tun_singbox_dns";
public const string TunSingboxInboundFileName = "ServiceLib.Sample.tun_singbox_inbound";
public const string TunSingboxRulesFileName = "ServiceLib.Sample.tun_singbox_rules";
public const string DNSV2rayNormalFileName = "ServiceLib.Sample.dns_v2ray_normal";
public const string DNSSingboxNormalFileName = "ServiceLib.Sample.dns_singbox_normal";
public const string ClashMixinYaml = "ServiceLib.Sample.clash_mixin_yaml";
public const string ClashTunYaml = "ServiceLib.Sample.clash_tun_yaml";

Expand Down Expand Up @@ -121,12 +122,17 @@ public class Global

public static readonly List<string> 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<string> 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<string> DNSTemplateSources = new() {
"",
@"https://cdn.jsdelivr.net/gh/runetfreedom/russia-v2ray-custom-routing-list@main/",
};

public static readonly Dictionary<string, string> UserAgentTexts = new()
Expand Down
20 changes: 18 additions & 2 deletions v2rayN/ServiceLib/Handler/ConfigHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using ServiceLib.Common;
using System.Data;
using System.Text.RegularExpressions;

namespace ServiceLib.Handler
Expand Down Expand Up @@ -1633,7 +1634,7 @@ public static int InitRouting(Config config, bool blImportAdvancedRules = false)
public static int InitExternalRouting(Config config, bool blImportAdvancedRules = false)
{
var downloadHandle = new DownloadService();
var templateContent = Task.Run(() => downloadHandle.TryDownloadString(config.constItem.routeRulesTemplateSourceUrl, false, "")).Result;
var templateContent = Task.Run(() => downloadHandle.TryDownloadString(config.constItem.routeRulesTemplateSourceUrl, true, "")).Result;
if (String.IsNullOrEmpty(templateContent))
return InitBuiltinRouting(config, blImportAdvancedRules); // fallback

Expand Down Expand Up @@ -1776,6 +1777,19 @@ public static int SaveDNSItems(Config config, DNSItem item)
}
}

public static string GetDNSConfig(Config config, string configName)
{
if (!string.IsNullOrEmpty(config?.constItem.dnsTemplateSourceUrl))
{
var downloadHandle = new DownloadService();
var templateContent = Task.Run(() => downloadHandle.TryDownloadString(config.constItem.dnsTemplateSourceUrl + configName, true, "")).Result;
if (templateContent != null)
return templateContent;
}

return Utils.GetEmbedText(Global.ServiceLibSamplePath + configName);
}

#endregion DNS

#region Regional Presets
Expand All @@ -1788,13 +1802,15 @@ public static bool ApplyRegionalPreset(Config config, EPresetType type)
config.constItem.geoSourceUrl = "";
config.constItem.srsSourceUrl = "";
config.constItem.routeRulesTemplateSourceUrl = "";
config.constItem.dnsTemplateSourceUrl = "";

return true;

case EPresetType.Russia:
config.constItem.geoSourceUrl = Global.GeoFilesSources[1];
config.constItem.srsSourceUrl = Global.SingboxRulesetSources[1];
config.constItem.routeRulesTemplateSourceUrl = Global.RoutingRulesSources[1];
config.constItem.dnsTemplateSourceUrl = Global.DNSTemplateSources[1];

return true;
}
Expand Down
1 change: 1 addition & 0 deletions v2rayN/ServiceLib/Models/ConfigItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public class ConstItem
public string? geoSourceUrl { get; set; }
public string? srsSourceUrl { get; set; }
public string? routeRulesTemplateSourceUrl { get; set; }
public string? dnsTemplateSourceUrl { get; set; }
}

[Serializable]
Expand Down
9 changes: 9 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1351,4 +1351,7 @@
<data name="TbSettingsChinaUserTip" xml:space="preserve">
<value>Users in China region can ignore this item</value>
</data>
<data name="TbSettingsDNSTemplatesSource" xml:space="preserve">
<value>DNS templates source (optional)</value>
</data>
</root>
3 changes: 3 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.ru.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1048,4 +1048,7 @@
<data name="TbSettingsChinaUserTip" xml:space="preserve">
<value>Используйте Настройки -> Региональные пресеты вместо изменения этого поля</value>
</data>
<data name="TbSettingsDNSTemplatesSource" xml:space="preserve">
<value>Источник DNS шаблонов</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -1177,11 +1177,11 @@ private int GenDns(ProfileItem? node, SingboxConfig singboxConfig)
var strDNS = string.Empty;
if (_config.tunModeItem.enableTun)
{
strDNS = Utils.IsNullOrEmpty(item?.tunDNS) ? Utils.GetEmbedText(Global.TunSingboxDNSFileName) : item?.tunDNS;
strDNS = Utils.IsNullOrEmpty(item?.tunDNS) ? ConfigHandler.GetDNSConfig(_config, Global.TunSingboxDNSFileName) : item?.tunDNS;
}
else
{
strDNS = Utils.IsNullOrEmpty(item?.normalDNS) ? Utils.GetEmbedText(Global.DNSSingboxNormalFileName) : item?.normalDNS;
strDNS = Utils.IsNullOrEmpty(item?.normalDNS) ? ConfigHandler.GetDNSConfig(_config, Global.DNSSingboxNormalFileName) : item?.normalDNS;
}

var dns4Sbox = JsonUtils.Deserialize<Dns4Sbox>(strDNS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ private int GenDns(ProfileItem? node, V2rayConfig v2rayConfig)
var domainStrategy4Freedom = item?.domainStrategy4Freedom;
if (Utils.IsNullOrEmpty(normalDNS))
{
normalDNS = Utils.GetEmbedText(Global.DNSV2rayNormalFileName);
normalDNS = ConfigHandler.GetDNSConfig(_config, Global.DNSV2rayNormalFileName);
}

//Outbound Freedom domainStrategy
Expand Down
6 changes: 3 additions & 3 deletions v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public DNSSettingViewModel(Func<EViewAction, object?, Task<bool>>? updateView)

ImportDefConfig4V2rayCmd = ReactiveCommand.CreateFromTask(async () =>
{
normalDNS = Utils.GetEmbedText(Global.DNSV2rayNormalFileName);
normalDNS = ConfigHandler.GetDNSConfig(null, Global.DNSV2rayNormalFileName);
});

ImportDefConfig4SingboxCmd = ReactiveCommand.CreateFromTask(async () =>
{
normalDNS2 = Utils.GetEmbedText(Global.DNSSingboxNormalFileName);
tunDNS2 = Utils.GetEmbedText(Global.TunSingboxDNSFileName);
normalDNS2 = ConfigHandler.GetDNSConfig(null, Global.DNSSingboxNormalFileName);
tunDNS2 = ConfigHandler.GetDNSConfig(null, Global.TunSingboxDNSFileName);
});
}

Expand Down
3 changes: 3 additions & 0 deletions v2rayN/ServiceLib/ViewModels/OptionSettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class OptionSettingViewModel : MyReactiveObject
[Reactive] public string GeoFileSourceUrl { get; set; }
[Reactive] public string SrsFileSourceUrl { get; set; }
[Reactive] public string RoutingRulesSourceUrl { get; set; }
[Reactive] public string DNSTemplatesUrl { get; set; }

#endregion UI

Expand Down Expand Up @@ -170,6 +171,7 @@ public OptionSettingViewModel(Func<EViewAction, object?, Task<bool>>? updateView
GeoFileSourceUrl = _config.constItem.geoSourceUrl;
SrsFileSourceUrl = _config.constItem.srsSourceUrl;
RoutingRulesSourceUrl = _config.constItem.routeRulesTemplateSourceUrl;
DNSTemplatesUrl = _config.constItem.dnsTemplateSourceUrl;

#endregion UI

Expand Down Expand Up @@ -325,6 +327,7 @@ private async Task SaveSettingAsync()
_config.constItem.geoSourceUrl = GeoFileSourceUrl;
_config.constItem.srsSourceUrl = SrsFileSourceUrl;
_config.constItem.routeRulesTemplateSourceUrl = RoutingRulesSourceUrl;
_config.constItem.dnsTemplateSourceUrl = DNSTemplatesUrl;

//systemProxy
_config.systemProxyItem.systemProxyExceptions = systemProxyExceptions;
Expand Down
21 changes: 21 additions & 0 deletions v2rayN/v2rayN.Desktop/Views/OptionSettingWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
Expand Down Expand Up @@ -676,6 +677,26 @@
Text="{x:Static resx:ResUI.TbSettingsChinaUserTip}"
TextWrapping="Wrap" />

<TextBlock
Grid.Row="25"
Grid.Column="0"
VerticalAlignment="Center"
Classes="Margin8"
Text="{x:Static resx:ResUI.TbSettingsDNSTemplatesSource}" />
<ComboBox
x:Name="cmbDNSTemplatesUrl"
Grid.Row="25"
Grid.Column="1"
Width="300"
Classes="Margin8" />
<TextBlock
Grid.Row="25"
Grid.Column="2"
VerticalAlignment="Center"
Classes="Margin8"
Text="{x:Static resx:ResUI.TbSettingsChinaUserTip}"
TextWrapping="Wrap" />

</Grid>
</ScrollViewer>
</TabItem>
Expand Down
5 changes: 5 additions & 0 deletions v2rayN/v2rayN.Desktop/Views/OptionSettingWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public OptionSettingWindow()
{
cmbRoutingRulesSourceUrl.Items.Add(it);
});
Global.DNSTemplateSources.ForEach(it =>
{
cmbDNSTemplatesUrl.Items.Add(it);
});
foreach (EGirdOrientation it in Enum.GetValues(typeof(EGirdOrientation)))
{
cmbMainGirdOrientation.Items.Add(it.ToString());
Expand Down Expand Up @@ -147,6 +151,7 @@ public OptionSettingWindow()
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.DNSTemplatesUrl, v => v.cmbDNSTemplatesUrl.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);
Expand Down
25 changes: 25 additions & 0 deletions v2rayN/v2rayN/Views/OptionSettingWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
Expand Down Expand Up @@ -923,6 +924,30 @@
Text="{x:Static resx:ResUI.TbSettingsChinaUserTip}"
TextWrapping="Wrap" />

<TextBlock
Grid.Row="25"
Grid.Column="0"
Margin="{StaticResource Margin8}"
VerticalAlignment="Center"
Style="{StaticResource ToolbarTextBlock}"
Text="{x:Static resx:ResUI.TbSettingsDNSTemplatesSource}" />
<ComboBox
x:Name="cmbDNSTemplatesUrl"
Grid.Row="25"
Grid.Column="1"
Width="300"
Margin="{StaticResource Margin8}"
IsEditable="True"
Style="{StaticResource DefComboBox}" />
<TextBlock
Grid.Row="25"
Grid.Column="2"
Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
Style="{StaticResource ToolbarTextBlock}"
Text="{x:Static resx:ResUI.TbSettingsChinaUserTip}"
TextWrapping="Wrap" />

</Grid>
</ScrollViewer>
</TabItem>
Expand Down
5 changes: 5 additions & 0 deletions v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ public OptionSettingWindow()
{
cmbRoutingRulesSourceUrl.Items.Add(it);
});
Global.DNSTemplateSources.ForEach(it =>
{
cmbDNSTemplatesUrl.Items.Add(it);
});
foreach (EGirdOrientation it in Enum.GetValues(typeof(EGirdOrientation)))
{
cmbMainGirdOrientation.Items.Add(it.ToString());
Expand Down Expand Up @@ -160,6 +164,7 @@ public OptionSettingWindow()
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.DNSTemplatesUrl, v => v.cmbDNSTemplatesUrl.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);
Expand Down

0 comments on commit 587f410

Please sign in to comment.