添加简单的分流功能;修改修改非管理员模式下tun模式启动逻辑

This commit is contained in:
John Edwerd 2024-01-11 23:32:00 +08:00
parent 02225ad0b8
commit 08788751a3
8 changed files with 166 additions and 16 deletions

View file

@ -41,6 +41,7 @@ namespace v2rayN.Handler
msg = ResUI.FailedGenDefaultConfiguration; msg = ResUI.FailedGenDefaultConfiguration;
return -1; return -1;
} }
Outbound4Sbox originOutBound = JsonUtils.DeepCopy(singboxConfig.outbounds[0]);
GenLog(singboxConfig); GenLog(singboxConfig);
@ -48,6 +49,8 @@ namespace v2rayN.Handler
GenOutbound(node, singboxConfig.outbounds[0]); GenOutbound(node, singboxConfig.outbounds[0]);
GenOutbounds(singboxConfig, originOutBound);
GenMoreOutbounds(node, singboxConfig); GenMoreOutbounds(node, singboxConfig);
GenRouting(singboxConfig); GenRouting(singboxConfig);
@ -205,6 +208,32 @@ namespace v2rayN.Handler
return inbound; return inbound;
} }
/// <summary>
/// 生成分流的出站规则
/// </summary>
private int GenOutbounds(SingboxConfig singboxConfig, Outbound4Sbox outbound)
{
var routing = ConfigHandler.GetDefaultRouting(_config);
var ruleSet = JsonUtils.Deserialize<List<RulesItem>>(routing.ruleSet);
if (ruleSet == null) return -1;
int index = 1;
foreach (var item in ruleSet)
{
string outBoundIndexId = item.bingServerIndexId;
if (outBoundIndexId == null) continue;
var serverItem = LazyConfig.Instance.GetProfileItem(outBoundIndexId);
if (serverItem == null) continue;
var newOutbounds = JsonUtils.DeepCopy(outbound);
newOutbounds.tag = $"{(serverItem.indexId.Equals(_config.indexId) ? "[proxy]" : $"[{index++}]")}{serverItem.remarks} ## {serverItem.indexId}";
singboxConfig.outbounds.Add(newOutbounds);
GenOutbound(serverItem, newOutbounds);
}
return 0;
}
private int GenOutbound(ProfileItem node, Outbound4Sbox outbound) private int GenOutbound(ProfileItem node, Outbound4Sbox outbound)
{ {
try try
@ -520,8 +549,18 @@ namespace v2rayN.Handler
if (routing != null) if (routing != null)
{ {
var rules = JsonUtils.Deserialize<List<RulesItem>>(routing.ruleSet); var rules = JsonUtils.Deserialize<List<RulesItem>>(routing.ruleSet);
foreach (var item in rules!) int index = 1;
foreach (var item in rules)
{ {
// 分流修改
string outBoundIndexId = item.bingServerIndexId;
if (outBoundIndexId == null) continue;
var serverItem = LazyConfig.Instance.GetProfileItem(outBoundIndexId);
if (serverItem == null) continue;
item.outboundTag = $"{(serverItem.indexId.Equals(_config.indexId) ? "[proxy]" : $"[{index++}]")}{serverItem.remarks} ## {serverItem.indexId}";
// end
if (item.enabled) if (item.enabled)
{ {
GenRoutingUserRule(item, singboxConfig.route.rules); GenRoutingUserRule(item, singboxConfig.route.rules);
@ -535,8 +574,18 @@ namespace v2rayN.Handler
if (lockedItem != null) if (lockedItem != null)
{ {
var rules = JsonUtils.Deserialize<List<RulesItem>>(lockedItem.ruleSet); var rules = JsonUtils.Deserialize<List<RulesItem>>(lockedItem.ruleSet);
foreach (var item in rules!) int index = 1;
foreach (var item in rules)
{ {
// 分流修改
string outBoundIndexId = item.bingServerIndexId;
if (outBoundIndexId == null) continue;
var serverItem = LazyConfig.Instance.GetProfileItem(outBoundIndexId);
if (serverItem == null) continue;
item.outboundTag = $"{(serverItem.indexId.Equals(_config.indexId) ? "[proxy]" : $"[{index++}]")}{serverItem.remarks} ## {serverItem.indexId}";
// end
GenRoutingUserRule(item, singboxConfig.route.rules); GenRoutingUserRule(item, singboxConfig.route.rules);
} }
} }

View file

@ -43,6 +43,7 @@ namespace v2rayN.Handler
msg = ResUI.FailedGenDefaultConfiguration; msg = ResUI.FailedGenDefaultConfiguration;
return -1; return -1;
} }
Outbounds4Ray originOutBound = JsonUtils.DeepCopy(v2rayConfig.outbounds[0]);
GenLog(v2rayConfig); GenLog(v2rayConfig);
@ -52,6 +53,8 @@ namespace v2rayN.Handler
GenOutbound(node, v2rayConfig.outbounds[0]); GenOutbound(node, v2rayConfig.outbounds[0]);
GenOutbounds(v2rayConfig, originOutBound);
GenMoreOutbounds(node, v2rayConfig); GenMoreOutbounds(node, v2rayConfig);
GenDns(v2rayConfig); GenDns(v2rayConfig);
@ -187,8 +190,18 @@ namespace v2rayN.Handler
v2rayConfig.routing.domainStrategy = routing.domainStrategy; v2rayConfig.routing.domainStrategy = routing.domainStrategy;
} }
var rules = JsonUtils.Deserialize<List<RulesItem>>(routing.ruleSet); var rules = JsonUtils.Deserialize<List<RulesItem>>(routing.ruleSet);
int index = 1;
foreach (var item in rules) foreach (var item in rules)
{ {
// 分流
string outBoundIndexId = item.bingServerIndexId;
if (outBoundIndexId == null) continue;
var serverItem = LazyConfig.Instance.GetProfileItem(outBoundIndexId);
if (serverItem == null) continue;
item.outboundTag = $"{(serverItem.indexId.Equals(_config.indexId) ? "[proxy]" : $"[{index++}]")}{serverItem.remarks} ## {serverItem.indexId}";
// end
if (item.enabled) if (item.enabled)
{ {
var item2 = JsonUtils.Deserialize<RulesItem4Ray>(JsonUtils.Serialize(item)); var item2 = JsonUtils.Deserialize<RulesItem4Ray>(JsonUtils.Serialize(item));
@ -203,8 +216,18 @@ namespace v2rayN.Handler
if (lockedItem != null) if (lockedItem != null)
{ {
var rules = JsonUtils.Deserialize<List<RulesItem>>(lockedItem.ruleSet); var rules = JsonUtils.Deserialize<List<RulesItem>>(lockedItem.ruleSet);
int index = 1;
foreach (var item in rules) foreach (var item in rules)
{ {
// 分流修改
string outBoundIndexId = item.bingServerIndexId;
if (outBoundIndexId == null) continue;
var serverItem = LazyConfig.Instance.GetProfileItem(outBoundIndexId);
if (serverItem == null) continue;
item.outboundTag = $"{(serverItem.indexId.Equals(_config.indexId) ? "[proxy]" : $"[{index++}]")}{serverItem.remarks} ## {serverItem.indexId}";
// end
var item2 = JsonUtils.Deserialize<RulesItem4Ray>(JsonUtils.Serialize(item)); var item2 = JsonUtils.Deserialize<RulesItem4Ray>(JsonUtils.Serialize(item));
GenRoutingUserRule(item2, v2rayConfig); GenRoutingUserRule(item2, v2rayConfig);
} }
@ -293,6 +316,32 @@ namespace v2rayN.Handler
return 0; return 0;
} }
/// <summary>
/// 生成分流的出站规则
/// </summary>
private int GenOutbounds(V2rayConfig v2rayConfig, Outbounds4Ray outbound)
{
var routing = ConfigHandler.GetDefaultRouting(_config);
var ruleSet = JsonUtils.Deserialize<List<RulesItem>>(routing.ruleSet);
if (ruleSet == null) return -1;
int index = 1;
foreach (var item in ruleSet)
{
string outBoundIndexId = item.bingServerIndexId;
if (outBoundIndexId == null) continue;
var serverItem = LazyConfig.Instance.GetProfileItem(outBoundIndexId);
if (serverItem == null) continue;
var newOutbounds = JsonUtils.DeepCopy(outbound);
newOutbounds.tag = $"{(serverItem.indexId.Equals(_config.indexId) ? "[proxy]" : $"[{index++}]")}{serverItem.remarks} ## {serverItem.indexId}";
v2rayConfig.outbounds.Add(newOutbounds);
GenOutbound(serverItem, newOutbounds);
}
return 0;
}
private int GenOutbound(ProfileItem node, Outbounds4Ray outbound) private int GenOutbound(ProfileItem node, Outbounds4Ray outbound)
{ {
try try

View file

@ -12,6 +12,9 @@
public string outboundTag { get; set; } public string outboundTag { get; set; }
// 分流修改
public string bingServerIndexId { get; set; }
public List<string> ip { get; set; } public List<string> ip { get; set; }
public List<string> domain { get; set; } public List<string> domain { get; set; }

View file

@ -1,8 +1,4 @@
[ [
{
"protocol": [ "dns" ],
"outbound": "dns_out"
},
{ {
"network": "udp", "network": "udp",
"port": [ "port": [
@ -20,12 +16,5 @@
"ff00::/8" "ff00::/8"
], ],
"outbound": "block" "outbound": "block"
},
{
"source_ip_cidr": [
"224.0.0.0/3",
"ff00::/8"
],
"outbound": "block"
} }
] ]

View file

@ -1621,6 +1621,12 @@ namespace v2rayN.ViewModels
if (_config.tunModeItem.enableTun != EnableTun) if (_config.tunModeItem.enableTun != EnableTun)
{ {
_config.tunModeItem.enableTun = EnableTun; _config.tunModeItem.enableTun = EnableTun;
// 非管理员运行时tun模式开启逻辑修改
if (!Utils.IsAdministrator())
{
RebootAsAdmin();
return;
}
Reload(); Reload();
} }
} }
@ -1839,7 +1845,7 @@ namespace v2rayN.ViewModels
if (_config.uiItem.autoHideStartup) if (_config.uiItem.autoHideStartup)
{ {
Observable.Range(1, 1) Observable.Range(1, 1)
.Delay(TimeSpan.FromSeconds(2)) .Delay(TimeSpan.FromSeconds(0.5)) // 隐藏时间修改
.Subscribe(x => .Subscribe(x =>
{ {
Application.Current.Dispatcher.Invoke(() => Application.Current.Dispatcher.Invoke(() =>

View file

@ -212,7 +212,8 @@ namespace v2rayN.Views
var IsAdministrator = Utils.IsAdministrator(); var IsAdministrator = Utils.IsAdministrator();
this.Title = $"{Utils.GetVersion()} - {(IsAdministrator ? ResUI.RunAsAdmin : ResUI.NotRunAsAdmin)}"; this.Title = $"{Utils.GetVersion()} - {(IsAdministrator ? ResUI.RunAsAdmin : ResUI.NotRunAsAdmin)}";
spEnableTun.Visibility = IsAdministrator ? Visibility.Visible : Visibility.Collapsed; // 非管理员运行时tun模式开启逻辑修改
// spEnableTun.Visibility = IsAdministrator ? Visibility.Visible : Visibility.Collapsed;
//if (_config.uiItem.autoHideStartup) //if (_config.uiItem.autoHideStartup)
//{ //{

View file

@ -52,7 +52,22 @@
Margin="4" Margin="4"
MaxDropDownHeight="1000" MaxDropDownHeight="1000"
Style="{StaticResource DefComboBox}" /> Style="{StaticResource DefComboBox}" />
<TextBlock
Grid.Column="1"
Margin="230,0,186,0"
VerticalAlignment="Center"
Style="{StaticResource ToolbarTextBlock}"
Text="outboundIndexId" />
<ComboBox
x:Name="cmbOutboundIndexId"
Grid.Column="1"
Width="200"
Margin="354,4,0,5"
HorizontalAlignment="Left"
Style="{StaticResource DefComboBox}"
IsEnabled="False" />
<TextBlock <TextBlock
Grid.Row="1" Grid.Row="1"
Grid.Column="0" Grid.Column="0"

View file

@ -1,6 +1,8 @@
using ReactiveUI; using ReactiveUI;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Windows; using System.Windows;
using v2rayN.Handler;
using v2rayN.Mode; using v2rayN.Mode;
using v2rayN.ViewModels; using v2rayN.ViewModels;
@ -31,6 +33,29 @@ namespace v2rayN.Views
cmbOutboundTag.Items.Add(Global.ProxyTag); cmbOutboundTag.Items.Add(Global.ProxyTag);
cmbOutboundTag.Items.Add(Global.DirectTag); cmbOutboundTag.Items.Add(Global.DirectTag);
cmbOutboundTag.Items.Add(Global.BlockTag); cmbOutboundTag.Items.Add(Global.BlockTag);
// 分流实现
cmbOutboundIndexId.Items.Add(Global.ProxyTag);
cmbOutboundIndexId.Items.Add(Global.DirectTag);
cmbOutboundIndexId.Items.Add(Global.BlockTag);
var config = LazyConfig.Instance.GetConfig();
var allServerItems = LazyConfig.Instance.ProfileItems(null, "");
string tagName = null;
int index = 1;
foreach (var serverItem in allServerItems)
{
if (serverItem == null || serverItem.configType == EConfigType.Custom)
{
continue;
}
tagName = $"[{(serverItem.indexId.Equals(config.indexId) ? "proxy" : index++)}]{serverItem.remarks}";
cmbOutboundTag.Items.Add(tagName);
cmbOutboundIndexId.Items.Add(serverItem.indexId);
}
// end
Global.RuleProtocols.ForEach(it => Global.RuleProtocols.ForEach(it =>
{ {
clbProtocol.Items.Add(it); clbProtocol.Items.Add(it);
@ -54,6 +79,19 @@ namespace v2rayN.Views
this.WhenActivated(disposables => this.WhenActivated(disposables =>
{ {
// 分流
cmbOutboundIndexId.SelectedItem = cmbOutboundIndexId.Items.Cast<object>().FirstOrDefault(item => item.ToString() == ViewModel.SelectedSource.bingServerIndexId);
cmbOutboundTag.SelectedIndex = cmbOutboundIndexId.SelectedIndex;
this.WhenAnyValue(v => v.cmbOutboundTag.SelectedItem)
.Where(selectedItem => selectedItem != null)
.Subscribe(selectedItem =>
{
cmbOutboundIndexId.SelectedIndex = cmbOutboundTag.SelectedIndex;
ViewModel.SelectedSource.bingServerIndexId = cmbOutboundIndexId.SelectedItem.ToString();
ViewModel.SelectedSource.outboundTag = cmbOutboundTag.Text;
})
.DisposeWith(disposables);
// end
this.Bind(ViewModel, vm => vm.SelectedSource.outboundTag, v => v.cmbOutboundTag.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedSource.outboundTag, v => v.cmbOutboundTag.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.SelectedSource.port, v => v.txtPort.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedSource.port, v => v.txtPort.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.SelectedSource.enabled, v => v.togEnabled.IsChecked).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedSource.enabled, v => v.togEnabled.IsChecked).DisposeWith(disposables);