节点.js代理请求并使用 AES 对其进行加密

Node.js proxy request and encrypt it using AES

本文关键字:加密 AES 代理 js 请求 节点      更新时间:2023-09-26

Express 应用程序中,对将请求代理到另一台服务器并在将响应发送到客户端(将在其中解密)之前对响应进行编码的最简单方法是什么?是否可以使用流来完成所有这些操作?

var request = require('request'),
    http = require('http'),
    crypto = require('crypto'),
    acceptor = http.createServer().listen(8089);
acceptor.on('request', function(r, s) {
    var ciph = crypto.createCipher('aes192', 'mypassword');
    // simple stream object to convert binary to string
    var Transform = require('stream').Transform;
    var BtoStr = new Transform({decodeStrings: false});
    BtoStr._transform = function(chunk, encoding, done) {
       done(null, chunk.toString('base64'));
    };
    // get html from Goog, could be made more dynamic
    request('http://google.com').pipe(ciph).pipe(BtoStr).pipe(s);

    //  try encrypt & decrypt to verify it works, will print cleartext to stdout
    //var decrypt = crypto.createDecipher('aes192', 'mypassword');
    //request('http://google.com').pipe(ciph).pipe(decrypt).pipe(process.stdout);
})