aboutsummaryrefslogtreecommitdiff
path: root/api/routes/getRemotePeers/index.js
diff options
context:
space:
mode:
authorkaotisk <kaotisk@arching-kaos.org>2024-12-07 03:29:33 +0200
committerkaotisk <kaotisk@arching-kaos.org>2024-12-07 03:29:33 +0200
commit61e46fdc08170e0c630ff9983b87fde6890e3caf (patch)
treebcf762db121728b2d37d7545729c34da99b2cc24 /api/routes/getRemotePeers/index.js
parent237a32846ad4a8f8634a68f0a92c71c49807de26 (diff)
downloadarching-kaos-tools-61e46fdc08170e0c630ff9983b87fde6890e3caf.tar.gz
arching-kaos-tools-61e46fdc08170e0c630ff9983b87fde6890e3caf.tar.bz2
arching-kaos-tools-61e46fdc08170e0c630ff9983b87fde6890e3caf.zip
New routes and fixes
Diffstat (limited to 'api/routes/getRemotePeers/index.js')
-rw-r--r--api/routes/getRemotePeers/index.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/api/routes/getRemotePeers/index.js b/api/routes/getRemotePeers/index.js
new file mode 100644
index 0000000..78dbba4
--- /dev/null
+++ b/api/routes/getRemotePeers/index.js
@@ -0,0 +1,54 @@
+/*
+ * Receives an SHA512SUM as a map's hash and if exists it
+ * returns the map's content
+ *
+ */
+
+const { spawn } = require('child_process');
+const config = require("../../config.js");
+
+module.exports = (req, res) => {
+ var args = req.url.split("/");
+ var ip = "";
+ if ( args.length === 4 )
+ {
+ ip = args[3];
+ }
+ var test = /^fc[0-9a-z]{1,2}:([0-9a-z]{1,4}:){1,6}[0-9a-z]{1,4}/
+ if (test.test(ip))
+ {
+ const command = spawn("curl", ["--retry-max-time","3","-s",`http://[${ip}]:8610/v0/peers`]);
+ var buffer = "";
+ command.stdout.on("data", data => {
+ buffer = buffer + data;
+ });
+
+ command.stderr.on("data", data => {
+ console.log(`stderr: ${data}`);
+ });
+
+ command.on('error', (error) => {
+ console.log(`error: ${error.message}`);
+ });
+
+ command.on("close", code => {
+ if ( code === 0 )
+ {
+ res.writeHead(200, { 'Content-Type': 'application/json'});
+ res.end(buffer);
+ }
+ else
+ {
+ res.writeHead(404, {'Content-Type': 'application/json'});
+ res.end(JSON.stringify({error:"Peer unreachable"}));
+ }
+ console.log(`child process exited with code ${code}`);
+ });
+ }
+ else
+ {
+ res.writeHead(404, {'Content-Type': 'application/json'});
+ res.end(JSON.stringify({error:"No IP"}));
+ }
+}
+