refs #134873
This commit is contained in:
parent
9571d0d469
commit
f145c79a93
|
@ -93,7 +93,7 @@ export async function getMachineId(): Promise<string> {
|
|||
async function getMacMachineId(): Promise<string | undefined> {
|
||||
try {
|
||||
const crypto = await import('crypto');
|
||||
const macAddress = await getMac();
|
||||
const macAddress = getMac();
|
||||
return crypto.createHash('sha256').update(macAddress, 'utf8').digest('hex');
|
||||
} catch (err) {
|
||||
errors.onUnexpectedError(err);
|
||||
|
|
|
@ -16,39 +16,18 @@ function validateMacAddress(candidate: string): boolean {
|
|||
return !invalidMacAddresses.has(tempCandidate);
|
||||
}
|
||||
|
||||
export function getMac(): Promise<string> {
|
||||
// eslint-disable-next-line no-async-promise-executor
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const timeout = setTimeout(() => reject('Unable to retrieve mac address (timeout after 10s)'), 10000);
|
||||
|
||||
try {
|
||||
resolve(await doGetMac());
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function doGetMac(): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const ifaces = networkInterfaces();
|
||||
for (let name in ifaces) {
|
||||
const networkInterface = ifaces[name];
|
||||
if (networkInterface) {
|
||||
for (const { mac } of networkInterface) {
|
||||
if (validateMacAddress(mac)) {
|
||||
return resolve(mac);
|
||||
}
|
||||
}
|
||||
export function getMac(): string {
|
||||
const ifaces = networkInterfaces();
|
||||
for (let name in ifaces) {
|
||||
const networkInterface = ifaces[name];
|
||||
if (networkInterface) {
|
||||
for (const { mac } of networkInterface) {
|
||||
if (validateMacAddress(mac)) {
|
||||
return mac;
|
||||
}
|
||||
}
|
||||
|
||||
reject('Unable to retrieve mac address (unexpected format)');
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
throw new Error('Unable to retrieve mac address (unexpected format)');
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ flakySuite('ID', () => {
|
|||
});
|
||||
|
||||
test('getMac', async () => {
|
||||
const macAddress = await getMac();
|
||||
const macAddress = getMac();
|
||||
assert.ok(/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/.test(macAddress), `Expected a MAC address, got: ${macAddress}`);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue