Skip to content

Instantly share code, notes, and snippets.

@rlidwka
Created November 8, 2012 20:17
Show Gist options
  • Save rlidwka/4041280 to your computer and use it in GitHub Desktop.
Save rlidwka/4041280 to your computer and use it in GitHub Desktop.
#!/usr/bin/node
var net = require('net');
var fs = require('fs');
var server = require('net').createServer(function(conn) {
var upsock = new net.Socket();
var send = [];
var connected = false;
var connecting = false;
console.log('===== CONNECT =====');
function onconnect() {
for(var i=0;i<send.length;i++) {
upsock.write(send[i]);
}
connected = true;
};
setTimeout(function() {
if (connecting) return;
console.log(conn.remoteAddress, 'connects to', 'ssh');
connecting = true;
upsock.connect(22, 'localhost', onconnect);
}, 2000);
conn.on('data', function(d) {
if (!connecting) {
var utf = d.toString('utf8');
if (utf.substr(0, 100).match(/[A-Za-z]{2,8} \/\S* HTTP\/\d\.\d/)) {
console.log(conn.remoteAddress, 'connects to', 'http');
connecting = true;
upsock.connect(44301, 'localhost', onconnect);
} else {
console.log(conn.remoteAddress, 'connects to', 'https');
connecting = true;
upsock.connect(44302, 'localhost', onconnect);
}
}
if (connected) {
if (upsock.writable) upsock.write(d);
} else {
send.push(d);
}
});
conn.on('close', function() {
upsock.end();
console.log('===== DISCONNECT =====');
});
upsock.on('data', function(d) {
if (conn.writable) conn.write(d);
});
upsock.on('close', function() {
conn.end();
});
});
server.listen(44300);
var http = require('http').createServer(function(req, res) {
console.log('request http:', req.url, req.headers);
res.writeHead(200, {'content-type': 'text/plain'});
res.end('qwertyuiop');
});
http.listen(44301);
var https = require('https').createServer({
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem'),
}, function(req, res) {
console.log('request https:', req.url, req.headers);
res.writeHead(200, {'content-type': 'text/plain'});
res.end('qwertyuiop');
});
https.listen(44302);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment