Skip to content

Commit

Permalink
Rework GpUpdateProcessor code
Browse files Browse the repository at this point in the history
  • Loading branch information
rinrab committed Jan 6, 2024
1 parent 1233e11 commit 73d3f89
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 66 deletions.
4 changes: 0 additions & 4 deletions AOVpnManager.Tests/GpUpdateProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public void Test()

// Create Vpn Connection
{
vpnManager.Expect(x => x.EnumarateVpnConnections()).Return(new VpnConnectionInfo[] { });
vpnManager.Expect(x => x.CreateVpnConnection("Name 1", "Profile 1"));
policyProvider.Expect(x => x.ReadSettings()).Return(new GroupPolicySettings("Profile 1", "Name 1"));
logger.Expect(x => x.Trace(null)).Repeat.Any().IgnoreArguments();
Expand All @@ -38,7 +37,6 @@ public void Test()
{
mocks.BackToRecordAll();

vpnManager.Expect(x => x.EnumarateVpnConnections()).Return(new VpnConnectionInfo[] { new VpnConnectionInfo("Name 1", "Profile 1") });
vpnManager.Expect(x => x.DeleteVpnConnection("Name 1"));
vpnManager.Expect(x => x.CreateVpnConnection("Name 1", "Profile 2"));
policyProvider.Expect(x => x.ReadSettings()).Return(new GroupPolicySettings("Profile 2", "Name 1"));
Expand All @@ -58,7 +56,6 @@ public void Test()
{
mocks.BackToRecordAll();

vpnManager.Expect(x => x.EnumarateVpnConnections()).Return(new VpnConnectionInfo[] { new VpnConnectionInfo("Name 1", "Profile 2") });
vpnManager.Expect(x => x.DeleteVpnConnection("Name 1"));
vpnManager.Expect(x => x.CreateVpnConnection("Name 2", "Profile 2"));
policyProvider.Expect(x => x.ReadSettings()).Return(new GroupPolicySettings("Profile 2", "Name 2"));
Expand All @@ -78,7 +75,6 @@ public void Test()
{
mocks.BackToRecordAll();

vpnManager.Expect(x => x.EnumarateVpnConnections()).Return(new VpnConnectionInfo[] { new VpnConnectionInfo("Name 2", "Profile 2") });
policyProvider.Expect(x => x.ReadSettings()).Return(new GroupPolicySettings("Profile 2", "Name 2"));
logger.Expect(x => x.Trace(null)).Repeat.Any().IgnoreArguments();

Expand Down
63 changes: 17 additions & 46 deletions AOVpnManager/GpUpdateProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,25 @@ public void Run()
{
GroupPolicySettings settings = policyProvider.ReadSettings();
string lastConnectionName = stateManager.GetLastConnectionName();
string oldVpnProfileHash = stateManager.GetLastVpnProfile();
string newVpnProfileHash = ComputeHash(settings.VpnProfileXml);

if (string.IsNullOrEmpty(settings.VpnProfileXml))
{
if (lastConnectionName != null)
{
try
{
vpnManager.DeleteVpnConnection(lastConnectionName);
logger.VpnConnectionDeleted(lastConnectionName);
}
catch (VpnConnectionNotFoundException)
{
}
bool settingsChanged = false;

stateManager.SetLastConnectionName(null);
}
if (lastConnectionName != settings.VpnConnectionName)
{
settingsChanged = true;
}
else
else if (oldVpnProfileHash != newVpnProfileHash)
{
string lastVpnProfile = stateManager.GetLastVpnProfile();
string currentVpnProfile = ComputeHash(settings.VpnProfileXml);
settingsChanged = true;
}

if (lastConnectionName != null && lastConnectionName != settings.VpnConnectionName)
if (settingsChanged)
{
if (lastConnectionName != null)
{
// Remove
try
{
vpnManager.DeleteVpnConnection(lastConnectionName);
Expand All @@ -56,28 +51,16 @@ public void Run()
}

stateManager.SetLastConnectionName(null);
stateManager.SetLastVpnProfile(null);
}

VpnConnectionInfo oldConnection = FindVpnConnection(settings.VpnConnectionName);

logger.Trace(string.Format("oldConnection: {0}", oldConnection));

stateManager.SetLastVpnProfile(currentVpnProfile);

if (oldConnection == null)
if (settings.VpnProfileXml != null)
{
// Create
stateManager.SetLastConnectionName(settings.VpnConnectionName);
vpnManager.CreateVpnConnection(settings.VpnConnectionName, settings.VpnProfileXml);
logger.VpnConnectionCreated(settings.VpnConnectionName);
}
else if (lastVpnProfile != currentVpnProfile)
{
stateManager.SetLastConnectionName(null);
vpnManager.DeleteVpnConnection(lastConnectionName);
logger.VpnConnectionDeleted(lastConnectionName);
stateManager.SetLastVpnProfile(newVpnProfileHash);

stateManager.SetLastConnectionName(settings.VpnConnectionName);
vpnManager.CreateVpnConnection(settings.VpnConnectionName, settings.VpnProfileXml);
logger.VpnConnectionCreated(settings.VpnConnectionName);
}
}
Expand All @@ -99,17 +82,5 @@ private string ComputeHash(string vpnProfileXml)
return rv.ToString();
}
}

private VpnConnectionInfo FindVpnConnection(string connectionName)
{
foreach (VpnConnectionInfo connection in vpnManager.EnumarateVpnConnections())
{
if (connection.ConnectionName == connectionName)
{
return connection;
}
}
return null;
}
}
}
2 changes: 0 additions & 2 deletions AOVpnManager/IVpnManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;

namespace AOVpnManager
{
public interface IVpnManager : IDisposable
{
void CreateVpnConnection(string connectionName, string profile);
void DeleteVpnConnection(string connectionName);
IEnumerable<VpnConnectionInfo> EnumarateVpnConnections();
}
}
14 changes: 0 additions & 14 deletions AOVpnManager/VpnManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Management.Infrastructure;
using System;
using System.Collections.Generic;
using System.Security;

namespace AOVpnManager
Expand Down Expand Up @@ -62,19 +61,6 @@ public void DeleteVpnConnection(string connectionName)
}
}

public IEnumerable<VpnConnectionInfo> EnumarateVpnConnections()
{
foreach (CimInstance instance in session.EnumerateInstances(NamespaceName, ClassName))
{
using (instance)
{
string connectionName = UnescapeConnectionName((string)instance.CimInstanceProperties[PropertyNames.ConnectionName].Value);
string profileXml = (string)instance.CimInstanceProperties[PropertyNames.ProfileXml].Value;
yield return new VpnConnectionInfo(connectionName, profileXml);
}
}
}

private static Exception ConvertCimException(CimException ex)
{
if (ex.NativeErrorCode == NativeErrorCode.NotFound)
Expand Down

0 comments on commit 73d3f89

Please sign in to comment.