Created
April 26, 2016 17:07
-
-
Save jonathanpmartins/63980506f1282c1c1dc2dbb6f84c1260 to your computer and use it in GitHub Desktop.
Simple Node server to encrypt Chrome 50 Payloads
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 http = require('http'); | |
var encrypt = require('./encrypt'); | |
function processPost(request, response, callback) { | |
var queryData = ""; | |
if(typeof callback !== 'function') return null; | |
if(request.method == 'POST') { | |
request.on('data', function(data) { | |
queryData += data; | |
if(queryData.length > 1e6) { | |
queryData = ""; | |
response.writeHead(413, {'Content-Type': 'text/plain'}).end(); | |
request.connection.destroy(); | |
} | |
}); | |
request.on('end', function() { | |
request.post = JSON.parse(queryData); | |
callback(); | |
}); | |
} else { | |
response.writeHead(405, {'Content-Type': 'text/plain'}); | |
response.end(); | |
} | |
} | |
function ub64(buffer) { | |
return buffer.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, ''); | |
} | |
var payload; | |
http.createServer(function(request, response) { | |
if(request.method == 'POST') { | |
processPost(request, response, function() { | |
payload = encrypt(request.post.data, { | |
endpoint: 'https://gcm-http.googleapis.com/gcm/' + request.post.regid, | |
keys: { | |
p256dh: request.post.p256dh, | |
auth: request.post.auth | |
} | |
}); | |
response.writeHead(200, "OK", {'Content-Type': 'text/plain'}); | |
response.end(JSON.stringify({ | |
body: ub64(payload.ciphertext), | |
salt: ub64(payload.salt), | |
dh: ub64(payload.serverPublicKey) | |
})); | |
}); | |
} else { | |
response.writeHead(200, "OK", {'Content-Type': 'text/plain'}); | |
response.end(); | |
} | |
}).listen(8888); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment