generate a single file for sing-box tun mode

pull/3773/head
2dust 2023-04-25 20:27:21 +08:00
parent b9beb4be9d
commit 4020213729
6 changed files with 179 additions and 29 deletions

View File

@ -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";

View File

@ -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<Inbound4Sbox>(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<List<Rule4Sbox>>(Utils.GetEmbedText(Global.TunSingboxRulesFileName));
singboxConfig.route.rules.AddRange(tunRules);
routingDirectExe(out List<string> lstDnsExe, out List<string> 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<string> lstDnsExe, out List<string> 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<Rule4Sbox> 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)
{

View File

@ -26,8 +26,8 @@
public class Route4Sbox
{
public List<Rule4Sbox> rules { get; set; }
public bool? auto_detect_interface { get; set; }
public List<Rule4Sbox> rules { get; set; }
}
[Serializable]
@ -40,6 +40,7 @@
public List<string>? protocol { get; set; }
public string type { get; set; }
public string mode { get; set; }
public string network { get; set; }
public List<int>? port { get; set; }
public List<string>? port_range { get; set; }
public List<string>? geosite { get; set; }
@ -49,6 +50,7 @@
public List<string>? domain_regex { get; set; }
public List<string>? geoip { get; set; }
public List<string>? ip_cidr { get; set; }
public List<string>? source_ip_cidr { get; set; }
public List<string>? 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; }

View File

@ -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
}

View File

@ -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"
}
]

View File

@ -12,7 +12,7 @@
<Copyright>Copyright © 2017-2023 (GPLv3)</Copyright>
<FileVersion>6.23</FileVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Downloader" Version="3.0.4" />
<PackageReference Include="MaterialDesignThemes" Version="4.7.1" />
@ -67,6 +67,12 @@
<EmbeddedResource Include="Sample\SampleInbound">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Sample\tun_singbox_rules">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Sample\tun_singbox_inbound">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="v2rayN.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>