所以我有一点问题,我无法找到解决问题的方法。我有一个用node.js编写的'webserver',它的一个功能是记录和加密客户端IP。每当我运行它并访问它时,它最终会给我这个错误:
Error: incorrect header check
at Zlib._handle.onerror (zlib.js:370:17)
我真的被卡住了。
这是完整的代码:
var http = require('http');
var fs = require('fs');
var express = require('express');
var app = express();
function process(arg) {
console.log(arg);
}
function EncryptIP() {
var crypto = require('crypto');
var fs = require('fs');
var zlib = require('zlib');
var password = new Buffer('__Password__');
var encrypt = crypto.createCipher('aes-256-cbc', password);
var gzip = zlib.createGzip();
var writeStream = fs.createWriteStream("../../Website/IP Logs/IP Log");
var readStream = fs.createReadStream("../../Website/IP Logs/IP Log");
readStream
.pipe(encrypt)
.pipe(gzip)
.pipe(writeStream)
.on('finish', function () {
console.log('Encryption: Finished Encrypting');
console.log(' ');
});
}
function DecryptIP() {
var crypto = require('crypto');
var fs = require('fs');
var zlib = require('zlib');
var password = new Buffer('__Password__');
var decryptStream = crypto.createDecipher('aes-256-cbc', password);
var gzip = zlib.createGunzip();
var readStream1 = fs.createReadStream('../../Website/IP Logs/IP Log');
console.log('');
readStream1 // reads current files
.pipe(gzip) // uncompresses
.pipe(decryptStream) // decrypts
.on('finish', function () { // finished
console.log('Completed Decrypting');
console.log(' ');
});
}
app.get('/', function(req, res) {
console.log("Webpage Requested");
var Filedata;
var fileReadStream = fs.createReadStream('../Webpages/index.html');
fileReadStream
.on('open', function () {
fileReadStream.pipe(res);
});
fileReadStream
.on('error', function (err) {
res.send(err);
});
var ip = req.connection.remoteAddress;
console.log("Logging their IP Address... IP: " + ip);
fs.readFile("../../Website/IP Logs/IP Log", function(err, data) {
DecryptIP();
if(err) {
process(err);
}
filedata = data + '\n' + ip;
fs.writeFile("../../Website/IP Logs/IP Log", filedata, function(err) {
if(err) {
process(err);
}
console.log("Log: IP saved...");
});
});
console.log("Encrypting their IP Address...");
EncryptIP();
});
app.get('/Admin%20Tools', function(req, res) {
res.send("<style>h2 {color: red;}</style><h2>Access Denied!</h2>");
console.log("Webpage Requested");
var ip = req.connection.remoteAddress;
console.log("Logging their IP Address... IP: " + ip);
fs.readFile("../../Website/IP Logs/IP Log", function(err, data) {
DecryptIP();
if(err) {
process(err);
}
filedata = data + '\n' + ip;
fs.writeFile("../../Website/IP Logs/IP Log", filedata, function(err) {
if(err) {
process(err);
}
console.log("Log: IP saved...");
});
var fileStream = fs.createWriteStream('../../Website/IP Logs/IP Log');
console.log("Encrypting their IP Address...");
EncryptIP();
});
});
app.get('/IP%20Logs', function(req, res) {
res.send("<style>h2 {color: red;}</style><h2>Access Denied!</h2>");
console.log("Webpage Requested");
var ip = req.connection.remoteAddress;
console.log("Logging their IP Address... IP: " + ip);
fs.readFile("../../Website/IP Logs/IP Log", function(err, data) {
DecryptIP();
if(err) {
process(err);
}
filedata = data + '\n' + ip;
fs.writeFile("../../Website/IP Logs/IP Log", filedata, function(err) {
if(err) {
process(err);
}
console.log("Log: IP saved...");
});
console.log("Encrypting their IP Address...");
EncryptIP();
});
});
app.get('/Webpages', function(req, res) {
res.send("<style>h2 {color: red;}</style><h2>Access Denied!</h2>");
console.log("Webpage Requested");
var ip = req.connection.remoteAddress;
console.log("Logging their IP Address... IP: " + ip);
fs.readFile("../../Website/IP Logs/IP Log", function(err, data) {
DecryptIP();
if(err) {
process(err);
}
filedata = data + '\n' + ip;
fs.writeFile("../../Website/IP Logs/IP Log", filedata, function(err) {
if(err) {
process(err);
}
console.log("Log: IP saved...");
});
console.log("Encrypting their IP Address...");
EncryptIP();
});
});
app.get('/Webserver', function(req, res) {
res.send("<style>h2 {color: red;}</style><h2>Access Denied!</h2>");
console.log("Webpage Requested");
console.log("Logging their IP Address...");
var ip = req.connection.remoteAddress;
fs.readFile("../../Website/IP Logs/IP Log", function(err, data) {
DecryptIP();
if(err) {
process(err);
}
filedata = data + '\n' + ip;
fs.writeFile("../../Website/IP Logs/IP Log", filedata, function(err) {
if(err) {
process(err);
}
console.log("Log: IP saved...");
});
console.log("Encrypting their IP Address...");
EncryptIP();
});
});
app.listen(8000);