diff --git a/v2rayN/ServiceLib/Enums/EViewAction.cs b/v2rayN/ServiceLib/Enums/EViewAction.cs index a72e8765..bf7360e2 100644 --- a/v2rayN/ServiceLib/Enums/EViewAction.cs +++ b/v2rayN/ServiceLib/Enums/EViewAction.cs @@ -29,6 +29,7 @@ public enum EViewAction DNSSettingWindow, RoutingSettingWindow, OptionSettingWindow, + FullConfigTemplateWindow, GlobalHotkeySettingWindow, SubSettingWindow, DispatcherSpeedTest, diff --git a/v2rayN/ServiceLib/Handler/AppHandler.cs b/v2rayN/ServiceLib/Handler/AppHandler.cs index ad9a4029..18ba19b8 100644 --- a/v2rayN/ServiceLib/Handler/AppHandler.cs +++ b/v2rayN/ServiceLib/Handler/AppHandler.cs @@ -64,6 +64,7 @@ public sealed class AppHandler SQLiteHelper.Instance.CreateTable(); SQLiteHelper.Instance.CreateTable(); SQLiteHelper.Instance.CreateTable(); + SQLiteHelper.Instance.CreateTable(); return true; } @@ -203,6 +204,16 @@ public sealed class AppHandler return await SQLiteHelper.Instance.TableAsync().FirstOrDefaultAsync(it => it.CoreType == eCoreType); } + public async Task?> FullConfigTemplateItem() + { + return await SQLiteHelper.Instance.TableAsync().ToListAsync(); + } + + public async Task GetFullConfigTemplateItem(ECoreType eCoreType) + { + return await SQLiteHelper.Instance.TableAsync().FirstOrDefaultAsync(it => it.CoreType == eCoreType); + } + #endregion SqliteHelper #region Core Type diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs index 3825fe74..c05424a2 100644 --- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs @@ -2238,6 +2238,54 @@ public class ConfigHandler #endregion Simple DNS + #region Custom Config + + public static async Task InitBuiltinFullConfigTemplate(Config config) + { + var items = await AppHandler.Instance.FullConfigTemplateItem(); + if (items.Count <= 0) + { + var item = new FullConfigTemplateItem() + { + Remarks = "V2ray", + CoreType = ECoreType.Xray, + }; + await SaveFullConfigTemplate(config, item); + + var item2 = new FullConfigTemplateItem() + { + Remarks = "sing-box", + CoreType = ECoreType.sing_box, + }; + await SaveFullConfigTemplate(config, item2); + } + + return 0; + } + public static async Task SaveFullConfigTemplate(Config config, FullConfigTemplateItem item) + { + if (item == null) + { + return -1; + } + + if (item.Id.IsNullOrEmpty()) + { + item.Id = Utils.GetGuid(false); + } + + if (await SQLiteHelper.Instance.ReplaceAsync(item) > 0) + { + return 0; + } + else + { + return -1; + } + } + + #endregion Custom Config + #region Regional Presets /// diff --git a/v2rayN/ServiceLib/Models/FullConfigTemplateItem.cs b/v2rayN/ServiceLib/Models/FullConfigTemplateItem.cs new file mode 100644 index 00000000..f3881325 --- /dev/null +++ b/v2rayN/ServiceLib/Models/FullConfigTemplateItem.cs @@ -0,0 +1,18 @@ +using SQLite; + +namespace ServiceLib.Models; + +[Serializable] +public class FullConfigTemplateItem +{ + [PrimaryKey] + public string Id { get; set; } + + public string Remarks { get; set; } + public bool Enabled { get; set; } = false; + public ECoreType CoreType { get; set; } + public string? Config { get; set; } + public string? TunConfig { get; set; } + public bool? AddProxyOnly { get; set; } = false; + public string? ProxyDetour { get; set; } +} diff --git a/v2rayN/ServiceLib/Resx/ResUI.Designer.cs b/v2rayN/ServiceLib/Resx/ResUI.Designer.cs index 6a021a2b..7a30e075 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.Designer.cs +++ b/v2rayN/ServiceLib/Resx/ResUI.Designer.cs @@ -186,6 +186,15 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 Please fill in the correct config template 的本地化字符串。 + /// + public static string FillCorrectConfigTemplateText { + get { + return ResourceManager.GetString("FillCorrectConfigTemplateText", resourceCulture); + } + } + /// /// 查找类似 Please fill in the correct custom DNS 的本地化字符串。 /// @@ -933,6 +942,15 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 Full Config Template Setting 的本地化字符串。 + /// + public static string menuFullConfigTemplate { + get { + return ResourceManager.GetString("menuFullConfigTemplate", resourceCulture); + } + } + /// /// 查找类似 Global Hotkey Setting 的本地化字符串。 /// @@ -2229,6 +2247,15 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 Do Not Add Non-Proxy Protocol Outbound 的本地化字符串。 + /// + public static string TbAddProxyProtocolOutboundOnly { + get { + return ResourceManager.GetString("TbAddProxyProtocolOutboundOnly", resourceCulture); + } + } + /// /// 查找类似 Address 的本地化字符串。 /// @@ -2508,6 +2535,24 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 This feature is intended for advanced users and those with special requirements. Once enabled, it will ignore the Core's basic settings, DNS settings, and routing settings. You must ensure that the system proxy port, traffic statistics, and other related configurations are set correctly — everything will be configured by you. 的本地化字符串。 + /// + public static string TbFullConfigTemplateDesc { + get { + return ResourceManager.GetString("TbFullConfigTemplateDesc", resourceCulture); + } + } + + /// + /// 查找类似 Enable Full Config Template 的本地化字符串。 + /// + public static string TbFullConfigTemplateEnable { + get { + return ResourceManager.GetString("TbFullConfigTemplateEnable", resourceCulture); + } + } + /// /// 查找类似 Global Hotkey Settings 的本地化字符串。 /// @@ -2724,6 +2769,24 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 v2ray Full Config Template 的本地化字符串。 + /// + public static string TbRayFullConfigTemplate { + get { + return ResourceManager.GetString("TbRayFullConfigTemplate", resourceCulture); + } + } + + /// + /// 查找类似 Add Outbound Config Only, routing.balancers and routing.rules.outboundTag, Click to view the document 的本地化字符串。 + /// + public static string TbRayFullConfigTemplateDesc { + get { + return ResourceManager.GetString("TbRayFullConfigTemplateDesc", resourceCulture); + } + } + /// /// 查找类似 Alias (remarks) 的本地化字符串。 /// @@ -2886,6 +2949,24 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 sing-box Full Config Template 的本地化字符串。 + /// + public static string TbSBFullConfigTemplate { + get { + return ResourceManager.GetString("TbSBFullConfigTemplate", resourceCulture); + } + } + + /// + /// 查找类似 Add Outbound and Endpoint Config Only, Click to view the document 的本地化字符串。 + /// + public static string TbSBFullConfigTemplateDesc { + get { + return ResourceManager.GetString("TbSBFullConfigTemplateDesc", resourceCulture); + } + } + /// /// 查找类似 Resolve Outbound Domains 的本地化字符串。 /// @@ -3696,6 +3777,15 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 Set Upstream Proxy Tag 的本地化字符串。 + /// + public static string TbSetUpstreamProxyDetour { + get { + return ResourceManager.GetString("TbSetUpstreamProxyDetour", resourceCulture); + } + } + /// /// 查找类似 Short Id 的本地化字符串。 /// diff --git a/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx b/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx index 25a3c80e..82520c5f 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx @@ -1470,4 +1470,34 @@ Custom DNS Enabled, This Page's Settings Invalid + + Please fill in the correct config template + + + Full Config Template Setting + + + Enable Full Config Template + + + v2ray Full Config Template + + + Add Outbound Config Only, routing.balancers and routing.rules.outboundTag, Click to view the document + + + Do Not Add Non-Proxy Protocol Outbound + + + Set Upstream Proxy Tag + + + sing-box Full Config Template + + + Add Outbound and Endpoint Config Only, Click to view the document + + + This feature is intended for advanced users and those with special requirements. Once enabled, it will ignore the Core's basic settings, DNS settings, and routing settings. You must ensure that the system proxy port, traffic statistics, and other related configurations are set correctly — everything will be configured by you. + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Resx/ResUI.hu.resx b/v2rayN/ServiceLib/Resx/ResUI.hu.resx index 20d0ab8e..7ee68495 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.hu.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.hu.resx @@ -1470,4 +1470,34 @@ Custom DNS Enabled, This Page's Settings Invalid + + Please fill in the correct config template + + + Full Config Template Setting + + + Enable Full Config Template + + + v2ray Full Config Template + + + Add Outbound Config Only, routing.balancers and routing.rules.outboundTag, Click to view the document + + + Do Not Add Non-Proxy Protocol Outbound + + + Set Upstream Proxy Tag + + + sing-box Full Config Template + + + Add Outbound and Endpoint Config Only, Click to view the document + + + This feature is intended for advanced users and those with special requirements. Once enabled, it will ignore the Core's basic settings, DNS settings, and routing settings. You must ensure that the system proxy port, traffic statistics, and other related configurations are set correctly — everything will be configured by you. + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Resx/ResUI.resx b/v2rayN/ServiceLib/Resx/ResUI.resx index a6009434..44343dee 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.resx @@ -1470,4 +1470,34 @@ Custom DNS Enabled, This Page's Settings Invalid + + Please fill in the correct config template + + + Full Config Template Setting + + + Enable Full Config Template + + + v2ray Full Config Template + + + Add Outbound Config Only, routing.balancers and routing.rules.outboundTag, Click to view the document + + + Do Not Add Non-Proxy Protocol Outbound + + + Set Upstream Proxy Tag + + + sing-box Full Config Template + + + Add Outbound and Endpoint Config Only, Click to view the document + + + This feature is intended for advanced users and those with special requirements. Once enabled, it will ignore the Core's basic settings, DNS settings, and routing settings. You must ensure that the system proxy port, traffic statistics, and other related configurations are set correctly — everything will be configured by you. + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Resx/ResUI.ru.resx b/v2rayN/ServiceLib/Resx/ResUI.ru.resx index 6207b163..e9ab6efe 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.ru.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.ru.resx @@ -1470,4 +1470,34 @@ Custom DNS Enabled, This Page's Settings Invalid + + Please fill in the correct config template + + + Full Config Template Setting + + + Enable Full Config Template + + + v2ray Full Config Template + + + Add Outbound Config Only, routing.balancers and routing.rules.outboundTag, Click to view the document + + + Do Not Add Non-Proxy Protocol Outbound + + + Set Upstream Proxy Tag + + + sing-box Full Config Template + + + Add Outbound and Endpoint Config Only, Click to view the document + + + This feature is intended for advanced users and those with special requirements. Once enabled, it will ignore the Core's basic settings, DNS settings, and routing settings. You must ensure that the system proxy port, traffic statistics, and other related configurations are set correctly — everything will be configured by you. + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx b/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx index 72c42604..95a427c3 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx @@ -1467,4 +1467,34 @@ 自定义 DNS 已启用,此页面配置将无效 + + 请填写正确的配置模板 + + + 完整配置模板设置 + + + 启用完整配置模板 + + + v2ray 完整配置模板 + + + 仅添加出站配置,routing.balancers 和 routing.rules.outboundTag,点击查看文档 + + + 不添加非代理协议出站 + + + 设置上游代理 tag + + + sing-box 完整配置模板 + + + 仅添加出站和端点配置,点击查看文档 + + + 此功能供高级用户和有特殊需求的用户使用。 启用此功能后,将忽略 Core 的基础设置,DNS 设置 ,路由设置。你需要保证系统代理的端口和流量统计等功能的配置正确,一切都由你来设置。 + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx b/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx index 84c8f929..f35c5faa 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx @@ -1467,4 +1467,34 @@ Custom DNS Enabled, This Page's Settings Invalid + + Please fill in the correct config template + + + Full Config Template Setting + + + Enable Full Config Template + + + v2ray Full Config Template + + + Add Outbound Config Only, routing.balancers and routing.rules.outboundTag, Click to view the document + + + Do Not Add Non-Proxy Protocol Outbound + + + Set Upstream Proxy Tag + + + sing-box Full Config Template + + + Add Outbound and Endpoint Config Only, Click to view the document + + + This feature is intended for advanced users and those with special requirements. Once enabled, it will ignore the Core's basic settings, DNS settings, and routing settings. You must ensure that the system proxy port, traffic statistics, and other related configurations are set correctly — everything will be configured by you. + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 6ea20a20..6563e991 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1,6 +1,7 @@ using System.Data; using System.Net; using System.Net.NetworkInformation; +using System.Text.Json.Nodes; namespace ServiceLib.Services.CoreConfig; @@ -78,7 +79,9 @@ public class CoreConfigSingboxService ret.Msg = string.Format(ResUI.SuccessfulConfiguration, ""); ret.Success = true; - ret.Data = JsonUtils.Serialize(singboxConfig); + + var fullConfigTemplate = await AppHandler.Instance.GetFullConfigTemplateItem(ECoreType.sing_box); + ret.Data = await ApplyFullConfigTemplate(fullConfigTemplate, singboxConfig); return ret; } catch (Exception ex) @@ -430,7 +433,9 @@ public class CoreConfigSingboxService await ConvertGeo2Ruleset(singboxConfig); ret.Success = true; - ret.Data = JsonUtils.Serialize(singboxConfig); + + var fullConfigTemplate = await AppHandler.Instance.GetFullConfigTemplateItem(ECoreType.sing_box); + ret.Data = await ApplyFullConfigTemplate(fullConfigTemplate, singboxConfig); return ret; } catch (Exception ex) @@ -2197,5 +2202,61 @@ public class CoreConfigSingboxService return 0; } + private async Task ApplyFullConfigTemplate(FullConfigTemplateItem fullConfigTemplate, SingboxConfig singboxConfig) + { + var fullConfigTemplateItem = fullConfigTemplate.Config; + if (_config.TunModeItem.EnableTun) + { + fullConfigTemplateItem = fullConfigTemplate.TunConfig; + } + + if (!fullConfigTemplate.Enabled || fullConfigTemplateItem.IsNullOrEmpty()) + { + return JsonUtils.Serialize(singboxConfig); + } + + var fullConfigTemplateNode = JsonNode.Parse(fullConfigTemplateItem); + if (fullConfigTemplateNode == null) + { + return JsonUtils.Serialize(singboxConfig); + } + + // Process outbounds + var customOutboundsNode = fullConfigTemplateNode["outbounds"] is JsonArray outbounds ? outbounds : new JsonArray(); + foreach (var outbound in singboxConfig.outbounds) + { + if (outbound.type.ToLower() is "direct" or "block") + { + if (fullConfigTemplate.AddProxyOnly == true) + { + continue; + } + } + else if (outbound.detour.IsNullOrEmpty() && (!fullConfigTemplate.ProxyDetour.IsNullOrEmpty()) && !Utils.IsPrivateNetwork(outbound.server ?? string.Empty)) + { + outbound.detour = fullConfigTemplate.ProxyDetour; + } + customOutboundsNode.Add(JsonUtils.DeepCopy(outbound)); + } + fullConfigTemplateNode["outbounds"] = customOutboundsNode; + + // Process endpoints + if (singboxConfig.endpoints != null && singboxConfig.endpoints.Count > 0) + { + var customEndpointsNode = fullConfigTemplateNode["endpoints"] is JsonArray endpoints ? endpoints : new JsonArray(); + foreach (var endpoint in singboxConfig.endpoints) + { + if (endpoint.detour.IsNullOrEmpty() && (!fullConfigTemplate.ProxyDetour.IsNullOrEmpty())) + { + endpoint.detour = fullConfigTemplate.ProxyDetour; + } + customEndpointsNode.Add(JsonUtils.DeepCopy(endpoint)); + } + fullConfigTemplateNode["endpoints"] = customEndpointsNode; + } + + return await Task.FromResult(JsonUtils.Serialize(fullConfigTemplateNode)); + } + #endregion private gen function } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs index 1986be14..96388ed3 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs @@ -3,6 +3,7 @@ using System.Net.NetworkInformation; using System.Text.Json; using System.Text.Json.Nodes; using System.Text.Json.Serialization; +using ServiceLib.Models; namespace ServiceLib.Services.CoreConfig; @@ -68,7 +69,9 @@ public class CoreConfigV2rayService ret.Msg = string.Format(ResUI.SuccessfulConfiguration, ""); ret.Success = true; - ret.Data = JsonUtils.Serialize(v2rayConfig); + + var fullConfigTemplate = await AppHandler.Instance.GetFullConfigTemplateItem(ECoreType.Xray); + ret.Data = await ApplyFullConfigTemplate(fullConfigTemplate, v2rayConfig); return ret; } catch (Exception ex) @@ -197,7 +200,9 @@ public class CoreConfigV2rayService } ret.Success = true; - ret.Data = JsonUtils.Serialize(v2rayConfig); + + var fullConfigTemplate = await AppHandler.Instance.GetFullConfigTemplateItem(ECoreType.Xray); + ret.Data = await ApplyFullConfigTemplate(fullConfigTemplate, v2rayConfig, true); return ret; } catch (Exception ex) @@ -1462,7 +1467,7 @@ public class CoreConfigV2rayService await GenDnsDomainsCompatible(node, obj, item); - v2rayConfig.dns = JsonUtils.Deserialize(obj.ToJsonString()); + v2rayConfig.dns = JsonUtils.Deserialize(JsonUtils.Serialize(obj)); } catch (Exception ex) { @@ -1839,5 +1844,83 @@ public class CoreConfigV2rayService return await Task.FromResult(0); } + private async Task ApplyFullConfigTemplate(FullConfigTemplateItem fullConfigTemplate, V2rayConfig v2rayConfig, bool handleBalancerAndRules = false) + { + if (!fullConfigTemplate.Enabled || fullConfigTemplate.Config.IsNullOrEmpty()) + { + return JsonUtils.Serialize(v2rayConfig); + } + + var fullConfigTemplateNode = JsonNode.Parse(fullConfigTemplate.Config); + if (fullConfigTemplateNode == null) + { + return JsonUtils.Serialize(v2rayConfig); + } + + // Handle balancer and rules modifications (for multiple load scenarios) + if (handleBalancerAndRules && v2rayConfig.routing?.balancers?.Count > 0) + { + var balancer = v2rayConfig.routing.balancers.First(); + + // Modify existing rules in custom config + var rulesNode = fullConfigTemplateNode["routing"]?["rules"]; + if (rulesNode != null) + { + foreach (var rule in rulesNode.AsArray()) + { + if (rule["outboundTag"]?.GetValue() == Global.ProxyTag) + { + rule.AsObject().Remove("outboundTag"); + rule["balancerTag"] = balancer.tag; + } + } + } + + // Ensure routing node exists + if (fullConfigTemplateNode["routing"] == null) + { + fullConfigTemplateNode["routing"] = new JsonObject(); + } + + // Handle balancers - append instead of override + if (fullConfigTemplateNode["routing"]["balancers"] is JsonArray customBalancersNode) + { + if (JsonNode.Parse(JsonUtils.Serialize(v2rayConfig.routing.balancers)) is JsonArray newBalancers) + { + foreach (var balancerNode in newBalancers) + { + customBalancersNode.Add(balancerNode?.DeepClone()); + } + } + } + else + { + fullConfigTemplateNode["routing"]["balancers"] = JsonNode.Parse(JsonUtils.Serialize(v2rayConfig.routing.balancers)); + } + } + + // Handle outbounds - append instead of override + var customOutboundsNode = fullConfigTemplateNode["outbounds"] is JsonArray outbounds ? outbounds : new JsonArray(); + foreach (var outbound in v2rayConfig.outbounds) + { + if (outbound.protocol.ToLower() is "blackhole" or "dns" or "freedom") + { + if (fullConfigTemplate.AddProxyOnly == true) + { + continue; + } + } + else if ((outbound.streamSettings?.sockopt?.dialerProxy.IsNullOrEmpty() == true) && (!fullConfigTemplate.ProxyDetour.IsNullOrEmpty()) && !(Utils.IsPrivateNetwork(outbound.settings?.servers?.FirstOrDefault()?.address ?? string.Empty) || Utils.IsPrivateNetwork(outbound.settings?.vnext?.FirstOrDefault()?.address ?? string.Empty))) + { + outbound.streamSettings ??= new StreamSettings4Ray(); + outbound.streamSettings.sockopt ??= new Sockopt4Ray(); + outbound.streamSettings.sockopt.dialerProxy = fullConfigTemplate.ProxyDetour; + } + customOutboundsNode.Add(JsonUtils.DeepCopy(outbound)); + } + + return await Task.FromResult(JsonUtils.Serialize(fullConfigTemplateNode)); + } + #endregion private gen function } diff --git a/v2rayN/ServiceLib/ViewModels/FullConfigTemplateViewModel.cs b/v2rayN/ServiceLib/ViewModels/FullConfigTemplateViewModel.cs new file mode 100644 index 00000000..3aa618bf --- /dev/null +++ b/v2rayN/ServiceLib/ViewModels/FullConfigTemplateViewModel.cs @@ -0,0 +1,110 @@ +using System.Reactive; +using DynamicData.Binding; +using ReactiveUI; +using ReactiveUI.Fody.Helpers; + +namespace ServiceLib.ViewModels; +public class FullConfigTemplateViewModel : MyReactiveObject +{ + #region Reactive + [Reactive] + public bool EnableFullConfigTemplate4Ray { get; set; } + + [Reactive] + public bool EnableFullConfigTemplate4Singbox { get; set; } + + [Reactive] + public string FullConfigTemplate4Ray { get; set; } + + [Reactive] + public string FullConfigTemplate4Singbox { get; set; } + + [Reactive] + public string FullTunConfigTemplate4Singbox { get; set; } + + [Reactive] + public bool AddProxyOnly4Ray { get; set; } + + [Reactive] + public bool AddProxyOnly4Singbox { get; set; } + + [Reactive] + public string ProxyDetour4Ray { get; set; } + + [Reactive] + public string ProxyDetour4Singbox { get; set; } + + public ReactiveCommand SaveCmd { get; } + #endregion Reactive + + public FullConfigTemplateViewModel(Func>? updateView) + { + _config = AppHandler.Instance.Config; + _updateView = updateView; + SaveCmd = ReactiveCommand.CreateFromTask(async () => + { + await SaveSettingAsync(); + }); + + _ = Init(); + } + private async Task Init() + { + var item = await AppHandler.Instance.GetFullConfigTemplateItem(ECoreType.Xray); + EnableFullConfigTemplate4Ray = item?.Enabled ?? false; + FullConfigTemplate4Ray = item?.Config ?? string.Empty; + AddProxyOnly4Ray = item?.AddProxyOnly ?? false; + ProxyDetour4Ray = item?.ProxyDetour ?? string.Empty; + + var item2 = await AppHandler.Instance.GetFullConfigTemplateItem(ECoreType.sing_box); + EnableFullConfigTemplate4Singbox = item2?.Enabled ?? false; + FullConfigTemplate4Singbox = item2?.Config ?? string.Empty; + FullTunConfigTemplate4Singbox = item2?.TunConfig ?? string.Empty; + AddProxyOnly4Singbox = item2?.AddProxyOnly ?? false; + ProxyDetour4Singbox = item2?.ProxyDetour ?? string.Empty; + } + + private async Task SaveSettingAsync() + { + if (!await SaveXrayConfigAsync()) + return; + + if (!await SaveSingboxConfigAsync()) + return; + + NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess); + _ = _updateView?.Invoke(EViewAction.CloseWindow, null); + } + + private async Task SaveXrayConfigAsync() + { + var item = await AppHandler.Instance.GetFullConfigTemplateItem(ECoreType.Xray); + item.Enabled = EnableFullConfigTemplate4Ray; + item.Config = null; + + item.Config = FullConfigTemplate4Ray; + + item.AddProxyOnly = AddProxyOnly4Ray; + item.ProxyDetour = ProxyDetour4Ray; + + await ConfigHandler.SaveFullConfigTemplate(_config, item); + return true; + } + + private async Task SaveSingboxConfigAsync() + { + var item = await AppHandler.Instance.GetFullConfigTemplateItem(ECoreType.sing_box); + item.Enabled = EnableFullConfigTemplate4Singbox; + item.Config = null; + item.TunConfig = null; + + item.Config = FullConfigTemplate4Singbox; + item.TunConfig = FullTunConfigTemplate4Singbox; + + item.AddProxyOnly = AddProxyOnly4Singbox; + item.ProxyDetour = ProxyDetour4Singbox; + + await ConfigHandler.SaveFullConfigTemplate(_config, item); + return true; + } +} diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index 36e20a87..8355996d 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -39,6 +39,7 @@ public class MainWindowViewModel : MyReactiveObject public ReactiveCommand RoutingSettingCmd { get; } public ReactiveCommand DNSSettingCmd { get; } + public ReactiveCommand FullConfigTemplateCmd { get; } public ReactiveCommand GlobalHotkeySettingCmd { get; } public ReactiveCommand RebootAsAdminCmd { get; } public ReactiveCommand ClearServerStatisticsCmd { get; } @@ -169,6 +170,10 @@ public class MainWindowViewModel : MyReactiveObject { await DNSSettingAsync(); }); + FullConfigTemplateCmd = ReactiveCommand.CreateFromTask(async () => + { + await FullConfigTemplateAsync(); + }); GlobalHotkeySettingCmd = ReactiveCommand.CreateFromTask(async () => { if (await _updateView?.Invoke(EViewAction.GlobalHotkeySettingWindow, null) == true) @@ -220,6 +225,7 @@ public class MainWindowViewModel : MyReactiveObject await ConfigHandler.InitBuiltinRouting(_config); await ConfigHandler.InitBuiltinDNS(_config); + await ConfigHandler.InitBuiltinFullConfigTemplate(_config); await ProfileExHandler.Instance.Init(); await CoreHandler.Instance.Init(_config, UpdateHandler); TaskHandler.Instance.RegUpdateTask(_config, UpdateTaskHandler); @@ -508,6 +514,15 @@ public class MainWindowViewModel : MyReactiveObject } } + private async Task FullConfigTemplateAsync() + { + var ret = await _updateView?.Invoke(EViewAction.FullConfigTemplateWindow, null); + if (ret == true) + { + await Reload(); + } + } + public async Task RebootAsAdmin() { ProcUtils.RebootAsAdmin(); diff --git a/v2rayN/v2rayN.Desktop/Views/FullConfigTemplateWindow.axaml b/v2rayN/v2rayN.Desktop/Views/FullConfigTemplateWindow.axaml new file mode 100644 index 00000000..bdeb1c93 --- /dev/null +++ b/v2rayN/v2rayN.Desktop/Views/FullConfigTemplateWindow.axaml @@ -0,0 +1,198 @@ + + + +