Skip to content

Commit

Permalink
Improved tun mode
Browse files Browse the repository at this point in the history
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
  • Loading branch information
2dust committed Jun 24, 2024
1 parent b77cc3c commit 6204223
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 52 deletions.
2 changes: 1 addition & 1 deletion v2rayN/ProtosLib/ProtosLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</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.Tools" Version="2.64.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
1 change: 1 addition & 0 deletions v2rayN/v2rayN/Enums/EInboundProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public enum EInboundProtocol
http2,
pac,
api,
api2,
speedtest = 21
}
}
3 changes: 2 additions & 1 deletion v2rayN/v2rayN/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ internal class Global
public const string SpeedPingTestUrl = @"https://www.google.com/generate_204";
public const string JuicityCoreUrl = "https://github.com/juicity/juicity/releases";
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 ConfigFileName = "guiNConfig.json";
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Handler/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,7 @@ public static RoutingItem GetDefaultRouting(Config config)

public static int InitBuiltinRouting(Config config, bool blImportAdvancedRules = false)
{
var ver = "V2-";
var ver = "V3-";
var items = LazyConfig.Instance.RoutingItems();
if (blImportAdvancedRules || items.Where(t => t.remarks.StartsWith(ver)).ToList().Count <= 0)
{
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Handler/CoreConfig/CoreConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static int GenerateClientConfig(ProfileItem node, string? fileName, out s
{
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);
if (configGenSingbox.GenerateClientConfigContent(node, out SingboxConfig? singboxConfig, out msg) != 0)
Expand Down
13 changes: 9 additions & 4 deletions v2rayN/v2rayN/Handler/CoreConfig/CoreConfigSingbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ private int GenInbounds(SingboxConfig singboxConfig)
var listen = "::";
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()
{
Expand Down Expand Up @@ -191,7 +192,7 @@ private int GenInbounds(SingboxConfig singboxConfig)
tunInbound.strict_route = _config.tunModeItem.strictRoute;
tunInbound.stack = _config.tunModeItem.stack;
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)
{
tunInbound.inet6_address = null;
Expand Down Expand Up @@ -844,12 +845,14 @@ private int GenDnsDomains(SingboxConfig singboxConfig)
.ToList();
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";
dns4Sbox.servers.Add(new()
{
tag = tag,
address = "223.5.5.5",
detour = Global.DirectTag,
//strategy = strategy
});
dns4Sbox.rules.Add(new()
{
Expand All @@ -868,7 +871,7 @@ private int GenExperimental(SingboxConfig singboxConfig)
singboxConfig.experimental ??= new Experimental4Sbox();
singboxConfig.experimental.clash_api = new Clash_Api4Sbox()
{
external_controller = $"{Global.Loopback}:{LazyConfig.Instance.StatePort}",
external_controller = $"{Global.Loopback}:{LazyConfig.Instance.StatePort2}",
};
}

Expand Down Expand Up @@ -950,12 +953,14 @@ private int ConvertGeo2Ruleset(SingboxConfig singboxConfig)
}
else
{
var geo = item.Split('-').FirstOrDefault() ?? geosite;
var value = item.Split('-').LastOrDefault();
singboxConfig.route.rule_set.Add(new()
{
type = "remote",
format = "binary",
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
});
}
Expand Down
74 changes: 45 additions & 29 deletions v2rayN/v2rayN/Handler/CoreHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ public void LoadCore()
CoreStart(node);

//In tun mode, do a delay check and restart the core
if (_config.tunModeItem.enableTun)
{
Observable.Range(1, 1)
.Delay(TimeSpan.FromSeconds(15))
.Subscribe(x =>
{
{
if (_process == null || _process.HasExited)
{
CoreStart(node);
ShowMsg(false, "Tun mode restart the core once");
Logging.SaveLog("Tun mode restart the core once");
}
}
});
}
//if (_config.tunModeItem.enableTun)
//{
// Observable.Range(1, 1)
// .Delay(TimeSpan.FromSeconds(15))
// .Subscribe(x =>
// {
// {
// if (_process == null || _process.HasExited)
// {
// CoreStart(node);
// ShowMsg(false, "Tun mode restart the core once");
// Logging.SaveLog("Tun mode restart the core once");
// }
// }
// });
//}
}
}

Expand Down Expand Up @@ -186,15 +186,16 @@ private void CoreStart(ProfileItem node)
ShowMsg(false, $"{Environment.OSVersion} - {(Environment.Is64BitOperatingSystem ? 64 : 32)}");
ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));

ECoreType coreType;
if (node.configType != EConfigType.Custom && _config.tunModeItem.enableTun)
{
coreType = ECoreType.sing_box;
}
else
{
coreType = LazyConfig.Instance.GetCoreType(node, node.configType);
}
//ECoreType coreType;
//if (node.configType != EConfigType.Custom && _config.tunModeItem.enableTun)
//{
// coreType = ECoreType.sing_box;
//}
//else
//{
// coreType = LazyConfig.Instance.GetCoreType(node, node.configType);
//}
var coreType = LazyConfig.Instance.GetCoreType(node, node.configType);
_config.runningCoreType = coreType;
var coreInfo = LazyConfig.Instance.GetCoreInfo(coreType);

Expand All @@ -206,20 +207,35 @@ private void CoreStart(ProfileItem node)
}
_process = proc;

//start a socks service
//start a pre service
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;
var itemSocks = new ProfileItem()
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,
configType = EConfigType.Socks,
address = Global.Loopback,
port = node.preSocksPort
};
_config.runningCoreType = preCoreType;
}
if (itemSocks != null)
{
string fileName2 = Utils.GetConfigPath(Global.CorePreConfigFileName);
if (CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2, out string msg2, out string configStr) == 0)
{
Expand Down
16 changes: 11 additions & 5 deletions v2rayN/v2rayN/Handler/LazyConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,26 @@ public sealed class LazyConfig
public static LazyConfig Instance => _instance.Value;

private int? _statePort;
private int? _statePort2;

public int StatePort
{
get
{
if (_statePort is null)
{
_statePort = Utils.GetFreePort(GetLocalPort(EInboundProtocol.api));
}

_statePort ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api));
return _statePort.Value;
}
}

public int StatePort2
{
get
{
_statePort2 ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api2));
return _statePort2.Value;
}
}

private Job _processJob = new();

public LazyConfig()
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Handler/StatisticsSingbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private async void Init()

try
{
url = $"ws://{Global.Loopback}:{LazyConfig.Instance.StatePort}/traffic";
url = $"ws://{Global.Loopback}:{LazyConfig.Instance.StatePort2}/traffic";

if (webSocket == null)
{
Expand Down
19 changes: 12 additions & 7 deletions v2rayN/v2rayN/Models/SingboxConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Dns4Sbox
public bool? disable_expire { get; set; }
public bool? independent_cache { get; set; }
public bool? reverse_mapping { get; set; }
public string? client_subnet { get; set; }
public Fakeip4Sbox? fakeip { get; set; }
}

Expand All @@ -44,12 +45,13 @@ public class Rule4Sbox
public string? outbound { get; set; }
public string? server { 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? mode { get; set; }
public List<string>? network { 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<string>? port_range { get; set; }
public List<string>? geosite { get; set; }
Expand All @@ -62,6 +64,7 @@ public class Rule4Sbox
public List<string>? source_ip_cidr { get; set; }
public List<string>? process_name { get; set; }
public List<string>? rule_set { get; set; }
public List<Rule4Sbox>? rules { get; set; }
}

[Serializable]
Expand Down Expand Up @@ -184,11 +187,13 @@ public class HyObfs4Sbox

public class Server4Sbox
{
public string tag { get; set; }
public string address { get; set; }
public string address_resolver { get; set; }
public string strategy { get; set; }
public string? tag { get; set; }
public string? address { get; set; }
public string? address_resolver { get; set; }
public string? address_strategy { get; set; }
public string? strategy { get; set; }
public string? detour { get; set; }
public string? client_subnet { get; set; }
}

public class Experimental4Sbox
Expand Down
17 changes: 16 additions & 1 deletion v2rayN/v2rayN/Sample/custom_routing_black
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,22 @@

"domain": [
"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"
]
},
{
Expand Down
4 changes: 4 additions & 0 deletions v2rayN/v2rayN/Sample/custom_routing_white
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
{
"outboundTag": "direct",
"ip": [
"223.5.5.5/32",
"119.29.29.29/32",
"180.76.76.76/32",
"114.114.114.114/32",
"geoip:private",
"geoip:cn"
]
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/v2rayN.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<ItemGroup>
<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="QRCoder.Xaml" Version="1.5.1" />
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
Expand Down

0 comments on commit 6204223

Please sign in to comment.