Skip to content

Commit

Permalink
Added Wireguard support (using sing-box)
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Jan 14, 2024
1 parent 005f26b commit 69cbdbd
Show file tree
Hide file tree
Showing 20 changed files with 239 additions and 23 deletions.
6 changes: 4 additions & 2 deletions v2rayN/v2rayN/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ internal class Global
{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()
Expand All @@ -149,7 +150,8 @@ internal class Global
{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" };
Expand Down
32 changes: 32 additions & 0 deletions v2rayN/v2rayN/Handler/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,34 @@ public static int AddTuicServer(Config config, ProfileItem profileItem, bool toF
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, "");
Expand Down Expand Up @@ -1125,6 +1153,10 @@ private static int AddBatchServers(Config config, string clipboardData, string s
{
addStatus = AddTuicServer(config, profileItem, false);
}
else if (profileItem.configType == EConfigType.Wireguard)
{
addStatus = AddWireguardServer(config, profileItem, false);
}

if (addStatus == 0)
{
Expand Down
13 changes: 13 additions & 0 deletions v2rayN/v2rayN/Handler/CoreConfigSingbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ private int GenInbounds(SingboxConfig singboxConfig)
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;
Expand Down Expand Up @@ -296,6 +298,17 @@ private int GenOutbound(ProfileItem node, Outbound4Sbox outbound)

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);

Expand Down
6 changes: 4 additions & 2 deletions v2rayN/v2rayN/Handler/CoreConfigV2ray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,8 @@ private int GenMoreOutbounds(ProfileItem node, V2rayConfig v2rayConfig)
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);
Expand All @@ -864,7 +865,8 @@ private int GenMoreOutbounds(ProfileItem node, V2rayConfig v2rayConfig)
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);
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Handler/CoreHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void LoadCore()
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)
{
Expand Down
3 changes: 1 addition & 2 deletions v2rayN/v2rayN/Handler/ProxySetting.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using static v2rayN.Handler.ProxySetting.InternetConnectionOption;

namespace v2rayN.Handler
Expand Down
3 changes: 2 additions & 1 deletion v2rayN/v2rayN/Mode/EConfigType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum EConfigType
VLESS = 5,
Trojan = 6,
Hysteria2 = 7,
Tuic = 8
Tuic = 8,
Wireguard = 9
}
}
12 changes: 3 additions & 9 deletions v2rayN/v2rayN/Mode/ProfileItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,12 @@ public string GetSummary()
}
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;
Expand Down
7 changes: 6 additions & 1 deletion v2rayN/v2rayN/Mode/SingboxConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ public class Outbound4Sbox
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; }
Expand Down
36 changes: 36 additions & 0 deletions v2rayN/v2rayN/Resx/ResUI.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions v2rayN/v2rayN/Resx/ResUI.resx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
12 changes: 12 additions & 0 deletions v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
4 changes: 4 additions & 0 deletions v2rayN/v2rayN/ViewModels/AddServerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ private void SaveServer()
case EConfigType.Tuic:
ret = ConfigHandler.AddTuicServer(_config, item);
break;

case EConfigType.Wireguard:
ret = ConfigHandler.AddWireguardServer(_config, item);
break;
}

if (ret == 0)
Expand Down
8 changes: 7 additions & 1 deletion v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class MainWindowViewModel : ReactiveObject
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; }
Expand Down Expand Up @@ -347,6 +348,10 @@ public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue, Action<EV
{
EditServer(true, EConfigType.Tuic);
});
AddWireguardServerCmd = ReactiveCommand.Create(() =>
{
EditServer(true, EConfigType.Wireguard);
});
AddCustomServerCmd = ReactiveCommand.Create(() =>
{
EditServer(true, EConfigType.Custom);
Expand Down Expand Up @@ -1390,7 +1395,8 @@ private void RebootAsAdmin()
{
Process.Start(startInfo);
MyAppExit(false);
} catch { }
}
catch { }
}

private void ImportOldGuiConfig()
Expand Down
3 changes: 1 addition & 2 deletions v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ private int SaveCoreType()
type = CoreType6;
break;

case 7:
case 8:
default:
continue;
}
item.coreType = (ECoreType)Enum.Parse(typeof(ECoreType), type);
Expand Down
Loading

0 comments on commit 69cbdbd

Please sign in to comment.