From ff642fd1ac368bc2c2b3b6376f15852c3e63fbed Mon Sep 17 00:00:00 2001
From: OnceUponATimeInAmerica
 <114700833+OnceUponATimeInAmerica@users.noreply.github.com>
Date: Tue, 31 Dec 2024 09:21:06 +0330
Subject: [PATCH] Add full support for  (one-click) Iran regional preset by
 using Chocolate4U's github repos for iran routing rules (#6384)

---
 v2rayN/ServiceLib/Enums/EPresetType.cs              |  1 +
 v2rayN/ServiceLib/Global.cs                         |  4 ++++
 v2rayN/ServiceLib/Handler/ConfigHandler.cs          | 10 ++++++++++
 v2rayN/ServiceLib/Resx/ResUI.Designer.cs            |  9 +++++++++
 v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx             |  3 +++
 v2rayN/ServiceLib/Resx/ResUI.hu.resx                |  3 +++
 v2rayN/ServiceLib/Resx/ResUI.resx                   |  3 +++
 v2rayN/ServiceLib/Resx/ResUI.ru.resx                |  3 +++
 v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx           |  3 +++
 v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx           |  3 +++
 v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs |  7 +++++++
 v2rayN/v2rayN.Desktop/Views/MainWindow.axaml        |  1 +
 v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs     |  1 +
 v2rayN/v2rayN/Views/MainWindow.xaml                 |  4 ++++
 v2rayN/v2rayN/Views/MainWindow.xaml.cs              |  1 +
 15 files changed, 56 insertions(+)

diff --git a/v2rayN/ServiceLib/Enums/EPresetType.cs b/v2rayN/ServiceLib/Enums/EPresetType.cs
index ffd89c11..855b10a1 100644
--- a/v2rayN/ServiceLib/Enums/EPresetType.cs
+++ b/v2rayN/ServiceLib/Enums/EPresetType.cs
@@ -4,5 +4,6 @@
     {
         Default = 0,
         Russia = 1,
+        Iran = 2,
     }
 }
\ No newline at end of file
diff --git a/v2rayN/ServiceLib/Global.cs b/v2rayN/ServiceLib/Global.cs
index 299791ee..a2beff87 100644
--- a/v2rayN/ServiceLib/Global.cs
+++ b/v2rayN/ServiceLib/Global.cs
@@ -124,21 +124,25 @@
         public static readonly List<string> GeoFilesSources = new() {
             "",
             @"https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/{0}.dat",
+            @"https://cdn.jsdelivr.net/gh/chocolate4u/Iran-v2ray-rules@release/{0}.dat",
         };
 
         public static readonly List<string> SingboxRulesetSources = new() {
             "",
             @"https://cdn.jsdelivr.net/gh/runetfreedom/russia-v2ray-rules-dat@release/sing-box/rule-set-{0}/{1}.srs",
+            @"https://cdn.jsdelivr.net/gh/chocolate4u/Iran-sing-box-rules@rule-set/{1}.srs",
         };
 
         public static readonly List<string> RoutingRulesSources = new() {
             "",
             @"https://cdn.jsdelivr.net/gh/runetfreedom/russia-v2ray-custom-routing-list@main/v2rayN/template.json",
+            @"https://cdn.jsdelivr.net/gh/Chocolate4U/Iran-v2ray-rules@main/v2rayN/template.json",
         };
 
         public static readonly List<string> DNSTemplateSources = new() {
             "",
             @"https://cdn.jsdelivr.net/gh/runetfreedom/russia-v2ray-custom-routing-list@main/v2rayN/",
+            @"https://cdn.jsdelivr.net/gh/Chocolate4U/Iran-v2ray-rules@main/v2rayN/",
         };
 
         public static readonly Dictionary<string, string> UserAgentTexts = new()
diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs
index 2424db49..29a76a09 100644
--- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs
+++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs
@@ -1868,6 +1868,16 @@ namespace ServiceLib.Handler
                     await SaveDNSItems(config, await GetExternalDNSItem(ECoreType.Xray, Global.DNSTemplateSources[1] + "v2ray.json"));
                     await SaveDNSItems(config, await GetExternalDNSItem(ECoreType.sing_box, Global.DNSTemplateSources[1] + "sing_box.json"));
 
+                    return true;
+                
+                case EPresetType.Iran:
+                    config.ConstItem.GeoSourceUrl = Global.GeoFilesSources[2];
+                    config.ConstItem.SrsSourceUrl = Global.SingboxRulesetSources[2];
+                    config.ConstItem.RouteRulesTemplateSourceUrl = Global.RoutingRulesSources[2];
+
+                    await SaveDNSItems(config, await GetExternalDNSItem(ECoreType.Xray, Global.DNSTemplateSources[2] + "v2ray.json"));
+                    await SaveDNSItems(config, await GetExternalDNSItem(ECoreType.sing_box, Global.DNSTemplateSources[2] + "sing_box.json"));
+
                     return true;
             }
 
diff --git a/v2rayN/ServiceLib/Resx/ResUI.Designer.cs b/v2rayN/ServiceLib/Resx/ResUI.Designer.cs
index f0bd6dde..6b854e1b 100644
--- a/v2rayN/ServiceLib/Resx/ResUI.Designer.cs
+++ b/v2rayN/ServiceLib/Resx/ResUI.Designer.cs
@@ -1257,6 +1257,15 @@ namespace ServiceLib.Resx {
             }
         }
         
+        /// <summary>
+        ///   查找类似 Iran 的本地化字符串。
+        /// </summary>
+        public static string menuRegionalPresetsIran {
+            get {
+                return ResourceManager.GetString("menuRegionalPresetsIran", resourceCulture);
+            }
+        }
+        
         /// <summary>
         ///   查找类似 Reload 的本地化字符串。
         /// </summary>
diff --git a/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx b/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx
index 610deeca..e9c4a62b 100644
--- a/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx
+++ b/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx
@@ -1129,6 +1129,9 @@
   <data name="menuRegionalPresetsRussia" xml:space="preserve">
     <value>روسیه</value>
   </data>
+  <data name="menuRegionalPresetsIran" xml:space="preserve">
+    <value>ایران</value>
+  </data>
   <data name="TbSettingsChinaUserTip" xml:space="preserve">
     <value>کاربران در منطقه چین می توانند این مورد را نادیده بگیرند</value>
   </data>
diff --git a/v2rayN/ServiceLib/Resx/ResUI.hu.resx b/v2rayN/ServiceLib/Resx/ResUI.hu.resx
index cee28544..053efb6a 100644
--- a/v2rayN/ServiceLib/Resx/ResUI.hu.resx
+++ b/v2rayN/ServiceLib/Resx/ResUI.hu.resx
@@ -1342,6 +1342,9 @@
   <data name="menuRegionalPresetsRussia" xml:space="preserve">
     <value>Oroszország</value>
   </data>
+  <data name="menuRegionalPresetsIran" xml:space="preserve">
+    <value>Irán</value>
+  </data>
   <data name="TbSettingsChinaUserTip" xml:space="preserve">
     <value>A Kínában élő felhasználók figyelmen kívül hagyhatják ezt a tételt</value>
   </data>
diff --git a/v2rayN/ServiceLib/Resx/ResUI.resx b/v2rayN/ServiceLib/Resx/ResUI.resx
index d657b941..96a78013 100644
--- a/v2rayN/ServiceLib/Resx/ResUI.resx
+++ b/v2rayN/ServiceLib/Resx/ResUI.resx
@@ -1342,6 +1342,9 @@
   <data name="menuRegionalPresetsRussia" xml:space="preserve">
     <value>Russia</value>
   </data>
+  <data name="menuRegionalPresetsIran" xml:space="preserve">
+    <value>Iran</value>
+  </data>
   <data name="TbSettingsChinaUserTip" xml:space="preserve">
     <value>Users in China region can ignore this item</value>
   </data>
diff --git a/v2rayN/ServiceLib/Resx/ResUI.ru.resx b/v2rayN/ServiceLib/Resx/ResUI.ru.resx
index 2e795617..326bf5d3 100644
--- a/v2rayN/ServiceLib/Resx/ResUI.ru.resx
+++ b/v2rayN/ServiceLib/Resx/ResUI.ru.resx
@@ -1039,6 +1039,9 @@
   <data name="menuRegionalPresetsRussia" xml:space="preserve">
     <value>Россия</value>
   </data>
+  <data name="menuRegionalPresetsIran" xml:space="preserve">
+    <value>Иран</value>
+  </data>
   <data name="TbSettingsChinaUserTip" xml:space="preserve">
     <value>Используйте Настройки -&gt; Региональные пресеты вместо изменения этого поля</value>
   </data>
diff --git a/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx b/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx
index 93878d73..aab81184 100644
--- a/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx
+++ b/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx
@@ -1342,6 +1342,9 @@
   <data name="menuRegionalPresetsRussia" xml:space="preserve">
     <value>俄罗斯</value>
   </data>
+  <data name="menuRegionalPresetsIran" xml:space="preserve">
+    <value>伊朗</value>
+  </data>
   <data name="menuAddServerViaImage" xml:space="preserve">
     <value>扫描图片中的二维码</value>
   </data>
diff --git a/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx b/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx
index df522429..f91c0044 100644
--- a/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx
+++ b/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx
@@ -1222,6 +1222,9 @@
   <data name="menuRegionalPresetsRussia" xml:space="preserve">
     <value>俄羅斯</value>
   </data>
+  <data name="menuRegionalPresetsIran" xml:space="preserve">
+    <value>伊朗</value>
+  </data>
   <data name="menuAddServerViaImage" xml:space="preserve">
     <value>掃描圖片中的二維碼</value>
   </data>
diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
index c5216f86..4645200d 100644
--- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
+++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
@@ -50,6 +50,8 @@ namespace ServiceLib.ViewModels
 
         public ReactiveCommand<Unit, Unit> RegionalPresetRussiaCmd { get; }
 
+        public ReactiveCommand<Unit, Unit> RegionalPresetIranCmd { get; }
+
         public ReactiveCommand<Unit, Unit> ReloadCmd { get; }
 
         [Reactive]
@@ -197,6 +199,11 @@ namespace ServiceLib.ViewModels
                 await ApplyRegionalPreset(EPresetType.Russia);
             });
 
+            RegionalPresetIranCmd = ReactiveCommand.CreateFromTask(async () =>
+            {
+                await ApplyRegionalPreset(EPresetType.Iran);
+            });
+
             #endregion WhenAnyValue && ReactiveCommand
 
             Init();
diff --git a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml
index 0a23b12b..35f558aa 100644
--- a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml
+++ b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml
@@ -80,6 +80,7 @@
                         <MenuItem Header="{x:Static resx:ResUI.menuRegionalPresets}">
                             <MenuItem x:Name="menuRegionalPresetsDefault" Header="{x:Static resx:ResUI.menuRegionalPresetsDefault}" />
                             <MenuItem x:Name="menuRegionalPresetsRussia" Header="{x:Static resx:ResUI.menuRegionalPresetsRussia}" />
+                            <MenuItem x:Name="menuRegionalPresetsIran" Header="{x:Static resx:ResUI.menuRegionalPresetsIran}" />
                         </MenuItem>
                         <MenuItem x:Name="menuBackupAndRestore" Header="{x:Static resx:ResUI.menuBackupAndRestore}" />
                         <MenuItem x:Name="menuOpenTheFileLocation" Header="{x:Static resx:ResUI.menuOpenTheFileLocation}" />
diff --git a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs
index 0d1566e9..79f4eea8 100644
--- a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs
+++ b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs
@@ -104,6 +104,7 @@ namespace v2rayN.Desktop.Views
                 this.BindCommand(ViewModel, vm => vm.OpenTheFileLocationCmd, v => v.menuOpenTheFileLocation).DisposeWith(disposables);
                 this.BindCommand(ViewModel, vm => vm.RegionalPresetDefaultCmd, v => v.menuRegionalPresetsDefault).DisposeWith(disposables);
                 this.BindCommand(ViewModel, vm => vm.RegionalPresetRussiaCmd, v => v.menuRegionalPresetsRussia).DisposeWith(disposables);
+                this.BindCommand(ViewModel, vm => vm.RegionalPresetIranCmd, v => v.menuRegionalPresetsIran).DisposeWith(disposables);
 
                 this.BindCommand(ViewModel, vm => vm.ReloadCmd, v => v.menuReload).DisposeWith(disposables);
                 this.OneWayBind(ViewModel, vm => vm.BlReloadEnabled, v => v.menuReload.IsEnabled).DisposeWith(disposables);
diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml b/v2rayN/v2rayN/Views/MainWindow.xaml
index 7c67cc47..699d3f1c 100644
--- a/v2rayN/v2rayN/Views/MainWindow.xaml
+++ b/v2rayN/v2rayN/Views/MainWindow.xaml
@@ -194,6 +194,10 @@
                                         x:Name="menuRegionalPresetsRussia"
                                         Height="{StaticResource MenuItemHeight}"
                                         Header="{x:Static resx:ResUI.menuRegionalPresetsRussia}" />
+                                    <MenuItem
+                                        x:Name="menuRegionalPresetsIran"
+                                        Height="{StaticResource MenuItemHeight}"
+                                        Header="{x:Static resx:ResUI.menuRegionalPresetsIran}" />
                                 </MenuItem>
                                 <MenuItem
                                     x:Name="menuBackupAndRestore"
diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml.cs b/v2rayN/v2rayN/Views/MainWindow.xaml.cs
index b0292f1d..c2e745f3 100644
--- a/v2rayN/v2rayN/Views/MainWindow.xaml.cs
+++ b/v2rayN/v2rayN/Views/MainWindow.xaml.cs
@@ -102,6 +102,7 @@ namespace v2rayN.Views
                 this.BindCommand(ViewModel, vm => vm.OpenTheFileLocationCmd, v => v.menuOpenTheFileLocation).DisposeWith(disposables);
                 this.BindCommand(ViewModel, vm => vm.RegionalPresetDefaultCmd, v => v.menuRegionalPresetsDefault).DisposeWith(disposables);
                 this.BindCommand(ViewModel, vm => vm.RegionalPresetRussiaCmd, v => v.menuRegionalPresetsRussia).DisposeWith(disposables);
+                this.BindCommand(ViewModel, vm => vm.RegionalPresetIranCmd, v => v.menuRegionalPresetsIran).DisposeWith(disposables);
 
                 this.BindCommand(ViewModel, vm => vm.ReloadCmd, v => v.menuReload).DisposeWith(disposables);
                 this.OneWayBind(ViewModel, vm => vm.BlReloadEnabled, v => v.menuReload.IsEnabled).DisposeWith(disposables);