Created
September 6, 2013 20:57
-
-
Save zeekay/6469945 to your computer and use it in GitHub Desktop.
Script injection example.
This file contains hidden or 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
var http = require('http'), | |
stream = require('stream'); | |
var config = { | |
host: 'nodejs.org', | |
target_port: 80, | |
listen_port: 8042, | |
script: '<script>alert("hi")</script>', | |
}; | |
console.log('Forwarding Reqs to: ' + config.host) | |
console.log('Listening at Port: ' + config.listen_port) | |
var server = http.createServer(function(frontendReq, frontendRes) { | |
// Stream for injecting scripts | |
var injector = new stream.Transform() | |
// Pass through chunks | |
injector._transform = function(chunk, encoding, callback) { | |
this.push(chunk) | |
callback() | |
} | |
// Before emitting end inject script | |
injector._flush = function(callback) { | |
this.push(config.script) | |
callback() | |
} | |
// Request options | |
frontendReq.headers['accept-encoding'] = '' | |
var opts = { | |
port: config.target_port, | |
host: config.host, | |
method: frontendReq.method, | |
path: frontendReq.url, | |
headers: frontendReq.headers, | |
} | |
// Make request | |
var backendReq = http.request(opts, function(backendRes) { | |
var headers = backendRes.headers, | |
statusCode = backendRes.statusCode, | |
contentType = headers['content-type'], | |
contentLength = headers['content-length']; | |
if (contentType === 'text/html') { | |
// If this is an html page, inject our scripts and update header | |
headers['content-length'] = parseInt(contentLength, 10) + config.script.length | |
frontendRes.writeHead(statusCode, headers) | |
backendRes.pipe(injector).pipe(frontendRes) | |
} else { | |
// ...otherwise just pipe it through normally using original headers | |
frontendRes.writeHead(statusCode, headers) | |
backendRes.pipe(frontendRes) | |
} | |
}) | |
backendReq.end() | |
}) | |
server.listen(config.listen_port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi