now override or else server props
bb7a19b5b1915524a023efc8a4661ab641bd385d
7 files changed
src/WireGuardConfigGenerator/DataModel/Root.cssrc/WireGuardConfigGenerator/Helpers/CryptoUtils.cssrc/WireGuardConfigGenerator/Helpers/TreeHelpers.cssrc/WireGuardConfigGenerator/UserControlPeer.Designer.cssrc/WireGuardConfigGenerator/UserControlPeer.cssrc/WireGuardConfigGenerator/UserControlServer.Designer.cssrc/WireGuardConfigGenerator/UserControlServer.cs
diff --git a/src/WireGuardConfigGenerator/DataModel/Root.cs b/src/WireGuardConfigGenerator/DataModel/Root.cs
index 66c94a9..25b348e 100644
--- a/src/WireGuardConfigGenerator/DataModel/Root.cs
+++ b/src/WireGuardConfigGenerator/DataModel/Root.cs
@@ -21,7 +21,7 @@ public class Root
{
if (File.Exists(path))
{
- var root = await CryptoUtils.DecryptAndDecompressFromFileAsync<Root>(path, password) ?? new();
+ Root? root = await CryptoUtils.DecryptAndDecompressFromFileAsync<Root>(path, password);
if (root != null)
{
Groups = root.Groups;
@@ -48,6 +48,7 @@ public class WireGuardItem
public string? AllowedIPs { get; set; }
public string Address { get; set; } = "0.0.0.0";
public string? DNS { get; set; }
+ public int PersistentKeepalive { get; set; }
}
public class Server : WireGuardItem
@@ -75,7 +76,8 @@ public class Server : WireGuardItem
public class Peer : WireGuardItem
{
- public bool OverrideAllowedIPs { get; set; }
+ public bool UseAllowedIPs { get; set; }
+
public int PersistentMaxConnections { get; set; }
public int PersistentMinConnections { get; set; }
public string? PersistentConnectionInterval { get; set; }
@@ -98,7 +100,6 @@ public class Peer : WireGuardItem
public bool UsePersistentMinConnections { get; set; }
public bool UsePersistentConnectionInterval { get; set; }
public bool UsePersistentConnectionTimeout { get; set; }
- public int PersistentKeepalive { get; set; }
[JsonIgnore]
public Server? ParentServer { get; set; }
diff --git a/src/WireGuardConfigGenerator/Helpers/CryptoUtils.cs b/src/WireGuardConfigGenerator/Helpers/CryptoUtils.cs
index 6e71b8d..bdfc10a 100644
--- a/src/WireGuardConfigGenerator/Helpers/CryptoUtils.cs
+++ b/src/WireGuardConfigGenerator/Helpers/CryptoUtils.cs
@@ -101,6 +101,22 @@ public static class CryptoUtils
public static async Task<T?> DecryptAndDecompressFromFileAsync<T>(string filePath, string password, JsonSerializerOptions? options = null, CancellationToken ct = default)
{
+ try
+ {
+ return await InternalDecryptAndDecompressFromFileAsync<T>(filePath, password, options, ct);
+
+ }
+ catch
+ {
+ return default;
+ }
+ }
+
+ private static async Task<T?> InternalDecryptAndDecompressFromFileAsync<T>(string filePath, string password, JsonSerializerOptions? options = null, CancellationToken ct = default)
+ {
+ if (!File.Exists(filePath))
+ return default;
+
options ??= new JsonSerializerOptions();
// 1. Asynch read file
@@ -108,7 +124,8 @@ public static class CryptoUtils
// 2. Salt and ciphertext
if (encryptedData.Length < SaltSize)
- throw new ArgumentException("Encrypted data is too short to contain salt.");
+ return default;
+ //throw new ArgumentException("Encrypted data is too short to contain salt.");
byte[] salt = new byte[SaltSize];
byte[] ciphertext = new byte[encryptedData.Length - SaltSize];
diff --git a/src/WireGuardConfigGenerator/Helpers/TreeHelpers.cs b/src/WireGuardConfigGenerator/Helpers/TreeHelpers.cs
index dde18c8..d4764b8 100644
--- a/src/WireGuardConfigGenerator/Helpers/TreeHelpers.cs
+++ b/src/WireGuardConfigGenerator/Helpers/TreeHelpers.cs
@@ -87,6 +87,8 @@ public static class TreeHelpers
ListenPort = settings.ListenPort + (group.Servers.Count * 100) - ((root.Groups.Count - 1) * 1000),
Address = settings.Address,
AllowedIPs = $"{GetSubnet(settings.Address)}.0/24",
+ DNS = "8.8.8.8",
+ PersistentKeepalive = 25,
Endpoint = settings.Endpoint,
PostUp = string.Format(settings.PostUp, name),
PrivateKey = privateKey,
diff --git a/src/WireGuardConfigGenerator/UserControlPeer.Designer.cs b/src/WireGuardConfigGenerator/UserControlPeer.Designer.cs
index 8be5cce..2483bc6 100644
--- a/src/WireGuardConfigGenerator/UserControlPeer.Designer.cs
+++ b/src/WireGuardConfigGenerator/UserControlPeer.Designer.cs
@@ -34,7 +34,9 @@
txtConf = new TextBox();
tabPage1 = new TabPage();
groupBox1 = new GroupBox();
- chkOverrideAllowedIPs = new CheckBox();
+ chkUseKeepAlive = new CheckBox();
+ chkUseDns = new CheckBox();
+ chkUseAllowedIPs = new CheckBox();
txtAllowedIPs = new TextBox();
label5 = new Label();
txtDnsServers = new TextBox();
@@ -131,7 +133,9 @@
// groupBox1
//
groupBox1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
- groupBox1.Controls.Add(chkOverrideAllowedIPs);
+ groupBox1.Controls.Add(chkUseKeepAlive);
+ groupBox1.Controls.Add(chkUseDns);
+ groupBox1.Controls.Add(chkUseAllowedIPs);
groupBox1.Controls.Add(txtAllowedIPs);
groupBox1.Controls.Add(label5);
groupBox1.Controls.Add(txtDnsServers);
@@ -157,16 +161,38 @@
groupBox1.TabStop = false;
groupBox1.Text = "peer";
//
- // chkOverrideAllowedIPs
- //
- chkOverrideAllowedIPs.AutoSize = true;
- chkOverrideAllowedIPs.Location = new Point(94, 158);
- chkOverrideAllowedIPs.Name = "chkOverrideAllowedIPs";
- chkOverrideAllowedIPs.Size = new Size(69, 19);
- chkOverrideAllowedIPs.TabIndex = 26;
- chkOverrideAllowedIPs.Text = "override";
- chkOverrideAllowedIPs.UseVisualStyleBackColor = true;
- chkOverrideAllowedIPs.CheckedChanged += Override_CheckedChanged;
+ // chkUseKeepAlive
+ //
+ chkUseKeepAlive.AutoSize = true;
+ chkUseKeepAlive.Location = new Point(94, 211);
+ chkUseKeepAlive.Name = "chkUseKeepAlive";
+ chkUseKeepAlive.Size = new Size(69, 19);
+ chkUseKeepAlive.TabIndex = 30;
+ chkUseKeepAlive.Text = "override";
+ chkUseKeepAlive.UseVisualStyleBackColor = true;
+ chkUseKeepAlive.CheckedChanged += UseKeepAlive_CheckedChanged;
+ //
+ // chkUseDns
+ //
+ chkUseDns.AutoSize = true;
+ chkUseDns.Location = new Point(94, 185);
+ chkUseDns.Name = "chkUseDns";
+ chkUseDns.Size = new Size(69, 19);
+ chkUseDns.TabIndex = 29;
+ chkUseDns.Text = "override";
+ chkUseDns.UseVisualStyleBackColor = true;
+ chkUseDns.CheckedChanged += Dns_CheckedChanged;
+ //
+ // chkUseAllowedIPs
+ //
+ chkUseAllowedIPs.AutoSize = true;
+ chkUseAllowedIPs.Location = new Point(94, 158);
+ chkUseAllowedIPs.Name = "chkUseAllowedIPs";
+ chkUseAllowedIPs.Size = new Size(69, 19);
+ chkUseAllowedIPs.TabIndex = 26;
+ chkUseAllowedIPs.Text = "override";
+ chkUseAllowedIPs.UseVisualStyleBackColor = true;
+ chkUseAllowedIPs.CheckedChanged += AllowedIP_CheckedChanged;
//
// txtAllowedIPs
//
@@ -180,7 +206,7 @@
// label5
//
label5.AutoSize = true;
- label5.Location = new Point(10, 158);
+ label5.Location = new Point(10, 162);
label5.Name = "label5";
label5.Size = new Size(65, 15);
label5.TabIndex = 24;
@@ -188,8 +214,9 @@
//
// txtDnsServers
//
+ txtDnsServers.Enabled = false;
txtDnsServers.Font = new Font("Consolas", 8.25F);
- txtDnsServers.Location = new Point(240, 183);
+ txtDnsServers.Location = new Point(169, 184);
txtDnsServers.Name = "txtDnsServers";
txtDnsServers.Size = new Size(159, 20);
txtDnsServers.TabIndex = 23;
@@ -197,7 +224,7 @@
// label7
//
label7.AutoSize = true;
- label7.Location = new Point(166, 184);
+ label7.Location = new Point(6, 186);
label7.Name = "label7";
label7.Size = new Size(69, 15);
label7.TabIndex = 22;
@@ -235,8 +262,9 @@
//
// txtPersistenKeepAlive
//
+ txtPersistenKeepAlive.Enabled = false;
txtPersistenKeepAlive.Font = new Font("Consolas", 8.25F);
- txtPersistenKeepAlive.Location = new Point(81, 183);
+ txtPersistenKeepAlive.Location = new Point(169, 210);
txtPersistenKeepAlive.Name = "txtPersistenKeepAlive";
txtPersistenKeepAlive.Size = new Size(40, 20);
txtPersistenKeepAlive.TabIndex = 12;
@@ -244,7 +272,7 @@
// label8
//
label8.AutoSize = true;
- label8.Location = new Point(18, 184);
+ label8.Location = new Point(13, 211);
label8.Name = "label8";
label8.Size = new Size(59, 15);
label8.TabIndex = 11;
@@ -416,8 +444,10 @@
private Button button3;
private TextBox txtDnsServers;
private Label label7;
- private CheckBox chkOverrideAllowedIPs;
+ private CheckBox chkUseAllowedIPs;
private TextBox txtAllowedIPs;
private Label label5;
+ private CheckBox chkUseDns;
+ private CheckBox chkUseKeepAlive;
}
}
diff --git a/src/WireGuardConfigGenerator/UserControlPeer.cs b/src/WireGuardConfigGenerator/UserControlPeer.cs
index c4182e6..74c76d8 100644
--- a/src/WireGuardConfigGenerator/UserControlPeer.cs
+++ b/src/WireGuardConfigGenerator/UserControlPeer.cs
@@ -28,28 +28,29 @@ public partial class UserControlPeer : UserControl
if (this.peer == null)
return;
+ var server = peer.ParentServer;
+
+ if (server == null)
+ return;
+
string config = $"""
[Interface]
PrivateKey = {this.peer.PrivateKey}
ListenPort = {this.peer.ListenPort}
Address = {this.peer.Address}
- """ + (string.IsNullOrEmpty(peer.DNS) ? "" : $"{Environment.NewLine}DNS = {peer.DNS}{Environment.NewLine}");
-
- var server = peer.ParentServer;
-
- if (server == null)
- return;
+ """
+ + (this.peer.UseDNS ? (string.IsNullOrEmpty(peer.DNS) ? "" : $"{Environment.NewLine}DNS = {peer.DNS}") : $"{Environment.NewLine}DNS = {server.DNS}{Environment.NewLine}")
+ ;
config += $"""
{Environment.NewLine}
[Peer]
Eindpoint = {server.Endpoint}
PublicKey = {server.PubKey}
- """ + (this.peer.OverrideAllowedIPs ? $"{Environment.NewLine}AllowedIPs = {peer.AllowedIPs}" : $"{Environment.NewLine}AllowedIPs = {server.AllowedIPs}") +
- $"""
- {Environment.NewLine}PersistentKeepalive = {peer.PersistentKeepalive}
-
- """;
+ """
+ + (this.peer.UseAllowedIPs ? $"{Environment.NewLine}AllowedIPs = {peer.AllowedIPs}" : $"{Environment.NewLine}AllowedIPs = {server.AllowedIPs}")
+ + (this.peer.UsePersistentKeepalive ? $"{Environment.NewLine}PersistentKeepalive = {peer.PersistentKeepalive}" : $"{Environment.NewLine}PersistentKeepalive = {server.PersistentKeepalive}")
+ ;
this.txtConf.Text = config;
}
@@ -86,19 +87,30 @@ public partial class UserControlPeer : UserControl
this.lblName.Text = peer.Name;
- if (string.IsNullOrWhiteSpace(peer.AllowedIPs))
- peer.AllowedIPs = $"{peer.ParentServer?.Address}";
-
this.txtPrivateKey.Text = peer.PrivateKey;
this.txtPublicKey.Text = peer.PubKey;
this.txtListenPort.Text = peer.ListenPort.ToString();
this.txtAddress.Text = peer.Address?.Split('/')[0];
this.comboBox1.SelectedItem = peer.Address?.Split('/')[1];
- this.txtPersistenKeepAlive.Text = peer.PersistentKeepalive.ToString();
- this.txtDnsServers.Text = peer.DNS;
- this.chkOverrideAllowedIPs.Checked = this.peer.OverrideAllowedIPs;
- this.txtAllowedIPs.Text = peer.AllowedIPs;
+ if (peer.UsePersistentKeepalive)
+ this.txtPersistenKeepAlive.Text = peer.PersistentKeepalive.ToString();
+ else
+ this.txtPersistenKeepAlive.Text = peer.ParentServer?.PersistentKeepalive.ToString();
+
+ if (peer.UseDNS)
+ this.txtDnsServers.Text = $"{peer.DNS}";
+ else
+ this.txtDnsServers.Text = $"{peer.ParentServer?.DNS}";
+
+ if (peer.UseAllowedIPs)
+ this.txtAllowedIPs.Text = $"{peer.AllowedIPs}";
+ else
+ this.txtAllowedIPs.Text = $"{peer.ParentServer?.Address}";
+
+ this.chkUseAllowedIPs.Checked = this.peer.UseAllowedIPs;
+ this.chkUseDns.Checked = this.peer.UseDNS;
+ this.chkUseKeepAlive.Checked = this.peer.UsePersistentKeepalive;
}
@@ -111,10 +123,19 @@ public partial class UserControlPeer : UserControl
this.peer.PubKey = this.txtPublicKey.Text;
this.peer.ListenPort = int.TryParse(this.txtListenPort.Text, out int port) ? port : 0;
this.peer.Address = $"{this.txtAddress.Text}/{this.comboBox1.SelectedItem}";
- this.peer.PersistentKeepalive = int.TryParse(this.txtPersistenKeepAlive.Text, out int pka) ? pka : 0;
- this.peer.DNS = this.txtDnsServers.Text;
- this.peer.OverrideAllowedIPs = this.chkOverrideAllowedIPs.Checked;
- this.peer.AllowedIPs = this.txtAllowedIPs.Text;
+
+ this.peer.UseAllowedIPs = this.chkUseAllowedIPs.Checked;
+ this.peer.UseDNS = this.chkUseDns.Checked;
+ this.peer.UsePersistentKeepalive = this.chkUseKeepAlive.Checked;
+
+ if(this.peer.UseDNS)
+ this.peer.DNS = this.txtDnsServers.Text;
+
+ if (this.peer.UseAllowedIPs)
+ this.peer.AllowedIPs = this.txtAllowedIPs.Text;
+
+ if (this.peer.UsePersistentKeepalive)
+ this.peer.PersistentKeepalive = int.TryParse(this.txtPersistenKeepAlive.Text, out int pka) ? pka : 0;
this.buttonEdit.Enabled = true;
this.buttonCancel.Enabled = false;
@@ -163,8 +184,18 @@ public partial class UserControlPeer : UserControl
MakeConfig();
}
- private void Override_CheckedChanged(object sender, EventArgs e)
+ private void AllowedIP_CheckedChanged(object sender, EventArgs e)
+ {
+ this.txtAllowedIPs.Enabled = this.chkUseAllowedIPs.Checked;
+ }
+
+ private void Dns_CheckedChanged(object sender, EventArgs e)
+ {
+ this.txtDnsServers.Enabled = this.chkUseDns.Checked;
+ }
+
+ private void UseKeepAlive_CheckedChanged(object sender, EventArgs e)
{
- this.txtAllowedIPs.Enabled = this.chkOverrideAllowedIPs.Checked;
+ this.txtPersistenKeepAlive.Enabled = this.chkUseKeepAlive.Checked;
}
}
diff --git a/src/WireGuardConfigGenerator/UserControlServer.Designer.cs b/src/WireGuardConfigGenerator/UserControlServer.Designer.cs
index 653ceb3..8394ef6 100644
--- a/src/WireGuardConfigGenerator/UserControlServer.Designer.cs
+++ b/src/WireGuardConfigGenerator/UserControlServer.Designer.cs
@@ -44,6 +44,9 @@
txtConf = new TextBox();
tabPage1 = new TabPage();
groupBox1 = new GroupBox();
+ label10 = new Label();
+ txtDns = new TextBox();
+ label9 = new Label();
txtAllowedIPs = new TextBox();
label8 = new Label();
button3 = new Button();
@@ -56,7 +59,8 @@
buttonEdit = new Button();
lblName = new Label();
label7 = new Label();
- label9 = new Label();
+ label11 = new Label();
+ txtPersistentKeepalive = new TextBox();
tabControl1.SuspendLayout();
tabPage0.SuspendLayout();
tabPage1.SuspendLayout();
@@ -136,17 +140,17 @@
txtPostUp.AcceptsReturn = true;
txtPostUp.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
txtPostUp.Font = new Font("Consolas", 8.25F);
- txtPostUp.Location = new Point(73, 198);
+ txtPostUp.Location = new Point(73, 231);
txtPostUp.Multiline = true;
txtPostUp.Name = "txtPostUp";
txtPostUp.ScrollBars = ScrollBars.Both;
- txtPostUp.Size = new Size(429, 129);
+ txtPostUp.Size = new Size(429, 96);
txtPostUp.TabIndex = 9;
//
// label5
//
label5.AutoSize = true;
- label5.Location = new Point(22, 198);
+ label5.Location = new Point(21, 231);
label5.Name = "label5";
label5.Size = new Size(45, 15);
label5.TabIndex = 8;
@@ -220,6 +224,10 @@
// groupBox1
//
groupBox1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
+ groupBox1.Controls.Add(label11);
+ groupBox1.Controls.Add(txtPersistentKeepalive);
+ groupBox1.Controls.Add(label10);
+ groupBox1.Controls.Add(txtDns);
groupBox1.Controls.Add(label9);
groupBox1.Controls.Add(txtAllowedIPs);
groupBox1.Controls.Add(label8);
@@ -246,10 +254,36 @@
groupBox1.TabStop = false;
groupBox1.Text = "server";
//
+ // label10
+ //
+ label10.AutoSize = true;
+ label10.Location = new Point(36, 170);
+ label10.Name = "label10";
+ label10.Size = new Size(30, 15);
+ label10.TabIndex = 25;
+ label10.Text = "DNS";
+ //
+ // txtDns
+ //
+ txtDns.Font = new Font("Consolas", 8.25F);
+ txtDns.Location = new Point(73, 169);
+ txtDns.Name = "txtDns";
+ txtDns.Size = new Size(108, 20);
+ txtDns.TabIndex = 24;
+ //
+ // label9
+ //
+ label9.AutoSize = true;
+ label9.Location = new Point(327, 210);
+ label9.Name = "label9";
+ label9.Size = new Size(153, 15);
+ label9.TabIndex = 23;
+ label9.Text = "(multiple subnets or 0.0.0.0)";
+ //
// txtAllowedIPs
//
txtAllowedIPs.Font = new Font("Consolas", 8.25F);
- txtAllowedIPs.Location = new Point(73, 172);
+ txtAllowedIPs.Location = new Point(73, 195);
txtAllowedIPs.Name = "txtAllowedIPs";
txtAllowedIPs.Size = new Size(248, 20);
txtAllowedIPs.TabIndex = 22;
@@ -257,7 +291,7 @@
// label8
//
label8.AutoSize = true;
- label8.Location = new Point(2, 175);
+ label8.Location = new Point(2, 196);
label8.Name = "label8";
label8.Size = new Size(65, 15);
label8.TabIndex = 21;
@@ -364,14 +398,22 @@
label7.TabIndex = 14;
label7.Text = "Name:";
//
- // label9
+ // label11
//
- label9.AutoSize = true;
- label9.Location = new Point(327, 177);
- label9.Name = "label9";
- label9.Size = new Size(153, 15);
- label9.TabIndex = 23;
- label9.Text = "(multiple subnets or 0.0.0.0)";
+ label11.AutoSize = true;
+ label11.Location = new Point(202, 170);
+ label11.Name = "label11";
+ label11.Size = new Size(108, 15);
+ label11.TabIndex = 27;
+ label11.Text = "PersistentKeepalive";
+ //
+ // txtPersistentKeepalive
+ //
+ txtPersistentKeepalive.Font = new Font("Consolas", 8.25F);
+ txtPersistentKeepalive.Location = new Point(316, 169);
+ txtPersistentKeepalive.Name = "txtPersistentKeepalive";
+ txtPersistentKeepalive.Size = new Size(76, 20);
+ txtPersistentKeepalive.TabIndex = 26;
//
// UserControlServer
//
@@ -421,5 +463,9 @@
private TextBox txtAllowedIPs;
private Label label8;
private Label label9;
+ private Label label10;
+ private TextBox txtDns;
+ private Label label11;
+ private TextBox txtPersistentKeepalive;
}
}
diff --git a/src/WireGuardConfigGenerator/UserControlServer.cs b/src/WireGuardConfigGenerator/UserControlServer.cs
index a492977..761420d 100644
--- a/src/WireGuardConfigGenerator/UserControlServer.cs
+++ b/src/WireGuardConfigGenerator/UserControlServer.cs
@@ -29,6 +29,8 @@ public partial class UserControlServer : UserControl
this.comboBox1.SelectedItem = server.Address?.Split('/')[1];
this.txtPostUp.Text = server.PostUp;
this.txtAllowedIPs.Text = server.AllowedIPs;
+ this.txtDns.Text = server.DNS;
+ this.txtPersistentKeepalive.Text = server.PersistentKeepalive.ToString();
}
private void Edit_Click(object sender, EventArgs e)
@@ -63,6 +65,8 @@ public partial class UserControlServer : UserControl
this.server.PostUp = this.txtPostUp.Text;
this.server.PubKey = this.txtPublicKey.Text;
this.server.AllowedIPs = this.txtAllowedIPs.Text;
+ this.server.DNS = this.txtDns.Text;
+ this.server.PersistentKeepalive = int.TryParse(this.txtPersistentKeepalive.Text, out int PersistentKeepalive) ? PersistentKeepalive : 0;
this.buttonEdit.Enabled = true;
this.buttonCancel.Enabled = false;