Skip to content

Commit

Permalink
Add saving LastConnectionName.
Browse files Browse the repository at this point in the history
  • Loading branch information
rinrab committed Jan 1, 2024
1 parent 72024fa commit 812c3c1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
34 changes: 34 additions & 0 deletions AOVpnManager.Tests/StateManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Win32;

namespace AOVpnManager.Tests
{
[TestClass]
public class StateManagerTests
{
[TestMethod]
public void LastConnctionName()
{
IStateManager stateManager = new StateManager(Registry.CurrentUser, @"Test\AOVpnManager");

try
{
stateManager.UpdateLastConnectionName("Contosa Vpn");
Assert.AreEqual("Contosa Vpn", stateManager.ReadLastConnectionName());

stateManager.UpdateLastConnectionName("Contosa Always On Vpn");
Assert.AreEqual("Contosa Always On Vpn", stateManager.ReadLastConnectionName());

stateManager.UpdateLastConnectionName(null);
Assert.AreEqual(null, stateManager.ReadLastConnectionName());

stateManager.UpdateLastConnectionName("Contosa Vpn v2");
Assert.AreEqual("Contosa Vpn v2", stateManager.ReadLastConnectionName());
}
finally
{
stateManager.Clean();
}
}
}
}
9 changes: 9 additions & 0 deletions AOVpnManager/IStateManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AOVpnManager
{
public interface IStateManager
{
string ReadLastConnectionName();
void UpdateLastConnectionName(string connectionName);
void Clean();
}
}
3 changes: 3 additions & 0 deletions AOVpnManager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ static int Main(string[] args)
}

IGroupPolicyProvider policyProvider = new GroupPolicyProvider(Registry.LocalMachine, @"SOFTWARE\Policies\AOVpnManager");
IStateManager stateManager = new StateManager(Registry.LocalMachine, @"SOFTWARE\AOVpnManager");

logger.Started();
int exitCode = 0;

try
{
GroupPolicySettings settings = policyProvider.ReadSettings();
string lastConnectionName = stateManager.ReadLastConnectionName();

if (string.IsNullOrEmpty(settings.Profile) || string.IsNullOrEmpty(settings.ConnectionName))
{
Expand All @@ -37,6 +39,7 @@ static int Main(string[] args)
{
using (VpnManager vpnManager = new VpnManager())
{
stateManager.UpdateLastConnectionName(settings.ConnectionName);
using (CimInstance oldInstance = vpnManager.GetVpnConnection(settings.ConnectionName))
{
logger.Trace("oldInstance: " + oldInstance?.ToString());
Expand Down
51 changes: 51 additions & 0 deletions AOVpnManager/StateManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.Win32;

namespace AOVpnManager
{
public class StateManager : IStateManager
{
private const string LastConnectionName = nameof(LastConnectionName);

private readonly RegistryKey root;
private readonly string path;

public StateManager(RegistryKey root, string path)
{
this.root = root;
this.path = path;
}

public void Clean()
{
root.DeleteSubKey(path, false);
}

public string ReadLastConnectionName()
{
using (RegistryKey key = root.OpenSubKey(path))
{
return (string)key?.GetValue(LastConnectionName);
}
}

public void UpdateLastConnectionName(string connectionName)
{
using (RegistryKey key = OpenOrCreateKey())
{
if (connectionName == null)
{
key.DeleteValue(LastConnectionName);
}
else
{
key.SetValue(LastConnectionName, connectionName);
}
}
}

private RegistryKey OpenOrCreateKey()
{
return root.OpenSubKey(path, true) ?? root.CreateSubKey(path, true);
}
}
}

0 comments on commit 812c3c1

Please sign in to comment.