Last active
February 1, 2018 10:32
-
-
Save HeGanjie/41fd69520191881001f1f143fb13c8d9 to your computer and use it in GitHub Desktop.
Lite http dynamic proxy in js
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 | |
// test cmd: | |
// curl --proxytunnel -x http://localhost:8000 --proxy-basic --proxy-user a:b httpbin.org/get | |
var http = require('http') | |
var net = require('net') | |
var url = require('url') | |
function connect(cReq, cSock) { | |
let u = url.parse('http://' + cReq.url) | |
console.log('has one connect: ', cReq.url, '\n', cReq.headers) | |
let auth = _.chain(cReq.headers) | |
.get('proxy-authorization') | |
.thru(str => { | |
let sp = (str || '').split(' ') | |
let authInfo = sp[1] && Buffer.from(sp[1], 'base64').toString() || '' | |
let [userName, pwd] = authInfo.split(':') | |
return userName === 'admin' && pwd === 'admin' | |
}) | |
.value() | |
if (!auth) { | |
cSock.end() | |
return | |
} | |
let pSock = net.connect(u.port, u.hostname, function() { | |
cSock.write('HTTP/1.0 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(8000, '0.0.0.0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment