Add Wireguard PresharedKey

This commit is contained in:
DHR60 2026-05-04 13:50:37 +08:00
parent 5f41a60be8
commit 23ed2a73f9
18 changed files with 104 additions and 40 deletions

View file

@ -27,6 +27,7 @@ public class WireguardFmt : BaseFmt
item.SetProtocolExtra(item.GetProtocolExtra() with item.SetProtocolExtra(item.GetProtocolExtra() with
{ {
WgPublicKey = GetQueryDecoded(query, "publickey"), WgPublicKey = GetQueryDecoded(query, "publickey"),
WgPresharedKey = GetQueryDecoded(query, "presharedkey"),
WgReserved = GetQueryDecoded(query, "reserved"), WgReserved = GetQueryDecoded(query, "reserved"),
WgInterfaceAddress = GetQueryDecoded(query, "address"), WgInterfaceAddress = GetQueryDecoded(query, "address"),
WgMtu = int.TryParse(GetQueryDecoded(query, "mtu"), out var mtuVal) ? mtuVal : 1280, WgMtu = int.TryParse(GetQueryDecoded(query, "mtu"), out var mtuVal) ? mtuVal : 1280,
@ -48,20 +49,25 @@ public class WireguardFmt : BaseFmt
remark = "#" + Utils.UrlEncode(item.Remarks); remark = "#" + Utils.UrlEncode(item.Remarks);
} }
var protoExtra = item.GetProtocolExtra();
var dicQuery = new Dictionary<string, string>(); var dicQuery = new Dictionary<string, string>();
if (!item.GetProtocolExtra().WgPublicKey.IsNullOrEmpty()) if (!protoExtra.WgPublicKey.IsNullOrEmpty())
{ {
dicQuery.Add("publickey", Utils.UrlEncode(item.GetProtocolExtra().WgPublicKey)); dicQuery.Add("publickey", Utils.UrlEncode(protoExtra.WgPublicKey));
} }
if (!item.GetProtocolExtra().WgReserved.IsNullOrEmpty()) if (!protoExtra.WgPresharedKey.IsNullOrEmpty())
{ {
dicQuery.Add("reserved", Utils.UrlEncode(item.GetProtocolExtra().WgReserved)); dicQuery.Add("presharedkey", Utils.UrlEncode(protoExtra.WgPresharedKey));
} }
if (!item.GetProtocolExtra().WgInterfaceAddress.IsNullOrEmpty()) if (!protoExtra.WgReserved.IsNullOrEmpty())
{ {
dicQuery.Add("address", Utils.UrlEncode(item.GetProtocolExtra().WgInterfaceAddress)); dicQuery.Add("reserved", Utils.UrlEncode(protoExtra.WgReserved));
} }
dicQuery.Add("mtu", Utils.UrlEncode(item.GetProtocolExtra().WgMtu > 0 ? item.GetProtocolExtra().WgMtu.ToString() : "1280")); if (!protoExtra.WgInterfaceAddress.IsNullOrEmpty())
{
dicQuery.Add("address", Utils.UrlEncode(protoExtra.WgInterfaceAddress));
}
dicQuery.Add("mtu", Utils.UrlEncode(protoExtra.WgMtu > 0 ? protoExtra.WgMtu.ToString() : "1280"));
return ToUri(EConfigType.WireGuard, item.Address, item.Port, item.Password, dicQuery, remark); return ToUri(EConfigType.WireGuard, item.Address, item.Port, item.Password, dicQuery, remark);
} }
} }

View file

@ -173,7 +173,7 @@ public class Peer4Sbox
public string? pre_shared_key { get; set; } public string? pre_shared_key { get; set; }
public List<string> allowed_ips { get; set; } public List<string> allowed_ips { get; set; }
public int? persistent_keepalive_interval { get; set; } public int? persistent_keepalive_interval { get; set; }
public List<int> reserved { get; set; } public List<int>? reserved { get; set; }
} }
public class Tls4Sbox public class Tls4Sbox

View file

@ -163,6 +163,7 @@ public class WireguardPeer4Ray
{ {
public string endpoint { get; set; } public string endpoint { get; set; }
public string publicKey { get; set; } public string publicKey { get; set; }
public string? preSharedKey { get; set; }
} }
public class VnextItem4Ray public class VnextItem4Ray

View file

@ -3321,6 +3321,15 @@ namespace ServiceLib.Resx {
} }
} }
/// <summary>
/// 查找类似 PreSharedKey 的本地化字符串。
/// </summary>
public static string TbPreSharedKey {
get {
return ResourceManager.GetString("TbPreSharedKey", resourceCulture);
}
}
/// <summary> /// <summary>
/// 查找类似 Socks port 的本地化字符串。 /// 查找类似 Socks port 的本地化字符串。
/// </summary> /// </summary>

View file

@ -1725,4 +1725,7 @@ The "Get Certificate" action may fail if a self-signed certificate is used or if
<data name="TbSettingsBindInterfaceTip" xml:space="preserve"> <data name="TbSettingsBindInterfaceTip" xml:space="preserve">
<value>For multi-interface environments, enter the name of the interface to bind. Only effective on Windows systems and TUN mode</value> <value>For multi-interface environments, enter the name of the interface to bind. Only effective on Windows systems and TUN mode</value>
</data> </data>
<data name="TbPreSharedKey" xml:space="preserve">
<value>PreSharedKey</value>
</data>
</root> </root>

View file

@ -1716,4 +1716,13 @@ The "Get Certificate" action may fail if a self-signed certificate is used or if
<data name="TbSettingsBindInterfaceTip" xml:space="preserve"> <data name="TbSettingsBindInterfaceTip" xml:space="preserve">
<value>Pour les environnements multi-interfaces, entrez le nom de l'interface à lier. Ne fonctionne que sur les systèmes Windows et en mode TUN</value> <value>Pour les environnements multi-interfaces, entrez le nom de l'interface à lier. Ne fonctionne que sur les systèmes Windows et en mode TUN</value>
</data> </data>
<data name="menuUdpTestServer" xml:space="preserve">
<value>Test Configurations UDP Delay</value>
</data>
<data name="TbSettingsUdpTestUrl" xml:space="preserve">
<value>UDP Test Url</value>
</data>
<data name="TbPreSharedKey" xml:space="preserve">
<value>PreSharedKey</value>
</data>
</root> </root>

View file

@ -1725,4 +1725,7 @@ The "Get Certificate" action may fail if a self-signed certificate is used or if
<data name="TbSettingsBindInterfaceTip" xml:space="preserve"> <data name="TbSettingsBindInterfaceTip" xml:space="preserve">
<value>For multi-interface environments, enter the name of the interface to bind. Only effective on Windows systems and TUN mode</value> <value>For multi-interface environments, enter the name of the interface to bind. Only effective on Windows systems and TUN mode</value>
</data> </data>
<data name="TbPreSharedKey" xml:space="preserve">
<value>PreSharedKey</value>
</data>
</root> </root>

View file

@ -1725,4 +1725,7 @@ The "Get Certificate" action may fail if a self-signed certificate is used or if
<data name="TbSettingsBindInterfaceTip" xml:space="preserve"> <data name="TbSettingsBindInterfaceTip" xml:space="preserve">
<value>For multi-interface environments, enter the name of the interface to bind. Only effective on Windows systems and TUN mode</value> <value>For multi-interface environments, enter the name of the interface to bind. Only effective on Windows systems and TUN mode</value>
</data> </data>
<data name="TbPreSharedKey" xml:space="preserve">
<value>PreSharedKey</value>
</data>
</root> </root>

View file

@ -1725,4 +1725,7 @@
<data name="TbSettingsBindInterfaceTip" xml:space="preserve"> <data name="TbSettingsBindInterfaceTip" xml:space="preserve">
<value>Для среды с несколькими сетевыми интерфейсами укажите имя интерфейса для привязки. Работает только в Windows и режиме TUN</value> <value>Для среды с несколькими сетевыми интерфейсами укажите имя интерфейса для привязки. Работает только в Windows и режиме TUN</value>
</data> </data>
<data name="TbPreSharedKey" xml:space="preserve">
<value>PreSharedKey</value>
</data>
</root> </root>

View file

