我一直在尝试做一个小项目。我在其中加密了文件夹中的一些文件以进行分发。在运行时解密模块时,我遇到了一个问题。
var express = require('express');
var router = new express.Router()
var macaddress = require('macaddress');
var path = require('path');
var controller = require(path.resolve(__dirname , 'EncryptedScriptController'));
router.use("*" , controller.validate);
module.exports = router ;
exports.validate = validate;
function validate(req, res, next){
if (req.headers.host !== 'localhost:3000') {
console.log('invalid Domain Name');
process.exit(1);
return;
}
next();
}
(function() {
var macaddress = require('macaddress');
macaddress.one(function (err, mac) {
if(mac !== '72:f0:87:bd:cb:d1'){
console.log("MAC Address Does not match");
process.exit(1);
}
});
})()
codeencryptor.js
/**
* Created by ishanarora on 30/03/19.
*/
const simpleencrypter = require('simple-encryptor')(process.env.APP_SECRET);
const fs = require('fs');
const path = require('path');
var walk = function(dir, dirname, done) {
var results = {is_directory: 1};
fs.readdir(dir, function (err, list) {
if (err) return done(err);
var i = 0;
(function next() {
var file = list[i++];
if (!file) return done(null, results);
var file2 = dir + '/' + file;
fs.stat(file2, function (err, stat) {
if (stat && stat.isDirectory()) {
walk(file2, file, function (err, res) {
results[file] = res;
next();
});
} else {
var data = fs.readFileSync(file2);
if (!results.files) {
results.files = {}
}
results.files[file] = {is_directory: 0, code: simpleencrypter.encrypt(data.toString())}
next();
}
});
})();
});
};
walk(__dirname + `/dirname`, 'dirname', function(err, data) {
var output = {};
output['dirname'] = data;
fs.writeFile('./routes/encryptedScript.js', JSON.stringify(output), function (err, data){
console.log(data);
});
});
这是APP.js中的解密
var fs = require('fs');
var simpleDecryptor = require('simple-encryptor')(process.env.APP_SECRET);
var nodeEval = require('node-eval');
var path = require('path');
var encrypted = fs.readFileSync(path.join(__dirname + '/routes/encryptedScript.js'));
var dictionary = JSON.parse(encrypted.toString());
function EvalDict(dict, str) {
var result = []
var keys = Object.keys(dict);
for (var i = 0; i < keys.length; i++) {
if (dict[keys[i]].is_directory) {
str += `/${keys[i]}`;
var files = Object.keys(dict[keys[i]].files);
for (var j = 0; j < files.length; j++) {
if (dict[keys[i]].files[files[j]].is_directory) {
var res = EvalDict(dict[keys[i]].files[files[j]], str);
result.append(res);
return result;
} else {
var fileCode = simpleDecryptor.decrypt(dict[keys[i]].files[files[j]].code)
var fileName = str + "/" + files[j]
var dictL = {key : fileName, code : fileCode};
result.push(dictL);
}
}
}
return result;
}
}
var output = EvalDict(dictionary, './routes');
var requireFolder = {}
for(var elem of output) {
var moduleName = elem.key;
console.log(moduleName);
requireFolder[moduleName] = nodeEval(elem.code, elem.key.toString());
}
I am getting this error.
CALLING INITIALIZE POOL
./routes/dirname/EncryptedScriptController.js
./routes/dirname/index.js
module.js:557
throw err;
^
Error: Cannot find module '/Users/ishanarora/Documents/Bulbul/CodeSettings/codeObs/routes/dirname/EncryptedScriptController'
at Function.Module._resolveFilename (module.js:555:15)
at Function.Module._load (module.js:482:25)
at Module.require (module.js:604:17)
at sandbox.require.id (/Users/ishanarora/Documents/Bulbul/CodeSettings/codeObs/node_modules/node-eval/index.js:61:48)
at Object.<anonymous> (/Users/ishanarora/Documents/Bulbul/CodeSettings/codeObs/routes/dirname/index.js:9:18)
at _commonjsEval (/Users/ishanarora/Documents/Bulbul/CodeSettings/codeObs/node_modules/node-eval/index.js:80:21)
at module.exports (/Users/ishanarora/Documents/Bulbul/CodeSettings/codeObs/node_modules/node-eval/index.js:36:19)
at Object.<anonymous> (/Users/ishanarora/Documents/Bulbul/CodeSettings/codeObs/app.js:57:33)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:190:16)
at bootstrap_node.js:662:3
我的代码在此目录中。