From 32cfe0271525895b14ed3906e36a12e369166956 Mon Sep 17 00:00:00 2001 From: Hellojack <106379370+H1JK@users.noreply.github.com> Date: Thu, 9 Jun 2022 11:40:10 +0800 Subject: [PATCH] [Netch] Support WireGuard protocol --- Netch/Resources/zh-CN | 4 ++ Netch/Servers/V2ray/V2rayConfig.cs | 10 ++++ Netch/Servers/V2ray/V2rayConfigUtils.cs | 28 +++++++++--- Netch/Servers/WireGuard/WireGuardForm.cs | 20 ++++++++ Netch/Servers/WireGuard/WireGuardServer.cs | 38 ++++++++++++++++ Netch/Servers/WireGuard/WireGuardUtil.cs | 53 ++++++++++++++++++++++ 6 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 Netch/Servers/WireGuard/WireGuardForm.cs create mode 100644 Netch/Servers/WireGuard/WireGuardServer.cs create mode 100644 Netch/Servers/WireGuard/WireGuardUtil.cs diff --git a/Netch/Resources/zh-CN b/Netch/Resources/zh-CN index 9c90f9ef..d9a968ef 100644 --- a/Netch/Resources/zh-CN +++ b/Netch/Resources/zh-CN @@ -69,6 +69,10 @@ "Plugin": "插件", "Plugin Options": "插件参数", "Remote Address": "远端地址", + "Local Addresses": "本地地址(可多个)", + "Public Key": "节点公钥", + "Private Key": "私钥", + "PSK": "节点预共享密钥", "Subscription": "订阅", "Manage Subscriptions": "管理订阅", diff --git a/Netch/Servers/V2ray/V2rayConfig.cs b/Netch/Servers/V2ray/V2rayConfig.cs index 28fd1eec..2850097b 100644 --- a/Netch/Servers/V2ray/V2rayConfig.cs +++ b/Netch/Servers/V2ray/V2rayConfig.cs @@ -47,6 +47,16 @@ public class OutboundConfiguration public string pluginOpts { get; set; } public string[] pluginArgs { get; set; } + + public string[] localAddresses { get; set; } + + public string peerPublicKey { get; set; } + + public string privateKey { get; set; } + + public string preSharedKey { get; set; } + + public int mtu { get; set; } } public class VnextItem diff --git a/Netch/Servers/V2ray/V2rayConfigUtils.cs b/Netch/Servers/V2ray/V2rayConfigUtils.cs index 76e4786b..5f98c7f2 100644 --- a/Netch/Servers/V2ray/V2rayConfigUtils.cs +++ b/Netch/Servers/V2ray/V2rayConfigUtils.cs @@ -186,14 +186,17 @@ public static class V2rayConfigUtils break; case TrojanServer trojan: outbound.protocol = "trojan"; - outbound.settings.servers = new[] + outbound.settings = new OutboundConfiguration { - new ShadowsocksServerItem // I'm not serious + servers = new[] { - address = await server.AutoResolveHostnameAsync(), - port = server.Port, - method = "", - password = trojan.Password + new ShadowsocksServerItem // I'm not serious + { + address = await server.AutoResolveHostnameAsync(), + port = server.Port, + method = "", + password = trojan.Password + } } }; @@ -221,6 +224,19 @@ public static class V2rayConfigUtils } } break; + case WireGuardServer wg: + outbound.protocol = "wireguard"; + outbound.settings = new OutboundConfiguration + { + address = await server.AutoResolveHostnameAsync(), + port = server.Port, + localAddresses = wg.LocalAddresses.SplitOrDefault(), + peerPublicKey = wg.PeerPublicKey, + privateKey = wg.PrivateKey, + preSharedKey = wg.PreSharedKey, + mtu = wg.MTU + }; + break; } diff --git a/Netch/Servers/WireGuard/WireGuardForm.cs b/Netch/Servers/WireGuard/WireGuardForm.cs new file mode 100644 index 00000000..1a63a6be --- /dev/null +++ b/Netch/Servers/WireGuard/WireGuardForm.cs @@ -0,0 +1,20 @@ +using Netch.Forms; + +namespace Netch.Servers; + +[Fody.ConfigureAwait(true)] +public class WireGuardForm : ServerForm +{ + public WireGuardForm(WireGuardServer? server = default) + { + server ??= new WireGuardServer(); + Server = server; + CreateTextBox("LocalAddresses", "Local Addresses", s => true, s => server.LocalAddresses = s, server.LocalAddresses); + CreateTextBox("PeerPublicKey", "Public Key", s => true, s => server.PeerPublicKey = s, server.PeerPublicKey); + CreateTextBox("PrivateKey", "Private Key", s => true, s => server.PrivateKey = s, server.PrivateKey); + CreateTextBox("PreSharedKey", "PSK", s => true, s => server.PreSharedKey = s, server.PreSharedKey); + CreateTextBox("MTU", "MTU", s => int.TryParse(s, out _), s => server.MTU = int.Parse(s), server.MTU.ToString(), 76); + } + + protected override string TypeName { get; } = "WireGuard"; +} \ No newline at end of file diff --git a/Netch/Servers/WireGuard/WireGuardServer.cs b/Netch/Servers/WireGuard/WireGuardServer.cs new file mode 100644 index 00000000..26bb3d14 --- /dev/null +++ b/Netch/Servers/WireGuard/WireGuardServer.cs @@ -0,0 +1,38 @@ +using Netch.Models; + +namespace Netch.Servers; + +public class WireGuardServer : Server +{ + public override string Type { get; } = "WireGuard"; + + public override string MaskedData() + { + return $"{LocalAddress} + {MTU}"; + } + + /// + /// 本地地址 + /// + public string LocalAddresses { get; set; } = "172.16.0.2"; + + /// + /// 节点公钥 + /// + public string PeerPublicKey { get; set; } = string.Empty; + + /// + /// 私钥 + /// + public string PrivateKey { get; set; } + + /// + /// 节点预共享密钥 + /// + public string? PreSharedKey { get; set; } + + /// + /// MTU + /// + public int MTU { get; set; } = 1420; +} diff --git a/Netch/Servers/WireGuard/WireGuardUtil.cs b/Netch/Servers/WireGuard/WireGuardUtil.cs new file mode 100644 index 00000000..e7bcb446 --- /dev/null +++ b/Netch/Servers/WireGuard/WireGuardUtil.cs @@ -0,0 +1,53 @@ +using System.Text.Encodings.Web; +using System.Text.Json; +using System.Text.Json.Serialization; +using Netch.Interfaces; +using Netch.Models; +using Netch.Utils; + +namespace Netch.Servers; + +public class WireGuardUtil : IServerUtil +{ + public ushort Priority { get; } = 4; + + public string TypeName { get; } = "WireGuard"; + + public string FullName { get; } = "WireGuard"; + + public string ShortName { get; } = "WG"; + + public string[] UriScheme { get; } = { "wireguard" }; + + public Type ServerType { get; } = typeof(WireGuardServer); + + public void Edit(Server s) + { + new WireGuardForm((WireGuardServer)s).ShowDialog(); + } + + public void Create() + { + new WireGuardForm().ShowDialog(); + } + + public string GetShareLink(Server s) + { + return V2rayUtils.GetVShareLink(s, "wireguard"); + } + + public IServerController GetController() + { + return new V2rayController(); + } + + public IEnumerable ParseUri(string text) + { + return V2rayUtils.ParseVUri(text); + } + + public bool CheckServer(Server s) + { + return true; + } +} \ No newline at end of file