-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
153 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
const net = require('net'); | ||
|
||
/** | ||
* | ||
* @param {net.Socket} socket | ||
* @param {String} separator | ||
*/ | ||
function readUntil(socket, separator) { | ||
// This is definitely not the best implementation, | ||
// you can make it more efficient if you have nothing better to do | ||
return new Promise((resolve, reject) => { | ||
const result = []; | ||
function readUntilInner() { | ||
let data = socket.read(1); | ||
while (data !== null) { | ||
result.push(data); | ||
if (data.toString() === separator) { | ||
resolve(Buffer.concat(result).toString()); | ||
return; | ||
} | ||
data = socket.read(1); | ||
} | ||
if (socket.destroyed) { | ||
reject("Can't read line: connection closed"); | ||
return; | ||
} | ||
setTimeout(readUntilInner, 5); | ||
} | ||
readUntilInner(); | ||
}); | ||
} | ||
|
||
module.exports = { readUntil: readUntil }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict'; | ||
|
||
const net = require('net'); | ||
const sockUtil = require('./sock_util'); | ||
|
||
class TransparentProxy { | ||
constructor(proxiedHostname, proxiedPort, proxyHost, proxyPort) { | ||
this.proxiedHostname = proxiedHostname; | ||
this.proxiedPort = proxiedPort; | ||
this.proxyHost = proxyHost; | ||
this.proxyPort = proxyPort; | ||
this.server = net.createServer((connection) => { | ||
this.onConnect(connection); | ||
}); | ||
} | ||
|
||
/** | ||
* | ||
* @param {net.Socket} connection | ||
*/ | ||
async onConnect(connection) { | ||
const upstreamConnection = net.connect(this.proxyPort, this.proxyHost, () => { | ||
this.httpProxyConnect(connection, upstreamConnection); | ||
}); | ||
const onSocketError = (err) => { | ||
console.error(`Socket error: ${err}`); | ||
connection.destroy(); | ||
upstreamConnection.destroy(); | ||
}; | ||
const onSocketClose = () => { | ||
connection.destroy(); | ||
upstreamConnection.destroy(); | ||
}; | ||
connection.on('error', onSocketError); | ||
upstreamConnection.on('error', onSocketError); | ||
connection.on('close', onSocketClose); | ||
upstreamConnection.on('close', onSocketClose); | ||
// Add timeout? | ||
} | ||
|
||
/** | ||
* | ||
* @param {net.Socket} downstream | ||
* @param {net.Socket} upstream | ||
*/ | ||
async httpProxyConnect(downstream, upstream) { | ||
try { | ||
// Add keep-alive header? | ||
upstream.write(`CONNECT ${this.proxiedHostname}:${this.proxiedPort} HTTP/0.9\r\n\r\n`); | ||
const statusLine = await sockUtil.readUntil(upstream, '\n'); | ||
const statusCode = Number.parseInt(statusLine.split(' ')[1]); | ||
if (statusCode < 200 || statusCode > 299) { | ||
throw Error(`Server returned non-200 status code: ${statusCode}`); | ||
} | ||
while ((await sockUtil.readUntil(upstream, '\n')).trim().length !== 0) {} // read headers | ||
downstream.pipe(upstream); | ||
upstream.pipe(downstream); | ||
} catch (error) { | ||
console.error(`Error while connecting to http proxy: ${error}`); | ||
upstream.destroy(); // downstream is destroyed in onSocketClose function | ||
} | ||
} | ||
|
||
run(hostname, port) { | ||
this.server.listen(port, hostname); | ||
} | ||
} | ||
|
||
module.exports = { TransparentProxy: TransparentProxy }; |