Last active
December 26, 2023 02:57
-
-
Save chenbojian/959f9bad080c5af2c755f2771510437a to your computer and use it in GitHub Desktop.
simple nodejs http proxy implementation
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
# https://imququ.com/post/web-proxy.html | |
var http = require('http'); | |
var net = require('net'); | |
var url = require('url'); | |
function connect(cReq, cSock) { | |
var u = url.parse('http://' + cReq.url); | |
var pSock = net.connect(u.port, u.hostname, function() { | |
cSock.write('HTTP/1.1 200 Connection Established\r\n\r\n'); | |
pSock.pipe(cSock); | |
}).on('error', function(e) { | |
cSock.end(); | |
}); | |
cSock.pipe(pSock); | |
} | |
http.createServer().on('connect', connect).listen(8888, '0.0.0.0'); |
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
# https://imququ.com/post/web-proxy.html | |
var http = require('http'); | |
var net = require('net'); | |
var url = require('url'); | |
function request(cReq, cRes) { | |
var u = url.parse(cReq.url); | |
var options = { | |
hostname : u.hostname, | |
port : u.port || 80, | |
path : u.path, | |
method : cReq.method, | |
headers : cReq.headers | |
}; | |
var pReq = http.request(options, function(pRes) { | |
cRes.writeHead(pRes.statusCode, pRes.headers); | |
pRes.pipe(cRes); | |
}).on('error', function(e) { | |
cRes.end(); | |
}); | |
cReq.pipe(pReq); | |
} | |
http.createServer().on('request', request).listen(8888, '0.0.0.0'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment