Improved tun mode

Enabling tun mode.
Only one core will be run when using sing-box core;
When using a non-sing-box, the sing-box will be used to start an additional front Socks service to provide a tun entry, which will then run two cores
pull/5377/head
2dust 2024-06-24 15:19:42 +08:00
parent b77cc3c33b
commit 620422350f
13 changed files with 105 additions and 52 deletions

View File

@ -9,7 +9,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.27.0" /> <PackageReference Include="Google.Protobuf" Version="3.27.1" />
<PackageReference Include="Grpc.Net.Client" Version="2.63.0" /> <PackageReference Include="Grpc.Net.Client" Version="2.63.0" />
<PackageReference Include="Grpc.Tools" Version="2.64.0"> <PackageReference Include="Grpc.Tools" Version="2.64.0">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>

View File

@ -8,6 +8,7 @@
http2, http2,
pac, pac,
api, api,
api2,
speedtest = 21 speedtest = 21
} }
} }

View File

@ -23,7 +23,8 @@ namespace v2rayN
public const string SpeedPingTestUrl = @"https://www.google.com/generate_204"; public const string SpeedPingTestUrl = @"https://www.google.com/generate_204";
public const string JuicityCoreUrl = "https://github.com/juicity/juicity/releases"; public const string JuicityCoreUrl = "https://github.com/juicity/juicity/releases";
public const string CustomRoutingListUrl = @"https://raw.githubusercontent.com/2dust/v2rayCustomRoutingList/master/"; public const string CustomRoutingListUrl = @"https://raw.githubusercontent.com/2dust/v2rayCustomRoutingList/master/";
public const string SingboxRulesetUrl = @"https://raw.githubusercontent.com/SagerNet/sing-{0}/rule-set/{1}.srs"; public const string SingboxRulesetUrlGeosite = @"https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-{0}.srs";
public const string SingboxRulesetUrlGeoip = @"https://raw.githubusercontent.com/Loyalsoldier/geoip/release/srs/{0}.srs";
public const string PromotionUrl = @"aHR0cHM6Ly85LjIzNDQ1Ni54eXovYWJjLmh0bWw="; public const string PromotionUrl = @"aHR0cHM6Ly85LjIzNDQ1Ni54eXovYWJjLmh0bWw=";
public const string ConfigFileName = "guiNConfig.json"; public const string ConfigFileName = "guiNConfig.json";

View File

