Added Wireguard support (using sing-box)

pull/4655/head
2dust 2024-01-14 17:33:41 +08:00
parent 005f26ba7c
commit 69cbdbd20e
20 changed files with 239 additions and 23 deletions

View File

@ -138,7 +138,8 @@ namespace v2rayN
{EConfigType.VLESS,"vless://"},
{EConfigType.Trojan,"trojan://"},
{EConfigType.Hysteria2,"hysteria2://"},
{EConfigType.Tuic,"tuic://"}
{EConfigType.Tuic,"tuic://"},
{EConfigType.Wireguard,"wireguard://"}
};
public static readonly Dictionary<EConfigType, string> ProtocolTypes = new()
@ -149,7 +150,8 @@ namespace v2rayN
{EConfigType.VLESS,"vless"},
{EConfigType.Trojan,"trojan"},
{EConfigType.Hysteria2,"hysteria2"},
{EConfigType.Tuic,"tuic"}
{EConfigType.Tuic,"tuic"},
{EConfigType.Wireguard,"wireguard"}
};
public static readonly List<string> VmessSecuritys = new() { "aes-128-gcm", "chacha20-poly1305", "auto", "none", "zero" };

View File

@ -768,6 +768,34 @@ namespace v2rayN.Handler
return 0;
}
/// <summary>
/// Add or edit server
/// </summary>
/// <param name="config"></param>
/// <param name="profileItem"></param>
/// <returns></returns>
public static int AddWireguardServer(Config config, ProfileItem profileItem, bool toFile = true)
{
profileItem.configType = EConfigType.Wireguard;
profileItem.coreType = ECoreType.sing_box;
profileItem.address = profileItem.address.TrimEx();
profileItem.id = profileItem.id.TrimEx();
profileItem.publicKey = profileItem.publicKey.TrimEx();
profileItem.path = profileItem.path.TrimEx();
profileItem.requestHost = profileItem.requestHost.TrimEx();
profileItem.network = string.Empty;
if (profileItem.id.IsNullOrEmpty())
{
return -1;
}
AddServerCommon(config, profileItem, toFile);
return 0;
}
public static int SortServers(Config config, string subId, string colName, bool asc)
{
var lstModel = LazyConfig.Instance.ProfileItems(subId, "");
@ -1125,6 +1153,10 @@ namespace v2rayN.Handler
{
addStatus = AddTuicServer(config, profileItem, false);
}
else if (profileItem.configType == EConfigType.Wireguard)
{
addStatus = AddWireguardServer(config, profileItem, false);
}
if (addStatus == 0)
{

View File

@ -181,6 +181,8 @@ namespace v2rayN.Handler
tunInbound.mtu = _config.tunModeItem.mtu;
tunInbound.strict_route = _config.tunModeItem.strictRoute;
tunInbound.stack = _config.tunModeItem.stack;
tunInbound.sniff = _config.inbound[0].sniffingEnabled;
tunInbound.sniff_override_destination = _config.inbound[0].routeOnly ? false : _config.inbound[0].sniffingEnabled;
if (_config.tunModeItem.enableIPv6Address == false)
{
tunInbound.inet6_address = null;
@ -296,6 +298,17 @@ namespace v2rayN.Handler
GenOutboundMux(node, outbound);
}
else if (node.configType == EConfigType.Wireguard)
{
outbound.type = Global.ProtocolTypes[EConfigType.Wireguard];
outbound.private_key = node.id;
outbound.peer_public_key = node.publicKey;
outbound.reserved = Utils.String2List(node.path).Select(int.Parse).ToArray();
outbound.local_address = [.. Utils.String2List(node.requestHost)];
GenOutboundMux(node, outbound);
}
GenOutboundTls(node, outbound);

View File

@ -846,7 +846,8 @@ namespace v2rayN.Handler
if (prevNode is not null
&& prevNode.configType != EConfigType.Custom
&& prevNode.configType != EConfigType.Hysteria2
&& prevNode.configType != EConfigType.Tuic)
&& prevNode.configType != EConfigType.Tuic
&& prevNode.configType != EConfigType.Wireguard)
{
var prevOutbound = JsonUtils.Deserialize<Outbounds4Ray>(txtOutbound);
GenOutbound(prevNode, prevOutbound);
@ -864,7 +865,8 @@ namespace v2rayN.Handler
if (nextNode is not null
&& nextNode.configType != EConfigType.Custom
&& nextNode.configType != EConfigType.Hysteria2
&& nextNode.configType != EConfigType.Tuic)
&& nextNode.configType != EConfigType.Tuic
&& nextNode.configType != EConfigType.Wireguard)
{
var nextOutbound = JsonUtils.Deserialize<Outbounds4Ray>(txtOutbound);
GenOutbound(nextNode, nextOutbound);

View File

@ -70,7 +70,7 @@ namespace v2rayN.Handler
public int LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds)
{
int pid = -1;
var coreType = selecteds.Exists(t => t.configType == EConfigType.Hysteria2 || t.configType == EConfigType.Tuic) ? ECoreType.sing_box : ECoreType.Xray;
var coreType = selecteds.Exists(t => t.configType == EConfigType.Hysteria2 || t.configType == EConfigType.Tuic || t.configType == EConfigType.Wireguard) ? ECoreType.sing_box : ECoreType.Xray;
string configPath = Utils.GetConfigPath(Global.CoreSpeedtestConfigFileName);
if (CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType, out string msg) != 0)
{

View File

@ -1,5 +1,4 @@
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using static v2rayN.Handler.ProxySetting.InternetConnectionOption;
namespace v2rayN.Handler

View File

@ -9,6 +9,7 @@
VLESS = 5,
Trojan = 6,
Hysteria2 = 7,
Tuic = 8
Tuic = 8,
Wireguard = 9
}
}

View File

@ -47,18 +47,12 @@ namespace v2rayN.Mode
}
switch (configType)
{
case EConfigType.VMess:
case EConfigType.Shadowsocks:
case EConfigType.Socks:
case EConfigType.VLESS:
case EConfigType.Trojan:
case EConfigType.Hysteria2:
case EConfigType.Tuic:
summary += string.Format("{0}({1}:{2})", remarks, addr, port);
case EConfigType.Custom:
summary += string.Format("{0}", remarks);
break;
default:
summary += string.Format("{0}", remarks);
summary += string.Format("{0}({1}:{2})", remarks, addr, port);
break;
}
return summary;

View File

@ -112,7 +112,12 @@
public string congestion_control { get; set; }
public string? version { get; set; }
public string? network { get; set; }
public string packet_encoding { get; set; }
public string? packet_encoding { get; set; }
public string[]? local_address { get; set; }
public string? private_key { get; set; }
public string? peer_public_key { get; set; }
public int[]? reserved { get; set; }
public int? mtu { get; set; }
public Tls4Sbox tls { get; set; }
public Multiplex4Sbox multiplex { get; set; }
public Transport4Sbox transport { get; set; }

View File

@ -699,6 +699,15 @@ namespace v2rayN.Resx {
}
}
/// <summary>
/// 查找类似 Add [Wireguard] server 的本地化字符串。
/// </summary>
public static string menuAddWireguardServer {
get {
return ResourceManager.GetString("menuAddWireguardServer", resourceCulture);
}
}
/// <summary>
/// 查找类似 Check Update 的本地化字符串。
/// </summary>
@ -2140,6 +2149,15 @@ namespace v2rayN.Resx {
}
}
/// <summary>
/// 查找类似 Address(Ip,Ipv6) 的本地化字符串。
/// </summary>
public static string TbLocalAddress {
get {
return ResourceManager.GetString("TbLocalAddress", resourceCulture);
}
}
/// <summary>
/// 查找类似 Transport protocol(network) 的本地化字符串。
/// </summary>
@ -2185,6 +2203,15 @@ namespace v2rayN.Resx {
}
}
/// <summary>
/// 查找类似 PrivateKey 的本地化字符串。
/// </summary>
public static string TbPrivateKey {
get {
return ResourceManager.GetString("TbPrivateKey", resourceCulture);
}
}
/// <summary>
/// 查找类似 PublicKey 的本地化字符串。
/// </summary>
@ -2212,6 +2239,15 @@ namespace v2rayN.Resx {
}
}
/// <summary>
/// 查找类似 Reserved(2,3,4) 的本地化字符串。
/// </summary>
public static string TbReserved {
get {
return ResourceManager.GetString("TbReserved", resourceCulture);
}
}
/// <summary>
/// 查找类似 Reset 的本地化字符串。
/// </summary>

View File

@ -1168,4 +1168,16 @@
<data name="TbSettingsEnableIPv6Address" xml:space="preserve">
<value>Enable IPv6 Address</value>
</data>
<data name="menuAddWireguardServer" xml:space="preserve">
<value>Add [Wireguard] server</value>
</data>
<data name="TbPrivateKey" xml:space="preserve">
<value>PrivateKey</value>
</data>
<data name="TbReserved" xml:space="preserve">
<value>Reserved(2,3,4)</value>
</data>
<data name="TbLocalAddress" xml:space="preserve">
<value>Address(Ip,Ipv6)</value>
</data>
</root>

View File

@ -1165,4 +1165,16 @@
<data name="TbSettingsEnableIPv6Address" xml:space="preserve">
<value>启用IPv6</value>
</data>
<data name="menuAddWireguardServer" xml:space="preserve">
<value>添加[Wireguard]服务器</value>
</data>
<data name="TbPrivateKey" xml:space="preserve">
<value>PrivateKey</value>
</data>
<data name="TbReserved" xml:space="preserve">
<value>Reserved(2,3,4)</value>
</data>
<data name="TbLocalAddress" xml:space="preserve">
<value>Address(Ip,Ipv6)</value>
</data>
</root>

View File

@ -152,6 +152,10 @@ namespace v2rayN.ViewModels
case EConfigType.Tuic:
ret = ConfigHandler.AddTuicServer(_config, item);
break;
case EConfigType.Wireguard:
ret = ConfigHandler.AddWireguardServer(_config, item);
break;
}
if (ret == 0)

View File

@ -88,6 +88,7 @@ namespace v2rayN.ViewModels
public ReactiveCommand<Unit, Unit> AddTrojanServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddHysteria2ServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddTuicServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddWireguardServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddCustomServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddServerViaClipboardCmd { get; }
public ReactiveCommand<Unit, Unit> AddServerViaScanCmd { get; }
@ -347,6 +348,10 @@ namespace v2rayN.ViewModels
{
EditServer(true, EConfigType.Tuic);
});
AddWireguardServerCmd = ReactiveCommand.Create(() =>
{
EditServer(true, EConfigType.Wireguard);
});
AddCustomServerCmd = ReactiveCommand.Create(() =>
{
EditServer(true, EConfigType.Custom);
@ -1390,7 +1395,8 @@ namespace v2rayN.ViewModels
{
Process.Start(startInfo);
MyAppExit(false);
} catch { }
}
catch { }
}
private void ImportOldGuiConfig()

View File

@ -372,8 +372,7 @@ namespace v2rayN.ViewModels
type = CoreType6;
break;
case 7:
case 8:
default:
continue;
}
item.coreType = (ECoreType)Enum.Parse(typeof(ECoreType), type);

View File

@ -510,6 +510,83 @@
Margin="{StaticResource ServerItemMargin}"
Style="{StaticResource DefComboBox}" />
</Grid>
<Grid
x:Name="gridWireguard"
Grid.Row="2"
Visibility="Hidden">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="180" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Row="1"
Grid.Column="0"
Margin="{StaticResource ServerItemMargin}"
VerticalAlignment="Center"
Style="{StaticResource ToolbarTextBlock}"
Text="{x:Static resx:ResUI.TbPrivateKey}" />
<TextBox
x:Name="txtId9"
Grid.Row="1"
Grid.Column="1"
Width="400"
Margin="{StaticResource ServerItemMargin}"
Style="{StaticResource DefTextBox}" />
<TextBlock
Grid.Row="2"
Grid.Column="0"
Margin="{StaticResource ServerItemMargin}"
VerticalAlignment="Center"
Style="{StaticResource ToolbarTextBlock}"
Text="{x:Static resx:ResUI.TbPublicKey}" />
<TextBox
x:Name="txtPublicKey9"
Grid.Row="2"
Grid.Column="1"
Width="400"
Margin="{StaticResource ServerItemMargin}"
HorizontalAlignment="Left"
Style="{StaticResource DefTextBox}" />
<TextBlock
Grid.Row="3"
Grid.Column="0"
Margin="{StaticResource ServerItemMargin}"
VerticalAlignment="Center"
Style="{StaticResource ToolbarTextBlock}"
Text="{x:Static resx:ResUI.TbReserved}" />
<TextBox
x:Name="txtPath9"
Grid.Row="3"
Grid.Column="1"
Width="400"
Margin="{StaticResource ServerItemMargin}"
Style="{StaticResource DefTextBox}" />
<TextBlock
Grid.Row="4"
Grid.Column="0"
Margin="{StaticResource ServerItemMargin}"
VerticalAlignment="Center"
Style="{StaticResource ToolbarTextBlock}"
Text="{x:Static resx:ResUI.TbLocalAddress}" />
<TextBox
x:Name="txtRequestHost9"
Grid.Row="4"
Grid.Column="1"
Width="400"
Margin="{StaticResource ServerItemMargin}"
Style="{StaticResource DefTextBox}" />
</Grid>
<Separator
x:Name="sepa2"
@ -639,7 +716,7 @@
Margin="0,2"
Style="{DynamicResource MaterialDesignSeparator}" />
<Grid Grid.Row="6">
<Grid x:Name="gridTls" Grid.Row="6">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />

View File

@ -139,6 +139,16 @@ namespace v2rayN.Views
cmbHeaderType8.Items.Add(it);
});
break;
case EConfigType.Wireguard:
gridWireguard.Visibility = Visibility.Visible;
sepa2.Visibility = Visibility.Collapsed;
gridTransport.Visibility = Visibility.Collapsed;
gridTls.Visibility = Visibility.Collapsed;
cmbCoreType.IsEnabled = false;
break;
}
gridTlsMore.Visibility = Visibility.Hidden;
@ -188,6 +198,13 @@ namespace v2rayN.Views
this.Bind(ViewModel, vm => vm.SelectedSource.security, v => v.txtSecurity8.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.SelectedSource.headerType, v => v.cmbHeaderType8.Text).DisposeWith(disposables);
break;
case EConfigType.Wireguard:
this.Bind(ViewModel, vm => vm.SelectedSource.id, v => v.txtId9.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.SelectedSource.publicKey, v => v.txtPublicKey9.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.SelectedSource.path, v => v.txtPath9.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.SelectedSource.requestHost, v => v.txtRequestHost9.Text).DisposeWith(disposables);
break;
}
this.Bind(ViewModel, vm => vm.SelectedSource.network, v => v.cmbNetwork.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.SelectedSource.headerType, v => v.cmbHeaderType.Text).DisposeWith(disposables);

View File

@ -83,6 +83,7 @@
x:Name="menuAddTrojanServer"
Height="{StaticResource MenuItemHeight}"
Header="{x:Static resx:ResUI.menuAddTrojanServer}" />
<Separator Margin="-40,5" />
<MenuItem
x:Name="menuAddHysteria2Server"
Height="{StaticResource MenuItemHeight}"
@ -91,6 +92,10 @@
x:Name="menuAddTuicServer"
Height="{StaticResource MenuItemHeight}"
Header="{x:Static resx:ResUI.menuAddTuicServer}" />
<MenuItem
x:Name="menuAddWireguardServer"
Height="{StaticResource MenuItemHeight}"
Header="{x:Static resx:ResUI.menuAddWireguardServer}" />
<Separator Margin="-40,5" />
<MenuItem
x:Name="menuAddServerViaClipboard"

View File

@ -88,6 +88,7 @@ namespace v2rayN.Views
this.BindCommand(ViewModel, vm => vm.AddTrojanServerCmd, v => v.menuAddTrojanServer).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.AddHysteria2ServerCmd, v => v.menuAddHysteria2Server).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.AddTuicServerCmd, v => v.menuAddTuicServer).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.AddWireguardServerCmd, v => v.menuAddWireguardServer).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.AddCustomServerCmd, v => v.menuAddCustomServer).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.AddServerViaClipboardCmd, v => v.menuAddServerViaClipboard).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.AddServerViaScanCmd, v => v.menuAddServerViaScan).DisposeWith(disposables);

View File

@ -203,6 +203,5 @@ namespace v2rayN.Views
{
this.Close();
}
}
}