@xec-sh/cli
Version:
Xec: The Universal Shell for TypeScript
129 lines • 4.9 kB
JavaScript
import * as os from 'os';
import { createHash } from 'crypto';
import { execSync } from 'child_process';
import { existsSync, readFileSync } from 'fs';
export async function getMachineId() {
const platform = os.platform();
let rawId = '';
try {
switch (platform) {
case 'darwin': {
const output = execSync('ioreg -rd1 -c IOPlatformExpertDevice | grep UUID', { encoding: 'utf8' });
const match = output.match(/"IOPlatformUUID"\s*=\s*"([^"]+)"/);
if (match && match[1]) {
rawId = match[1];
}
else {
throw new Error('Failed to extract UUID from ioreg output');
}
break;
}
case 'linux': {
const machineIdPaths = [
'/etc/machine-id',
'/var/lib/dbus/machine-id',
'/sys/class/dmi/id/product_uuid'
];
let found = false;
for (const path of machineIdPaths) {
if (existsSync(path)) {
rawId = readFileSync(path, 'utf8').trim();
found = true;
break;
}
}
if (!found) {
const interfaces = os.networkInterfaces();
const macs = [];
for (const [name, ifaces] of Object.entries(interfaces)) {
if (name === 'lo')
continue;
for (const iface of ifaces || []) {
if (iface.mac && iface.mac !== '00:00:00:00:00:00') {
macs.push(iface.mac);
}
}
}
if (macs.length > 0) {
rawId = macs.sort().join(':');
}
else {
throw new Error('No machine ID sources found on Linux');
}
}
break;
}
case 'win32': {
try {
const output = execSync('wmic csproduct get UUID /value', { encoding: 'utf8' });
const match = output.match(/UUID=([^\r\n]+)/);
if (match && match[1]) {
rawId = match[1].trim();
}
else {
throw new Error('Failed to extract UUID from wmic output');
}
}
catch (error) {
const productIdOutput = execSync('wmic os get SerialNumber /value', { encoding: 'utf8' });
const productMatch = productIdOutput.match(/SerialNumber=([^\r\n]+)/);
if (productMatch && productMatch[1]) {
rawId = productMatch[1].trim();
}
else {
throw new Error('Failed to get Windows machine ID');
}
}
break;
}
default: {
const hostname = os.hostname();
const interfaces = os.networkInterfaces();
const macs = [];
for (const [name, ifaces] of Object.entries(interfaces)) {
for (const iface of ifaces || []) {
if (iface.mac && iface.mac !== '00:00:00:00:00:00') {
macs.push(iface.mac);
}
}
}
rawId = `${hostname}:${macs.sort().join(':')}`;
break;
}
}
const hash = createHash('sha256').update(rawId).digest('hex');
const uuid = [
hash.substring(0, 8),
hash.substring(8, 12),
'5' + hash.substring(13, 16),
hash.substring(16, 20),
hash.substring(20, 32)
].join('-');
return uuid;
}
catch (error) {
const fallbackId = [
os.hostname(),
os.cpus()[0]?.model || 'unknown',
os.totalmem().toString(),
Date.now().toString()
].join(':');
const hash = createHash('sha256').update(fallbackId).digest('hex');
const uuid = [
hash.substring(0, 8),
hash.substring(8, 12),
'5' + hash.substring(13, 16),
hash.substring(16, 20),
hash.substring(20, 32)
].join('-');
return uuid;
}
}
let cachedMachineId = null;
export async function getCachedMachineId() {
if (!cachedMachineId) {
cachedMachineId = await getMachineId();
}
return cachedMachineId;
}
//# sourceMappingURL=machine-id.js.map