// requirements
const express = require('express');
const fs = require('fs');
const axios = require('axios');
const chalk = require('chalk');
// application
const app = express();
const port = 4563; // you can change it as you wish.
// DO NOT CHANGE THEM!
const ipLicenseMap = {};
const usedLicenses = {};
const frameWork = "framework-dev"
const frameWorkPrefix = chalk.blue("[Framework]");
// configuration
const configRaw = fs.readFileSync('frameworkConfig.json');
const config = JSON.parse(configRaw);
// configuration -> values
const enableDiscordWebhooks = config.ENABLE_DISCORD_WEBHOOKS;
const discordWebhookLink = config.DISCORD_WEBHOOK_LINK;
// generatin' a unique license code according to the request from user.
function generateLicense() {
return Math.random().toString(36).substring(2, 10).toUpperCase();
}
app.get('/createpin', (req, res) => {
// getting client ip
const clientIP = req.ip;
if (ipLicenseMap[clientIP]) {
const previousPin = ipLicenseMap[clientIP];
const errorMessage = {
error: 'You have previously requested to generate a PIN.',
pin: previousPin,
providedBy: frameWork
};
res.status(403).json(errorMessage);
sendWebhookMessage(JSON.stringify(errorMessage)); // send webhook if its enabled.
return;
}
const licenseCode = generateLicense();
ipLicenseMap[clientIP] = licenseCode;
usedLicenses[licenseCode] = false; // license has not been used yet.
setTimeout(() => {
delete ipLicenseMap[clientIP];
console.log(`${frameWorkPrefix} ${clientIP} deleted from the license map.`);
}, 5 * 60 * 1000); // the timeout is cleared after 5 mins.
const successMessage = {
pin: licenseCode,
providedBy: frameWork
};
res.json(successMessage);
sendWebhookMessage(JSON.stringify(successMessage)); // send webhook if its enabled.
usedLicenses[licenseCode] = false;
});
app.get('/verify/:licensecode', (req, res) => {
const licenseCodeToVerify = req.params.licensecode;
if (isLicenseValid(licenseCodeToVerify)) {
const verificationMessage = {
message: 'Access granted. License code is valid.',
providedBy: frameWork
};
usedLicenses[licenseCodeToVerify] = true; // license has been used.
delete ipLicenseMap[licenseCodeToVerify];
res.json(verificationMessage);
sendWebhookMessage(JSON.stringify(verificationMessage));
} else {
const errorMessage = {
error: 'Access denied. Invalid license code.',
providedBy: frameWork
};
res.status(403).json(errorMessage);
sendWebhookMessage(JSON.stringify(errorMessage));
}
});
app.listen(port, () => {
console.log(`${frameWorkPrefix} Framework License is running at http://localhost:${port}`);
if (enableDiscordWebhooks) {
console.log(`${frameWorkPrefix} Discord Webhooks are enabled. Webhook Link: ${discordWebhookLink}`);
} else {
console.log(`${frameWorkPrefix} Discord Webhooks are disabled.`);
}
console.log(`\n${frameWorkPrefix} PINs are running at http://localhost:${port}/createPin\n${frameWorkPrefix} Verification system is running at http://localhost:${port}/verify/:licensecode`)
});
function sendWebhookMessage(message) {
if (enableDiscordWebhooks) {
axios.post(discordWebhookLink, {
content: message,
})
.then(response => {
console.log(`${frameWorkPrefix} Webhook message received.`);
})
.catch(error => {
console.error(`${frameWorkPrefix} Error sending webhook message.`);
});
} // you can also add else {} here to send log for skippin' message.
}
function isLicenseValid(licenseCode) {
if (!usedLicenses[licenseCode]) {
return true; // valid license
} else {
return false; // invalid ''
}
}