mirror of https://github.com/2dust/v2rayN
generate a single file for sing-box tun mode
parent
b9beb4be9d
commit
4020213729
|
@ -35,6 +35,8 @@
|
||||||
public const string v2raySampleInbound = "v2rayN.Sample.SampleInbound";
|
public const string v2raySampleInbound = "v2rayN.Sample.SampleInbound";
|
||||||
public const string TunSingboxFileName = "v2rayN.Sample.tun_singbox";
|
public const string TunSingboxFileName = "v2rayN.Sample.tun_singbox";
|
||||||
public const string TunSingboxDNSFileName = "v2rayN.Sample.tun_singbox_dns";
|
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 DefaultSecurity = "auto";
|
||||||
public const string DefaultNetwork = "tcp";
|
public const string DefaultNetwork = "tcp";
|
||||||
|
|
|
@ -77,6 +77,10 @@ namespace v2rayN.Handler
|
||||||
singboxConfig.log.level = _config.coreBasicItem.loglevel;
|
singboxConfig.log.level = _config.coreBasicItem.loglevel;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "warn":
|
||||||
|
singboxConfig.log.level = "warning";
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -100,6 +104,28 @@ namespace v2rayN.Handler
|
||||||
private int inbound(SingboxConfig singboxConfig)
|
private int inbound(SingboxConfig singboxConfig)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
if (_config.tunModeItem.enableTun)
|
||||||
|
{
|
||||||
|
singboxConfig.inbounds.Clear();
|
||||||
|
|
||||||
|
if (_config.tunModeItem.mtu <= 0)
|
||||||
|
{
|
||||||
|
_config.tunModeItem.mtu = Convert.ToInt32(Global.TunMtus[0]);
|
||||||
|
}
|
||||||
|
if (Utils.IsNullOrEmpty(_config.tunModeItem.stack))
|
||||||
|
{
|
||||||
|
_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];
|
var inbound = singboxConfig.inbounds[0];
|
||||||
inbound.listen_port = LazyConfig.Instance.GetLocalPort(Global.InboundSocks);
|
inbound.listen_port = LazyConfig.Instance.GetLocalPort(Global.InboundSocks);
|
||||||
|
@ -125,6 +151,7 @@ namespace v2rayN.Handler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Utils.SaveLog(ex.Message, ex);
|
Utils.SaveLog(ex.Message, ex);
|
||||||
|
@ -138,6 +165,15 @@ namespace v2rayN.Handler
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (_config.tunModeItem.enableTun)
|
||||||
|
{
|
||||||
|
singboxConfig.outbounds.Add(new()
|
||||||
|
{
|
||||||
|
type = "dns",
|
||||||
|
tag = "dns_out"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
var outbound = singboxConfig.outbounds[0];
|
var outbound = singboxConfig.outbounds[0];
|
||||||
outbound.server = node.address;
|
outbound.server = node.address;
|
||||||
outbound.server_port = node.port;
|
outbound.server_port = node.port;
|
||||||
|
@ -326,6 +362,28 @@ namespace v2rayN.Handler
|
||||||
{
|
{
|
||||||
try
|
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)
|
if (_config.routingBasicItem.enableRoutingAdvanced)
|
||||||
{
|
{
|
||||||
var routing = ConfigHandler.GetDefaultRouting(ref _config);
|
var routing = ConfigHandler.GetDefaultRouting(ref _config);
|
||||||
|
@ -361,6 +419,32 @@ namespace v2rayN.Handler
|
||||||
return 0;
|
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)
|
private int routingUserRule(RulesItem item, List<Rule4Sbox> rules)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@ -496,6 +580,14 @@ namespace v2rayN.Handler
|
||||||
private int dns(SingboxConfig singboxConfig)
|
private int dns(SingboxConfig singboxConfig)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
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 item = LazyConfig.Instance.GetDNSItem(ECoreType.sing_box);
|
||||||
var normalDNS = item?.normalDNS;
|
var normalDNS = item?.normalDNS;
|
||||||
|
@ -510,6 +602,7 @@ namespace v2rayN.Handler
|
||||||
singboxConfig.dns = obj;
|
singboxConfig.dns = obj;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Utils.SaveLog(ex.Message, ex);
|
Utils.SaveLog(ex.Message, ex);
|
||||||
|
|
|
@ -26,8 +26,8 @@
|
||||||
|
|
||||||
public class Route4Sbox
|
public class Route4Sbox
|
||||||
{
|
{
|
||||||
public List<Rule4Sbox> rules { get; set; }
|
|
||||||
public bool? auto_detect_interface { get; set; }
|
public bool? auto_detect_interface { get; set; }
|
||||||
|
public List<Rule4Sbox> rules { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
|
@ -40,6 +40,7 @@
|
||||||
public List<string>? protocol { get; set; }
|
public List<string>? protocol { get; set; }
|
||||||
public string type { get; set; }
|
public string type { get; set; }
|
||||||
public string mode { get; set; }
|
public string mode { get; set; }
|
||||||
|
public string network { get; set; }
|
||||||
public List<int>? port { get; set; }
|
public List<int>? port { get; set; }
|
||||||
public List<string>? port_range { get; set; }
|
public List<string>? port_range { get; set; }
|
||||||
public List<string>? geosite { get; set; }
|
public List<string>? geosite { get; set; }
|
||||||
|
@ -49,6 +50,7 @@
|
||||||
public List<string>? domain_regex { get; set; }
|
public List<string>? domain_regex { get; set; }
|
||||||
public List<string>? geoip { get; set; }
|
public List<string>? geoip { get; set; }
|
||||||
public List<string>? ip_cidr { get; set; }
|
public List<string>? ip_cidr { get; set; }
|
||||||
|
public List<string>? source_ip_cidr { get; set; }
|
||||||
|
|
||||||
public List<string>? process_name { get; set; }
|
public List<string>? process_name { get; set; }
|
||||||
}
|
}
|
||||||
|
@ -59,7 +61,7 @@
|
||||||
public string type { get; set; }
|
public string type { get; set; }
|
||||||
public string tag { get; set; }
|
public string tag { get; set; }
|
||||||
public string listen { 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 domain_strategy { get; set; }
|
||||||
public string interface_name { get; set; }
|
public string interface_name { get; set; }
|
||||||
public string inet4_address { get; set; }
|
public string inet4_address { get; set; }
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -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"
|
||||||
|
}
|
||||||
|
]
|
|
@ -67,6 +67,12 @@
|
||||||
<EmbeddedResource Include="Sample\SampleInbound">
|
<EmbeddedResource Include="Sample\SampleInbound">
|
||||||
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
|
||||||
</EmbeddedResource>
|
</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">
|
<EmbeddedResource Include="v2rayN.ico">
|
||||||
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
|
||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
|
|
Loading…
Reference in New Issue