Last active
April 4, 2020 20:19
-
-
Save shubham7298/c5c25fc7bea5a011f05afa780646de29 to your computer and use it in GitHub Desktop.
HTTP/HTTPS Proxy server
This file contains 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
const net = require('net'); | |
const server = net.createServer(); | |
const PORT = 9000; | |
// Emitted when an error occurs. The 'close' event will be called directly following this event. | |
server.on('error', (err) => { | |
console.log('Error'); | |
console.log(err); | |
}); | |
// Emitted when the server closes. Note that if connections exist, this event is not emitted until all the connections are ended. | |
server.on('close', () => { | |
console.log('Closing client request'); | |
}); | |
// Emitted when a new connection is made. Socket object, the connection object is available to event handler. Socket is an instance of net.Socket. | |
server.on("connection", (clientListenerSocket) => { | |
// Client connected to proxy | |
let targetPort = 80, targetAddr; | |
let isTLSConnection = false; | |
clientListenerSocket.on('error', err => { | |
console.log('CLIENT TO PROXY ERROR -> ', targetAddr); | |
console.log(err); | |
}); | |
clientListenerSocket.on('close', () => { | |
console.log('Connection closed from client -> ', targetAddr); | |
}); | |
// We will only need the first packet data to get the host details. | |
clientListenerSocket.once('data', (data) => { | |
isTLSConnection = data.toString().indexOf('CONNECT') !== -1; | |
if (isTLSConnection) { | |
// HTTPS | |
targetPort = 443; | |
targetAddr = data.toString().split('CONNECT ')[1].split(' ')[0].split(':')[0]; | |
console.log(targetAddr); | |
} | |
else { | |
// HTTP | |
console.log('*******************', data.toString()) | |
targetAddr = data.toString().split('Host: ')[1].split('\r\n')[0]; | |
} | |
clientListenerSocket.on('data', (chunk) => { | |
// console.log("c2p --->",chunk.toString('utf8')); | |
proxyListenerSocket.write(chunk); | |
}); | |
let proxyListenerSocket = net.createConnection({ | |
host: targetAddr, | |
port: targetPort | |
}); | |
proxyListenerSocket.on("connect", () => { | |
if (isTLSConnection) { | |
//Send Back OK to HTTPS CONNECT Request | |
clientListenerSocket.write('HTTP/1.1 200 OK\r\n\n'); | |
} else { | |
proxyListenerSocket.write(data); | |
} | |
}); | |
proxyListenerSocket.on("data", (chunk) => { | |
// console.log("p2s --->",chunk.toString('utf8')); | |
clientListenerSocket.write(chunk); | |
}) | |
proxyListenerSocket.on("error", (err) => { | |
console.log('PROXY TO SERVER ERROR'); | |
console.log(err); | |
}); | |
proxyListenerSocket.on('end', () => { | |
console.log('Connection end from proxy -> ', targetAddr); | |
}); | |
}); | |
}); | |
server.listen(PORT, () => console.log(`Proxy server running on ${PORT}`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment