Nodejs Get Server Hardware Information

Need

Use nodejs to obtain server hardware information (MAC address, IP address, CPU serial number, motherboard serial number), and generate a string of unique licenses

Code

To get server hardware information using Node.js, you can use some third-party modules to help you gather the required information. Here is sample code that uses some common modules to get hardware information and generate a unique license:

const os = require('os');
const crypto = require('crypto');
const macaddress = require('macaddress');

// get MAC address
function getMacAddress() {
  return new Promise((resolve, reject) => {
    macaddress.one((err, mac) => {
      if (err) {
        reject(err);
      } else {
        resolve(mac);
      }
    });
  });
}

// get ip address
function getIpAddress() {
  const ifaces = os.networkInterfaces();
  for (const iface in ifaces) {
    const addresses = ifaces[iface];
    for (const addr of addresses) {
      if (addr.family === 'IPv4' && !addr.internal) {
        return addr.address;
      }
    }
  }
  return 'Unknown';
}

// Get CPU serial number
function getCpuSerial() {
  const cpus = os.cpus();
  const serials = [];
  for (const cpu of cpus) {
    if (cpu.hasOwnProperty('serialNumber')) {
      serials.push(cpu.serialNumber);
    }
  }
  return serials.join(',');
}

// Get the motherboard serial number
function getMotherboardSerial() {
  return os.platform() === 'win32' ? os.userInfo().username : os.hostname();
}

// Generate a unique license
function generateLicense() {
  const macPromise = getMacAddress();
  const ip = getIpAddress();
  const cpuSerial = getCpuSerial();
  const motherboardSerial = getMotherboardSerial();

  return Promise.all([macPromise])
    .then(([mac]) => {
      const data = `${mac}${ip}${cpuSerial}${motherboardSerial}`;
      const hash = crypto.createHash('md5').update(data).digest('hex');
      return hash;
    });
}

// Test to generate a license
generateLicense()
  .then(license => {
    console.log('License:', license);
  })
  .catch(err => {
    console.error('Error:', err);
  });

The above code uses the following three third-party modules:

  1. macaddress: Used to get the MAC address. You can install this module with npm install macaddress command.
  2. crypto: Node.js built-in encryption module, used to generate hash values.

Make sure you have installed these two modules via npm install command.

Note that these hardware information may vary by operating system and hardware device. Obtaining the motherboard serial number may not be easy in some cases. On Windows platforms, I have used the username of the current user in the examples as an alternative to the serial number of the motherboard, but this is not a unique identifier. You may need to adjust the code according to your specific needs.

In addition, please note that the generated license is only a hash value generated based on hardware information, and it does not contain any verification mechanism. In practical applications, you may need to further encrypt, sign or perform other verification steps on the license to ensure its uniqueness and security.

Comments