Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Implement Auto-Generation of Private Keys and Fix Syntax Highlighting…
Browse files Browse the repository at this point in the history
… (Ref: #36)

This commit introduces a significant enhancement and a fix, as discussed in wiresock#36. The main changes are:

1. **Auto-Generation of Private Keys**: Implemented functionality in `ApplySyntaxHighlighting` to automatically generate a new private key when the 'privatekey' field is found empty. This ensures that users are provided with a valid private key without manual intervention, enhancing the usability for configuring tunnels.

2. **Syntax Highlighting Fix**: Modified the `ApplySyntaxHighlighting` method to return a boolean indicating whether the text content was altered (specifically when a new private key is generated and inserted). If the text is changed, `ApplySyntaxHighlighting` is re-invoked to ensure that the syntax highlighting is correctly applied to the newly inserted key. This fix addresses the issue where syntax highlighting was not correctly applied after the insertion of a new private key.

Together, these changes improve the functionality and user experience of WireSockUI by automating key generation and ensuring consistent syntax highlighting.
  • Loading branch information
wiresock committed Nov 18, 2023
1 parent 1f25ed6 commit 37a8cc4
Showing 1 changed file with 56 additions and 12 deletions.
68 changes: 56 additions & 12 deletions WireSockUI/Forms/frmEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ public FrmEdit()
Text = Resources.EditProfileTitleNew;
txtEditor.Text = Resources.template_conf;

ApplySyntaxHighlighting();
var textChanged = ApplySyntaxHighlighting();
if (textChanged)
{
// Call it again to reapply highlighting
ApplySyntaxHighlighting();
}
}

public FrmEdit(string config)
Expand All @@ -39,17 +44,23 @@ public FrmEdit(string config)
txtProfileName.Text = config.ToLowerInvariant();
txtEditor.Text = File.ReadAllText(Path.Combine(Global.ConfigsFolder, config + ".conf"));

ApplySyntaxHighlighting();
var textChanged = ApplySyntaxHighlighting();
if (textChanged)
{
// Call it again to reapply highlighting
ApplySyntaxHighlighting();
}
}

public string ReturnValue { get; private set; }

private void ApplySyntaxHighlighting()
private bool ApplySyntaxHighlighting()
{
if (_highlighting) return;
if (_highlighting) return false;
_highlighting = true;

var hasErrors = false;
var textChanged = false;

// Saving the original settings
var originalIndex = txtEditor.SelectionStart;
Expand Down Expand Up @@ -129,23 +140,50 @@ private void ApplySyntaxHighlighting()
{
// base64 256-bit keys
case "privatekey":
{
if (string.IsNullOrEmpty(value))
{
// Generate a new private key
var newPrivateKey = Curve25519.CreateRandomPrivateKey();
var base64PrivateKey = Convert.ToBase64String(newPrivateKey);

// Insert the new private key into the text editor
txtEditor.SelectionStart = m.Groups["value"].Index;
txtEditor.SelectionLength = m.Groups["value"].Length;
txtEditor.SelectedText = base64PrivateKey;

// Update the public key display
txtPublicKey.Text = Convert.ToBase64String(Curve25519.GetPublicKey(newPrivateKey));
textChanged = true; // Set flag to true as text is changed
}
else
{
try
{
var binaryKey = Convert.FromBase64String(value);
if (binaryKey.Length != 32)
throw new FormatException();

txtPublicKey.Text = Convert.ToBase64String(Curve25519.GetPublicKey(binaryKey));
}
catch (FormatException)
{
txtEditor.UnderlineSelection();
hasErrors = true;
}
}
}
break;
case "publickey":
case "presharedkey":
{
if (!string.IsNullOrEmpty(value))
try
{
if (key == "privatekey")
txtPublicKey.Text = string.Empty;

var binaryKey = Convert.FromBase64String(value);

if (binaryKey.Length != 32)
throw new FormatException();

// Convert Peer.PrivateKey into PublicKey for display
if (key == "privatekey")
txtPublicKey.Text = Convert.ToBase64String(Curve25519.GetPublicKey(binaryKey));
}
catch (FormatException)
{
Expand Down Expand Up @@ -255,6 +293,7 @@ private void ApplySyntaxHighlighting()
btnSave.Enabled = !hasErrors;

_highlighting = false;
return textChanged;
}

private void Initialize()
Expand Down Expand Up @@ -316,7 +355,12 @@ private void OnProcessClick(object sender, EventArgs e)

private void OnProfileChanged(object sender, EventArgs e)
{
ApplySyntaxHighlighting();
var textChanged = ApplySyntaxHighlighting();
if (textChanged)
{
// Call it again to reapply highlighting
ApplySyntaxHighlighting();
}
}
}
}

0 comments on commit 37a8cc4

Please sign in to comment.