-
-
Save JimLiu/1618b6e580a6b4783bbe to your computer and use it in GitHub Desktop.
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
"use strict"; | |
var http = require("http"); | |
var url = require("url"); | |
var publicIp; | |
var fakeIp = "220.181.111." + Math.floor(Math.random() * 254 + 1); | |
var server = 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"] = fakeIp; | |
options.headers["Client-IP"] = fakeIp; | |
if (options.path.indexOf(publicIp) > -1) { | |
options.path = options.path.replace(publicIp, fakeIp); | |
} | |
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); | |
}); | |
}); | |
function start(server) { | |
var jsonString = ""; | |
var request = http.request({ | |
hostname: "jsonip.com" | |
}, function(response) { | |
response.on("data", function(data) { | |
jsonString += data; | |
}); | |
response.on("end", function() { | |
publicIp = JSON.parse(jsonString).ip; | |
if (publicIp) { | |
console.log("Public IP: %s", publicIp); | |
server.listen(8080); | |
} else { | |
console.log("CANNOT get public IP!"); | |
} | |
}); | |
}); | |
request.end(); | |
} | |
start(server); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment