Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain what the code does #12

Open
gajus opened this issue Jan 28, 2017 · 1 comment
Open

Explain what the code does #12

gajus opened this issue Jan 28, 2017 · 1 comment

Comments

@gajus
Copy link

gajus commented Jan 28, 2017

Can someone with understanding of https and streams explain me whats going on here?

node-mitm-proxy/proxy.js

Lines 140 to 159 in 864c252

server.addListener('upgrade', function(req, socket, upgradeHead) {
var proxy = net.createConnection(that.options.mitm_port, 'localhost');
proxy.on('connect', function() {
socket.write( "HTTP/1.0 200 Connection established\r\nProxy-agent: Netscape-Proxy/1.1\r\n\r\n");
});
// connect pipes
proxy.on( 'data', function(d) { socket.write(d) });
socket.on('data', function(d) { try { proxy.write(d) } catch(err) {}});
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() });
});

@gajus
Copy link
Author

gajus commented Jan 28, 2017

To start with, it could be written as:

server.addListener('upgrade', function(req, socket, upgradeHead) {
  var proxy = net.createConnection(that.options.mitm_port, 'localhost');

  proxy.on('connect', function() {
    socket.write( "HTTP/1.0 200 Connection established\r\nProxy-agent: Netscape-Proxy/1.1\r\n\r\n");

    proxy.pipe(socket);
    socket.pipe(proxy);
  });
});

or am I missing something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant