Skip to content

Commit 703f962

Browse files
committed
Add xray tun support
1 parent 5304190 commit 703f962

15 files changed

Lines changed: 187 additions & 294 deletions

v2rayN/ServiceLib/Common/WindowsUtils.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,23 @@ public static void RegWriteValue(string path, string name, object value)
5353

5454
public static async Task RemoveTunDevice()
5555
{
56-
try
56+
var tunNameList = new List<string> { "singbox_tun", "xray_tun" };
57+
foreach (var tunName in tunNameList)
5758
{
58-
var sum = MD5.HashData(Encoding.UTF8.GetBytes("wintunsingbox_tun"));
59-
var guid = new Guid(sum);
60-
var pnpUtilPath = @"C:\Windows\System32\pnputil.exe";
61-
var arg = $$""" /remove-device "SWD\Wintun\{{{guid}}}" """;
59+
try
60+
{
61+
var sum = MD5.HashData(Encoding.UTF8.GetBytes($"wintun{tunName}"));
62+
var guid = new Guid(sum);
63+
var pnpUtilPath = @"C:\Windows\System32\pnputil.exe";
64+
var arg = $$""" /remove-device "SWD\Wintun\{{{guid}}}" """;
6265

63-
// Try to remove the device
64-
_ = await Utils.GetCliWrapOutput(pnpUtilPath, arg);
65-
}
66-
catch (Exception ex)
67-
{
68-
Logging.SaveLog(_tag, ex);
66+
// Try to remove the device
67+
_ = await Utils.GetCliWrapOutput(pnpUtilPath, arg);
68+
}
69+
catch (Exception ex)
70+
{
71+
Logging.SaveLog(_tag, ex);
72+
}
6973
}
7074
}
7175
}

v2rayN/ServiceLib/Global.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class Global
2424
public const string V2raySampleHttpResponseFileName = NamespaceSample + "SampleHttpResponse";
2525
public const string V2raySampleInbound = NamespaceSample + "SampleInbound";
2626
public const string V2raySampleOutbound = NamespaceSample + "SampleOutbound";
27+
public const string V2raySampleTunInbound = NamespaceSample + "SampleTunInbound";
28+
public const string V2raySampleTunRules = NamespaceSample + "SampleTunRules";
2729
public const string SingboxSampleOutbound = NamespaceSample + "SingboxSampleOutbound";
2830
public const string CustomRoutingFileName = NamespaceSample + "custom_routing_";
2931
public const string TunSingboxDNSFileName = NamespaceSample + "tun_singbox_dns";
@@ -48,6 +50,7 @@ public class Global
4850
public const string ProxyTag = "proxy";
4951
public const string DirectTag = "direct";
5052
public const string BlockTag = "block";
53+
public const string DnsOutboundTag = "dns";
5154
public const string DnsTag = "dns-module";
5255
public const string DirectDnsTag = "direct-dns";
5356
public const string BalancerTagSuffix = "-round";

v2rayN/ServiceLib/Handler/Builder/CoreConfigContextBuilder.cs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ public record CoreConfigContextBuilderAllResult(
3131
public CoreConfigContext ResolvedMainContext => PreSocksResult is not null
3232
? MainResult.Context with
3333
{
34-
TunProtectSsPort = PreSocksResult.Context.TunProtectSsPort,
35-
ProxyRelaySsPort = PreSocksResult.Context.ProxyRelaySsPort,
3634
ProtectDomainList = [.. MainResult.Context.ProtectDomainList ?? [], .. PreSocksResult.Context.ProtectDomainList ?? []],
3735
}
3836
: MainResult.Context;
@@ -58,8 +56,6 @@ public static async Task<CoreConfigContextBuilderResult> Build(Config config, Pr
5856
IsTunEnabled = config.TunModeItem.EnableTun,
5957
SimpleDnsItem = config.SimpleDNSItem,
6058
ProtectDomainList = [],
61-
TunProtectSsPort = 0,
62-
ProxyRelaySsPort = 0,
6359
RawDnsItem = await AppManager.Instance.GetDNSItem(coreType),
6460
RoutingItem = await ConfigHandler.GetDefaultRouting(config),
6561
};
@@ -148,37 +144,7 @@ public static async Task<CoreConfigContextBuilderAllResult> BuildAll(Config conf
148144
};
149145
}
150146

151-
if (!nodeContext.IsTunEnabled
152-
|| coreType != ECoreType.Xray
153-
|| node.ConfigType == EConfigType.Custom)
154-
{
155-
return null;
156-
}
157-
158-
var tunProtectSsPort = Utils.GetFreePort();
159-
var proxyRelaySsPort = Utils.GetFreePort();
160-
var preItem = new ProfileItem()
161-
{
162-
CoreType = ECoreType.sing_box,
163-
ConfigType = EConfigType.Shadowsocks,
164-
Address = Global.Loopback,
165-
Port = proxyRelaySsPort,
166-
Password = Global.None,
167-
};
168-
preItem.SetProtocolExtra(preItem.GetProtocolExtra() with
169-
{
170-
SsMethod = Global.None,
171-
});
172-
var preResult2 = await Build(nodeContext.AppConfig, preItem);
173-
return preResult2 with
174-
{
175-
Context = preResult2.Context with
176-
{
177-
ProtectDomainList = [.. nodeContext.ProtectDomainList ?? [], .. preResult2.Context.ProtectDomainList ?? []],
178-
TunProtectSsPort = tunProtectSsPort,
179-
ProxyRelaySsPort = proxyRelaySsPort,
180-
}
181-
};
147+
return null;
182148
}
183149

184150
/// <summary>

v2rayN/ServiceLib/Handler/ConfigHandler.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,20 +1416,7 @@ public static async Task<RetResult> AddGroupRegionServer(Config config, SubItem?
14161416
public static ProfileItem? GetPreSocksItem(Config config, ProfileItem node, ECoreType coreType)
14171417
{
14181418
ProfileItem? itemSocks = null;
1419-
if (node.ConfigType != EConfigType.Custom
1420-
&& coreType != ECoreType.sing_box
1421-
&& config.TunModeItem.EnableTun
1422-
&& config.TunModeItem.EnableLegacyProtect)
1423-
{
1424-
itemSocks = new ProfileItem()
1425-
{
1426-
CoreType = ECoreType.sing_box,
1427-
ConfigType = EConfigType.SOCKS,
1428-
Address = Global.Loopback,
1429-
Port = AppManager.Instance.GetLocalPort(EInboundProtocol.socks)
1430-
};
1431-
}
1432-
else if (node.ConfigType == EConfigType.Custom
1419+
if (node.ConfigType == EConfigType.Custom
14331420
&& node.PreSocksPort is > 0 and <= 65535)
14341421
{
14351422
var preCoreType = config.TunModeItem.EnableTun ? ECoreType.sing_box : ECoreType.Xray;

v2rayN/ServiceLib/Models/CoreConfigContext.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,4 @@ public record CoreConfigContext
1717
// TUN Compatibility
1818
public bool IsTunEnabled { get; init; } = false;
1919
public HashSet<string> ProtectDomainList { get; init; } = new();
20-
// -> tun inbound --(if routing proxy)--> relay outbound
21-
// -> proxy core (relay inbound --> proxy outbound --(dialerProxy)--> protect outbound)
22-
// -> protect inbound -> direct proxy outbound data -> internet
23-
public int TunProtectSsPort { get; init; } = 0;
24-
public int ProxyRelaySsPort { get; init; } = 0;
2520
}

v2rayN/ServiceLib/Models/V2rayConfig.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ public class Inboundsettings4Ray
7575
public bool? allowTransparent { get; set; }
7676

7777
public List<AccountsItem4Ray>? accounts { get; set; }
78+
79+
public string? name { get; set; }
80+
81+
public List<int>? MTU { get; set; }
82+
83+
public List<string>? gateway { get; set; }
84+
85+
public List<string>? autoSystemRoutingTable { get; set; }
86+
87+
public string? autoOutboundsInterface { get; set; }
88+
89+
// public List<string>? dns { get; set; }
7890
}
7991

8092
public class UsersItem4Ray
@@ -509,6 +521,8 @@ public class AccountsItem4Ray
509521
public class Sockopt4Ray
510522
{
511523
public string? dialerProxy { get; set; }
524+
[JsonPropertyName("interface")]
525+
public string? Interface { get; set; }
512526
}
513527

514528
public class FragmentItem4Ray
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"tag": "tun",
3+
"protocol": "tun",
4+
"settings": {
5+
"name": "xray_tun",
6+
"MTU": [
7+
9000
8+
],
9+
"gateway": [
10+
"172.18.0.1/30",
11+
"fdfe:dcba:9876::1/126"
12+
],
13+
"autoSystemRoutingTable": [
14+
"0.0.0.0/0",
15+
"::/0"
16+
],
17+
"autoOutboundsInterface": "auto"
18+
},
19+
"sniffing": {
20+
"enabled": true,
21+
"destOverride": [
22+
"http",
23+
"tls"
24+
]
25+
}
26+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"network": "udp",
4+
"port": "135,137-139,5353",
5+
"outboundTag": "block"
6+
},
7+
{
8+
"ip": [
9+
"224.0.0.0/3",
10+
"ff00::/8"
11+
],
12+
"outboundTag": "block"
13+
}
14+
]

v2rayN/ServiceLib/ServiceLib.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
<EmbeddedResource Include="Sample\SampleHttpResponse" />
3939
<EmbeddedResource Include="Sample\SampleInbound" />
4040
<EmbeddedResource Include="Sample\SampleOutbound" />
41+
<EmbeddedResource Include="Sample\SampleTunInbound" />
42+
<EmbeddedResource Include="Sample\SampleTunRules" />
4143
<EmbeddedResource Include="Sample\SingboxSampleClientConfig" />
4244
<EmbeddedResource Include="Sample\SingboxSampleOutbound" />
4345
<EmbeddedResource Include="Sample\tun_singbox_dns" />

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

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -61,52 +61,6 @@ public RetResult GenerateClientConfigContent()
6161
ret.Success = true;
6262

6363
ret.Data = ApplyFullConfigTemplate();
64-
if (!context.AppConfig.TunModeItem.EnableLegacyProtect
65-
&& context.TunProtectSsPort is > 0 and <= 65535)
66-
{
67-
var ssInbound = new
68-
{
69-
type = "shadowsocks",
70-
tag = "tun-protect-ss",
71-
listen = Global.Loopback,
72-
listen_port = context.TunProtectSsPort,
73-
method = "none",
74-
password = "none",
75-
};
76-
var directRule = new Rule4Sbox()
77-
{
78-
inbound = new List<string> { ssInbound.tag },
79-
outbound = Global.DirectTag,
80-
};
81-
var singboxConfigNode = JsonUtils.ParseJson(ret.Data.ToString())!.AsObject();
82-
var inboundsNode = singboxConfigNode["inbounds"]!.AsArray();
83-
inboundsNode.Add(JsonUtils.SerializeToNode(ssInbound, new JsonSerializerOptions
84-
{
85-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
86-
}));
87-
var routeNode = singboxConfigNode["route"]?.AsObject();
88-
var rulesNode = routeNode?["rules"]?.AsArray();
89-
var protectRuleNode = JsonUtils.SerializeToNode(directRule,
90-
new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
91-
if (rulesNode != null)
92-
{
93-
rulesNode.Insert(0, protectRuleNode);
94-
}
95-
else
96-
{
97-
var newRulesNode = new JsonArray() { protectRuleNode };
98-
if (routeNode is null)
99-
{
100-
var newRouteNode = new JsonObject() { ["rules"] = newRulesNode };
101-
singboxConfigNode["route"] = newRouteNode;
102-
}
103-
else
104-
{
105-
routeNode["rules"] = newRulesNode;
106-
}
107-
}
108-
ret.Data = JsonUtils.Serialize(singboxConfigNode);
109-
}
11064
return ret;
11165
}
11266
catch (Exception ex)

0 commit comments

Comments
 (0)