@ -1722,4 +1722,7 @@
<data name="TbSettingsBindInterfaceTip" xml:space="preserve"> <data name="TbSettingsBindInterfaceTip" xml:space="preserve">
<value>用于多网口环境,填写要绑定的网口名称,仅生效于 Windows 系统和 TUN 模式</value> <value>用于多网口环境,填写要绑定的网口名称,仅生效于 Windows 系统和 TUN 模式</value>
</data> </data>
<data name="TbPreSharedKey" xml:space="preserve">
<value>PreSharedKey</value>
</data>
</root> </root>

View file

@ -1722,4 +1722,7 @@
<data name="TbSettingsBindInterfaceTip" xml:space="preserve"> <data name="TbSettingsBindInterfaceTip" xml:space="preserve">
<value>For multi-interface environments, enter the name of the interface to bind. Only effective on Windows systems and TUN mode</value> <value>For multi-interface environments, enter the name of the interface to bind. Only effective on Windows systems and TUN mode</value>
</data> </data>
<data name="TbPreSharedKey" xml:space="preserve">
<value>PreSharedKey</value>
</data>
</root> </root>

View file

@ -318,13 +318,12 @@ public partial class CoreConfigSingboxService
{ {
var peer = new Peer4Sbox var peer = new Peer4Sbox
{ {
public_key = protocolExtra.WgPublicKey, public_key = protocolExtra.WgPublicKey ?? string.Empty,
pre_shared_key = protocolExtra.WgPresharedKey, pre_shared_key = protocolExtra.WgPresharedKey,
reserved = Utils.String2List(protocolExtra.WgReserved)?.Select(int.Parse).ToList(), reserved = Utils.String2List(protocolExtra.WgReserved)?.Select(int.Parse).ToList(),
address = _node.Address, address = _node.Address,
port = _node.Port, port = _node.Port,
// TODO default ["0.0.0.0/0", "::/0"] allowed_ips = ["0.0.0.0/0", "::/0"],
allowed_ips = new() { "0.0.0.0/0", "::/0" },
}; };
endpoint.private_key = _node.Password; endpoint.private_key = _node.Password;
endpoint.mtu = protocolExtra.WgMtu > 0 ? protocolExtra.WgMtu : Global.TunMtus.First(); endpoint.mtu = protocolExtra.WgMtu > 0 ? protocolExtra.WgMtu : Global.TunMtus.First();

View file

@ -258,7 +258,8 @@ public partial class CoreConfigV2rayService
var peer = new WireguardPeer4Ray var peer = new WireguardPeer4Ray
{ {
publicKey = protocolExtra.WgPublicKey ?? "", publicKey = protocolExtra.WgPublicKey ?? "",
endpoint = address + ":" + _node.Port.ToString() endpoint = address + ":" + _node.Port.ToString(),
preSharedKey = protocolExtra.WgPresharedKey,
}; };
var setting = new Outboundsettings4Ray var setting = new Outboundsettings4Ray
{ {

View file

@ -53,8 +53,9 @@ public class AddServerViewModel : MyReactiveObject
[Reactive] [Reactive]
public string WgPublicKey { get; set; } public string WgPublicKey { get; set; }
//[Reactive] [Reactive]
//public string WgPresharedKey { get; set; } public string WgPresharedKey { get; set; }
[Reactive] [Reactive]
public string WgInterfaceAddress { get; set; } public string WgInterfaceAddress { get; set; }
@ -159,17 +160,8 @@ public class AddServerViewModel : MyReactiveObject
switch (SelectedSource.GetNetwork()) switch (SelectedSource.GetNetwork())
{ {
case nameof(ETransport.raw): case nameof(ETransport.raw):
Host = value;
break;
case nameof(ETransport.ws): case nameof(ETransport.ws):
Host = value;
break;
case nameof(ETransport.httpupgrade): case nameof(ETransport.httpupgrade):
Host = value;
break;
case nameof(ETransport.xhttp): case nameof(ETransport.xhttp):
Host = value; Host = value;
break; break;
@ -202,13 +194,7 @@ public class AddServerViewModel : MyReactiveObject
break; break;
case nameof(ETransport.ws): case nameof(ETransport.ws):
Path = value;
break;
case nameof(ETransport.httpupgrade): case nameof(ETransport.httpupgrade):
Path = value;
break;
case nameof(ETransport.xhttp): case nameof(ETransport.xhttp):
Path = value; Path = value;
break; break;
@ -303,6 +289,7 @@ public class AddServerViewModel : MyReactiveObject
VlessEncryption = protocolExtra.VlessEncryption?.IsNullOrEmpty() == false ? protocolExtra.VlessEncryption : Global.None; VlessEncryption = protocolExtra.VlessEncryption?.IsNullOrEmpty() == false ? protocolExtra.VlessEncryption : Global.None;
SsMethod = protocolExtra.SsMethod ?? string.Empty; SsMethod = protocolExtra.SsMethod ?? string.Empty;
WgPublicKey = protocolExtra.WgPublicKey ?? string.Empty; WgPublicKey = protocolExtra.WgPublicKey ?? string.Empty;
WgPresharedKey = protocolExtra.WgPresharedKey ?? string.Empty;
WgInterfaceAddress = protocolExtra.WgInterfaceAddress ?? string.Empty; WgInterfaceAddress = protocolExtra.WgInterfaceAddress ?? string.Empty;
WgReserved = protocolExtra.WgReserved ?? string.Empty; WgReserved = protocolExtra.WgReserved ?? string.Empty;
WgMtu = protocolExtra.WgMtu ?? 1280; WgMtu = protocolExtra.WgMtu ?? 1280;
@ -401,6 +388,7 @@ public class AddServerViewModel : MyReactiveObject
VlessEncryption = VlessEncryption.NullIfEmpty(), VlessEncryption = VlessEncryption.NullIfEmpty(),
SsMethod = SsMethod.NullIfEmpty(), SsMethod = SsMethod.NullIfEmpty(),
WgPublicKey = WgPublicKey.NullIfEmpty(), WgPublicKey = WgPublicKey.NullIfEmpty(),
WgPresharedKey = WgPresharedKey.NullIfEmpty(),
WgInterfaceAddress = WgInterfaceAddress.NullIfEmpty(), WgInterfaceAddress = WgInterfaceAddress.NullIfEmpty(),
WgReserved = WgReserved.NullIfEmpty(), WgReserved = WgReserved.NullIfEmpty(),
WgMtu = WgMtu >= 576 ? WgMtu : null, WgMtu = WgMtu >= 576 ? WgMtu : null,

View file

@ -508,7 +508,7 @@
Grid.Row="2" Grid.Row="2"
ColumnDefinitions="300,Auto" ColumnDefinitions="300,Auto"
IsVisible="False" IsVisible="False"
RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto"> RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,Auto">
<TextBlock <TextBlock
Grid.Row="1" Grid.Row="1"
@ -542,38 +542,52 @@
Grid.Column="0" Grid.Column="0"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center" VerticalAlignment="Center"
Text="{x:Static resx:ResUI.TbPreSharedKey}" />
<TextBox
x:Name="txtPreSharedKey9"
Grid.Row="3"
Grid.Column="1"
Width="400"
Margin="{StaticResource Margin4}"
HorizontalAlignment="Left" />
<TextBlock
Grid.Row="4"
Grid.Column="0"
Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
Text="{x:Static resx:ResUI.TbReserved}" /> Text="{x:Static resx:ResUI.TbReserved}" />
<TextBox <TextBox
x:Name="txtPath9" x:Name="txtPath9"
Grid.Row="3" Grid.Row="4"
Grid.Column="1" Grid.Column="1"
Width="400" Width="400"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
Watermark="2,3,4" /> Watermark="2,3,4" />
<TextBlock <TextBlock
Grid.Row="4" Grid.Row="5"
Grid.Column="0" Grid.Column="0"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center" VerticalAlignment="Center"
Text="{x:Static resx:ResUI.TbLocalAddress}" /> Text="{x:Static resx:ResUI.TbLocalAddress}" />
<TextBox <TextBox
x:Name="txtRequestHost9" x:Name="txtRequestHost9"
Grid.Row="4" Grid.Row="5"
Grid.Column="1" Grid.Column="1"
Width="400" Width="400"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
Watermark="Ipv4,Ipv6" /> Watermark="Ipv4,Ipv6" />
<TextBlock <TextBlock
Grid.Row="5" Grid.Row="6"
Grid.Column="0" Grid.Column="0"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center" VerticalAlignment="Center"
Text="{x:Static resx:ResUI.TbMtu}" /> Text="{x:Static resx:ResUI.TbMtu}" />
<TextBox <TextBox
x:Name="txtShortId9" x:Name="txtShortId9"
Grid.Row="5" Grid.Row="6"
Grid.Column="1" Grid.Column="1"
Width="200" Width="200"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"

View file

@ -185,6 +185,7 @@ public partial class AddServerWindow : WindowBase<AddServerViewModel>
case EConfigType.WireGuard: case EConfigType.WireGuard:
this.Bind(ViewModel, vm => vm.SelectedSource.Password, v => v.txtId9.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedSource.Password, v => v.txtId9.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.WgPublicKey, v => v.txtPublicKey9.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.WgPublicKey, v => v.txtPublicKey9.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.WgPresharedKey, v => v.txtPreSharedKey9.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.WgReserved, v => v.txtPath9.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.WgReserved, v => v.txtPath9.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.WgInterfaceAddress, v => v.txtRequestHost9.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.WgInterfaceAddress, v => v.txtRequestHost9.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.WgMtu, v => v.txtShortId9.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.WgMtu, v => v.txtShortId9.Text).DisposeWith(disposables);

View file

@ -678,6 +678,7 @@
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="300" /> <ColumnDefinition Width="300" />
@ -721,10 +722,26 @@
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center" VerticalAlignment="Center"
Style="{StaticResource ToolbarTextBlock}" Style="{StaticResource ToolbarTextBlock}"
Text="{x:Static resx:ResUI.TbPreSharedKey}" />
<TextBox
x:Name="txtPreSharedKey9"
Grid.Row="3"
Grid.Column="1"
Width="400"
Margin="{StaticResource Margin4}"
HorizontalAlignment="Left"
Style="{StaticResource DefTextBox}" />
<TextBlock
Grid.Row="4"
Grid.Column="0"
Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
Style="{StaticResource ToolbarTextBlock}"
Text="{x:Static resx:ResUI.TbReserved}" /> Text="{x:Static resx:ResUI.TbReserved}" />
<TextBox <TextBox
x:Name="txtPath9" x:Name="txtPath9"
Grid.Row="3" Grid.Row="4"
Grid.Column="1" Grid.Column="1"
Width="400" Width="400"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
@ -732,7 +749,7 @@
Style="{StaticResource DefTextBox}" /> Style="{StaticResource DefTextBox}" />
<TextBlock <TextBlock
Grid.Row="4" Grid.Row="5"
Grid.Column="0" Grid.Column="0"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center" VerticalAlignment="Center"
@ -740,7 +757,7 @@
Text="{x:Static resx:ResUI.TbLocalAddress}" /> Text="{x:Static resx:ResUI.TbLocalAddress}" />
<TextBox <TextBox
x:Name="txtRequestHost9" x:Name="txtRequestHost9"
Grid.Row="4" Grid.Row="5"
Grid.Column="1" Grid.Column="1"
Width="400" Width="400"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
@ -748,7 +765,7 @@
Style="{StaticResource DefTextBox}" /> Style="{StaticResource DefTextBox}" />
<TextBlock <TextBlock
Grid.Row="5" Grid.Row="6"
Grid.Column="0" Grid.Column="0"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center" VerticalAlignment="Center"
@ -756,7 +773,7 @@
Text="{x:Static resx:ResUI.TbMtu}" /> Text="{x:Static resx:ResUI.TbMtu}" />
<TextBox <TextBox
x:Name="txtShortId9" x:Name="txtShortId9"
Grid.Row="5" Grid.Row="6"
Grid.Column="1" Grid.Column="1"
Width="200" Width="200"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"

View file

@ -184,6 +184,7 @@ public partial class AddServerWindow
case EConfigType.WireGuard: case EConfigType.WireGuard:
this.Bind(ViewModel, vm => vm.SelectedSource.Password, v => v.txtId9.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedSource.Password, v => v.txtId9.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.WgPublicKey, v => v.txtPublicKey9.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.WgPublicKey, v => v.txtPublicKey9.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.WgPresharedKey, v => v.txtPreSharedKey9.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.WgReserved, v => v.txtPath9.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.WgReserved, v => v.txtPath9.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.WgInterfaceAddress, v => v.txtRequestHost9.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.WgInterfaceAddress, v => v.txtRequestHost9.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.WgMtu, v => v.txtShortId9.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.WgMtu, v => v.txtShortId9.Text).DisposeWith(disposables);