Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/node/id.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { networkInterfaces } from 'os';
7
import { TernarySearchTree } from '../common/ternarySearchTree.js';
8
import * as uuid from '../common/uuid.js';
9
import { getMac } from './macAddress.js';
10
import { isWindows } from '../common/platform.js';
11
12
// http://www.techrepublic.com/blog/data-center/mac-address-scorecard-for-common-virtual-machine-platforms/
13
// VMware ESX 3, Server, Workstation, Player 00-50-56, 00-0C-29, 00-05-69
14
// Microsoft Hyper-V, Virtual Server, Virtual PC 00-03-FF
15
// Parallels Desktop, Workstation, Server, Virtuozzo 00-1C-42
16
// Virtual Iron 4 00-0F-4B
17
// Red Hat Xen 00-16-3E
18
// Oracle VM 00-16-3E
19
// XenSource 00-16-3E
20
// Novell Xen 00-16-3E
21
// Sun xVM VirtualBox 08-00-27
22
export const virtualMachineHint: { value(): number } = new class {
23
24
private _virtualMachineOUIs?: TernarySearchTree<string, boolean>;
25
private _value?: number;
26
27
private _isVirtualMachineMacAddress(mac: string): boolean {
28
if (!this._virtualMachineOUIs) {
29
this._virtualMachineOUIs = TernarySearchTree.forStrings<boolean>();
30
31
// dash-separated
32
this._virtualMachineOUIs.set('00-50-56', true);
33
this._virtualMachineOUIs.set('00-0C-29', true);
34
this._virtualMachineOUIs.set('00-05-69', true);
35
this._virtualMachineOUIs.set('00-03-FF', true);
36
this._virtualMachineOUIs.set('00-1C-42', true);
37
this._virtualMachineOUIs.set('00-16-3E', true);
38
this._virtualMachineOUIs.set('08-00-27', true);
39
40
// colon-separated
41
this._virtualMachineOUIs.set('00:50:56', true);
42
this._virtualMachineOUIs.set('00:0C:29', true);
43
this._virtualMachineOUIs.set('00:05:69', true);
44
this._virtualMachineOUIs.set('00:03:FF', true);
45
this._virtualMachineOUIs.set('00:1C:42', true);
46
this._virtualMachineOUIs.set('00:16:3E', true);
47
this._virtualMachineOUIs.set('08:00:27', true);
48
}
49
return !!this._virtualMachineOUIs.findSubstr(mac);
50
}
51
52
value(): number {
53
if (this._value === undefined) {
54
let vmOui = 0;
55
let interfaceCount = 0;
56
57
const interfaces = networkInterfaces();
58
for (const name in interfaces) {
59
const networkInterface = interfaces[name];
60
if (networkInterface) {
61
for (const { mac, internal } of networkInterface) {
62
if (!internal) {
63
interfaceCount += 1;
64
if (this._isVirtualMachineMacAddress(mac.toUpperCase())) {
65
vmOui += 1;
66
}
67
}
68
}
69
}
70
}
71
this._value = interfaceCount > 0
72
? vmOui / interfaceCount
73
: 0;
74
}
75
76
return this._value;
77
}
78
};
79
80
let machineId: Promise<string>;
81
export async function getMachineId(errorLogger: (error: any) => void): Promise<string> {
82
if (!machineId) {
83
machineId = (async () => {
84
const id = await getMacMachineId(errorLogger);
85
86
return id || uuid.generateUuid(); // fallback, generate a UUID
87
})();
88
}
89
90
return machineId;
91
}
92
93
async function getMacMachineId(errorLogger: (error: any) => void): Promise<string | undefined> {
94
try {
95
const crypto = await import('crypto');
96
const macAddress = getMac();
97
return crypto.createHash('sha256').update(macAddress, 'utf8').digest('hex');
98
} catch (err) {
99
errorLogger(err);
100
return undefined;
101
}
102
}
103
104
const SQM_KEY: string = 'Software\\Microsoft\\SQMClient';
105
export async function getSqmMachineId(errorLogger: (error: any) => void): Promise<string> {
106
if (isWindows) {
107
const Registry = await import('@vscode/windows-registry');
108
try {
109
return Registry.GetStringRegKey('HKEY_LOCAL_MACHINE', SQM_KEY, 'MachineId') || '';
110
} catch (err) {
111
errorLogger(err);
112
return '';
113
}
114
}
115
return '';
116
}
117
118
export async function getDevDeviceId(errorLogger: (error: any) => void): Promise<string> {
119
try {
120
const deviceIdPackage = await import('@vscode/deviceid');
121
const id = await deviceIdPackage.getDeviceId();
122
return id;
123
} catch (err) {
124
errorLogger(err);
125
return uuid.generateUuid();
126
}
127
}
128
129