我正在尝试使用 NodeJS、Angular 和 node webkit 创建代理。我让 HTTP 服务器获取了我所有的请求和响应数据。但是, https.createServer 调用没有调用我的请求处理程序。我尝试了一些不同的东西,但似乎没有任何东西可以导致 https 被处理。
App.factory('proxy', ['$rootScope', 'http', 'https', 'fs', 'net', 'urlParser', function($rootScope, http, https, fs, net, urlParser){
'use strict';
var intercept = false;
/*
* Handle requests dependent on protocol
* input: (obj)request, (obj)response, (str) type
*/
var handle_req = function(req, res, type){
console.log('listening for '+type);
/*
* Options used in the http(s).request method
*/
var req_options = {
hostname: req.headers.host,
port: (type === "https" ? 443 : 80),
path: urlParser.getPath(req.url),
method: req.method,
agent: false,
headers: req.headers
};
/*
* Handle requests and all events that may happen with response
*/
var proxy_request = (type === "https" ? https : http).request(req_options, function(response){
res.writeHead(response.statusCode, response.headers);
response.on('data',function(chunk){
if(type === 'https')
res.stdout.write(chunk);
else
res.write(chunk);
});
response.on('end', function(){
$rootScope.$broadcast('Proxy.response', response.headers);
res.end();
});
response.on('close', function(){
response.connection.end();
});
});
/*
* Other functions to listen and handle on the request portion.
*/
req.on('data', function(chunk){
proxy_request.write(chunk, 'binary');
$rootScope.$broadcast('Proxy.request', req.headers);
});
req.on('end', function(){
proxy_request.end();
});
req.on('close', function(){
proxy_request.end();
});
req.on('error', function(){
proxy_request.end();
console.log("Proxy request errored out.");
});
};
/*
* Options for https listening
*/
var https_opts = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
};
/*
* Create HTTPS server
*/
var httpsProxy = https.createServer(https_opts, function(req, res){
handle_req(req, res, "https");
});
/*
* Create HTTP server
*/
var httpProxy = http.createServer(function(req, res){
handle_req(req, res, "http");
});
/*
* Handle upgrade from HTTP to HTTPS
*/
httpProxy.on('upgrade',function(req, socket, upgradeHead){
console.log('upgrading');
var proxy = net.createConnection(8000, 'localhost');
proxy.on('connect', function(){
socket.write("HTTP/1.0 200 Connection Established");
});
proxy.on('data', function(chunk){
socket.write(chunk);
});
socket.on('data', function(chunk){
proxy.write(chunk);
});
proxy.on('end', function(){
socket.end();
});
socket.on('end', function(){
proxy.end()
});
proxy.on('close', function(){
socket.end();
});
socket.on('close', function(){
proxy.end();
});
proxy.on('error', function(){
socket.end();
});
socket.on('error', function(){
proxy.end();
});
});
httpProxy.on('error',function(){
console.log("Error on server");
});
return {
start: function() {
httpProxy.listen(8080);
httpsProxy.listen(8000);
console.log("Proxy listening");
},
stop: function() {
httpProxy.close();
httpsProxy.close();
console.log("Proxy has stopped listening")
},
getResponse: function(){
Proxy.emit('Proxy.request.forward');
},
sendResponse: function(){
Proxy.emit('Proxy.response.forward');
},
toggleIntercept: function(){
intercept = !intercept;
}
}
}]);