Skip to content

Instantly share code, notes, and snippets.

@coolaj86
Last active October 16, 2020 07:11
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save coolaj86/eb6d480edf92f99848b5a2552ef10f90 to your computer and use it in GitHub Desktop.
Save coolaj86/eb6d480edf92f99848b5a2552ef10f90 to your computer and use it in GitHub Desktop.
Raw TCP and TLS to HTTP and HTTPS in node.js
'use strict';
var net = require('net');
var http = require('http');
var http80 = http.createServer(function (req, res) {
res.end('Hello, World!');
});
var tcp80 = net.createServer(function (client) {
// wait just to show that async works
setTimeout(function () {
http80.emit('connection', client);
}, 2000);
});
tcp80.listen(80, function () {
console.log('listening on 80');
});
'use strict';
var net = require('net');
var http = require('http');
var sni = require('sni');
var http80 = http.createServer(function (req, res) {
res.end('Hello, World!');
});
var tcp80 = net.createServer(function (client) {
client.once('data', function (chunk) {
if (sni(chunk)) {
console.log('looks like tls or https');
} else if (/http\/1/i.test(chunk.toString())) {
console.log('looks like http');
} else {
console.log('looks like tcp');
}
//client.push(chunk);
http80.emit('connection', client);
client.pause();
process.nextTick(function () {
client.emit('data', chunk);
client.resume();
});
});
});
tcp80.listen(80, function () {
console.log('listening on 80');
});
'use strict';
var net = require('net');
var tls = require('tls');
var https = require('https');
var sni = require('sni');
var tlsOpts = require('localhost.daplie.com-certificates').merge({});
var https443 = https.createServer(tlsOpts, function (req, res) {
res.end('Happy Encrypted Day!');
});
var tls443 = tls.createServer(tlsOpts, function (socket) {
socket.on('data', function (chunk) {
console.log('chunk', chunk.toString());
});
});
var tcp443 = net.createServer(function (socket) {
socket.once('data', function (chunk) {
var servername = sni(chunk);
console.log('servername:', servername);
if (/^ssh\./.test(servername)) {
tls443.emit('connection', client);
}
else {
https443.emit('connection', client);
}
// this didn't work
//client.emit('data', chunk);
// but this doesn't work either
// put the data back where it came from
socket.push(chunk);
client.emit('readable', chunk);
});
});
tcp443.listen(443, function () {
console.log('listening on 443');
});
'use strict';
var net = require('net');
var tls = require('tls');
var https = require('https');
var tlsOpts = require('localhost.daplie.com-certificates').merge({});
var https443 = https.createServer(tlsOpts, function (req, res) {
res.end('Happy Encrypted Day!');
});
var tls443 = tls.createServer(tlsOpts, function (socket) {
socket.on('data', function (chunk) {
console.log('chunk', chunk.toString());
});
});
var tcp443 = net.createServer(function (socket) {
// either works, but not both at once
//tls443.emit('connection', socket);
https443.emit('connection', socket);
});
tcp443.listen(443, function () {
console.log('listening on 443');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment