Created
February 20, 2015 12:57
-
-
Save warpech/7158104e558c0d6349c5 to your computer and use it in GitHub Desktop.
Man in the middle
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
var express = require('express') | |
var bodyParser = require('body-parser'); | |
var chalk = require('chalk'); | |
var app = express() | |
app.use(bodyParser.text()); | |
app.use(function(req, res, next) { | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, spy-type"); | |
next(); | |
}); | |
app.post('/', function(req, res, next) { | |
// Handle the post for this route | |
if(typeof req.body == "object") { | |
req.body = JSON.stringify(req.body) | |
} | |
if(req.headers['spy-type'] == "response") { | |
console.log(chalk.cyan(req.body)); | |
} | |
else { | |
console.log(chalk.green(req.body)); | |
} | |
res.send(); | |
}); | |
app.listen(3000); |
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
{ | |
"name": "spyserver", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"body-parser": "^1.12.0", | |
"chalk": "^0.5.1", | |
"express": "^4.11.2" | |
} | |
} |
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
var spyServer = "http://localhost:3000/"; | |
var _xhrsend = XMLHttpRequest.prototype.send; | |
XMLHttpRequest.prototype.send = function () { | |
spyRequest.apply(this, arguments); | |
_xhrsend.apply(this, arguments); | |
} | |
var _wssend = WebSocket.prototype.send; | |
WebSocket.prototype.send = function () { | |
spyRequest.apply(this, arguments); | |
_wssend.apply(this, arguments); | |
if(!this.spied) { | |
this.addEventListener('message', function(ev) { | |
spyResponse.call(this, ev.data); | |
}) | |
this.spied = true; | |
} | |
} | |
function spyRequest() { | |
var spyXhr = new XMLHttpRequest(); | |
spyXhr.open("POST", spyServer); | |
spyXhr.setRequestHeader("spy-type", "request"); | |
_xhrsend.apply(spyXhr, arguments); | |
} | |
function spyResponse() { | |
var spyXhr = new XMLHttpRequest(); | |
spyXhr.open("POST", spyServer); | |
spyXhr.setRequestHeader("spy-type", "response"); | |
_xhrsend.apply(spyXhr, arguments); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment