Skip to content

Commit

Permalink
Fixed "New Profile", refactored background UI updates and cleaned up …
Browse files Browse the repository at this point in the history
…code
  • Loading branch information
robvanoostenrijk committed Apr 9, 2023
1 parent dfa3a71 commit 3d39420
Show file tree
Hide file tree
Showing 21 changed files with 386 additions and 396 deletions.
29 changes: 13 additions & 16 deletions WireSockUI/Config/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ internal static void ValidateAddresses(string section, string key, string keyVal
public String PrivateKey
{
get { return _privateKey; }
set {
set
{
ValidateKey("Interface", "PrivateKey", value);
_privateKey = value;
}
Expand All @@ -89,7 +90,7 @@ public String PublicKey
if (!String.IsNullOrEmpty(_privateKey))
{
// Determine public key from private key data
return
return
Convert.ToBase64String(
Curve25519.GetPublicKey(
Convert.FromBase64String(this.PrivateKey)));
Expand Down Expand Up @@ -144,10 +145,7 @@ public String MTU
{
if (!String.IsNullOrWhiteSpace(value))
{

int mtu;

if (!int.TryParse(value, out mtu))
if (!int.TryParse(value, out int mtu))
throw new FormatException("\"MTU\" in \"Interface\", is not a numerical value.");

if (mtu < 576 || mtu > 65535)
Expand Down Expand Up @@ -180,7 +178,7 @@ public String PeerKey
/// </summary>
public String PresharedKey
{
get { return _presharedKey; }
get { return _presharedKey; }
set
{
ValidateKey("Peer", "PresharedKey", value);
Expand Down Expand Up @@ -210,11 +208,12 @@ public String AllowedIPs
public String Endpoint
{
get { return _endpoint; }
set {
set
{
if (!Native.IPHelper.IsValidAddress(value))
throw new FormatException("\"Endpoint\" in \"Peer\", is not a valid IPv4, IPv6 or domain address.");

_endpoint = value;
_endpoint = value;
}
}

Expand All @@ -232,10 +231,7 @@ public String PersistentKeepAlive
{
if (!String.IsNullOrWhiteSpace(value))
{

int mtu;

if (!int.TryParse(value, out mtu))
if (!int.TryParse(value, out int mtu))
throw new FormatException("\"PersistentKeepalive\" in \"Peer\", is not a numerical value.");

if (mtu < 0 || mtu > 65535)
Expand Down Expand Up @@ -316,7 +312,8 @@ public String Socks5Proxy
throw new FormatException("\"Endpoint\" in \"Peer\", is not a valid IPv4, IPv6 or domain address.");

_socks5Proxy = value;
} else
}
else
{
_socks5Proxy = null;
}
Expand Down Expand Up @@ -391,8 +388,8 @@ public static Profile LoadProfile(string profileName)
/// <summary>
/// Create an empty profile from scratch
/// </summary>
public Profile()
{
public Profile()
{
}

/// <summary>
Expand Down
11 changes: 4 additions & 7 deletions WireSockUI/Extensions/ControlTextExtender.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using System.ComponentModel;
using System.Resources;
using System.Windows.Forms;

namespace WireSockUI.Extensions
{
[ProvideProperty("ResourceKey", typeof(Control))]
public class ControlTextExtender: Component, System.ComponentModel.IExtenderProvider, ISupportInitialize
public class ControlTextExtender : Component, System.ComponentModel.IExtenderProvider, ISupportInitialize
{
private Dictionary<Control, string> _items;
private readonly Dictionary<Control, string> _items;

public ControlTextExtender() : base()
{
Expand All @@ -26,9 +25,7 @@ public bool CanExtend(object extendee)

public string GetResourceKey(Control item)
{
string value;

if (_items.TryGetValue(item, out value))
if (_items.TryGetValue(item, out string value))
return value;

return null;
Expand Down
6 changes: 2 additions & 4 deletions WireSockUI/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ namespace WireSockUI.Extensions
{
public static class DictionaryExtensions
{
public static TVal Get<TKey, TVal>(this Dictionary<TKey, TVal> dictionary, TKey key, TVal defaultVal = default(TVal))
public static TVal Get<TKey, TVal>(this Dictionary<TKey, TVal> dictionary, TKey key, TVal defaultVal = default)
{
TVal val;

if (dictionary.TryGetValue(key, out val))
if (dictionary.TryGetValue(key, out TVal val))
return val;

return defaultVal;
Expand Down
9 changes: 4 additions & 5 deletions WireSockUI/Extensions/MenuTextExtender.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using System.ComponentModel;
using System.Resources;
using System.Windows.Forms;

namespace WireSockUI.Extensions
{
[ProvideProperty("ResourceKey", typeof(ToolStripItem))]
internal class MenuTextExtender: Component, System.ComponentModel.IExtenderProvider, ISupportInitialize
internal class MenuTextExtender : Component, System.ComponentModel.IExtenderProvider, ISupportInitialize
{
private Dictionary<ToolStripItem, string> _items;

public MenuTextExtender() : base()
public MenuTextExtender() : base()
{
_items = new Dictionary<ToolStripItem, string>();
}
Expand Down Expand Up @@ -58,6 +57,6 @@ public void EndInit()
}
}


}
}
4 changes: 0 additions & 4 deletions WireSockUI/Extensions/TimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WireSockUI.Properties;

namespace WireSockUI.Extensions
Expand Down
6 changes: 3 additions & 3 deletions WireSockUI/Forms/EditForm.Designer.cs

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

61 changes: 32 additions & 29 deletions WireSockUI/Forms/EditForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ namespace WireSockUI.Forms
{
public partial class frmEdit : Form
{
private static Regex profileMatch = new Regex(@"^\s*((?<comment>[;#].*)|(?<section>\[\w+\])|((?<key>[a-zA-Z0-9]+)[ \t]*=[ \t]*(?<value>.*?)))$", RegexOptions.Multiline | RegexOptions.Compiled);
private static Regex multiValueMatch = new Regex(@"[^, ]*", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly Regex _profileMatch = new Regex(@"^\s*((?<comment>[;#].*)|(?<section>\[\w+\])|((?<key>[a-zA-Z0-9]+)[ \t]*=[ \t]*(?<value>.*?)))$", RegexOptions.Multiline | RegexOptions.Compiled);
private static readonly Regex _multiValueMatch = new Regex(@"[^, ]*", RegexOptions.IgnoreCase | RegexOptions.Compiled);

public String ReturnValue
{
get; private set;
}

private void applySyntaxHighlighting()
private void ApplySyntaxHighlighting()
{
bool hasErrors = false;

Expand All @@ -36,7 +36,7 @@ private void applySyntaxHighlighting()
txtEditor.SelectionColor = originalColor;
txtEditor.SelectionFont = new Font(txtEditor.SelectionFont, FontStyle.Regular);

foreach (Match m in profileMatch.Matches(txtEditor.Text))
foreach (Match m in _profileMatch.Matches(txtEditor.Text))
{
if (m.Groups["comment"].Success)
{
Expand Down Expand Up @@ -102,7 +102,7 @@ private void applySyntaxHighlighting()
// base64 256-bit keys
case "privatekey":
case "publickey":
case "presharedkey":
case "presharedkey":
{
if (!String.IsNullOrEmpty(value))
{
Expand Down Expand Up @@ -133,7 +133,7 @@ private void applySyntaxHighlighting()
case "allowedips":
case "disallowedips":
{
foreach (Match e in multiValueMatch.Matches(value))
foreach (Match e in _multiValueMatch.Matches(value))
{
if (!String.IsNullOrWhiteSpace(e.Value) && !IPHelper.IsValidIPNetwork(e.Value))
{
Expand All @@ -149,7 +149,7 @@ private void applySyntaxHighlighting()
// IPv4/IPv6 values
case "dns":
{
foreach (Match e in multiValueMatch.Matches(value))
foreach (Match e in _multiValueMatch.Matches(value))
{
if (!String.IsNullOrWhiteSpace(e.Value) && !IPHelper.IsValidIPAddress(e.Value))
{
Expand Down Expand Up @@ -194,7 +194,7 @@ private void applySyntaxHighlighting()
case "allowedapps":
case "disallowedapps":
{
foreach (Match e in multiValueMatch.Matches(value))
foreach (Match e in _multiValueMatch.Matches(value))
{
if (!String.IsNullOrWhiteSpace(e.Value) && !Regex.IsMatch(e.Value, @"^[a-z0-9_-]+$", RegexOptions.IgnoreCase))
{
Expand Down Expand Up @@ -232,34 +232,37 @@ private void applySyntaxHighlighting()
btnSave.Enabled = !hasErrors;
}

public frmEdit()
private void Initialize()
{
InitializeComponent();

this.Icon = Resources.ico;

txtProfileName.SetCueBanner(Resources.EditProfileCue);
}

public frmEdit(string config): this()
public frmEdit()
{
if (String.IsNullOrEmpty(config))
{
Text = Resources.EditProfileTitleNew;
txtEditor.Text = Resources.template_conf;
}
else
{
Text = String.Format(Resources.EditProfileTitle, config);
Initialize();

txtProfileName.Text = config.ToLowerInvariant();
txtEditor.Text = File.ReadAllText(Path.Combine(Global.ConfigsFolder, config + ".conf"));
}
this.Text = Resources.EditProfileTitleNew;
txtEditor.Text = Resources.template_conf;

applySyntaxHighlighting();
ApplySyntaxHighlighting();
}

private void SaveProfile(object sender, EventArgs e)
public frmEdit(string config)
{
Initialize();

Text = String.Format(Resources.EditProfileTitle, config);

txtProfileName.Text = config.ToLowerInvariant();
txtEditor.Text = File.ReadAllText(Path.Combine(Global.ConfigsFolder, config + ".conf"));

ApplySyntaxHighlighting();
}

private void OnSaveClick(object sender, EventArgs e)
{
String tmpProfile = Path.GetTempFileName();
File.WriteAllText(tmpProfile, txtEditor.Text);
Expand Down Expand Up @@ -292,11 +295,11 @@ private void SaveProfile(object sender, EventArgs e)
File.Move(tmpProfile, profilePath);

this.ReturnValue = txtProfileName.Text;

Close();
}
private void FindProcess(object sender, EventArgs e)

private void OnProcessClick(object sender, EventArgs e)
{
using (TaskManager taskManager = new TaskManager())
{
Expand All @@ -308,9 +311,9 @@ private void FindProcess(object sender, EventArgs e)
}
}

private void ProfileChanged(object sender, EventArgs e)
private void OnProfileChanged(object sender, EventArgs e)
{
applySyntaxHighlighting();
ApplySyntaxHighlighting();
}
}
}
Loading

0 comments on commit 3d39420

Please sign in to comment.