Skip to content

Instantly share code, notes, and snippets.

@ishan-marikar
Last active March 1, 2017 15:02
Show Gist options
  • Save ishan-marikar/691c4c01c764335e232e91074c4478ae to your computer and use it in GitHub Desktop.
Save ishan-marikar/691c4c01c764335e232e91074c4478ae to your computer and use it in GitHub Desktop.
An SSH/HTTPS proxy that listens on port 443 and proxies traffic depending on what kind of data it is.
const net = require('net');
const LISTENING_PORT = 443;
const HTTPS_PORT = 4443;
const SSH_PORT = 22;
var proxySwitch = function(connection) {
connection.once('data', function(buffer) {
// A TLS handshake record starts with byte 22.
if (buffer[0] === 22) {
// 'tis an SSL connection
var httpsProxy = net.createConnection(HTTPS_PORT, function() {
httpsProxy.write(buffer);
connection.pipe(httpsProxy).pipe(connection);
});
errorHandlers(proxy, connection);
} else {
// 'tis an SSH connection
var sshProxy = net.createConnection(SSH_PORT, function() {
connection.pipe(sshProxy).pipe(connection);
});
errorHandlers(proxy, connection);
}
});
};
var terminateConnections = function(to, from) {
to.on('error', function(error) {
if (error.code != 'ECONNRESET' && error.code !== 'EPIPE')
console.log('Something weird happened:', error.code);
to.destroy();
from.destroy();
});
};
var errorHandlers = function(proxy, connection) {
terminateConnections(proxy, connection);
terminateConnections(connection, proxy);
};
net.createServer(proxySwitch).listen(LISTENING_PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment