diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index 1bebc68b..7b2813d1 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -35,6 +35,8 @@ public const string v2raySampleInbound = "v2rayN.Sample.SampleInbound"; public const string TunSingboxFileName = "v2rayN.Sample.tun_singbox"; public const string TunSingboxDNSFileName = "v2rayN.Sample.tun_singbox_dns"; + public const string TunSingboxInboundFileName = "v2rayN.Sample.tun_singbox_inbound"; + public const string TunSingboxRulesFileName = "v2rayN.Sample.tun_singbox_rules"; public const string DefaultSecurity = "auto"; public const string DefaultNetwork = "tcp"; diff --git a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs index adb5d522..d4ff68c1 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs @@ -77,6 +77,10 @@ namespace v2rayN.Handler singboxConfig.log.level = _config.coreBasicItem.loglevel; break; + case "warn": + singboxConfig.log.level = "warning"; + break; + default: break; } @@ -101,27 +105,50 @@ namespace v2rayN.Handler { try { - var inbound = singboxConfig.inbounds[0]; - inbound.listen_port = LazyConfig.Instance.GetLocalPort(Global.InboundSocks); - inbound.sniff = _config.inbound[0].sniffingEnabled; - inbound.sniff_override_destination = _config.inbound[0].sniffingEnabled; - - //http - var inbound2 = Utils.DeepCopy(inbound); - inbound2.listen_port = LazyConfig.Instance.GetLocalPort(Global.InboundHttp); - inbound2.type = Global.InboundHttp; - inbound2.tag = Global.InboundHttp; - singboxConfig.inbounds.Add(inbound2); - - if (_config.inbound[0].allowLANConn) + if (_config.tunModeItem.enableTun) { - if (_config.inbound[0].newPort4LAN) + singboxConfig.inbounds.Clear(); + + if (_config.tunModeItem.mtu <= 0) { + _config.tunModeItem.mtu = Convert.ToInt32(Global.TunMtus[0]); } - else + if (Utils.IsNullOrEmpty(_config.tunModeItem.stack)) { - inbound.listen = "::"; - inbound2.listen = "::"; + _config.tunModeItem.stack = Global.TunStacks[0]; + } + + var tunInbound = Utils.FromJson(Utils.GetEmbedText(Global.TunSingboxInboundFileName)); + tunInbound.mtu = _config.tunModeItem.mtu; + tunInbound.strict_route = _config.tunModeItem.strictRoute; + tunInbound.stack = _config.tunModeItem.stack; + + singboxConfig.inbounds.Add(tunInbound); + } + else + { + var inbound = singboxConfig.inbounds[0]; + inbound.listen_port = LazyConfig.Instance.GetLocalPort(Global.InboundSocks); + inbound.sniff = _config.inbound[0].sniffingEnabled; + inbound.sniff_override_destination = _config.inbound[0].sniffingEnabled; + + //http + var inbound2 = Utils.DeepCopy(inbound); + inbound2.listen_port = LazyConfig.Instance.GetLocalPort(Global.InboundHttp); + inbound2.type = Global.InboundHttp; + inbound2.tag = Global.InboundHttp; + singboxConfig.inbounds.Add(inbound2); + + if (_config.inbound[0].allowLANConn) + { + if (_config.inbound[0].newPort4LAN) + { + } + else + { + inbound.listen = "::"; + inbound2.listen = "::"; + } } } } @@ -138,6 +165,15 @@ namespace v2rayN.Handler { try { + if (_config.tunModeItem.enableTun) + { + singboxConfig.outbounds.Add(new() + { + type = "dns", + tag = "dns_out" + }); + } + var outbound = singboxConfig.outbounds[0]; outbound.server = node.address; outbound.server_port = node.port; @@ -326,6 +362,28 @@ namespace v2rayN.Handler { try { + if (_config.tunModeItem.enableTun) + { + singboxConfig.route.auto_detect_interface = true; + + var tunRules = Utils.FromJson>(Utils.GetEmbedText(Global.TunSingboxRulesFileName)); + singboxConfig.route.rules.AddRange(tunRules); + + routingDirectExe(out List lstDnsExe, out List lstDirectExe); + singboxConfig.route.rules.Add(new() + { + port = new() { 53 }, + outbound = "dns_out", + process_name = lstDnsExe + }); + + singboxConfig.route.rules.Add(new() + { + outbound = "direct", + process_name = lstDirectExe + }); + } + if (_config.routingBasicItem.enableRoutingAdvanced) { var routing = ConfigHandler.GetDefaultRouting(ref _config); @@ -361,6 +419,32 @@ namespace v2rayN.Handler return 0; } + private void routingDirectExe(out List lstDnsExe, out List lstDirectExe) + { + lstDnsExe = new(); + lstDirectExe = new(); + var coreInfos = LazyConfig.Instance.GetCoreInfos(); + foreach (var it in coreInfos) + { + if (it.coreType == ECoreType.v2rayN) + { + continue; + } + foreach (var it2 in it.coreExes) + { + if (!lstDnsExe.Contains(it2) && it.coreType != ECoreType.sing_box) + { + lstDnsExe.Add($"{it2}.exe"); + } + + if (!lstDirectExe.Contains(it2)) + { + lstDirectExe.Add($"{it2}.exe"); + } + } + } + } + private int routingUserRule(RulesItem item, List rules) { try @@ -497,18 +581,27 @@ namespace v2rayN.Handler { try { - var item = LazyConfig.Instance.GetDNSItem(ECoreType.sing_box); - var normalDNS = item?.normalDNS; - if (string.IsNullOrWhiteSpace(normalDNS)) - { - return 0; - } - - var obj = Utils.ParseJson(normalDNS); - if (obj?.ContainsKey("servers") == true) + if (_config.tunModeItem.enableTun) { + var tunDNS = Utils.GetEmbedText(Global.TunSingboxDNSFileName); + var obj = Utils.ParseJson(tunDNS); singboxConfig.dns = obj; } + else + { + var item = LazyConfig.Instance.GetDNSItem(ECoreType.sing_box); + var normalDNS = item?.normalDNS; + if (string.IsNullOrWhiteSpace(normalDNS)) + { + return 0; + } + + var obj = Utils.ParseJson(normalDNS); + if (obj?.ContainsKey("servers") == true) + { + singboxConfig.dns = obj; + } + } } catch (Exception ex) { diff --git a/v2rayN/v2rayN/Mode/SingboxConfig.cs b/v2rayN/v2rayN/Mode/SingboxConfig.cs index dfcf0f4d..1afcf0d6 100644 --- a/v2rayN/v2rayN/Mode/SingboxConfig.cs +++ b/v2rayN/v2rayN/Mode/SingboxConfig.cs @@ -26,8 +26,8 @@ public class Route4Sbox { - public List rules { get; set; } public bool? auto_detect_interface { get; set; } + public List rules { get; set; } } [Serializable] @@ -40,6 +40,7 @@ public List? protocol { get; set; } public string type { get; set; } public string mode { get; set; } + public string network { get; set; } public List? port { get; set; } public List? port_range { get; set; } public List? geosite { get; set; } @@ -49,6 +50,7 @@ public List? domain_regex { get; set; } public List? geoip { get; set; } public List? ip_cidr { get; set; } + public List? source_ip_cidr { get; set; } public List? process_name { get; set; } } @@ -59,7 +61,7 @@ public string type { get; set; } public string tag { get; set; } public string listen { get; set; } - public int listen_port { get; set; } + public int? listen_port { get; set; } public string domain_strategy { get; set; } public string interface_name { get; set; } public string inet4_address { get; set; } diff --git a/v2rayN/v2rayN/Sample/tun_singbox_inbound b/v2rayN/v2rayN/Sample/tun_singbox_inbound new file mode 100644 index 00000000..845ea137 --- /dev/null +++ b/v2rayN/v2rayN/Sample/tun_singbox_inbound @@ -0,0 +1,12 @@ +{ + "type":"tun", + "tag":"tun-in", + "interface_name":"singbox_tun", + "inet4_address":"172.19.0.1/30", + "mtu":9000, + "auto_route":true, + "strict_route":false, + "stack":"system", + "endpoint_independent_nat":true, + "sniff":true +} \ No newline at end of file diff --git a/v2rayN/v2rayN/Sample/tun_singbox_rules b/v2rayN/v2rayN/Sample/tun_singbox_rules new file mode 100644 index 00000000..93762e27 --- /dev/null +++ b/v2rayN/v2rayN/Sample/tun_singbox_rules @@ -0,0 +1,35 @@ +[ + { + "inbound": [ "dns_in" ], + "outbound": "dns_out" + }, + { + "protocol": [ "dns" ], + "outbound": "dns_out" + }, + { + "network": "udp", + "port": [ + 135, + 137, + 138, + 139, + 5353 + ], + "outbound": "block" + }, + { + "ip_cidr": [ + "224.0.0.0/3", + "ff00::/8" + ], + "outbound": "block" + }, + { + "source_ip_cidr": [ + "224.0.0.0/3", + "ff00::/8" + ], + "outbound": "block" + } +] \ No newline at end of file diff --git a/v2rayN/v2rayN/v2rayN.csproj b/v2rayN/v2rayN/v2rayN.csproj index eeb85117..bcf2fac6 100644 --- a/v2rayN/v2rayN/v2rayN.csproj +++ b/v2rayN/v2rayN/v2rayN.csproj @@ -12,7 +12,7 @@ Copyright © 2017-2023 (GPLv3) 6.23 - + @@ -67,6 +67,12 @@ Never + + Never + + + Never + Never