Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/node/macAddress.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
8
const invalidMacAddresses = new Set([
9
'00:00:00:00:00:00',
10
'ff:ff:ff:ff:ff:ff',
11
'ac:de:48:00:11:22'
12
]);
13
14
function validateMacAddress(candidate: string): boolean {
15
const tempCandidate = candidate.replace(/\-/g, ':').toLowerCase();
16
return !invalidMacAddresses.has(tempCandidate);
17
}
18
19
export function getMac(): string {
20
const ifaces = networkInterfaces();
21
for (const name in ifaces) {
22
const networkInterface = ifaces[name];
23
if (networkInterface) {
24
for (const { mac } of networkInterface) {
25
if (validateMacAddress(mac)) {
26
return mac;
27
}
28
}
29
}
30
}
31
32
throw new Error('Unable to retrieve mac address (unexpected format)');
33
}
34
35