diff --git a/packages/api/src/controllers/config.js b/packages/api/src/controllers/config.js index 3b36d4df..f26eaae2 100644 --- a/packages/api/src/controllers/config.js +++ b/packages/api/src/controllers/config.js @@ -16,6 +16,7 @@ const { getAuthProviderFromReq } = require('../auth/authProvider'); const { checkLicense, checkLicenseKey } = require('../utility/checkLicense'); const storage = require('./storage'); const { getAuthProxyUrl } = require('../utility/authProxy'); +const { getPublicHardwareFingerprint } = require('../utility/hardwareFingerprint'); const lock = new AsyncLock(); @@ -188,12 +189,12 @@ module.exports = { startTrial_meta: true, async startTrial() { try { - const ipResp = await axios.default.get('https://api.ipify.org?format=json'); + const fingerprint = await getPublicHardwareFingerprint(); const resp = await axios.default.post(`${getAuthProxyUrl()}/trial-license`, { type: 'premium-trial', days: 30, - publicIp: ipResp.data.ip, + fingerprint, }); const { token } = resp.data; diff --git a/packages/api/src/utility/hardwareFingerprint.js b/packages/api/src/utility/hardwareFingerprint.js new file mode 100644 index 00000000..a9b8d3e2 --- /dev/null +++ b/packages/api/src/utility/hardwareFingerprint.js @@ -0,0 +1,72 @@ +const axios = require('axios'); +const os = require('os'); +const crypto = require('crypto'); + +async function getPublicIp() { + try { + const resp = await axios.default.get('https://api.ipify.org?format=json'); + return resp.data.ip || 'unknown-ip'; + } catch (err) { + return 'unknown-ip'; + } +} + +function getMacAddress() { + const interfaces = os.networkInterfaces(); + for (let iface of Object.values(interfaces)) { + for (let config of iface) { + if (config.mac && config.mac !== '00:00:00:00:00:00') { + return config.mac; + } + } + } + return '00:00:00:00:00:00'; +} + +async function getHardwareFingerprint() { + const publicIp = await getPublicIp(); + const macAddress = getMacAddress(); + const platform = os.platform(); + const release = os.release(); + const hostname = os.hostname(); + const totalMemory = os.totalmem(); + + return { + publicIp, + macAddress, + platform, + release, + hostname, + totalMemory, + }; +} + +async function getHardwareFingerprintHash(data = undefined) { + if (!data) { + data = await getHardwareFingerprint(); + } + const fingerprintData = JSON.stringify(data); + const hash = crypto.createHash('sha256').update(fingerprintData).digest('hex'); + return hash; +} + +async function getPublicHardwareFingerprint() { + const fingerprint = await getHardwareFingerprint(); + const hash = await getHardwareFingerprintHash(fingerprint); + return { + hash, + payload: { + platform: fingerprint.platform, + }, + }; +} + +// getHardwareFingerprint().then(console.log); +// getHardwareFingerprintHash().then(console.log); +// getPublicHardwareFingerprint().then(console.log); + +module.exports = { + getHardwareFingerprint, + getHardwareFingerprintHash, + getPublicHardwareFingerprint, +};