Created
December 3, 2022 02:51
-
-
Save InvoxiPlayGames/5af3d80b20220121e607476515819b24 to your computer and use it in GitHub Desktop.
tcp-to-tls-proxy.js - A simple NodeJS proxy to forward a given unencrypted TCP connection to a TLS encrypted server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
tcp-to-tls-proxy.js - A simple NodeJS proxy to forward a given unencrypted TCP connection to a TLS encrypted server. | |
by InvoxiPlayGames, hereby released into the public domain. Not that there's much to it to license. | |
I created this as a way to connect to my modern, TLS 1.3-supporting XMPP server from iChat on a | |
PowerBook running OS X Leopard (hence the default ports), but this can be used for anything that | |
accepts a raw TLS connection where a legacy client can only handle an unencrypted connection. | |
Please be safe with this, don't run this over an external network as it *is* reducing security. | |
To use, replace the host and port in tls_settings with your target server, and host_port with | |
the port number you wish to host the proxy on, then run "node tcp-to-tls-proxy.js". No dependencies! | |
*/ | |
const net = require("net"); | |
const tls = require("tls"); | |
var tls_settings = { | |
host: "target-hostname.lit", | |
port: 5223 | |
}; | |
var host_port = 5222; | |
function connection_proxy(conn) { | |
var remote_address = conn.remoteAddress + ':' + conn.remotePort; | |
var client = tls.connect(tls_settings, () => { | |
console.log(`Client at ${remote_address} connected, and proxy established.`); | |
conn.on('data', (data) => { client.write(data) }); | |
client.on('data', (data) => { conn.write(data) }); | |
conn.on('close', () => { console.log(`Client at ${remote_address} has dropped connection.`); client.end() }); | |
client.on('end', () => { console.log(`Server has dropped connection to ${remote_address}.`); conn.end(); }); | |
}); | |
} | |
var server = net.createServer(); | |
server.on('connection', connection_proxy); | |
server.listen(host_port, () => { | |
console.log(`Proxy listening on ${host_port} (target: ${tls_settings.host}:${tls_settings.port})`) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment