This commit is contained in:
SteVen Batten 2021-11-01 09:15:19 -07:00
parent 9571d0d469
commit f145c79a93
No known key found for this signature in database
GPG key ID: 49BCA7901CAC9BF6
3 changed files with 13 additions and 34 deletions

View file

@ -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);

View file

@ -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)');
}

View file

@ -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}`);
});
});