258 lines
11 KiB
JavaScript
258 lines
11 KiB
JavaScript
const { MacModel } = require('../models')
|
|
const {logMsg} = require('../services/logger')
|
|
const secretKeyJwt = process.env.SECRETKEY;
|
|
const { info, log } = require('winston');
|
|
const moment = require('moment');
|
|
const Cryptr = require('cryptr');
|
|
|
|
|
|
module.exports.addMacValue= async(req, res, next)=>{
|
|
try {
|
|
|
|
const cryptr = new Cryptr('7B-q;Vn@oGc^aPtk:0QP/SyDoHw!,(L2{2uZP&jZw5l]*>s~,%Z+kaD1tloQU', { encoding: 'base64', pbkdf2Iterations: 10000, saltLength: 5 });
|
|
app_name = null;
|
|
let {key, mac_id, company_name, app_version } = req.body
|
|
if(req.body.hasOwnProperty('app_name')){
|
|
app_name = req.body.app_name;
|
|
}else{
|
|
app_name = null;
|
|
}
|
|
key = cryptr.decrypt(key);
|
|
mac_id = cryptr.decrypt(mac_id);
|
|
company_name = cryptr.decrypt(company_name);
|
|
app_version = cryptr.decrypt(app_version);
|
|
|
|
const mac = await MacModel.findOne({where:{licency_key: key},raw:true});
|
|
|
|
console.log(mac);
|
|
//Check License Key IN DB
|
|
if (mac && mac.licency_key == key)
|
|
{
|
|
//Check isActive and App Version are Same
|
|
if (mac.isActive == false && mac.app_version.charAt(0) == app_version.charAt(0) ) {
|
|
|
|
today = moment().format('YYYY/MM/DD');
|
|
if(mac.expiry_type == 'TRAIL'){
|
|
after10days = moment().add(10, 'day');
|
|
formatedDate = after10days.format('YYYY/MM/DD');
|
|
}else if(mac.expiry_type == 'ONEYR'){
|
|
after1year = moment().add(1, 'year');
|
|
formatedDate = after1year.format('YYYY/MM/DD');
|
|
}else if(mac.expiry_type == 'LYFT'){
|
|
after20year = moment().add(20, 'year');
|
|
formatedDate = after20year.format('YYYY/MM/DD');
|
|
}
|
|
|
|
const info={
|
|
mac_id: mac_id,
|
|
company_name: company_name,
|
|
activation_date: today,
|
|
expiry_date : formatedDate,
|
|
isActive : true,
|
|
app_name : app_name
|
|
}
|
|
|
|
const macUpdate = await MacModel.update(info, {where:{id: mac.id}});
|
|
|
|
|
|
if (macUpdate) {
|
|
logMsg.info({status:200,message:"Your license has been successfully activated. ", data: { expiry_date : cryptr.encrypt(formatedDate), expiry_type : cryptr.encrypt(mac.expiry_type) }});
|
|
res.send({status:200,message:"Your license has been successfully activated. ", data: { expiry_date : cryptr.encrypt(formatedDate), expiry_type : cryptr.encrypt(mac.expiry_type) }})
|
|
}
|
|
|
|
} else {
|
|
|
|
if (mac.isActive == true && mac.app_version.charAt(0) == app_version.charAt(0) && mac_id == mac.mac_id )
|
|
{
|
|
today = moment().format('YYYY/MM/DD');
|
|
formatedDate = moment(mac.expiry_date).format('YYYY/MM/DD');
|
|
|
|
if(formatedDate > today){
|
|
|
|
const info={
|
|
mac_id: mac_id,
|
|
company_name: company_name,
|
|
activation_date: today,
|
|
expiry_date : formatedDate,
|
|
isActive : true,
|
|
app_name : app_name
|
|
}
|
|
const macUpdate = await MacModel.update(info, {where:{id: mac.id}});
|
|
|
|
if (macUpdate) {
|
|
logMsg.info({status:200,message:"Your license has been successfully activated.", data: { expiry_date : cryptr.encrypt(formatedDate), expiry_type : cryptr.encrypt(mac.expiry_type) }});
|
|
res.send({status:200,message:" Your license has been successfully activated.", data: { expiry_date : cryptr.encrypt(formatedDate), expiry_type : cryptr.encrypt(mac.expiry_type) }})
|
|
}
|
|
|
|
}else{
|
|
|
|
logMsg.info({status:404,message:"Please check the license key and try again. For further assistance, contact our support team.", data: "No Data"});
|
|
res.send({status:404,message:"Please check the license key and try again. For further assistance, contact our support team.", data: "No Data"})
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
logMsg.info({status:404,message:"The license key you provided is not valid. Please check the license key and try again. For further assistance, contact our support team.", data: "No Data"});
|
|
res.send({status:404,message:"The license key you provided is not valid. Please check the license key and try again. For further assistance, contact our support team.", data: "No Data"})
|
|
|
|
}
|
|
|
|
}
|
|
}else{
|
|
logMsg.info({status:404,message:"The license key you provided is not valid. Please check the license key and try again. For further assistance, contact our support team.", data: "No Data"});
|
|
res.send({status:404,message:"The license key you provided is not valid. Please check the license key and try again. For further assistance, contact our support team.", data: "No Data"})
|
|
}
|
|
} catch (error) {
|
|
logMsg.info("addMacValue ->Internal Server Error!", error);
|
|
console.error(error);
|
|
res.status(404).send('addMacValue ->Internal Server Error!', error);
|
|
}
|
|
}
|
|
|
|
module.exports.createLicencyKey = async (req, res, next) => {
|
|
try {
|
|
const { licency_key } = req.body;
|
|
|
|
// Check if license key already exists and is active
|
|
const mac = await MacModel.findOne({
|
|
where: { licency_key },
|
|
raw: true,
|
|
});
|
|
|
|
if (mac) {
|
|
logMsg.info({
|
|
status: 409,
|
|
message: "licency_key already exists and is active.",
|
|
data: null,
|
|
});
|
|
return res.status(409).send({
|
|
status: 409,
|
|
message: "licency_key already exists and is active.",
|
|
data: null,
|
|
});
|
|
}
|
|
|
|
// Create new license entry
|
|
const info = {
|
|
licency_key: licency_key,
|
|
expiry_type: 'TRAIL',
|
|
app_version: '1.0.0',
|
|
isActive: false,
|
|
};
|
|
|
|
const created = await MacModel.create(info);
|
|
|
|
logMsg.info({
|
|
status: 201,
|
|
message: "licency_key created.",
|
|
data: created,
|
|
});
|
|
|
|
return res.status(201).send({
|
|
status: 201,
|
|
message: "licency_key created.",
|
|
data: created,
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error(error);
|
|
logMsg.error({ message: 'Licency key creation failed', error: error.message });
|
|
|
|
return res.status(500).send({
|
|
status: 500,
|
|
message: 'Licency key creation failed!',
|
|
error: error.message,
|
|
});
|
|
}
|
|
};
|
|
|
|
|
|
module.exports.encryptLicenseKey= async(req, res, next)=>{
|
|
|
|
|
|
|
|
const cryptr = new Cryptr('myTotallySecretKey', { saltLength: 5 });
|
|
|
|
//const encryptedString = cryptr.encrypt('Gowtham');
|
|
const decryptedString = cryptr.decrypt('cf1496215367f9713a67c81c6c22c5409f46ea5b0be610cf60f5b8717aac8ac1691c05e6d68b2178a9e4ce1f');
|
|
res.send({decryptedString:decryptedString});
|
|
|
|
};
|
|
|
|
module.exports.decryptLicenseKey= async(req, res, next)=>{
|
|
|
|
const algorithm = 'aes-256-cbc';
|
|
const key = crypto.randomBytes(32);
|
|
const iv = crypto.randomBytes(16);
|
|
|
|
const decipher = crypto.createDecipheriv(algorithm, '51 70 c0 16 42 ef 9a 13 2e e6 9d d0 b0 02 cf 1e c2 31 dd 04 7e 77 cc 5d aa ea 72 5a cb c8 4a b9', '7e 4e c7 f6 a3 27 ac 0f 4f c9 d8 19 0a 36 de f0');
|
|
|
|
let decrypted = decipher.update(req.query.encrypted_value, 'hex', 'utf8');
|
|
decrypted += decipher.final('utf8');
|
|
res.send({decrypted:decrypted});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// var expiryDate = new Date(mac.expiry_date);
|
|
|
|
// //Check expired or not!
|
|
// if (expiryDate > new Date()) {
|
|
// const info={
|
|
// licency_key: key,
|
|
|
|
// company_name: company_name,
|
|
// activation_date:today,
|
|
// expiry_date:nextFiscalYearEndDate,
|
|
// }
|
|
// logMsg.info({status:404,message:" Your Mac is Already Activated!", data: info});
|
|
// res.send({status:404,message:" Your Mac is Already Activated!", data: info});
|
|
|
|
// } else {
|
|
// logMsg.info({status:404,message:" Product Expired!", data: "No Data!"});
|
|
// res.send({status:404,message:" Product Expired!", data: "No Data!"});
|
|
// }
|
|
|
|
|
|
// const macId = JSON.parse(mac.mac_id);
|
|
// var DBData= [];
|
|
// macId.forEach(function(macid) {
|
|
// DBData.push(macid.mac)
|
|
// });
|
|
// var BodyData = [];
|
|
// var BodyValue = req.body.mac_id;
|
|
// BodyValue.forEach(function(body) {
|
|
// BodyData.push(body.mac)
|
|
// });
|
|
|
|
// function hasCommonElement(arr1, arr2) {
|
|
// for (let element of arr1) {
|
|
// if (arr2.includes(element)) {
|
|
// return true;
|
|
// }
|
|
// }
|
|
// return false;
|
|
// }
|
|
|
|
// const isMatch = hasCommonElement(DBData, BodyData);
|
|
|
|
// if (isMatch) {
|
|
// const info={
|
|
// licency_key:req.body.key,
|
|
// mail: req.body.mail,
|
|
// company_name:req.body.company_name,
|
|
// activation_date:today,
|
|
// expiry_date:nextFiscalYearEndDate,
|
|
// }
|
|
// logMsg.info({status:200,message:" Your Mac is Already Activated!", data: info});
|
|
// res.send({status:200,message:" Your Mac is Already Activated!", data: info});
|
|
// } else {
|
|
// logMsg.info({status:404,message:" Your Mac is Not Match!", data: "No Data!"});
|
|
// res.send({status:404,message:" Your Mac is Not Match!", data: "No Data!"});
|
|
// }
|