77 lines
2.6 KiB
JavaScript
77 lines
2.6 KiB
JavaScript
|
const axios = require('axios');
|
||
|
|
||
|
class License {
|
||
|
constructor(config) {
|
||
|
this.url = config.url;
|
||
|
this.licenseKey = config.licenseKey;
|
||
|
this.product = config.product;
|
||
|
this.version = config.version;
|
||
|
this.publicApiKey = config.publicKey;
|
||
|
// this.hwid = getHWID(); <- You can add this if you want to use HWID lock
|
||
|
this.banned = true;
|
||
|
}
|
||
|
|
||
|
async load() {
|
||
|
try {
|
||
|
// Make a request to the license server
|
||
|
const res = await axios.post(
|
||
|
this.url,
|
||
|
{
|
||
|
licensekey: this.licenseKey,
|
||
|
product: this.product,
|
||
|
version: this.version,
|
||
|
// hwid: this.hwid, <- You can add this if you want to use HWID lock
|
||
|
},
|
||
|
{
|
||
|
headers: {Authorization: this.publicApiKey},
|
||
|
}
|
||
|
);
|
||
|
|
||
|
// Validate request body
|
||
|
if (!res.data.status_code || !res.data.status_id) {
|
||
|
throw new Error('Invalid authentication');
|
||
|
}
|
||
|
|
||
|
// Verify authentication
|
||
|
if (res.data.status_overview !== 'success') {
|
||
|
throw new Error('Authentication failed: ' + res.data.status_msg);
|
||
|
}
|
||
|
|
||
|
// Validate hash
|
||
|
const hash = res.data.status_id;
|
||
|
const hash_split = hash.split('694201337');
|
||
|
const decoded_hash = Buffer.from(hash_split[0], 'base64').toString();
|
||
|
const license_first = this.licenseKey.substr(0, 2);
|
||
|
const license_last = this.licenseKey.substr(this.licenseKey.length - 2);
|
||
|
const public_api_key_first = this.publicApiKey.substr(0, 2);
|
||
|
|
||
|
// Text-based validation
|
||
|
if (decoded_hash !== `${license_first}${license_last}${public_api_key_first}`) {
|
||
|
throw new Error('Authentication failed: Hash mismatch');
|
||
|
}
|
||
|
|
||
|
// Time-based validation
|
||
|
let epoch_time_full = Math.floor(Date.now() / 1000);
|
||
|
const epoch_time = epoch_time_full.toString().substr(0, epoch_time_full.toString().length - 2);
|
||
|
|
||
|
// Check if timestamp is expired
|
||
|
if (parseInt(epoch_time) - parseInt(hash_split[1]) > 1) {
|
||
|
throw new Error('Authentication failed: Timestamp expired');
|
||
|
}
|
||
|
|
||
|
// License is valid
|
||
|
this.banned = false;
|
||
|
return true;
|
||
|
} catch (error) {
|
||
|
throw new Error('License authorization failed');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// check if license is valid
|
||
|
isNotBlacklisted() {
|
||
|
return !this.banned;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = License;
|