Created
August 10, 2022 19:28
-
-
Save FlyInk13/7aac54156a73ccf4598d45d5baa300ab to your computer and use it in GitHub Desktop.
simple node js proxy
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
/* jshint esversion: 6 */ | |
let http = require('http'), | |
net = require('net'), | |
url = require('url'), | |
settings = { | |
port: 8081, | |
username: "username", | |
password: "password" | |
}; | |
var server = http.createServer(function(request, response) { | |
switch (Auth(request, request.connection.remoteAddress)) { | |
case 407: | |
console.log("http: ", request.connection.remoteAddress, "\t", request.url); | |
response.writeHead(407, { | |
"Proxy-Authenticate": "Basic realm=\"qwerty\"" | |
}); | |
return response.end(); | |
case 401: | |
console.log("http: ", request.connection.remoteAddress, "\t", request.url); | |
response.writeHead(401, { | |
'Content-Type': 'text/html; charset=utf-8' | |
}); | |
return response.end("Unauthorized"); | |
} | |
var ph = url.parse(request.url); | |
var proxyRequest = http.request({ | |
port: ph.port, | |
hostname: ph.hostname, | |
method: request.method, | |
path: ph.path, | |
headers: request.headers | |
}); | |
proxyRequest.on('response', function(proxyResponse) { | |
proxyResponse.on('data', (chunk) => response.write(chunk, 'binary')); | |
proxyResponse.on('end', () => response.end()); | |
proxyResponse.on('error', () => response.end()); | |
response.writeHead(proxyResponse.statusCode, proxyResponse.headers); | |
}); | |
request.on('data', (chunk) => proxyRequest.write(chunk, 'binary')); | |
request.on('end', () => proxyRequest.end()); | |
request.on('error', () => proxyRequest.end()); | |
}).on('connect', function(request, socketRequest, head) { | |
if (Auth(request, request.connection.remoteAddress)) { | |
console.log("https:", request.connection.remoteAddress, "\t", request.url); | |
socketRequest.end( | |
"HTTP/" + request.httpVersion + " 407 Proxy Authentication Required\r\n" + | |
"Proxy-Authenticate: Basic realm=\"qwertys\"\r\n" + | |
"\r\n" | |
); | |
return; | |
} | |
var ph = url.parse('http://' + request.url); | |
var socket = net.connect(ph.port, ph.hostname, function() { | |
socket.write(head); | |
socketRequest.write("HTTP/" + request.httpVersion + " 200 Connection established\r\n\r\n"); | |
}); | |
socket.on('data', (chunk) => socketRequest.write(chunk)); | |
socket.on('end', () => socketRequest.end()); | |
socket.on('error', function() { | |
socketRequest.write("HTTP/" + request.httpVersion + " 500 Connection error\r\n\r\n"); | |
socketRequest.end(); | |
}); | |
socketRequest.on('data', (chunk) => socket.write(chunk)); | |
socketRequest.on('end', () => socket.end()); | |
socketRequest.on('error', () => socket.end()); | |
}).listen(settings.port); | |
function Auth(request, ip) { | |
// if (ip === 'your ipv6') { | |
// return false; | |
// } | |
if (!request.headers["proxy-authorization"]) { | |
console.log("auth: ", request.connection.remoteAddress, "\t", 407); | |
return 407; | |
} else { | |
var base64 = request.headers["proxy-authorization"].replace(/^.+?\s/, ""), | |
auth = new Buffer(base64, 'base64').toString().split(/:/); | |
if (auth[0] !== settings.username || auth[1] !== settings.password) { | |
console.log("auth>", request.connection.remoteAddress, "\t", 401, auth[0], auth[1]); | |
return 401; | |
} | |
} | |
} | |
process.on("uncaughtException", function(e) { // Игнорирование ошибок | |
console.error("uncaughtException", e.stack); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment