aboutsummaryrefslogtreecommitdiffstats
path: root/index.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2021-05-14 15:43:35 -0400
committerAdam Soltys <[email protected]>2021-05-14 22:17:20 -0400
commit9a56065bbb93a3c7c6dd655b37a2ac825d175f14 (patch)
tree5ca096c4533d062821f1f8e964a1889d52ac5634 /index.js
parent4d2379b801ed0a95e8538a64873a0be503e74672 (diff)
downloadcoinos-server-9a56065bbb93a3c7c6dd655b37a2ac825d175f14.tar.gz
coinos-server-9a56065bbb93a3c7c6dd655b37a2ac825d175f14.zip
build endpoint
Diffstat (limited to 'index.js')
-rw-r--r--index.js42
1 files changed, 41 insertions, 1 deletions
diff --git a/index.js b/index.js
index 7304862f..871e7530 100644
--- a/index.js
+++ b/index.js
@@ -1,10 +1,12 @@
const axios = require("axios");
+const crypto = require("crypto");
const bodyParser = require("body-parser");
const compression = require("compression");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const express = require("express");
const { Op } = require("sequelize");
+const fs = require("fs");
ah = require("express-async-handler");
@@ -80,6 +82,44 @@ if (config.lnurl) {
require("./routes/lnurl");
}
+const sigHeaderName = "X-Hub-Signature-256";
+const sigHashAlg = "sha256";
+function verifyPostData(req, res, next) {
+ try {
+ if (!req.rawBody) {
+ return next("Request body empty");
+ }
+
+ const sig = Buffer.from(req.get(sigHeaderName) || "", "utf8");
+ const hmac = crypto.createHmac(sigHashAlg, config.github);
+ const digest = Buffer.from(
+ sigHashAlg + "=" + hmac.update(req.rawBody).digest("hex"),
+ "utf8"
+ );
+
+ if (sig.length !== digest.length || !crypto.timingSafeEqual(digest, sig)) {
+ return next(
+ `Request body digest (${digest}) did not match ${sigHeaderName} (${sig})`
+ );
+ }
+ } catch (e) {
+ console.log(e.message);
+ }
+
+ return next();
+}
+
+app.post("/build", verifyPostData, (req, res) => {
+ fs.writeFileSync("build.json", req.body.payload);
+
+ const { exec } = require("child_process");
+ exec("bash build.sh", (error, stdout, stderr) => {
+ if (error !== null) {
+ console.log(`exec error: ${error}`);
+ }
+ });
+});
+
app.use((err, req, res, next) => {
const details = {
path: req.path,
@@ -104,6 +144,6 @@ server.listen(config.port, () =>
process.on("SIGINT", process.exit);
-process.on('uncaughtException', function (exception) {
+process.on("uncaughtException", function(exception) {
console.log(exception);
});