Fix sing-box TUN custom config inbound (#9259)

This commit is contained in:
VinnyTheFemboy 2026-05-08 14:12:09 +03:00 committed by GitHub
parent 2dc967bc04
commit f5059f1165
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 2 deletions

View file

@ -1,6 +1,7 @@
using AwesomeAssertions; using AwesomeAssertions;
using ServiceLib.Common; using ServiceLib.Common;
using ServiceLib.Enums; using ServiceLib.Enums;
using ServiceLib.Manager;
using ServiceLib.Models; using ServiceLib.Models;
using ServiceLib.Services.CoreConfig; using ServiceLib.Services.CoreConfig;
using Xunit; using Xunit;
@ -28,6 +29,31 @@ public class CoreConfigSingboxServiceTests
singboxConfig.inbounds.Should().Contain(i => i.type == nameof(EInboundProtocol.mixed)); singboxConfig.inbounds.Should().Contain(i => i.type == nameof(EInboundProtocol.mixed));
} }
[Fact]
public void GenerateClientConfigContent_TunWithLoopbackPreSocks_ShouldKeepMixedInbound()
{
var config = CoreConfigTestFactory.CreateConfig(ECoreType.sing_box);
CoreConfigTestFactory.BindAppManagerConfig(config);
var node = CoreConfigTestFactory.CreateSocksNode(ECoreType.sing_box);
node.Address = Global.Loopback;
node.Port = 1080;
var context = CoreConfigTestFactory.CreateContext(config, node, ECoreType.sing_box) with
{
IsTunEnabled = true,
};
var result = new CoreConfigSingboxService(context).GenerateClientConfigContent();
result.Success.Should().BeTrue($"ret msg: {result.Msg}");
var cfg = JsonUtils.Deserialize<SingboxConfig>(result.Data!.ToString())!;
cfg.inbounds.Should().Contain(i =>
i.type == nameof(EInboundProtocol.mixed)
&& i.listen == Global.Loopback
&& i.listen_port == AppManager.Instance.GetLocalPort(EInboundProtocol.socks));
cfg.inbounds.Should().Contain(i => i.type == "tun");
}
[Fact] [Fact]
public void GenerateClientConfigContent_PolicyGroup_ShouldExpandChildrenAndBuildSelector() public void GenerateClientConfigContent_PolicyGroup_ShouldExpandChildrenAndBuildSelector()
{ {

View file

@ -8,10 +8,10 @@ public partial class CoreConfigSingboxService
{ {
var listen = "0.0.0.0"; var listen = "0.0.0.0";
var listenPort = AppManager.Instance.GetLocalPort(EInboundProtocol.socks); var listenPort = AppManager.Instance.GetLocalPort(EInboundProtocol.socks);
var isUsingLocalMixedPort = _node.Address == Global.Loopback && _node.Port == listenPort;
_coreConfig.inbounds = []; _coreConfig.inbounds = [];
if (!context.IsTunEnabled if (!context.IsTunEnabled || !isUsingLocalMixedPort)
|| (context.IsTunEnabled && _node.Address != Global.Loopback && _node.Port != listenPort))
{ {
var inbound = new Inbound4Sbox() var inbound = new Inbound4Sbox()
{ {