From c42dcd287638e0fc226037be2efbdf83b673bf57 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Sat, 17 Jan 2026 16:08:36 +0800 Subject: [PATCH] Add process matching rules support (#8643) * Add process matching rules support * Fix --- v2rayN/ServiceLib/Models/SingboxConfig.cs | 1 + v2rayN/ServiceLib/Models/V2rayConfig.cs | 2 + v2rayN/ServiceLib/Resx/ResUI.Designer.cs | 2 +- v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx | 2 +- v2rayN/ServiceLib/Resx/ResUI.fr.resx | 2 +- v2rayN/ServiceLib/Resx/ResUI.hu.resx | 2 +- v2rayN/ServiceLib/Resx/ResUI.resx | 2 +- v2rayN/ServiceLib/Resx/ResUI.ru.resx | 2 +- v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx | 2 +- v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx | 2 +- .../Singbox/SingboxRoutingService.cs | 46 +++++++++++++++++-- .../CoreConfig/V2ray/V2rayRoutingService.cs | 15 ++++++ 12 files changed, 69 insertions(+), 11 deletions(-) diff --git a/v2rayN/ServiceLib/Models/SingboxConfig.cs b/v2rayN/ServiceLib/Models/SingboxConfig.cs index b4041772..c0cd2e08 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -68,6 +68,7 @@ public class Rule4Sbox public List? ip_cidr { get; set; } public List? source_ip_cidr { get; set; } public List? process_name { get; set; } + public List? process_path { get; set; } public List? rule_set { get; set; } public List? rules { get; set; } public string? action { get; set; } diff --git a/v2rayN/ServiceLib/Models/V2rayConfig.cs b/v2rayN/ServiceLib/Models/V2rayConfig.cs index 54566238..d9575432 100644 --- a/v2rayN/ServiceLib/Models/V2rayConfig.cs +++ b/v2rayN/ServiceLib/Models/V2rayConfig.cs @@ -259,6 +259,8 @@ public class RulesItem4Ray public List? domain { get; set; } public List? protocol { get; set; } + + public List? process { get; set; } } public class BalancersItem4Ray diff --git a/v2rayN/ServiceLib/Resx/ResUI.Designer.cs b/v2rayN/ServiceLib/Resx/ResUI.Designer.cs index 8adf0472..bf30c985 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.Designer.cs +++ b/v2rayN/ServiceLib/Resx/ResUI.Designer.cs @@ -3286,7 +3286,7 @@ namespace ServiceLib.Resx { } /// - /// 查找类似 Full process name (Tun mode) 的本地化字符串。 + /// 查找类似 Process (Tun mode) 的本地化字符串。 /// public static string TbRoutingRuleProcess { get { diff --git a/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx b/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx index 1c9cd81d..3a720cdd 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx @@ -1027,7 +1027,7 @@ پروتکل sing-box Mux - نام کامل فرانید (حالت Tun) + Process (Tun mode) IP or IP CIDR diff --git a/v2rayN/ServiceLib/Resx/ResUI.fr.resx b/v2rayN/ServiceLib/Resx/ResUI.fr.resx index 1531421d..f1710de6 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.fr.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.fr.resx @@ -1024,7 +1024,7 @@ Protocole de multiplexage Mux (sing-box) - Nom complet du processus (mode Tun) + Process (Tun mode) IP ou IP CIDR diff --git a/v2rayN/ServiceLib/Resx/ResUI.hu.resx b/v2rayN/ServiceLib/Resx/ResUI.hu.resx index c7a852c9..4787f0f1 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.hu.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.hu.resx @@ -1027,7 +1027,7 @@ sing-box Mux protokoll - Teljes folyamatnév (Tun mód) + Process (Tun mode) IP vagy IP CIDR diff --git a/v2rayN/ServiceLib/Resx/ResUI.resx b/v2rayN/ServiceLib/Resx/ResUI.resx index b07980ab..6ad8a6e4 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.resx @@ -1027,7 +1027,7 @@ sing-box Mux Protocol - Full process name (Tun mode) + Process (Tun mode) IP or IP CIDR diff --git a/v2rayN/ServiceLib/Resx/ResUI.ru.resx b/v2rayN/ServiceLib/Resx/ResUI.ru.resx index 16267356..3a0f9fef 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.ru.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.ru.resx @@ -1027,7 +1027,7 @@ Протокол Mux для sing-box - Полное имя процесса (режим TUN) + Process (Tun mode) IP-адрес или сеть CIDR diff --git a/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx b/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx index 33b531ab..b37e0ccb 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx @@ -1024,7 +1024,7 @@ sing-box Mux 多路复用协议 - 进程名全称 (Tun 模式) + 进程 (Tun 模式) IP 或 IP CIDR diff --git a/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx b/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx index b04e73a4..b5acd049 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx @@ -1024,7 +1024,7 @@ sing-box Mux 多路復用協定 - 行程名全稱 (Tun 模式) + 行程 (Tun 模式) IP 或 IP CIDR diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs index b9e1cc1f..58bcaf99 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs @@ -280,9 +280,49 @@ public partial class CoreConfigSingboxService if (_config.TunModeItem.EnableTun && item.Process?.Count > 0) { - rule3.process_name = item.Process; - rules.Add(rule3); - hasDomainIp = true; + var ruleProcName = JsonUtils.DeepCopy(rule3); + var ruleProcPath = JsonUtils.DeepCopy(rule3); + foreach (var process in item.Process) + { + // sing-box doesn't support this, fall back to process name match + if (process is "self/" or "xray/") + { + ruleProcName.process_name.Add(Utils.GetExeName("sing-box")); + continue; + } + + if (process.Contains('/') || process.Contains('\\')) + { + var procPath = process; + if (Utils.IsWindows()) + { + procPath = procPath.Replace('/', '\\'); + } + ruleProcPath.process_path.Add(procPath); + continue; + } + + // sing-box strictly matches the exe suffix on Windows + var procName = process; + if (Utils.IsWindows() && !procName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) + { + procName += ".exe"; + } + + ruleProcName.process_name.Add(procName); + } + + if (ruleProcName.process_name.Count > 0) + { + rules.Add(ruleProcName); + hasDomainIp = true; + } + + if (ruleProcPath.process_path.Count > 0) + { + rules.Add(ruleProcPath); + hasDomainIp = true; + } } if (!hasDomainIp diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs index 83df2ab2..96984094 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs @@ -77,12 +77,17 @@ public partial class CoreConfigV2rayService { rule.inboundTag = null; } + if (rule.process?.Count == 0) + { + rule.process = null; + } var hasDomainIp = false; if (rule.domain?.Count > 0) { var it = JsonUtils.DeepCopy(rule); it.ip = null; + it.process = null; it.type = "field"; for (var k = it.domain.Count - 1; k >= 0; k--) { @@ -99,6 +104,16 @@ public partial class CoreConfigV2rayService { var it = JsonUtils.DeepCopy(rule); it.domain = null; + it.process = null; + it.type = "field"; + v2rayConfig.routing.rules.Add(it); + hasDomainIp = true; + } + if (_config.TunModeItem.EnableTun && rule.process?.Count > 0) + { + var it = JsonUtils.DeepCopy(rule); + it.domain = null; + it.ip = null; it.type = "field"; v2rayConfig.routing.rules.Add(it); hasDomainIp = true;