diff --git a/git.sh b/git.sh new file mode 100644 index 0000000..1405299 --- /dev/null +++ b/git.sh @@ -0,0 +1,10 @@ +#/bin/sh + +# Fetch the newest code +git fetch origin gh-pages + +# Hard reset +git reset --hard origin/gh-pages + +# Force pull +git pull origin gh-pages --force \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 3d4460d..446ce7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "fork-privacy-test-pages", + "name": "privacy-test-pages", "version": "1.0.0", "lockfileVersion": 1, "requires": true, @@ -235,6 +235,11 @@ "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" }, + "node-cmd": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/node-cmd/-/node-cmd-4.0.0.tgz", + "integrity": "sha512-3OHy8KI8MuwADyugQRZBsaqe3c0r3yxQSoLsDBVk7vAjPmfG01512MPBQjfmBJxrH+2qURbiBf/ZyoimrhdA6A==" + }, "on-finished": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", diff --git a/package.json b/package.json index 7565132..92d0a5f 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "homepage": "https://github.com/duckduckgo/privacy-test-pages#readme", "dependencies": { "express": "^4.17.1", + "node-cmd": "^4.0.0", "ws": "^7.4.0" } } diff --git a/server.js b/server.js index 7c0eb3d..20d94b9 100644 --- a/server.js +++ b/server.js @@ -3,6 +3,8 @@ const ws = require('ws'); const app = express(); const port = process.env.PORT || 3000; const url = require('url'); +const cmd = require('node-cmd'); +const crypto = require('crypto'); function fullUrl(req) { return url.format({ @@ -35,14 +37,26 @@ app.use(express.static('.', { } })); -// // endpoint for updating the app -// app.post('/git', (req, res) => { -// if (req.headers['x-github-event'] == "push") { -// /* Here will be our updating code */ -// } - -// return res.sendStatus(200); -// }); +// endpoint for updating the app (https://support.glitch.com/t/tutorial-how-to-auto-update-your-project-with-github/8124) +app.post('/git', (req, res) => { + const hmac = crypto.createHmac('sha1', process.env.SECRET); + const sig = 'sha1=' + hmac.update(JSON.stringify(req.body)).digest('hex'); + + if (req.headers['x-github-event'] === 'push' && crypto.timingSafeEqual(sig, req.headers['x-hub-signature'])) { + cmd.run('chmod 777 git.sh'); /* :/ Fix no perms after updating */ + cmd.get('./git.sh', (err, data) => { // Run our script + if (data) console.log(data); + if (err) console.log(err); + }); + cmd.run('refresh'); // Refresh project + + console.log('> [GIT] Updated with origin/gh-pages'); + + return res.sendStatus(200); + } else { + return res.sendStatus(403); + } +}); // dummy websocket server const wss = new ws.Server({server: listener, path: '/block-me/web-socket'});