Skip to content

Commit

Permalink
Add endpoint for auto update via GH hooks. (#17)
Browse files Browse the repository at this point in the history
* Add endpoint for auto update via GH hooks.

* use crypto.timingSafeEqual instead of ==

* == -> ===
  • Loading branch information
kdzwinel authored Dec 18, 2020
1 parent dc85051 commit 2d22984
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
10 changes: 10 additions & 0 deletions git.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
30 changes: 22 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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'});
Expand Down

0 comments on commit 2d22984

Please sign in to comment.