Skip to content

Commit 49f6557

Browse files
authored
Replace protect-ss with protect-socks (2dust#9052)
1 parent a69e407 commit 49f6557

5 files changed

Lines changed: 37 additions & 60 deletions

File tree

v2rayN/ServiceLib/Handler/Builder/CoreConfigContextBuilder.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public record CoreConfigContextBuilderAllResult(
2525
[.. MainResult.ValidatorResult.Warnings, .. PreSocksResult?.ValidatorResult.Warnings ?? []]);
2626

2727
/// <summary>
28-
/// The main context with TunProtectSsPort/ProxyRelaySsPort and ProtectDomainList merged in
28+
/// The main context with TunProtectSocksPort/ProxyRelaySocksPort and ProtectDomainList merged in
2929
/// from the pre-socks result (if any). Pass this to the core runner.
3030
/// </summary>
3131
public CoreConfigContext ResolvedMainContext => PreSocksResult is not null
3232
? MainResult.Context with
3333
{
34-
TunProtectSsPort = PreSocksResult.Context.TunProtectSsPort,
35-
ProxyRelaySsPort = PreSocksResult.Context.ProxyRelaySsPort,
34+
TunProtectSocksPort = PreSocksResult.Context.TunProtectSocksPort,
35+
ProxyRelaySocksPort = PreSocksResult.Context.ProxyRelaySocksPort,
3636
ProtectDomainList = [.. MainResult.Context.ProtectDomainList ?? [], .. PreSocksResult.Context.ProtectDomainList ?? []],
3737
}
3838
: MainResult.Context;
@@ -58,8 +58,8 @@ public static async Task<CoreConfigContextBuilderResult> Build(Config config, Pr
5858
IsTunEnabled = config.TunModeItem.EnableTun,
5959
SimpleDnsItem = config.SimpleDNSItem,
6060
ProtectDomainList = [],
61-
TunProtectSsPort = 0,
62-
ProxyRelaySsPort = 0,
61+
TunProtectSocksPort = 0,
62+
ProxyRelaySocksPort = 0,
6363
RawDnsItem = await AppManager.Instance.GetDNSItem(coreType),
6464
RoutingItem = await ConfigHandler.GetDefaultRouting(config),
6565
};
@@ -155,28 +155,23 @@ public static async Task<CoreConfigContextBuilderAllResult> BuildAll(Config conf
155155
return null;
156156
}
157157

158-
var tunProtectSsPort = Utils.GetFreePort();
159-
var proxyRelaySsPort = Utils.GetFreePort();
158+
var tunProtectSocksPort = Utils.GetFreePort();
159+
var proxyRelaySocksPort = Utils.GetFreePort();
160160
var preItem = new ProfileItem()
161161
{
162162
CoreType = ECoreType.sing_box,
163-
ConfigType = EConfigType.Shadowsocks,
163+
ConfigType = EConfigType.SOCKS,
164164
Address = Global.Loopback,
165-
Port = proxyRelaySsPort,
166-
Password = Global.None,
165+
Port = proxyRelaySocksPort,
167166
};
168-
preItem.SetProtocolExtra(preItem.GetProtocolExtra() with
169-
{
170-
SsMethod = Global.None,
171-
});
172167
var preResult2 = await Build(nodeContext.AppConfig, preItem);
173168
return preResult2 with
174169
{
175170
Context = preResult2.Context with
176171
{
177172
ProtectDomainList = [.. nodeContext.ProtectDomainList ?? [], .. preResult2.Context.ProtectDomainList ?? []],
178-
TunProtectSsPort = tunProtectSsPort,
179-
ProxyRelaySsPort = proxyRelaySsPort,
173+
TunProtectSocksPort = tunProtectSocksPort,
174+
ProxyRelaySocksPort = proxyRelaySocksPort,
180175
}
181176
};
182177
}

v2rayN/ServiceLib/Models/CoreConfigContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public record CoreConfigContext
2020
// -> tun inbound --(if routing proxy)--> relay outbound
2121
// -> proxy core (relay inbound --> proxy outbound --(dialerProxy)--> protect outbound)
2222
// -> protect inbound -> direct proxy outbound data -> internet
23-
public int TunProtectSsPort { get; init; } = 0;
24-
public int ProxyRelaySsPort { get; init; } = 0;
23+
public int TunProtectSocksPort { get; init; } = 0;
24+
public int ProxyRelaySocksPort { get; init; } = 0;
2525
}

v2rayN/ServiceLib/Services/CoreConfig/Singbox/CoreConfigSingboxService.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,23 @@ public RetResult GenerateClientConfigContent()
6262

6363
ret.Data = ApplyFullConfigTemplate();
6464
if (!context.AppConfig.TunModeItem.EnableLegacyProtect
65-
&& context.TunProtectSsPort is > 0 and <= 65535)
65+
&& context.TunProtectSocksPort is > 0 and <= 65535)
6666
{
67+
// Replace relay proxy outbound, avoid mux or other feature cause issue, and add a socks inbound for tun protect
68+
var relayProxyIndex = _coreConfig.outbounds.FindIndex(o => o.tag == Global.ProxyTag);
69+
_coreConfig.outbounds[relayProxyIndex] = new Outbound4Sbox()
70+
{
71+
type = Global.ProtocolTypes[EConfigType.SOCKS],
72+
tag = Global.ProxyTag,
73+
server = Global.Loopback,
74+
server_port = context.ProxyRelaySocksPort,
75+
};
6776
var ssInbound = new
6877
{
69-
type = "shadowsocks",
70-
tag = "tun-protect-ss",
78+
type = "socks",
79+
tag = "tun-protect-socks",
7180
listen = Global.Loopback,
72-
listen_port = context.TunProtectSsPort,
73-
method = "none",
74-
password = "none",
81+
listen_port = context.TunProtectSocksPort,
7582
};
7683
var directRule = new Rule4Sbox()
7784
{

v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,6 @@ private void FillOutboundMux(Outbound4Sbox outbound)
345345
{
346346
try
347347
{
348-
// The synthetic TUN relay outbound talks to the local Xray shadowsocks relay.
349-
// Xray cannot terminate sing-box h2mux, so muxing here turns local relay traffic
350-
// into sp.mux.sing-box.arpa pseudo-destinations and breaks DNS over TUN.
351-
if (IsTunRelayProxyOutbound())
352-
{
353-
return;
354-
}
355-
356348
var muxEnabled = _node.MuxEnabled ?? _config.CoreBasicItem.MuxEnabled;
357349
if (muxEnabled && _config.Mux4SboxItem.Protocol.IsNotEmpty())
358350
{
@@ -372,21 +364,6 @@ private void FillOutboundMux(Outbound4Sbox outbound)
372364
}
373365
}
374366

375-
private bool IsTunRelayProxyOutbound()
376-
{
377-
if (!context.IsTunEnabled
378-
|| _node.ConfigType != EConfigType.Shadowsocks
379-
|| _node.Address != Global.Loopback
380-
|| _node.Port != context.ProxyRelaySsPort
381-
|| _node.Password != Global.None)
382-
{
383-
return false;
384-
}
385-
386-
var protocolExtra = _node.GetProtocolExtra();
387-
return protocolExtra.SsMethod == Global.None;
388-
}
389-
390367
private void FillOutboundTls(Outbound4Sbox outbound)
391368
{
392369
try

v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public RetResult GenerateClientConfigContent()
1717
{
1818
if (!context.AppConfig.TunModeItem.EnableLegacyProtect
1919
&& context.IsTunEnabled
20-
&& context.TunProtectSsPort is > 0 and <= 65535
21-
&& context.ProxyRelaySsPort is > 0 and <= 65535)
20+
&& context.TunProtectSocksPort is > 0 and <= 65535
21+
&& context.ProxyRelaySocksPort is > 0 and <= 65535)
2222
{
2323
return GenerateClientProxyRelayConfig();
2424
}
@@ -309,17 +309,16 @@ public RetResult GenerateClientProxyRelayConfig()
309309
var protectNode = new ProfileItem()
310310
{
311311
CoreType = ECoreType.Xray,
312-
ConfigType = EConfigType.Shadowsocks,
312+
ConfigType = EConfigType.SOCKS,
313313
Address = Global.Loopback,
314-
Port = context.TunProtectSsPort,
315-
Password = Global.None,
314+
Port = context.TunProtectSocksPort,
316315
};
317316
protectNode.SetProtocolExtra(protectNode.GetProtocolExtra() with
318317
{
319318
SsMethod = Global.None,
320319
});
321320

322-
const string protectTag = "tun-protect-ss";
321+
const string protectTag = "tun-protect-socks";
323322
foreach (var outbound in _coreConfig.outbounds
324323
.Where(o => o.streamSettings?.sockopt?.dialerProxy?.IsNullOrEmpty() ?? true))
325324
{
@@ -368,7 +367,7 @@ public RetResult GenerateClientProxyRelayConfig()
368367
var hasBalancer = _coreConfig.routing.balancers is { Count: > 0 };
369368
_coreConfig.routing.rules.Add(new()
370369
{
371-
inboundTag = ["proxy-relay-ss"],
370+
inboundTag = ["proxy-relay-socks"],
372371
outboundTag = hasBalancer ? null : Global.ProxyTag,
373372
balancerTag = hasBalancer ? Global.ProxyTag + Global.BalancerTagSuffix : null,
374373
type = "field"
@@ -380,15 +379,14 @@ public RetResult GenerateClientProxyRelayConfig()
380379
configNode["inbounds"]!.AsArray().Add(new
381380
{
382381
listen = Global.Loopback,
383-
port = context.ProxyRelaySsPort,
384-
protocol = "shadowsocks",
382+
port = context.ProxyRelaySocksPort,
383+
protocol = "socks",
385384
settings = new
386385
{
387-
network = "tcp,udp",
388-
method = Global.None,
389-
password = Global.None,
386+
auth = "noauth",
387+
udp = true,
390388
},
391-
tag = "proxy-relay-ss",
389+
tag = "proxy-relay-socks",
392390
});
393391

394392
ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");

0 commit comments

Comments
 (0)