Skip to content

Instantly share code, notes, and snippets.

@HeGanjie
Last active Feb 1, 2018
Embed
What would you like to do?
Lite http dynamic proxy in js
// 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