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

Already on GitHub? Sign in to your account

https proxy: socket hang up #36

Closed
johnae opened this Issue Apr 23, 2011 · 16 comments

Comments

Projects
None yet
4 participants

johnae commented Apr 23, 2011

I'm getting this error when using https:

An error has occurred: {"stack":"Error: socket hang up\n at CleartextStream. (http.js:1272:45)\n at CleartextStream.emit (events.js:61:17)\n at Array.0 (tls.js:617:22)\n at EventEmitter._tickCallback (node.js:126:26)","message":"socket hang up"}

When running http it all works just fine.

Someone else has recently had a similar problem and was able to solve it (not sure if this would apply here though):

http://stackoverflow.com/questions/5680543/nodejs-https-request-for-oauth2-token-google-facebook-produce-error-socket-ha

Contributor

Marak commented Apr 23, 2011

Would you mine letting me know which version of node you are running? Also, could you run the test suite and confirm all tests are passing?

johnae commented Apr 23, 2011

Yep I'm running latest nodejs (0.4.7). Running vows test/*-test.js --spec --https fails. 8 specs are broken. Those are:

♢ node-http-proxy/https

When using server created by httpProxy.createServer() with latency and without a valid target server
✗ should receive 500 responseCode
» expected null, got {
stack: 'Error: socket hang up\n at Socket. (http.js:1272:45)\n at Socket.emit (events.js:64:17)\n at Array.2 (net.js:829:12)\n at EventEmitter._tickCallback (node.js:126:26)',
arguments: undefined,
type: undefined,
message: 'socket hang up'
}

When using server created by httpProxy.createServer() with no latency and a valid target server
✗ should receive 'hello localhost'
» expected null, got {
stack: 'Error: socket hang up\n at CleartextStream. (http.js:1272:45)\n at CleartextStream.emit (events.js:61:17)\n at Array.3 (tls.js:617:22)\n at EventEmitter._tickCallback (node.js:126:26)',
arguments: undefined,
type: undefined,
message: 'socket hang up'
}

♢ node-http-proxy/proxy-table/https

When using server created by httpProxy.createServer() when passed a routing table and routing by Hostname an incoming request to unknown.com
✗ should receive 404 responseCode
» expected null, got {
stack: 'Error: socket hang up\n at Socket. (http.js:1272:45)\n at Socket.emit (events.js:64:17)\n at Array.0 (net.js:829:12)\n at EventEmitter._tickCallback (node.js:126:26)',
arguments: undefined,
type: undefined,
message: 'socket hang up'
} // helpers.js:107
When using server created by httpProxy.createServer() when passed a routing table and routing by RegExp an incoming request to unknown.com
✗ should receive 404 responseCode
» expected null, got {
stack: 'Error: socket hang up\n at Socket. (http.js:1272:45)\n at Socket.emit (events.js:64:17)\n at Array.1 (net.js:829:12)\n at EventEmitter._tickCallback (node.js:126:26)',
arguments: undefined,
type: undefined,
message: 'socket hang up'
} // helpers.js:107
When using server created by httpProxy.createServer() when passed a routing file an incoming request to unknown.com
✗ should receive 404 responseCode
» expected null, got {
stack: 'Error: socket hang up\n at Socket. (http.js:1272:45)\n at Socket.emit (events.js:64:17)\n at Array.2 (net.js:829:12)\n at EventEmitter._tickCallback (node.js:126:26)',
arguments: undefined,
type: undefined,
message: 'socket hang up'
} // helpers.js:107

When using server created by httpProxy.createServer() when passed a routing file an incoming request to dynamic.com after the file has been modified
✗ should receive 'hello dynamic.com'
» expected 'hello dynamic.com',
got undefined (==) // proxy-table-test.js:99
When using an instance of ProxyTable combined with HttpProxy directly an incoming request to foo.com
✗ should receive 'hello foo.com'
» expected null, got {
stack: 'Error: socket hang up\n at CleartextStream. (http.js:1272:45)\n at CleartextStream.emit (events.js:61:17)\n at Array.1 (tls.js:617:22)\n at EventEmitter._tickCallback (node.js:126:26)',
arguments: undefined,
type: undefined,
message: 'socket hang up'
} // helpers.js:76
When using an instance of ProxyTable combined with HttpProxy directly an incoming request to bar.com
✗ should receive 'hello bar.com'
» expected null, got {
stack: 'Error: socket hang up\n at CleartextStream. (http.js:1272:45)\n at CleartextStream.emit (events.js:61:17)\n at Array.1 (tls.js:617:22)\n at EventEmitter._tickCallback (node.js:126:26)',
arguments: undefined,
type: undefined,
message: 'socket hang up'
}

Owner

indexzero commented Apr 23, 2011

What version of Node.js are you running on? This looks like joyent/node#849, but ry said the issue is resolved. TLS / SSL stability has been one of the focal points of the node.js core team for the last several months, I will try to triage this issue with them.

Any tcpdump or other low-level network output would help us greatly.

Contributor

Marak commented Apr 23, 2011

He's running 4.7. I'm actually seeing the same errors on my machine when running the https tests. I'm on v0.5.0-pre.

Owner

indexzero commented Apr 23, 2011

Looks like issues 849/670 rearing their ugly head. HTTPS is by no means perfect in node.js core yet. I will re-escalate and try to get some dev time on it. HTTPS >>> Windows support (imo), but who knows if I'll get anyones attention.

Owner

indexzero commented May 17, 2011

This was actually not related to node.js core, but a typo in my test code. Thanks to @olauzon for finding this problem and fixing it in 1ee6bef. Will be pushed out in v0.5.2

@indexzero indexzero closed this May 17, 2011

johnae commented May 17, 2011

Ok but I got the actual original error when doing real proxying NOT when running the tests. I'll try out 0.5.2 and see if that works.

johnae commented May 17, 2011

Just tried it with 0.5.2 and I'm still getting the same error.

Contributor

olauzon commented May 17, 2011

@johnae please let us know how it goes.

@indexzero indexzero reopened this May 17, 2011

Owner

indexzero commented May 17, 2011

@johnae Can you please supply some sample code? Now that we've established that this is not a node.js core issue (since all of the HTTPS tests are passing) we need to try to reproduce it locally to fix it.

johnae commented May 18, 2011

Sure. I put together something that fails in the same way. This doesn't use the certificates I actually use (they are real signed certs). I chose to use certificate_authority - a ruby gem for cert generation. I think I've used it correctly, anyway - the error is the same as what I get. I proxy to a locally running rails app but here I chose to proxy to http://kernel.org instead and I get the same error. If you need better tests, please don't hesitate to ask, though I suspect this is sufficient since it results in the same error:

require 'rubygems' if RUBY_VERSION.to_f < 1.9
unless Gem.available?('certificate_authority')
  `gem install certificate_authority`
end

require 'certificate_authority'

root = CertificateAuthority::Certificate.new
root.subject.common_name= "http://localhost"
root.serial_number.number=1
root.key_material.generate_key
root.signing_entity = true
signing_profile = {"extensions" => {"keyUsage" => {"usage" => ["critical", "keyCertSign"] }} }
root.sign!(signing_profile)

plain_cert = CertificateAuthority::Certificate.new
plain_cert.subject.common_name= "http://localhost"
plain_cert.serial_number.number=1
plain_cert.key_material.generate_key
plain_cert.parent = root # or intermediate
plain_cert.sign!

pem = ''
pem << plain_cert.to_pem << root.to_pem

File.open("#{ENV['HOME']}/.Trash/certs.crt",'w') do |f|
  f.write pem
end

File.open("#{ENV['HOME']}/.Trash/key.key",'w') do |f|
  f.write plain_cert.key_material.private_key.to_s
end

File.open("#{ENV['HOME']}/.Trash/https-proxy.js",'w') do |f|
  f.write %Q(
  var fs = require('fs'),
      https = require('https'),
      httpProxy = require('http-proxy');

  var options = {
    https: {
        key: fs.readFileSync(process.env['HOME']+'/.Trash/key.key','utf8'),
        cert: fs.readFileSync(process.env['HOME']+'/.Trash/certs.crt','utf8')
    }
  };

  httpProxy.createServer(80, 'kernel.org', options).listen(8001);)
end

`node #{ENV['HOME']}/.Trash/https-proxy.js`

johnae commented May 18, 2011

Ohh, above code assumes you're on a Mac which may have been stupid. Just change all refs to ENV['HOME']/.Trash to something that works for you (/tmp perhaps?).

Owner

indexzero commented May 18, 2011

Can you try this with a manually generated certificate or with the certificates in /test/fixtures (https://github.com/nodejitsu/node-http-proxy/tree/master/test/fixtures) in the node-http-proxy repo? I want to try to reduce the surface area of this bug and remove any external dependencies (like Ruby or this certificate_authority gem).

Contributor

olauzon commented May 18, 2011

@johnae in addition to what @indexzero said, could you try with v0.5.3?

Owner

indexzero commented May 19, 2011

@johnae Thanks for bearing with us on this! This was actually a nuance in the API that @olauzon picked up on. You are trying to proxy HTTPS to HTTP which before the version we just released, v0.5.4, was not possible from the httpProxy.createServer() API.

No changes need to be made to your code, but if you're interested in how to proxy HTTPS to HTTPS (as opposed to HTTPS to HTTP) checkout our examples:

https://github.com/nodejitsu/node-http-proxy/blob/master/examples/proxy-https-to-https.js
https://github.com/nodejitsu/node-http-proxy/blob/master/examples/proxy-https-to-http.js

@indexzero indexzero closed this May 19, 2011

johnae commented May 19, 2011

I see, I just assumed it would work. Just tried 0.5.7 and now it seems to work fine. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment