如何将加密的url传递给节点代理?

时间:2019-05-08 22:37:21

标签: node.js proxy cryptography

我以前从未使用过nodejs,所以我想知道如何将加密的url作为GET参数传递 我在github上找到了这个:https://github.com/MrSwitch/proxy-server

我想将加密的url作为十六进制字符串传递,如下所示: https://example-proxy.com/223AF3584014AB2C3D88

其中223AF3584014AB2C3D88是加密的网址

注意:我使用python加密网址

var url = require('url');
var http = require('http');
var querystring = require('querystring');
var https = require('https');
var merge = require('lodash');

var access_controls_headers = {'Access-Control-Allow-Origin' : "*"};

var request = function(opts, callback){
var req = (opts.protocol === 'https:'? https : http ).request(opts, callback);
req.on('error', callback);
return req;
};


module.exports = proxy;
module.exports.intervene = function(options,callback){callback();};

function proxy(req,res){


// Maintain a collection of URL overriding parameters
var params = {};


// Is the entire path the request?
// i.e. http://proxy-server/http://thirdparty.com/request/to/be/proxied
var resourceURL = req.url.replace(/^\/+/,'');
if( resourceURL && !resourceURL.match(/^[a-z]+:\/\/[a-z\.\-]+/i) ){

    // Otherwise update the default parameters
    params = querystring.parse(resourceURL.replace(/.*\?/,''));

    // Redefine the URL
    resourceURL = params.url;
    delete params.url;
}

if( !resourceURL || !resourceURL.match(/^[a-z]+:\/\/[a-z\.\-]+/i) ){
    error(res);
    return;
}

// Options
var proxyOptions = url.parse(resourceURL);
proxyOptions.headers = {};
merge( proxyOptions.headers, req.headers, querystring.parse( params.headers ) );
proxyOptions.method = params.method || req.method;
proxyOptions.agent = false;

// remove unwanted headers in the request
delete proxyOptions.headers.host;

// Augment the request
// Lets go and see if the value in here matches something which is stored locally
module.exports.intervene( proxyOptions, proxyRequest( req ).bind( null, proxyOptions, res ) );
}


function proxyRequest( req ){

// Buffer the request
// TODO

// Return a function once the authorization has been granted
return function( options, res){

    var connector = request(options, proxyResponse.bind(null,res));
    req.pipe(connector, {end:true});
}
}


function proxyResponse(clientResponse,serverResponse){
var headers = {};
if( serverResponse instanceof Error ){
    return error(clientResponse);
}
merge( headers, serverResponse.headers, access_controls_headers );
clientResponse.writeHeader(serverResponse.statusCode, headers);
serverResponse.pipe(clientResponse, {end:true});
}

function error(res){
res.writeHead(400,access_controls_headers);
res.end();
}

我尝试使用加密货币执行以下操作:

var crypto = require("crypto")

function encrypt(key, data) {
        var cipher = crypto.createCipher('aes-256-cbc', key);
        var crypted = cipher.update(text, 'utf-8', 'hex');
        crypted += cipher.final('hex');

        return crypted;
}

function decrypt(key, data) {
        var decipher = crypto.createDecipher('aes-256-cbc', key);
        var decrypted = decipher.update(data, 'hex', 'utf-8');
        decrypted += decipher.final('utf-8');

        return decrypted;
}

var key = "0123456789abcdef";
var text = "https://www.google.com";
console.log("Original Text: " + text);

var encryptedText = encrypt(key, text);
console.log("Encrypted Text: " + encryptedText);

var decryptedText = decrypt(key, encryptedText);
console.log("Decrypted Text: " + decryptedText);

现在我想使用解密请求以加密值代理,例如: a6e74aebbf7aded6cd1d45bdce6f87a560ba3a0a227e9f41d78955777182d5da 像https://www.proxy-example.com/a6e74aebbf7aded6cd1d45bdce6f87a560ba3a0a227e9f41d78955777182d5da

0 个答案:

没有答案