@ -1615,7 +1615,7 @@ namespace v2rayN.Handler
public static int InitBuiltinRouting(Config config, bool blImportAdvancedRules = false) public static int InitBuiltinRouting(Config config, bool blImportAdvancedRules = false)
{ {
var ver = "V2-"; var ver = "V3-";
var items = LazyConfig.Instance.RoutingItems(); var items = LazyConfig.Instance.RoutingItems();
if (blImportAdvancedRules || items.Where(t => t.remarks.StartsWith(ver)).ToList().Count <= 0) if (blImportAdvancedRules || items.Where(t => t.remarks.StartsWith(ver)).ToList().Count <= 0)
{ {

View File

@ -27,7 +27,7 @@ namespace v2rayN.Handler.CoreConfig
{ {
return GenerateClientCustomConfig(node, fileName, out msg); return GenerateClientCustomConfig(node, fileName, out msg);
} }
else if (config.tunModeItem.enableTun || LazyConfig.Instance.GetCoreType(node, node.configType) == ECoreType.sing_box) else if (LazyConfig.Instance.GetCoreType(node, node.configType) == ECoreType.sing_box)
{ {
var configGenSingbox = new CoreConfigSingbox(config); var configGenSingbox = new CoreConfigSingbox(config);
if (configGenSingbox.GenerateClientConfigContent(node, out SingboxConfig? singboxConfig, out msg) != 0) if (configGenSingbox.GenerateClientConfigContent(node, out SingboxConfig? singboxConfig, out msg) != 0)

View File

@ -120,7 +120,8 @@ namespace v2rayN.Handler.CoreConfig
var listen = "::"; var listen = "::";
singboxConfig.inbounds = []; singboxConfig.inbounds = [];
if (!_config.tunModeItem.enableTun || _config.tunModeItem.enableTun && _config.tunModeItem.enableExInbound) if (!_config.tunModeItem.enableTun
|| (_config.tunModeItem.enableTun && _config.tunModeItem.enableExInbound && _config.runningCoreType == ECoreType.sing_box))
{ {
var inbound = new Inbound4Sbox() var inbound = new Inbound4Sbox()
{ {
@ -191,7 +192,7 @@ namespace v2rayN.Handler.CoreConfig
tunInbound.strict_route = _config.tunModeItem.strictRoute; tunInbound.strict_route = _config.tunModeItem.strictRoute;
tunInbound.stack = _config.tunModeItem.stack; tunInbound.stack = _config.tunModeItem.stack;
tunInbound.sniff = _config.inbound[0].sniffingEnabled; tunInbound.sniff = _config.inbound[0].sniffingEnabled;
tunInbound.sniff_override_destination = _config.inbound[0].routeOnly ? false : _config.inbound[0].sniffingEnabled; //tunInbound.sniff_override_destination = _config.inbound[0].routeOnly ? false : _config.inbound[0].sniffingEnabled;
if (_config.tunModeItem.enableIPv6Address == false) if (_config.tunModeItem.enableIPv6Address == false)
{ {
tunInbound.inet6_address = null; tunInbound.inet6_address = null;
@ -844,12 +845,14 @@ namespace v2rayN.Handler.CoreConfig
.ToList(); .ToList();
if (lstDomain != null && lstDomain.Count > 0) if (lstDomain != null && lstDomain.Count > 0)
{ {
//var strategy = dns4Sbox.servers.Where(t => !Utils.IsNullOrEmpty(t.strategy)).Select(t => t.strategy).FirstOrDefault();
var tag = "local_local"; var tag = "local_local";
dns4Sbox.servers.Add(new() dns4Sbox.servers.Add(new()
{ {
tag = tag, tag = tag,
address = "223.5.5.5", address = "223.5.5.5",
detour = Global.DirectTag, detour = Global.DirectTag,
//strategy = strategy
}); });
dns4Sbox.rules.Add(new() dns4Sbox.rules.Add(new()
{ {
@ -868,7 +871,7 @@ namespace v2rayN.Handler.CoreConfig
singboxConfig.experimental ??= new Experimental4Sbox(); singboxConfig.experimental ??= new Experimental4Sbox();
singboxConfig.experimental.clash_api = new Clash_Api4Sbox() singboxConfig.experimental.clash_api = new Clash_Api4Sbox()
{ {
external_controller = $"{Global.Loopback}:{LazyConfig.Instance.StatePort}", external_controller = $"{Global.Loopback}:{LazyConfig.Instance.StatePort2}",
}; };
} }
@ -950,12 +953,14 @@ namespace v2rayN.Handler.CoreConfig
} }
else else
{ {
var geo = item.Split('-').FirstOrDefault() ?? geosite;
var value = item.Split('-').LastOrDefault();
singboxConfig.route.rule_set.Add(new() singboxConfig.route.rule_set.Add(new()
{ {
type = "remote", type = "remote",
format = "binary", format = "binary",
tag = item, tag = item,
url = string.Format(Global.SingboxRulesetUrl, item.StartsWith(geosite) ? geosite : geoip, item), url = string.Format(geo.Equals(geosite) ? Global.SingboxRulesetUrlGeosite : Global.SingboxRulesetUrlGeoip, value),
download_detour = Global.ProxyTag download_detour = Global.ProxyTag
}); });
} }

View File

@ -57,22 +57,22 @@ namespace v2rayN.Handler
CoreStart(node); CoreStart(node);
//In tun mode, do a delay check and restart the core //In tun mode, do a delay check and restart the core
if (_config.tunModeItem.enableTun) //if (_config.tunModeItem.enableTun)
{ //{
Observable.Range(1, 1) // Observable.Range(1, 1)
.Delay(TimeSpan.FromSeconds(15)) // .Delay(TimeSpan.FromSeconds(15))
.Subscribe(x => // .Subscribe(x =>
{ // {
{ // {
if (_process == null || _process.HasExited) // if (_process == null || _process.HasExited)
{ // {
CoreStart(node); // CoreStart(node);
ShowMsg(false, "Tun mode restart the core once"); // ShowMsg(false, "Tun mode restart the core once");
Logging.SaveLog("Tun mode restart the core once"); // Logging.SaveLog("Tun mode restart the core once");
} // }
} // }
}); // });
} //}
} }
} }
@ -186,15 +186,16 @@ namespace v2rayN.Handler
ShowMsg(false, $"{Environment.OSVersion} - {(Environment.Is64BitOperatingSystem ? 64 : 32)}"); ShowMsg(false, $"{Environment.OSVersion} - {(Environment.Is64BitOperatingSystem ? 64 : 32)}");
ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))); ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
ECoreType coreType; //ECoreType coreType;
if (node.configType != EConfigType.Custom && _config.tunModeItem.enableTun) //if (node.configType != EConfigType.Custom && _config.tunModeItem.enableTun)
{ //{
coreType = ECoreType.sing_box; // coreType = ECoreType.sing_box;
} //}
else //else
{ //{
coreType = LazyConfig.Instance.GetCoreType(node, node.configType); // coreType = LazyConfig.Instance.GetCoreType(node, node.configType);
} //}
var coreType = LazyConfig.Instance.GetCoreType(node, node.configType);
_config.runningCoreType = coreType; _config.runningCoreType = coreType;
var coreInfo = LazyConfig.Instance.GetCoreInfo(coreType); var coreInfo = LazyConfig.Instance.GetCoreInfo(coreType);
@ -206,13 +207,25 @@ namespace v2rayN.Handler
} }
_process = proc; _process = proc;
//start a socks service //start a pre service
if (_process != null && !_process.HasExited) if (_process != null && !_process.HasExited)
{ {
if ((node.configType == EConfigType.Custom && node.preSocksPort > 0)) ProfileItem? itemSocks = null;
var preCoreType = ECoreType.sing_box;
if (node.configType != EConfigType.Custom && coreType != ECoreType.sing_box && _config.tunModeItem.enableTun)
{ {
var preCoreType = _config.tunModeItem.enableTun ? ECoreType.sing_box : ECoreType.Xray; itemSocks = new ProfileItem()
var itemSocks = new ProfileItem() {
coreType = preCoreType,
configType = EConfigType.Socks,
address = Global.Loopback,
port = LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks)
};
}
else if ((node.configType == EConfigType.Custom && node.preSocksPort > 0))
{
preCoreType = _config.tunModeItem.enableTun ? ECoreType.sing_box : ECoreType.Xray;
itemSocks = new ProfileItem()
{ {
coreType = preCoreType, coreType = preCoreType,
configType = EConfigType.Socks, configType = EConfigType.Socks,
@ -220,6 +233,9 @@ namespace v2rayN.Handler
port = node.preSocksPort port = node.preSocksPort
}; };
_config.runningCoreType = preCoreType; _config.runningCoreType = preCoreType;
}
if (itemSocks != null)
{
string fileName2 = Utils.GetConfigPath(Global.CorePreConfigFileName); string fileName2 = Utils.GetConfigPath(Global.CorePreConfigFileName);
if (CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2, out string msg2, out string configStr) == 0) if (CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2, out string msg2, out string configStr) == 0)
{ {

View File

@ -13,20 +13,26 @@ namespace v2rayN.Handler
public static LazyConfig Instance => _instance.Value; public static LazyConfig Instance => _instance.Value;
private int? _statePort; private int? _statePort;
private int? _statePort2;
public int StatePort public int StatePort
{ {
get get
{ {
if (_statePort is null) _statePort ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api));
{
_statePort = Utils.GetFreePort(GetLocalPort(EInboundProtocol.api));
}
return _statePort.Value; return _statePort.Value;
} }
} }
public int StatePort2
{
get
{
_statePort2 ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api2));
return _statePort2.Value;
}
}
private Job _processJob = new(); private Job _processJob = new();
public LazyConfig() public LazyConfig()

View File

@ -28,7 +28,7 @@ namespace v2rayN.Handler
try try
{ {
url = $"ws://{Global.Loopback}:{LazyConfig.Instance.StatePort}/traffic"; url = $"ws://{Global.Loopback}:{LazyConfig.Instance.StatePort2}/traffic";
if (webSocket == null) if (webSocket == null)
{ {

View File

@ -28,6 +28,7 @@
public bool? disable_expire { get; set; } public bool? disable_expire { get; set; }
public bool? independent_cache { get; set; } public bool? independent_cache { get; set; }
public bool? reverse_mapping { get; set; } public bool? reverse_mapping { get; set; }
public string? client_subnet { get; set; }
public Fakeip4Sbox? fakeip { get; set; } public Fakeip4Sbox? fakeip { get; set; }
} }
@ -44,12 +45,13 @@
public string? outbound { get; set; } public string? outbound { get; set; }
public string? server { get; set; } public string? server { get; set; }
public bool? disable_cache { get; set; } public bool? disable_cache { get; set; }
public List<string>? inbound { 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 List<string>? network { get; set; }
public bool? ip_is_private { get; set; } public bool? ip_is_private { get; set; }
public string? client_subnet { get; set; }
public List<string>? inbound { get; set; }
public List<string>? protocol { get; set; }
public List<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; }
@ -62,6 +64,7 @@
public List<string>? source_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; }
public List<string>? rule_set { get; set; } public List<string>? rule_set { get; set; }
public List<Rule4Sbox>? rules { get; set; }
} }
[Serializable] [Serializable]
@ -184,11 +187,13 @@
public class Server4Sbox public class Server4Sbox
{ {
public string tag { get; set; } public string? tag { get; set; }
public string address { get; set; } public string? address { get; set; }
public string address_resolver { get; set; } public string? address_resolver { get; set; }
public string strategy { get; set; } public string? address_strategy { get; set; }
public string? strategy { get; set; }
public string? detour { get; set; } public string? detour { get; set; }
public string? client_subnet { get; set; }
} }
public class Experimental4Sbox public class Experimental4Sbox

View File

@ -16,7 +16,22 @@
"domain": [ "domain": [
"geosite:geolocation-!cn", "geosite:geolocation-!cn",
"geosite:greatfire" "geosite:greatfire"
]
},
{
"outboundTag": "proxy",
"ip": [
"1.0.0.1",
"1.1.1.1",
"8.8.8.8",
"8.8.4.4",
"geoip:facebook",
"geoip:fastly",
"geoip:google",
"geoip:netflix",
"geoip:telegram",
"geoip:twitter"
] ]
}, },
{ {

View File

@ -22,6 +22,10 @@
{ {
"outboundTag": "direct", "outboundTag": "direct",
"ip": [ "ip": [
"223.5.5.5/32",
"119.29.29.29/32",
"180.76.76.76/32",
"114.114.114.114/32",
"geoip:private", "geoip:private",
"geoip:cn" "geoip:cn"
] ]

View File

@ -16,7 +16,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Downloader" Version="3.0.6" /> <PackageReference Include="Downloader" Version="3.0.6" />
<PackageReference Include="MaterialDesignThemes" Version="5.0.0" /> <PackageReference Include="MaterialDesignThemes" Version="5.1.0" />
<PackageReference Include="H.NotifyIcon.Wpf" Version="2.0.131" /> <PackageReference Include="H.NotifyIcon.Wpf" Version="2.0.131" />
<PackageReference Include="QRCoder.Xaml" Version="1.5.1" /> <PackageReference Include="QRCoder.Xaml" Version="1.5.1" />
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" /> <PackageReference Include="sqlite-net-pcl" Version="1.9.172" />