aboutsummaryrefslogtreecommitdiff
path: root/api/routes
diff options
context:
space:
mode:
authorkaotisk <kaotisk@arching-kaos.org>2025-07-20 19:01:16 +0300
committerkaotisk <kaotisk@arching-kaos.org>2025-07-20 19:01:16 +0300
commit5b31cb2bbc5726d9c6268475cba7e981423f1e4b (patch)
treecab2339ee3a57efccb05ff599cc56dfbb5175473 /api/routes
parentcd464e1508df4f136ce3272231eeb623f47cd578 (diff)
downloadarching-kaos-tools-5b31cb2bbc5726d9c6268475cba7e981423f1e4b.tar.gz
arching-kaos-tools-5b31cb2bbc5726d9c6268475cba7e981423f1e4b.tar.bz2
arching-kaos-tools-5b31cb2bbc5726d9c6268475cba7e981423f1e4b.zip
[api] Added route /ns_get to resolve keys
Diffstat (limited to 'api/routes')
-rw-r--r--api/routes/getAKNSKey/index.js78
1 files changed, 78 insertions, 0 deletions
diff --git a/api/routes/getAKNSKey/index.js b/api/routes/getAKNSKey/index.js
new file mode 100644
index 0000000..c5adff0
--- /dev/null
+++ b/api/routes/getAKNSKey/index.js
@@ -0,0 +1,78 @@
+const { spawn } = require('child_process');
+const fs = require('fs');
+const config = require("../../config.js");
+
+/*
+ * Gets the local latest zblock AKA zlatest
+ *
+ * Returns:
+ * - JSON object
+ * { zlatest: "Qm..." }
+ *
+ */
+function replyIfOkay(key, res)
+{
+ const program = "ak-ns";
+ const command = spawn(program, ["-rk", key]);
+ var buffer = "";
+ command.stdout.on("data", data => {
+ buffer += data;
+ });
+ command.stderr.on("data", data => {
+ console.log(`stderr: ${data}`);
+ });
+ command.on('error', (error) => {
+ console.log(`error: ${error.message}`);
+ });
+ command.on("close", code => {
+ console.log(`child process ${program} exited with code ${code}`);
+ if (code === 0){
+ buffer = buffer.trim()
+ res.writeHead(200, {'Content-Type': 'application/json'});
+ res.end(JSON.stringify({
+ key:`${key}`,
+ resolved:`${buffer}`
+ }));
+ } else {
+ res.writeHead(404, {'Content-Type': 'application/json'});
+ res.end({"error":"unreachable"});
+ }
+ });
+}
+
+function getAKNSKey(req, res)
+{
+ var args = req.url.split("/");
+ var key = args[3];
+ regex= /[a-fA-F0-9]{40}/
+ if (regex.test(key))
+ {
+ key = key.toUpperCase();
+ var path = config.akNSDir+"/"+key;
+ try
+ {
+ if(fs.existsSync(path))
+ {
+ replyIfOkay(key, res)
+ }
+ else
+ {
+ res.writeHead(404, {'Content-Type': 'application/json'});
+ res.end(JSON.stringify({"error":"not found"}));
+ }
+ }
+ catch (error)
+ {
+ res.writeHead(404, {'Content-Type': 'application/json'});
+ res.end(JSON.stringify({"error":error.message}));
+ }
+ }
+ else
+ {
+ res.writeHead(404, {'Content-Type': 'application/json'});
+ res.end(JSON.stringify({error:"No hash"}));
+ }
+}
+
+module.exports = getAKNSKey;
+