Last active
August 27, 2020 11:08
-
-
Save kfox/2931690 to your computer and use it in GitHub Desktop.
Cat Attack! coffeescript and node.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
# to run: | |
# 1. npm install -g coffee-script | |
# 2. npm install http-proxy | |
# 3. coffee kittenproxy.coffee | |
# 4. change browser proxy settings to <thishost>:8080 | |
httpProxy = require("http-proxy") | |
address = process.argv[2] or "0.0.0.0" | |
port = process.argv[3] or 8080 | |
server = httpProxy.createServer((req, res, proxy) -> | |
isImage = false | |
req.headers.accept.split(",").forEach (mimetype) -> | |
isImage = true if mimetype.match(/^image\//)? | |
if isImage | |
res.writeHead 301, | |
Location: "http://placekitten.com/200/200" | |
res.end() | |
else | |
buffer = httpProxy.buffer(req) | |
proxy.proxyRequest req, res, | |
host: req.headers.host | |
port: 80 | |
buffer: buffer | |
) | |
server.proxy.on "end", (req) -> | |
console.log "Connection to " + req.headers.host + | |
" URL: " + req.url | |
server.listen port, address | |
console.log "listening on %s:%d", address, port |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment