Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/dotnet/Telemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
using Microsoft.ApplicationInsights;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.InternalAbstractions;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using System.Linq;

namespace Microsoft.DotNet.Cli
{
Expand All @@ -27,6 +31,8 @@ public class Telemetry : ITelemetry
private const string RuntimeId = "Runtime Id";
private const string ProductVersion = "Product Version";
private const string TelemetryProfile = "Telemetry Profile";
private const string MachineID = "Machine Id";
private const string AllMachineIDs = "All Machine Ids";

public bool Enabled { get; }

Expand Down Expand Up @@ -72,6 +78,9 @@ private void InitializeTelemetry()
_commonProperties.Add(RuntimeId, RuntimeEnvironment.GetRuntimeIdentifier());
_commonProperties.Add(ProductVersion, Product.Version);
_commonProperties.Add(TelemetryProfile, Environment.GetEnvironmentVariable(TelemetryProfileEnvironmentVariable));
var hashedMacs = HashSha256(GetMacs());
_commonProperties.Add(MachineID, hashedMacs.FirstOrDefault());
_commonProperties.Add(AllMachineIDs, string.Join(",", hashedMacs));
_commonMeasurements = new Dictionary<string, double>();
}
catch (Exception)
Expand Down Expand Up @@ -145,6 +154,54 @@ private Dictionary<string, string> GetEventProperties(IDictionary<string, string
{
return _commonProperties;
}
}

// Note: Reason for byte->string->byte in following 2 functions.
// The Mac address must match the same format that `getmac` or `ifconfig -a || ip link` would present a mac address(e.g. FF-FF-FF-FF-FF-FF).
// The hashed mac address needs to be the same hashed value as produced by the other distinct sources given the same input. (e.g. VsCode)
private List<string> GetMacs()
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
var macs = new List<string>();

if (nics == null || nics.Length < 1)
{
macs.Add(string.Empty);
return macs;
}

foreach (NetworkInterface adapter in nics)
{
IPInterfaceProperties properties = adapter.GetIPProperties();

PhysicalAddress address = adapter.GetPhysicalAddress();
byte[] bytes = address.GetAddressBytes();
macs.Add(string.Join("-", bytes.Select(x => x.ToString("X2"))));

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

if (macs.Count >= 10)
{
break;
}
}
return macs;
}

// The hashed mac address needs to be the same hashed value as produced by the other distinct sources given the same input. (e.g. VsCode)
private List<string> HashSha256(List<string> texts)
{
var sha256 = SHA256.Create();
var hashedStrings = new List<string>();
foreach (var text in texts)
{
byte[] bytes = Encoding.UTF8.GetBytes(text);
byte[] hash = sha256.ComputeHash(bytes);
StringBuilder hashString = new StringBuilder();
foreach (byte x in hash)
{
hashString.AppendFormat("{0:x2}", x);
}
hashedStrings.Add(hashString.ToString());
}
return hashedStrings;
}
}
}
3 changes: 2 additions & 1 deletion src/dotnet/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"Microsoft.Win32.Registry": {
"version": "4.0.0",
"exclude": "compile"
}
},
"System.Net.NetworkInformation": "4.1.0-rc3-24201-00"
},
"frameworks": {
"netcoreapp1.0": {
Expand Down