Help, sometimes the end event does not fire on the client in this proxy example

40 views
Skip to first unread message

auro tripathy

unread,
Jun 2, 2014, 2:42:17 PM6/2/14
to
Hi:

Here's the entire code for client and server. In the equivalent http/tcp proxy, it always fires. Any hints will be greatly appreciated.

Client

var httpp = require('httpp');
var net = require('udt');

// make a request to a tunneling proxy
var options = {
    port: 1337,
    hostname: '127.0.0.1',
    method: 'CONNECT',
    path: 'localhost:80'
};

var req = httpp.request(options);
console.log('Connecting to '+ options.path + ' via proxy');
req.end();

req.on('connect', function(res, socket, head) {
    console.log('got connected!');

//    make a request over an HTTP tunnel
    socket.write('GET /test.html HTTP/1.1\r\n' +
                 'Host: localhost:80\r\n' +
                 'Connection: close\r\n' +
                 '\r\n');
    socket.on('data', function(chunk) {
console.log(chunk.toString());
    });
    socket.on('end', function() {
    console.log('end of data');
    });

});

Server

var httpp = require('httpp');
var net = require('net');
var url = require('url');

console.log('http/UDT PROXY server...');

// Create an HTTP tunneling proxy
var proxy = httpp.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('okay');
}).listen(1337);

proxy.on('request', function(req, socket, head) {
    console.log('got request...'+req.url);});

proxy.on('connect', function(req, cltSocket, head) {
    // connect to an origin server
    console.log("The URL is "+req.url);
    var srvUrl = url.parse('http://' + req.url);
    var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, function() {
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
'Proxy-agent: Node-Proxy\r\n' +
'\r\n');
srvSocket.write(head);
srvSocket.pipe(cltSocket);
cltSocket.pipe(srvSocket);
    });
});



Reply all
Reply to author
Forward
0 new messages