Skip to content

Commit

Permalink
Added parameter to exclude docker bridge interface
Browse files Browse the repository at this point in the history
  • Loading branch information
matihuf authored and MatthewKing committed Sep 19, 2023
1 parent 14c10f5 commit 50f739f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/DeviceId/Components/MacAddressDeviceIdComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@ namespace DeviceId.Components;
/// </summary>
public class MacAddressDeviceIdComponent : IDeviceIdComponent
{
private const string _dockerBridgeInterfaceName = "docker0";

/// <summary>
/// A value determining whether wireless devices should be excluded.
/// </summary>
private readonly bool _excludeWireless;

/// <summary>
/// A value determining whether docker bridge should be excluded.
/// </summary>
private readonly bool _excludeDockerBridge;

/// <summary>
/// Initializes a new instance of the <see cref="MacAddressDeviceIdComponent"/> class.
/// </summary>
/// <param name="excludeWireless">A value determining whether wireless devices should be excluded.</param>
public MacAddressDeviceIdComponent(bool excludeWireless)
/// <param name="excludeDockerBridge">A value determining whether docker bridge should be excluded.</param>
public MacAddressDeviceIdComponent(bool excludeWireless, bool excludeDockerBridge)
{
_excludeWireless = excludeWireless;
_excludeDockerBridge = excludeDockerBridge;
}

/// <summary>
Expand All @@ -30,7 +39,7 @@ public MacAddressDeviceIdComponent(bool excludeWireless)
public string GetValue()
{
var values = NetworkInterface.GetAllNetworkInterfaces()
.Where(x => !_excludeWireless || x.NetworkInterfaceType != NetworkInterfaceType.Wireless80211)
.Where(x => (!_excludeWireless || x.NetworkInterfaceType != NetworkInterfaceType.Wireless80211) && (!_excludeDockerBridge || x.Name != _dockerBridgeInterfaceName))
.Select(x => x.GetPhysicalAddress().ToString())
.Where(x => x != "000000000000")
.Select(x => MacAddressFormatter.FormatMacAddress(x))
Expand Down
5 changes: 3 additions & 2 deletions src/DeviceId/DeviceIdBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ public static DeviceIdBuilder AddOsVersion(this DeviceIdBuilder builder)
/// </summary>
/// <param name="builder">The <see cref="DeviceIdBuilder"/> to add the component to.</param>
/// <param name="excludeWireless">A value indicating whether wireless adapters should be excluded.</param>
/// <param name="excludeDockerBridge">A value determining whether docker bridge should be excluded.</param>
/// <returns>The <see cref="DeviceIdBuilder"/> instance.</returns>
public static DeviceIdBuilder AddMacAddress(this DeviceIdBuilder builder, bool excludeWireless = false)
public static DeviceIdBuilder AddMacAddress(this DeviceIdBuilder builder, bool excludeWireless = false, bool excludeDockerBridge = false)
{
return builder.AddComponent("MACAddress", new MacAddressDeviceIdComponent(excludeWireless));
return builder.AddComponent("MACAddress", new MacAddressDeviceIdComponent(excludeWireless, excludeDockerBridge));
}

/// <summary>
Expand Down

0 comments on commit 50f739f

Please sign in to comment.