Skip to content

Commit

Permalink
log machine id mappings for VS to VS Code (#11308)
Browse files Browse the repository at this point in the history
* log machine id mappings for VS to VS Code

* update comment

* in case execChildProcess throws
  • Loading branch information
bobbrow authored Aug 11, 2023
1 parent 44e3bbe commit 212fff2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Extension/src/id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import { execChildProcess } from './common';
import { isWindows } from './constants';
import { logLanguageServerEvent } from './telemetry';

/**
* Hash the MAC addresses on the machine (Windows-only) and log telemetry.
*/
export async function logMachineIdMappings(): Promise<void> {
if (!isWindows) {
return;
}

const macAddresses = await getMacAddresses();

// The first MAC address is the one Visual Studio uses
const primary = await getMachineId(macAddresses.shift());
if (primary) {
logLanguageServerEvent('machineIdMap', {primary});
}

// VS Code uses os.networkInterfaces() which has different sorting and availability,
// but all MAC addresses are returned by getmac.exe. The ID VS Code uses may change
// based on changes to the network configuration. Log the extras so we can assess
// how frequently this impacts the machine id.
for (const macAddress of macAddresses) {
const additional = await getMachineId(macAddress);
if (additional) {
logLanguageServerEvent('machineIdMap', {additional});
}
}
}

/**
* Parse the output of getmac.exe to get the list of MAC addresses for the PC.
*/
async function getMacAddresses(): Promise<string[]> {
try {
const output = await execChildProcess('getmac');
const regex = /(?:[a-z0-9]{2}[:\-]){5}[a-z0-9]{2}/gmi;
return output.match(regex) ?? [];
} catch (err) {
return [];
}
}

/**
* Code below is adapted from:
* - vscode\src\vs\base\node\id.ts
*/

async function getMachineId(macAddress?: string): Promise<string | undefined> {
if (!macAddress) {
return undefined;
}

try {
const crypto = await import('crypto');
const normalized = macAddress.toUpperCase().replace(/:/g, '-');
return crypto.createHash('sha256').update(normalized, 'utf8').digest('hex');
} catch (err) {
return undefined;
}
}
2 changes: 2 additions & 0 deletions Extension/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { PersistentState } from './LanguageServer/persistentState';
import { CppSettings } from './LanguageServer/settings';
import { logAndReturn, returns } from './Utility/Async/returns';
import { CppTools1 } from './cppTools1';
import { logMachineIdMappings } from './id';
import { disposeOutputChannels, log } from './logger';
import { PlatformInformation } from './platform';

Expand Down Expand Up @@ -214,6 +215,7 @@ function sendTelemetry(info: PlatformInformation): void {
break;
}
Telemetry.logDebuggerEvent("acquisition", telemetryProperties);
logMachineIdMappings().catch(logAndReturn.undefined);
}

async function checkVsixCompatibility(): Promise<void> {
Expand Down

0 comments on commit 212fff2

Please sign in to comment.