Skip to content

Instantly share code, notes, and snippets.

@heqian
Last active December 20, 2018 01:12
Show Gist options
  • Save heqian/a1035f90f86efe8fa1c6 to your computer and use it in GitHub Desktop.
Save heqian/a1035f90f86efe8fa1c6 to your computer and use it in GitHub Desktop.
"use strict";
var http = require("http");
var url = require("url");
var ip = "220.181.111." + Math.floor(Math.random() * 254 + 1);
http.createServer(function(request, response) {
console.log("[%s] %s", request.method, request.url);
var options = url.parse(request.url);
options.method = request.method;
options.headers = request.headers;
options.headers["X-Forwarded-For"] = ip;
options.headers["Client-IP"] = ip;
var proxyRequest = http.request(options, function(proxyResponse) {
response.writeHead(proxyResponse.statusCode, proxyResponse.headers);
proxyResponse.on("data", function(data) {
response.write(data, "binary");
});
proxyResponse.on("end", function() {
response.end();
});
proxyResponse.on("error", function(error) {
console.error(error);
});
});
proxyRequest.on("error", function(error) {
console.error(error);
});
request.on("data", function(data) {
proxyRequest.write(data, "binary");
});
request.on("end", function() {
proxyRequest.end();
});
request.on("error", function(error) {
console.error(error);
});
response.on("error", function(error) {
console.error(error);
});
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment