-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.coffee
73 lines (63 loc) · 2.78 KB
/
app.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
fs = require("fs")
http = require("http")
url = require("url")
httpProxy = require("http-proxy")
querystring = require("querystring")
multiparty = require("multiparty")
curlify = require("request-as-curl")
# Make hostname filter, port and output params required
argv = require('optimist').usage('Usage: $0 --hostname [hostname filter] --port [port] --output [output file] --format [siege,raw,curl]').demand(['hostname','port','output','format']).argv
# Set up an http proxy
proxyServer = httpProxy.createProxyServer({})
# Create the http server
server = http.createServer (req, res) ->
# Parse the URL
urlObj = url.parse(req.url)
# Create proxy target using the hostname provided in the URL
# Since we just need to log the request it will just go on to the original destination
target = "#{urlObj.protocol}//#{urlObj.host}"
target = "#{target}:#{urlObtwbj.port}" if urlObj.port
# We only care about logging specific requests
appendToLog = if urlObj.host is argv.hostname then true else false
# Handle request differently depending on the output format
switch argv.format
when "siege"
# Just make sure that if there is a multipart form or upload we can handle it properly
form = new multiparty.Form()
form.parse req, (err, fields, files) ->
# The body stream is complete
body = querystring.stringify(fields)
requestString = "#{req.url} #{req.method} #{body}"
# Write to output file
if appendToLog
console.info "REQUEST: #{requestString}"
fs.appendFile(argv.output, "#{requestString}\n", "utf8")
when "raw"
# Keep original complete request body as req.rawBody
body = ""
req.on "data", (chunk) -> body += chunk
req.on "end", ->
requestString = "#{req.url} #{req.method} #{body}"
# Write to output file
if appendToLog
console.info "REQUEST: #{requestString}"
fs.appendFile(argv.output, "#{requestString}\n", "utf8")
when "curl"
# Keep original complete request body as req.rawBody
body = ""
req.on "data", (chunk) -> body += chunk
req.on "end", ->
curlCmd = curlify(req)
# There seems to be an issue with request-as-curl and normal http requests?
# It appends the hostname to the url and we can't really use that.
# TODO Maybe *I* am fucking something up here.
requestString = curlCmd.replace "curl '#{target}", "curl '"
# Write to output file
if appendToLog
console.info "REQUEST: #{requestString}"
fs.appendFile(argv.output, "#{requestString}\n", "utf8")
# Proxy the request to the original destination
proxyServer.web req, res, target: target
# Start the http server to the given port
console.info "Listening on port #{argv.port}"
server.listen argv.port