Skip to content

Commit b77cc3c

Browse files
committed
Add network attribute to route rule
#5256
1 parent 71bf9b4 commit b77cc3c

12 files changed

+75
-38
lines changed

v2rayN/v2rayN/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private void Init()
6262
//Under Win10
6363
if (Environment.OSVersion.Version.Major < 10)
6464
{
65-
Environment.SetEnvironmentVariable("DOTNET_EnableWriteXorExecute", "0", EnvironmentVariableTarget.Process);
65+
Environment.SetEnvironmentVariable("DOTNET_EnableWriteXorExecute", "0", EnvironmentVariableTarget.User);
6666
}
6767
}
6868

v2rayN/v2rayN/Common/Utils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static string GetEmbedText(string res)
7979
/// </summary>
8080
/// <param name="lst"></param>
8181
/// <returns></returns>
82-
public static string List2String(List<string> lst, bool wrap = false)
82+
public static string List2String(List<string>? lst, bool wrap = false)
8383
{
8484
try
8585
{

v2rayN/v2rayN/Global.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ internal class Global
174174
public static readonly List<string> LogLevels = new() { "debug", "info", "warning", "error", "none" };
175175
public static readonly List<string> InboundTags = new() { "socks", "http", "socks2", "http2" };
176176
public static readonly List<string> RuleProtocols = new() { "http", "tls", "bittorrent" };
177+
public static readonly List<string> RuleNetworks = new() { "", "tcp", "udp", "tcp,udp" };
177178
public static readonly List<string> destOverrideProtocols = ["http", "tls", "quic", "fakedns", "fakedns+others"];
178179
public static readonly List<string> TunMtus = new() { "1280", "1408", "1500", "9000" };
179180
public static readonly List<string> TunStacks = new() { "gvisor", "system" };

v2rayN/v2rayN/Handler/CoreConfig/CoreConfigSingbox.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ private int GenRouting(SingboxConfig singboxConfig)
552552
singboxConfig.route.rules.Add(new()
553553
{
554554
port = [53],
555-
network = "udp",
555+
network = ["udp"],
556556
outbound = dnsOutbound
557557
});
558558
}
@@ -668,6 +668,10 @@ private int GenRoutingUserRule(RulesItem item, List<Rule4Sbox> rules)
668668
rule.port = new List<int> { Utils.ToInt(item.port) };
669669
}
670670
}
671+
if (!Utils.IsNullOrEmpty(item.network))
672+
{
673+
rule.network = Utils.String2List(item.network);
674+
}
671675
if (item.protocol?.Count > 0)
672676
{
673677
rule.protocol = item.protocol;

v2rayN/v2rayN/Handler/CoreConfig/CoreConfigV2ray.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -220,39 +220,43 @@ private int GenRouting(V2rayConfig v2rayConfig)
220220
return 0;
221221
}
222222

223-
private int GenRoutingUserRule(RulesItem4Ray? rules, V2rayConfig v2rayConfig)
223+
private int GenRoutingUserRule(RulesItem4Ray? rule, V2rayConfig v2rayConfig)
224224
{
225225
try
226226
{
227-
if (rules == null)
227+
if (rule == null)
228228
{
229229
return 0;
230230
}
231-
if (Utils.IsNullOrEmpty(rules.port))
231+
if (Utils.IsNullOrEmpty(rule.port))
232232
{
233-
rules.port = null;
233+
rule.port = null;
234234
}
235-
if (rules.domain?.Count == 0)
235+
if (Utils.IsNullOrEmpty(rule.network))
236236
{
237-
rules.domain = null;
237+
rule.network = null;
238238
}
239-
if (rules.ip?.Count == 0)
239+
if (rule.domain?.Count == 0)
240240
{
241-
rules.ip = null;
241+
rule.domain = null;
242242
}
243-
if (rules.protocol?.Count == 0)
243+
if (rule.ip?.Count == 0)
244244
{
245-
rules.protocol = null;
245+
rule.ip = null;
246246
}
247-
if (rules.inboundTag?.Count == 0)
247+
if (rule.protocol?.Count == 0)
248248
{
249-
rules.inboundTag = null;
249+
rule.protocol = null;
250+
}
251+
if (rule.inboundTag?.Count == 0)
252+
{
253+
rule.inboundTag = null;
250254
}
251255

252256
var hasDomainIp = false;
253-
if (rules.domain?.Count > 0)
257+
if (rule.domain?.Count > 0)
254258
{
255-
var it = JsonUtils.DeepCopy(rules);
259+
var it = JsonUtils.DeepCopy(rule);
256260
it.ip = null;
257261
it.type = "field";
258262
for (int k = it.domain.Count - 1; k >= 0; k--)
@@ -266,22 +270,22 @@ private int GenRoutingUserRule(RulesItem4Ray? rules, V2rayConfig v2rayConfig)
266270
v2rayConfig.routing.rules.Add(it);
267271
hasDomainIp = true;
268272
}
269-
if (rules.ip?.Count > 0)
273+
if (rule.ip?.Count > 0)
270274
{
271-
var it = JsonUtils.DeepCopy(rules);
275+
var it = JsonUtils.DeepCopy(rule);
272276
it.domain = null;
273277
it.type = "field";
274278
v2rayConfig.routing.rules.Add(it);
275279
hasDomainIp = true;
276280
}
277281
if (!hasDomainIp)
278282
{
279-
if (!Utils.IsNullOrEmpty(rules.port)
280-
|| rules.protocol?.Count > 0
281-
|| rules.inboundTag?.Count > 0
283+
if (!Utils.IsNullOrEmpty(rule.port)
284+
|| rule.protocol?.Count > 0
285+
|| rule.inboundTag?.Count > 0
282286
)
283287
{
284-
var it = JsonUtils.DeepCopy(rules);
288+
var it = JsonUtils.DeepCopy(rule);
285289
it.type = "field";
286290
v2rayConfig.routing.rules.Add(it);
287291
}

v2rayN/v2rayN/Models/RulesItem.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@
44
public class RulesItem
55
{
66
public string id { get; set; }
7-
public string type { get; set; }
7+
public string? type { get; set; }
88

9-
public string port { get; set; }
9+
public string? port { get; set; }
10+
public string? network { get; set; }
1011

11-
public List<string> inboundTag { get; set; }
12+
public List<string>? inboundTag { get; set; }
1213

13-
public string outboundTag { get; set; }
14+
public string? outboundTag { get; set; }
1415

15-
public List<string> ip { get; set; }
16+
public List<string>? ip { get; set; }
1617

17-
public List<string> domain { get; set; }
18+
public List<string>? domain { get; set; }
1819

19-
public List<string> protocol { get; set; }
20+
public List<string>? protocol { get; set; }
2021

21-
public List<string> process { get; set; }
22+
public List<string>? process { get; set; }
2223

2324
public bool enabled { get; set; } = true;
2425
}

v2rayN/v2rayN/Models/SingboxConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class Rule4Sbox
4848
public List<string>? protocol { get; set; }
4949
public string? type { get; set; }
5050
public string? mode { get; set; }
51-
public string? network { get; set; }
51+
public List<string>? network { get; set; }
5252
public bool? ip_is_private { get; set; }
5353
public List<int>? port { get; set; }
5454
public List<string>? port_range { get; set; }

v2rayN/v2rayN/Models/V2rayConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ public class RulesItem4Ray
394394
public string? type { get; set; }
395395

396396
public string? port { get; set; }
397+
public string? network { get; set; }
397398

398399
public List<string>? inboundTag { get; set; }
399400

v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public void RefreshRulesItems()
130130
id = item.id,
131131
outboundTag = item.outboundTag,
132132
port = item.port,
133+
network = item.network,
133134
protocols = Utils.List2String(item.protocol),
134135
inboundTags = Utils.List2String(item.inboundTag),
135136
domains = Utils.List2String(item.domain),

v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,32 @@
125125
Margin="4"
126126
VerticalAlignment="Center"
127127
Style="{StaticResource ToolbarTextBlock}"
128+
Text="network" />
129+
<ComboBox
130+
x:Name="cmbNetwork"
131+
Grid.Row="4"
132+
Grid.Column="1"
133+
Width="200"
134+
Margin="4"
135+
MaxDropDownHeight="1000"
136+
Style="{StaticResource DefComboBox}" />
137+
138+
<TextBlock
139+
Grid.Row="5"
140+
Grid.Column="0"
141+
Margin="4"
142+
VerticalAlignment="Center"
143+
Style="{StaticResource ToolbarTextBlock}"
128144
Text="enabled" />
129145
<ToggleButton
130146
x:Name="togEnabled"
131-
Grid.Row="4"
147+
Grid.Row="5"
132148
Grid.Column="1"
133149
Margin="4"
134150
HorizontalAlignment="Left" />
135151

136152
<TextBlock
137-
Grid.Row="4"
153+
Grid.Row="5"
138154
Grid.Column="2"
139155
Margin="4"
140156
HorizontalAlignment="Left"

v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public RoutingRuleDetailsWindow(RulesItem rulesItem)
3939
{
4040
clbInboundTag.Items.Add(it);
4141
});
42+
Global.RuleNetworks.ForEach(it =>
43+
{
44+
cmbNetwork.Items.Add(it);
45+
});
4246

4347
if (!rulesItem.id.IsNullOrEmpty())
4448
{
@@ -56,6 +60,7 @@ public RoutingRuleDetailsWindow(RulesItem rulesItem)
5660
{
5761
this.Bind(ViewModel, vm => vm.SelectedSource.outboundTag, v => v.cmbOutboundTag.Text).DisposeWith(disposables);
5862
this.Bind(ViewModel, vm => vm.SelectedSource.port, v => v.txtPort.Text).DisposeWith(disposables);
63+
this.Bind(ViewModel, vm => vm.SelectedSource.network, v => v.cmbNetwork.Text).DisposeWith(disposables);
5964
this.Bind(ViewModel, vm => vm.SelectedSource.enabled, v => v.togEnabled.IsChecked).DisposeWith(disposables);
6065
this.Bind(ViewModel, vm => vm.Domain, v => v.txtDomain.Text).DisposeWith(disposables);
6166
this.Bind(ViewModel, vm => vm.IP, v => v.txtIP.Text).DisposeWith(disposables);

v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,27 +302,31 @@
302302
</DataGrid.ContextMenu>
303303
<DataGrid.Columns>
304304
<DataGridTextColumn
305-
Width="120"
305+
Width="110"
306306
Binding="{Binding outboundTag}"
307307
Header="outboundTag" />
308308
<DataGridTextColumn
309309
Width="80"
310310
Binding="{Binding port}"
311311
Header="port" />
312312
<DataGridTextColumn
313-
Width="100"
313+
Width="80"
314314
Binding="{Binding protocols}"
315315
Header="protocol" />
316316
<DataGridTextColumn
317317
Width="110"
318318
Binding="{Binding inboundTags}"
319319
Header="inboundTag" />
320320
<DataGridTextColumn
321-
Width="220"
321+
Width="90"
322+
Binding="{Binding network}"
323+
Header="network" />
324+
<DataGridTextColumn
325+
Width="190"
322326
Binding="{Binding domains}"
323327
Header="domain" />
324328
<DataGridTextColumn
325-
Width="220"
329+
Width="190"
326330
Binding="{Binding ips}"
327331
Header="ip" />
328332
<DataGridTextColumn

0 commit comments

Comments
 (0)