commit 51771bb03843f4d39e79ca8beb069d0f3f230897 Author: Ruakij Date: Thu Feb 15 14:47:42 2024 +0100 Initial commit diff --git a/plain-to-tls-proxy.js b/plain-to-tls-proxy.js new file mode 100644 index 0000000..354af94 --- /dev/null +++ b/plain-to-tls-proxy.js @@ -0,0 +1,75 @@ +const net = require('net'); +const tls = require('tls'); +const fs = require('fs'); + +const listenPort = 80; + +const targetHost = 'localhost'; +const targetPort = 443; + +const logFile = fs.createWriteStream('proxy.log', { flags: 'a' }); + +// log with timestamp +function log(message) { + let date = new Date(); + let time = date.toLocaleString(); + + const line = `[${time}] ${message}\n` + + logFile.write(line); + console.log(line); +} + +const server = net.createServer((client) => { + // Log the client connection + let clientAddress = client.remoteAddress + ':' + client.remotePort; + log(`Client connected from ${clientAddress}`); + + // Create a socket to connect to the target host + const options = { + host: targetHost, + port: targetPort, + rejectUnauthorized: false + }; + const target = tls.connect(options, () => { + // Log the target connection + log(`Target connected to ${targetHost}:${targetPort}`); + + // Pipe the data between the client and the target + client.pipe(target).pipe(client); + + // Log the data sent and received + client.on('data', (data) => { + log(`Client sent: ${data.toString('utf8')}`); + }); + target.on('data', (data) => { + log(`Target sent: ${data.toString('utf8')}`); + }); + }); + + // Handle the errors and close events + client.on('error', (err) => { + log(`Client error: ${err.message}`); + client.destroy(); + target.destroy(); + }); + target.on('error', (err) => { + log(`Target error: ${err.message}`); + client.destroy(); + target.destroy(); + }); + client.on('close', () => { + log(`Client disconnected from ${clientAddress}`); + target.destroy(); + }); + target.on('close', () => { + log(`Target disconnected from ${targetHost}:${targetPort}`); + client.destroy(); + }); +}); + + +log(`Start listening..`); +server.listen(listenPort, () => { + log(`Server listening on port ${listenPort}`); +});