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

Commit

Permalink
Implement Dynamic Naming of WireSock Virtual Network Interface Based …
Browse files Browse the repository at this point in the history
…on Config Files

This commit introduces a user-centric feature enabling dynamic naming of the WireSock VPN interface based on the associated configuration file. This enhancement, particularly beneficial for those managing multiple configurations in WireSock's virtual adapter mode, provides a more intuitive and efficient way to identify and interact with various network interfaces. This change is in direct response to the discussion in [WireSockUI Issue #17](wiresock#17), reflecting our commitment to community feedback and continuous improvement.
  • Loading branch information
wiresock committed Nov 19, 2023
1 parent 37a8cc4 commit f691c4e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
40 changes: 40 additions & 0 deletions WireSockUI/WireSockManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Management;
using WireSockUI.Config;
using WireSockUI.Properties;
using static WireSockUI.Native.WireguardBoosterExports;
Expand Down Expand Up @@ -224,6 +225,40 @@ private BackgroundWorker InitializeLogWorker(LogMessageCallback logMessageCallba
Dispose();
}

/// <summary>
/// Changes the NetConnectionID of a network adapter identified by its friendly name.
/// </summary>
/// <remarks>
/// This function uses Windows Management Instrumentation (WMI) to locate a network adapter based on its friendly name.
/// Once found, it changes the adapter's NetConnectionID to the specified new name. This is particularly useful for
/// managing and identifying network connections programmatically. The function iterates through all matching network
/// adapters and updates their NetConnectionID, if it is not null or empty.
/// </remarks>
/// <param name="adapterFriendlyName">The friendly name of the network adapter whose NetConnectionID is to be changed.</param>
/// <param name="newName">The new NetConnectionID to be assigned to the network adapter.</param>
/// <example>
/// <code>
/// ChangeNetConnectionIdByAdapterName("Ethernet", "NewEthernetConnection");
/// </code>
/// </example>
private static void ChangeNetConnectionIdByAdapterName(string adapterFriendlyName, string newName)
{
var query = new SelectQuery("Win32_NetworkAdapter", $"Name = '{adapterFriendlyName}'");
using (var searcher = new ManagementObjectSearcher(query))
{
foreach (var o in searcher.Get())
{
var obj = (ManagementObject)o;
// Check if NetConnectionID is not null or empty
if (obj["NetConnectionID"] != null && !string.IsNullOrEmpty(obj["NetConnectionID"].ToString()))
{
obj["NetConnectionID"] = newName;
obj.Put(); // Save changes
}
}
}
}

/// <summary>
/// Create a Wireguard tunnel using the specified configuration file.
/// </summary>
Expand All @@ -250,6 +285,11 @@ public bool Connect(string profile)
_handle = IntPtr.Zero;
return false;
}

if (TunnelMode == Mode.VirtualAdapter)
{
ChangeNetConnectionIdByAdapterName("Wiresock Virtual Adapter", profile);
}

if (!_startTunnel(_handle))
{
Expand Down
1 change: 1 addition & 0 deletions WireSockUI/WireSockUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Reference Include="System" />
<Reference Include="System.Design" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Management" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Drawing" />
Expand Down

0 comments on commit f691c4e

Please sign in to comment.