Client can override alloweip form server
287c75fbbc2b8943c050fde53db7d72079e8b42a
5 files changed
WireGuardConfigGenerator/DataModel/Root.csWireGuardConfigGenerator/Helpers/CryptoUtils.csWireGuardConfigGenerator/UserControlPeer.Designer.csWireGuardConfigGenerator/UserControlPeer.csWireGuardConfigGenerator/UserControlServer.Designer.cs
diff --git a/WireGuardConfigGenerator/DataModel/Root.cs b/WireGuardConfigGenerator/DataModel/Root.cs
index c5291b5..cf38336 100644
--- a/WireGuardConfigGenerator/DataModel/Root.cs
+++ b/WireGuardConfigGenerator/DataModel/Root.cs
@@ -1,5 +1,4 @@
-using System.Text.Json;
-using System.Text.Json.Serialization;
+using System.Text.Json.Serialization;
using WireGuardConfigGenerator.Helpers;
namespace WireGuardConfigGenerator.DataModel;
@@ -7,16 +6,10 @@ namespace WireGuardConfigGenerator.DataModel;
public class Root
{
- private static readonly JsonSerializerOptions options = new()
- {
- WriteIndented = true,
- DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
- };
-
public List<Group> Groups { get; set; } = [];
public async Task SaveAsync(string path, string password) =>
- await CryptoUtils.CompressAndEncryptToFileAsync(this, password, path);
+ await CryptoUtils.CompressAndEncryptToFileAsync(this, path, password);
public async Task LoadAsync(string path, string password)
{
@@ -65,14 +58,15 @@ public class Server : WireGuardItem
public bool UsePreDown { get; set; }
public bool UsePostDown { get; set; }
public string? Endpoint { get; set; }
+ public List<Peer> Peers { get; set; } = [];
[JsonIgnore]
public Group? ParentGroup { get; set; }
- public List<Peer> Peers { get; set; } = [];
}
public class Peer : WireGuardItem
{
+ public bool OverrideAllowedIPs { get; set; }
public int PersistentMaxConnections { get; set; }
public int PersistentMinConnections { get; set; }
public string? PersistentConnectionInterval { get; set; }
diff --git a/WireGuardConfigGenerator/Helpers/CryptoUtils.cs b/WireGuardConfigGenerator/Helpers/CryptoUtils.cs
index fad0be2..ee1e784 100644
--- a/WireGuardConfigGenerator/Helpers/CryptoUtils.cs
+++ b/WireGuardConfigGenerator/Helpers/CryptoUtils.cs
@@ -12,7 +12,7 @@ public static class CryptoUtils
private const int IVSize = 16; // 128 bits AES IV
private const int Iterations = 100000; // PBKDF2 iterations
- public static async Task<bool> CompressAndEncryptToFileAsync<T>(T model, string password, string filePath, JsonSerializerOptions? options = null, CancellationToken ct = default)
+ public static async Task<bool> CompressAndEncryptToFileAsync<T>(T model, string filePath, string password, JsonSerializerOptions? options = null, CancellationToken ct = default)
{
options ??= new JsonSerializerOptions
{
diff --git a/WireGuardConfigGenerator/UserControlPeer.Designer.cs b/WireGuardConfigGenerator/UserControlPeer.Designer.cs
index bf536b2..8be5cce 100644
--- a/WireGuardConfigGenerator/UserControlPeer.Designer.cs
+++ b/WireGuardConfigGenerator/UserControlPeer.Designer.cs
@@ -34,6 +34,9 @@
txtConf = new TextBox();
tabPage1 = new TabPage();
groupBox1 = new GroupBox();
+ chkOverrideAllowedIPs = new CheckBox();
+ txtAllowedIPs = new TextBox();
+ label5 = new Label();
txtDnsServers = new TextBox();
label7 = new Label();
button3 = new Button();
@@ -128,6 +131,9 @@
// groupBox1
//
groupBox1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
+ groupBox1.Controls.Add(chkOverrideAllowedIPs);
+ groupBox1.Controls.Add(txtAllowedIPs);
+ groupBox1.Controls.Add(label5);
groupBox1.Controls.Add(txtDnsServers);
groupBox1.Controls.Add(label7);
groupBox1.Controls.Add(button3);
@@ -151,10 +157,39 @@
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;
+ //
+ // txtAllowedIPs
+ //
+ txtAllowedIPs.Enabled = false;
+ txtAllowedIPs.Font = new Font("Consolas", 8.25F);
+ txtAllowedIPs.Location = new Point(169, 158);
+ txtAllowedIPs.Name = "txtAllowedIPs";
+ txtAllowedIPs.Size = new Size(230, 20);
+ txtAllowedIPs.TabIndex = 25;
+ //
+ // label5
+ //
+ label5.AutoSize = true;
+ label5.Location = new Point(10, 158);
+ label5.Name = "label5";
+ label5.Size = new Size(65, 15);
+ label5.TabIndex = 24;
+ label5.Text = "AllowedIPs";
+ //
// txtDnsServers
//
txtDnsServers.Font = new Font("Consolas", 8.25F);
- txtDnsServers.Location = new Point(240, 160);
+ txtDnsServers.Location = new Point(240, 183);
txtDnsServers.Name = "txtDnsServers";
txtDnsServers.Size = new Size(159, 20);
txtDnsServers.TabIndex = 23;
@@ -162,7 +197,7 @@
// label7
//
label7.AutoSize = true;
- label7.Location = new Point(145, 162);
+ label7.Location = new Point(166, 184);
label7.Name = "label7";
label7.Size = new Size(69, 15);
label7.TabIndex = 22;
@@ -201,7 +236,7 @@
// txtPersistenKeepAlive
//
txtPersistenKeepAlive.Font = new Font("Consolas", 8.25F);
- txtPersistenKeepAlive.Location = new Point(81, 160);
+ txtPersistenKeepAlive.Location = new Point(81, 183);
txtPersistenKeepAlive.Name = "txtPersistenKeepAlive";
txtPersistenKeepAlive.Size = new Size(40, 20);
txtPersistenKeepAlive.TabIndex = 12;
@@ -209,7 +244,7 @@
// label8
//
label8.AutoSize = true;
- label8.Location = new Point(10, 163);
+ label8.Location = new Point(18, 184);
label8.Name = "label8";
label8.Size = new Size(59, 15);
label8.TabIndex = 11;
@@ -226,7 +261,7 @@
// label1
//
label1.AutoSize = true;
- label1.Location = new Point(15, 134);
+ label1.Location = new Point(25, 134);
label1.Name = "label1";
label1.Size = new Size(49, 15);
label1.TabIndex = 6;
@@ -260,7 +295,7 @@
// label2
//
label2.AutoSize = true;
- label2.Location = new Point(15, 77);
+ label2.Location = new Point(13, 75);
label2.Name = "label2";
label2.Size = new Size(62, 15);
label2.TabIndex = 0;
@@ -277,7 +312,7 @@
// label6
//
label6.AutoSize = true;
- label6.Location = new Point(15, 106);
+ label6.Location = new Point(16, 104);
label6.Name = "label6";
label6.Size = new Size(59, 15);
label6.TabIndex = 2;
@@ -287,7 +322,7 @@
//
buttonSave.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonSave.Enabled = false;
- buttonSave.Location = new Point(381, 307);
+ buttonSave.Location = new Point(247, 307);
buttonSave.Name = "buttonSave";
buttonSave.Size = new Size(75, 23);
buttonSave.TabIndex = 22;
@@ -299,7 +334,7 @@
//
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Enabled = false;
- buttonCancel.Location = new Point(248, 307);
+ buttonCancel.Location = new Point(378, 307);
buttonCancel.Name = "buttonCancel";
buttonCancel.Size = new Size(75, 23);
buttonCancel.TabIndex = 21;
@@ -381,5 +416,8 @@
private Button button3;
private TextBox txtDnsServers;
private Label label7;
+ private CheckBox chkOverrideAllowedIPs;
+ private TextBox txtAllowedIPs;
+ private Label label5;
}
}
diff --git a/WireGuardConfigGenerator/UserControlPeer.cs b/WireGuardConfigGenerator/UserControlPeer.cs
index 6965b95..c4182e6 100644
--- a/WireGuardConfigGenerator/UserControlPeer.cs
+++ b/WireGuardConfigGenerator/UserControlPeer.cs
@@ -43,10 +43,12 @@ public partial class UserControlPeer : UserControl
config += $"""
{Environment.NewLine}
[Peer]
- Eindpoint ={server.Endpoint}
+ Eindpoint = {server.Endpoint}
PublicKey = {server.PubKey}
- AllowedIPs = {server.AllowedIPs}
- PersistentKeepalive = {peer.PersistentKeepalive}
+ """ + (this.peer.OverrideAllowedIPs ? $"{Environment.NewLine}AllowedIPs = {peer.AllowedIPs}" : $"{Environment.NewLine}AllowedIPs = {server.AllowedIPs}") +
+ $"""
+ {Environment.NewLine}PersistentKeepalive = {peer.PersistentKeepalive}
+
""";
this.txtConf.Text = config;
@@ -94,6 +96,9 @@ public partial class UserControlPeer : UserControl
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;
}
@@ -108,6 +113,8 @@ public partial class UserControlPeer : UserControl
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.buttonEdit.Enabled = true;
this.buttonCancel.Enabled = false;
@@ -155,4 +162,9 @@ public partial class UserControlPeer : UserControl
this.txtPublicKey.Text = peer.PubKey;
MakeConfig();
}
+
+ private void Override_CheckedChanged(object sender, EventArgs e)
+ {
+ this.txtAllowedIPs.Enabled = this.chkOverrideAllowedIPs.Checked;
+ }
}
diff --git a/WireGuardConfigGenerator/UserControlServer.Designer.cs b/WireGuardConfigGenerator/UserControlServer.Designer.cs
index 156ba57..653ceb3 100644
--- a/WireGuardConfigGenerator/UserControlServer.Designer.cs
+++ b/WireGuardConfigGenerator/UserControlServer.Designer.cs
@@ -44,6 +44,8 @@
txtConf = new TextBox();
tabPage1 = new TabPage();
groupBox1 = new GroupBox();
+ txtAllowedIPs = new TextBox();
+ label8 = new Label();
button3 = new Button();
button2 = new Button();
comboBox1 = new ComboBox();
@@ -54,8 +56,7 @@
buttonEdit = new Button();
lblName = new Label();
label7 = new Label();
- txtAllowedIPs = new TextBox();
- label8 = new Label();
+ label9 = new Label();
tabControl1.SuspendLayout();
tabPage0.SuspendLayout();
tabPage1.SuspendLayout();
@@ -219,6 +220,7 @@
// groupBox1
//
groupBox1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
+ groupBox1.Controls.Add(label9);
groupBox1.Controls.Add(txtAllowedIPs);
groupBox1.Controls.Add(label8);
groupBox1.Controls.Add(button3);
@@ -244,6 +246,23 @@
groupBox1.TabStop = false;
groupBox1.Text = "server";
//
+ // txtAllowedIPs
+ //
+ txtAllowedIPs.Font = new Font("Consolas", 8.25F);
+ txtAllowedIPs.Location = new Point(73, 172);
+ txtAllowedIPs.Name = "txtAllowedIPs";
+ txtAllowedIPs.Size = new Size(248, 20);
+ txtAllowedIPs.TabIndex = 22;
+ //
+ // label8
+ //
+ label8.AutoSize = true;
+ label8.Location = new Point(2, 175);
+ label8.Name = "label8";
+ label8.Size = new Size(65, 15);
+ label8.TabIndex = 21;
+ label8.Text = "AllowedIPs";
+ //
// button3
//
button3.Location = new Point(256, 59);
@@ -345,22 +364,14 @@
label7.TabIndex = 14;
label7.Text = "Name:";
//
- // txtAllowedIPs
- //
- txtAllowedIPs.Font = new Font("Consolas", 8.25F);
- txtAllowedIPs.Location = new Point(73, 172);
- txtAllowedIPs.Name = "txtAllowedIPs";
- txtAllowedIPs.Size = new Size(318, 20);
- txtAllowedIPs.TabIndex = 22;
+ // label9
//
- // label8
- //
- label8.AutoSize = true;
- label8.Location = new Point(2, 175);
- label8.Name = "label8";
- label8.Size = new Size(65, 15);
- label8.TabIndex = 21;
- label8.Text = "AllowedIPs";
+ 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)";
//
// UserControlServer
//
@@ -409,5 +420,6 @@
private Button button3;
private TextBox txtAllowedIPs;
private Label label8;
+ private Label label9;
}
}