我一直在尝试为HTTP和HTTPS请求创建代理服务器。 我从here开始关注此代码段。
但我支持公司代理,所以我必须稍微修改代码。对于HTTP请求,我相应更改了host
,port
和path
。
但我一直坚持配置HTTPS请求。
// httpUserRequest handles http requests via proxy
var server = http.createServer( httpUserRequest ).listen(port);
// add handler for HTTPS (which issues a CONNECT to the proxy)
server.addListener(
'connect',
function ( request, socketRequest, bodyhead ) {
var url = request['url'];
var httpVersion = request['httpVersion'];
var hostport = getHostPortFromString( url, 443 );
if ( debugging ){
console.log( ' = will connect to %s:%s', hostport[0], hostport[1] );
// console.log( request);
console.log(hostport[0]+':443');
}
// set up TCP connection
var proxySocket = new net.Socket();
proxySocket.connect(parseInt(hostport[1]), hostport[0],
function () {
if (debugging)
console.log( ' < connected to %s/%s', hostport[0], hostport[1] );
if ( debugging )
console.log( ' > writing head of length %d', bodyhead.length );
proxySocket.write( bodyhead );
// tell the caller the connection was successfully established
socketRequest.write( "HTTP/" + httpVersion + " 200 Connection established\r\n\r\n" );
}
);
proxySocket.on(
'data',
function ( chunk ) {
if ( debugging )
console.log( ' < data length = %d', chunk.length );
socketRequest.write( chunk );
}
);
proxySocket.on(
'end',
function () {
if ( debugging )
console.log( ' < end' );
socketRequest.end();
}
);
socketRequest.on(
'data',
function ( chunk ) {
if ( debugging )
console.log( ' > data length = %d', chunk.length );
proxySocket.write( chunk );
}
);
socketRequest.on(
'end',
function () {
if ( debugging )
console.log( ' > end' );
proxySocket.end();
}
);
proxySocket.on(
'error',
function ( err ) {
socketRequest.write( "HTTP/" + httpVersion + " 500 Connection error\r\n\r\n" );
if ( debugging ) {
console.log( ' < ERR: %s', err );
}
socketRequest.end();
}
);
socketRequest.on(
'error',
function ( err ) {
if ( debugging ) {
console.log( ' > ERR: %s', err );
}
proxySocket.end();
}
);
}
); // HTTPS connect listener
我不断获得connect ECONNREFUSED
我尝试了几件事:
request
得到了回复,但我不知道如何将回复转发给客户所以我的问题是:
request
获得回复,我该如何将其转发给客户?由于