Skip to content

Instantly share code, notes, and snippets.

@mrose17
Created July 9, 2013 04:28
Show Gist options
  • Save mrose17/5954723 to your computer and use it in GitHub Desktop.
Save mrose17/5954723 to your computer and use it in GitHub Desktop.
[node.js] does https.createServer(...).on('connect', function(request, socket, head) { ... })... result in a #fail because the socket passed to the connect handler is doing ciphertext not plaintext?
this gist has three files. get the first, test.js and run it with node. when you see:
now point your browser at http://127.0.0.1:8001
do that and open up the error console, you will see:
ws.open
ws.onmessage
hello world.
that is a win.
now kill 'node test' and put the other two files (test.crt and test.key) in the same directory as test.js and run it again with node. when you see:
now point your browser at https://127.0.0.1:8001
do that and in the error console you will see
ws.open
! Compressed bit must be 0 if no negotiated deflate-frame extension
ws.close
which i think is in error.
-----BEGIN CERTIFICATE-----
MIICvjCCAaagAwIBAgIJAPhzjZbbiwEgMA0GCSqGSIb3DQEBBQUAMBQxEjAQBgNV
BAMTCTEyNy4wLjAuMTAeFw0xMzA3MDkwMDAxNDBaFw0yMzA3MDcwMDAxNDBaMBQx
EjAQBgNVBAMTCTEyNy4wLjAuMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
ggEBAOAl4cl/nVs68EP3l/nvGOKRGsQ7RcsugPE7s/AFd/oKna+sV1Z90OaNnml9
lzHaoMnlGiKkQZTGV33VH7vuoybH8kMX0KRl1iODDiNf7qcC+xdIsbWX+oM/4Bee
Gly5jssWKeKvwfAW+4axzHXLE/qJLXXFUxSeY3WFWurR6jOuvDVOJBQiL61ivyIL
rvurX1h8fjYZYns7kLbsL7RbJmo4Y9oqMaLkm0vP3ugAqc/GdafePzeAIRGIaFBD
Rd/vTxKQB6ATLzHWKZ3n9MZMzNjrD3ealtQtBJZQMe/6HbntrWltvMWSrV0G+Sjr
Cgd/F0kFzDz5cQRt28bYA1gvFO0CAwEAAaMTMBEwDwYDVR0RBAgwBocEfwAAATAN
BgkqhkiG9w0BAQUFAAOCAQEAe2Du5Xb72zPRJg/RcjhuiV5IqNEnIA270vDZFjfs
PQQbcLLUgFG5s6uNCucKMhQlmRkMavhD2CvZmO2k1Nnw0zlNreCjmp9aWCtPYdaU
Wj7HbBmQQBQ3JMVj8JnFTnaSK6Hu4oLIt/HiZO8mPkk/DVhTk7UOqNJiI48OjuHf
1vwZ3LGgnEmhYQ5mBB/0ifx0I3Mml07Cx2RXMRH5CGmk7JMb1sKqCTYnZ/QJ3ATS
su3n0Q5Q4fSjV75Y/yjqvMJLWWHImC78GnscLt3cXSnBJQhVWrrJVhUsBCW6pXVz
ejqLonKppxnD+NnuBalHCdfgLaim81GuDeqNjpsPYOCX/w==
-----END CERTIFICATE-----
var fs = require('fs')
, http = require('http')
, https = require('https')
, net = require('net')
, url = require('url')
;
var keyData, crtData;
try { crtData = fs.readFileSync('./test.crt'); keyData = fs.readFileSync('./test.key'); } catch(ex) {}
var secureP = !!keyData;
var localhost = '127.0.0.1';
var localport = 8001;
var listener;
var server;
var service;
if (secureP) {
server = https.createServer({ key: keyData, cert: crtData });
client = https;
} else {
server = http.createServer();
client = http;
}
server.on('request', function(request, response) {/* jshint multistr: false */
console.log('server: request');
response.writeHead(200, { 'content-type' : 'text/html' });
response.end('<html><head><title>websocket test</title><script>var main = function() {\n'
+ ' ws = new WebSocket("ws' + (secureP ? 's' : '') + '://' + localhost + ':' + localport + '");\n'
+ ' ws.onopen = function(event) {\n'
+ ' console.log("ws.open"); ws.send("hello world.");\n'
+ ' };\n'
+ ' ws.onmessage = function(event) {\n'
+ ' console.log("ws.onmessage"); console.log(event.data);\n'
+ ' };\n'
+ ' ws.onclose = function(event) { console.log("ws.onclose"); };\n'
+ ' ws.onerror = function(event) { console.log("ws.onerror"); };\n'
+ '};</script></head><body onload="main();"></body></html>\n');
}).on('connect', function(request, socket, head) {/* jshint unused: false */
console.log('server: connect received');
socket.write('HTTP/1.1 200\r\n\r\n');
socket.on('error', function(err) {
console.log('server: error ' + err.message);
}).on('end', function() {
console.log('server: end');
}).on('close', function(errorP) {
console.log('server: close errorP=' + errorP);
process.exit(0);
});
listener = socket;
console.log('now point your browser at http' + (secureP ? 's' : '') + '://' + localhost + ':' + localport);
}).on('upgrade', function(request, socket, head) {
var h, hello, parts;
console.log('server: upgrade');
parts = url.parse(request.url, true);
hello = request.method + ' ' + parts.path + ' HTTP/' + request.httpVersion + '\r\n';
for (h in request.headers) {
if ((request.headers.hasOwnProperty(h)) && (h !== 'authorization')) hello += h + ': ' + request.headers[h] + '\r\n';
}
hello += '\r\n';
listener.write(hello);
listener.write(head);
socket.pipe(listener).pipe(socket);
}).on('clientError', function(err, socket) {/* jshint unused: false */
console.log('server: clientError ' + err.message);
}).listen(localport, localhost, function() {
console.log('server listening on http' + (secureP ? 's': '') + '//' + localhost + ':' + localport);
client1();
});
var client1 = function() {
client.request({ hostname : localhost
, port : localport
, path : '/'
, method : 'CONNECT'
, ca : crtData
}).on('connect', function(request, socket, head) {/* jshint unused: false */
var echo;
console.log('client1: connect confirmed');
socket.setKeepAlive(true);
socket.on('data', function(data) {
var h;
head = Buffer.concat([ head, data]);
if (!!echo) return;
h = head.toString().replace(localhost + ':' + localport, 'echo.websocket.org:80');
echo = new net.Socket({ allowHalfOpen: true });
echo.on('connect', function() {
console.log('client1: connected to ws://echo.websocket.org');
echo.write(h);
socket.pipe(echo).pipe(socket);
}).on('error', function(err) {
console.log('client1 connecting to ws://echo.websocket.org: error ' + err.message);
}).connect(80, 'echo.websocket.org');
}).on('error', function(err) {
console.log('client1: error ' + err.message);
});
}).on('error', function(err) {
console.log('client1 connecting: error ' + err.message);
}).end();
};
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA4CXhyX+dWzrwQ/eX+e8Y4pEaxDtFyy6A8Tuz8AV3+gqdr6xX
Vn3Q5o2eaX2XMdqgyeUaIqRBlMZXfdUfu+6jJsfyQxfQpGXWI4MOI1/upwL7F0ix
tZf6gz/gF54aXLmOyxYp4q/B8Bb7hrHMdcsT+oktdcVTFJ5jdYVa6tHqM668NU4k
FCIvrWK/Iguu+6tfWHx+Nhli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MPkpKRcuBaEjk+/mNZ9y84o2mSPG/eRoSjJHnMf3QKAHxmWJZpuWLNYYR4di0U
38nYRO0caKzNyda/njzQ7dL0uzWxkp7VAhLfv4JrGA7O6ewK5DQSpSAigio0zNQ+
2rwBAoGAI5Llz4hK3DErmOmXuTyLbJoX5tyV17qBvXykp4U3h4CRQ0i5bHfBraBz
GH5Ho4pICq0fmVt2WR8FouGUPac1Ch3YQkYTmYR79enA8CcI9KiTl+SzZN6NCWCI
91x2ZnmIPC9pbCTqw9fNUUfWBO8hc5oy1LK45js1fRXnrkydJ0o=
-----END RSA PRIVATE KEY-----
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment