diff --git a/src/controllers/index.js b/src/controllers/index.js index 7323ee4..36676ee 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -1,9 +1,11 @@ const apiV1 = require('./apiV1-controller') +const temp = require('./temp-controller') const products = require('./products-controller') const login = require('./login-controller') module.exports = { - apiV1, + apiV1, + temp, products, login } diff --git a/src/controllers/temp-controller.js b/src/controllers/temp-controller.js new file mode 100644 index 0000000..c9b8530 --- /dev/null +++ b/src/controllers/temp-controller.js @@ -0,0 +1,36 @@ +const base32 = require('hi-base32') +const crypto = require('crypto') +const { parseOfflinePreactivationRequest } = require('../lib/generateLicenseFile') + + +function checkRequestString(requestString) { + if (requestString !== null && typeof requestString === 'string') { + if (requestString.length > 0) { + return (requestString) + } + } + throw new Error('Invalid request string') +} + +module.exports = { + + async generateNewLicense(ctx) { + const licenseNum = base32.encode(crypto.randomBytes(15)) + const licenseNumNice = licenseNum.match(/.{4}/g).join('-') + ctx.body = '' + + `License for client:
${licenseNumNice}
` + + 'SQL command to insert license into database with 2 licensed modules ccengine (windows app) and cndata (CN data):
' +
+            `Insert Into License (productId, licenseNum, customerId) Values ('coc', '${licenseNum}', '123456');\n` +
+            `Insert Into LicensedModule (productId, licenseNum, moduleId) Values ('coc', '${licenseNum}', 'ccengine');\n` +
+            `Insert Into LicensedModule (productId, licenseNum, moduleId) Values ('coc', '${licenseNum}', 'cndata');\n` +
+            '
' + + ''; + }, + + async parseOfflineActivationRequest(ctx) { + const { body } = ctx.request + + ctx.body = await parseOfflinePreactivationRequest(checkRequestString(body.requestString)) + } + +} diff --git a/src/lib/generateLicenseFile.js b/src/lib/generateLicenseFile.js index 04ebb42..25e8946 100644 --- a/src/lib/generateLicenseFile.js +++ b/src/lib/generateLicenseFile.js @@ -39,3 +39,34 @@ exports.generateLicenseFile = async function (activationId, productId, systemPar console.log(output.toString('hex')); return output } + +exports.parseOfflinePreactivationRequest = async function (activationRequest) { + const beginTag = "-BEGIN ACTIVATION REQUEST-----" + let beginPos = activationRequest.indexOf(beginTag) + if (beginPos < 0) { + throw "Invalid activation request format" + } + beginPos = beginPos + beginTag.length; + activationRequest = activationRequest.substring(beginPos) + + const endTag = "-----END ACTIVATION REQUEST-" + let endPos = activationRequest.indexOf(endTag) + if (endPos < 0) { + throw "Invalid activation request format" + } + const data = activationRequest.substring(0, endPos) + + const buff = Buffer.from(data, 'base64'); + + const algorithm = 'aes-256-gcm'; + const password = Buffer.from('e73db572349005f1c41979baf8166a0900745119fa096b9c3efbcee11ddd8b88', 'hex'); + const decipher = crypto.createDecipheriv(algorithm, password, buff.subarray(0, 16)) + decipher.setAuthTag(buff.subarray(-16)) + let decrypted = decipher.update(buff.subarray(16, -16)) + decrypted = Buffer.concat([decrypted, decipher.final()]); + console.log(decrypted) + const inflateRaw = denodeify(zlib.inflateRaw) + const decompressed = await inflateRaw(decrypted); + console.log(JSON.stringify(JSON.parse(decompressed.toString()), null, 4)) + return JSON.parse(decompressed.toString('utf-8')); +} diff --git a/src/routes/index.js b/src/routes/index.js index 2759150..35d5c65 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -5,6 +5,7 @@ const backend = new Router() const apiV1 = require('./apiV1-router') api.use(apiV1) +const temp = require('./temp-router') const products = require('./products-router') const login = require('./login-router') @@ -13,21 +14,7 @@ backend.use(products) backend.use(login) router.use('/api', api.routes()) +router.use('/temp', temp) router.use('/backend', backend.routes()) -const base32 = require('hi-base32') -const crypto = require('crypto') -router.get('/temp/generate-new-license', async function(ctx) { - const licenseNum = base32.encode(crypto.randomBytes(15)) - const licenseNumNice = licenseNum.match(/.{4}/g).join('-') - ctx.body = '' + - `License for client:
${licenseNumNice}
` + - 'SQL command to insert license into database with 2 licensed modules ccengine (windows app) and cndata (CN data):
' +
-               `Insert Into License (productId, licenseNum, customerId) Values ('coc', '${licenseNum}', '123456');\n` +
-               `Insert Into LicensedModule (productId, licenseNum, moduleId) Values ('coc', '${licenseNum}', 'ccengine');\n` +
-               `Insert Into LicensedModule (productId, licenseNum, moduleId) Values ('coc', '${licenseNum}', 'cndata');\n` +
-               '
' + - '' -}); - module.exports = router diff --git a/src/routes/temp-router.js b/src/routes/temp-router.js new file mode 100644 index 0000000..f0f9c9a --- /dev/null +++ b/src/routes/temp-router.js @@ -0,0 +1,8 @@ +const Router = require('koa-router') +const ctrl = require('controllers').temp +const router = new Router() + +router.get('/generate-new-license', ctrl.generateNewLicense); +router.get('/parse-offline-activation-request', ctrl.parseOfflineActivationRequest); + +module.exports = router.routes() diff --git a/test/testOffineActivationRequest.js b/test/testOffineActivationRequest.js new file mode 100644 index 0000000..78ecdd1 --- /dev/null +++ b/test/testOffineActivationRequest.js @@ -0,0 +1,9 @@ +const { parseOfflinePreactivationRequest} = require('../src/lib/generateLicenseFile') + +parseOfflinePreactivationRequest(`-----BEGIN ACTIVATION REQUEST----- +8o4j86i3bfbWkHQ/quJGsjdjeDwkdaKFQb7L/+7nxueACWZ/x3msv8pt8iEbDoSX +i/MDZ5TRgav6Xw/bNl2K4uWvyBsHjnIn8+M3oxCVtxkLbXyOzdJfKrbls616mIUP +FiiQzW1Pa9P7L2ebXAPTDFe4v5OWyGRFfcrFvDMeK570KT4Ol/xwIE/obRF8WhXe +x2vr +-----END ACTIVATION REQUEST-----`).then(() => 0) + .catch((reason) => console.log(reason))