aboutsummaryrefslogtreecommitdiff
path: root/api/routes
diff options
context:
space:
mode:
Diffstat (limited to 'api/routes')
-rw-r--r--api/routes/getIPFSHash/index.js71
-rw-r--r--api/routes/getRemoteNodeInfo/index.js46
-rw-r--r--api/routes/getRemotePeers/index.js54
-rw-r--r--api/routes/getZblock/index.js66
4 files changed, 171 insertions, 66 deletions
diff --git a/api/routes/getIPFSHash/index.js b/api/routes/getIPFSHash/index.js
new file mode 100644
index 0000000..f412c80
--- /dev/null
+++ b/api/routes/getIPFSHash/index.js
@@ -0,0 +1,71 @@
+const { spawn } = require('child_process');
+const fs = require("fs");
+const config = require("../../config");
+
+/*
+ * Returns a cached zblock
+ *
+ * Returns:
+ * - JSON object
+ *
+ */
+function fetchIPFShash(zblock, res)
+{
+ regex= /Qm[A-Za-z0-9]{44}/;
+ if (regex.test(zblock)){
+ const path = `${config.ipfsArtifactsDir}/${zblock}`;
+ console.log(path)
+ try
+ {
+ if(fs.existsSync(path))
+ {
+ res.writeHead(200, {'Content-Type': 'application/json'});
+ res.end(JSON.stringify(JSON.parse(fs.readFileSync(path))));
+ }
+ else
+ {
+ res.writeHead(404, {'Content-Type': 'application/json'});
+ res.end(JSON.stringify({error:"invalid or unreachable"}));
+ }
+ }
+ 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:"Invalid data: regexp failed to pass"}));
+ }
+};
+
+module.exports = (req, res) => {
+ var args = req.url.split("/");
+ if ( (args[2] === 'ipfs_hash') && args[3] && typeof args[3] === "string" && args[3].length === 46 ){
+ regex= /Qm[A-Za-z0-9]{44}/;
+ if (regex.test(args[3]))
+ {
+ if (args[3] === "QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH" )
+ {
+ res.writeHead(404, {'Content-Type': 'application/json'});
+ res.end(JSON.stringify({error:"Genesis block"}));
+ }
+ else
+ {
+ fetchIPFShash(args[3],res);
+ }
+ }
+ else
+ {
+ res.writeHead(404, {'Content-Type': 'application/json'});
+ res.end(JSON.stringify({error:"Invalid data: regexp failed to pass"}));
+ }
+ }
+ else
+ {
+ res.writeHead(404, {'Content-Type': 'application/json'});
+ res.end(JSON.stringify({error:"Invalid data: no valid zblock was provided"}));
+ }
+}
diff --git a/api/routes/getRemoteNodeInfo/index.js b/api/routes/getRemoteNodeInfo/index.js
new file mode 100644
index 0000000..6cfff4b
--- /dev/null
+++ b/api/routes/getRemoteNodeInfo/index.js
@@ -0,0 +1,46 @@
+/*
+ * 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/node_info`]);
+ 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 => {
+ res.writeHead(200, { 'Content-Type': 'application/json'});
+ res.end(buffer);
+ console.log(`child process exited with code ${code}`);
+ });
+ }
+ else
+ {
+ res.writeHead(404, {'Content-Type': 'application/json'});
+ res.end(JSON.stringify({error:"No IP"}));
+ }
+}
+
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"}));
+ }
+}
+
diff --git a/api/routes/getZblock/index.js b/api/routes/getZblock/index.js
deleted file mode 100644
index 9826c8b..0000000
--- a/api/routes/getZblock/index.js
+++ /dev/null
@@ -1,66 +0,0 @@
-const { spawn } = require('child_process');
-const fs = require("fs");
-const config = require("../../config");
-
-/*
- * Returns a cached zblock
- *
- * Returns:
- * - JSON object
- *
- */
-function fetchZblock(zblock, res){
- regex= /Qm[A-Za-z0-9]{44}/;
- if (regex.test(zblock)){
- const command = spawn("ak-zblock",["--cache",zblock]);
-
- command.on("close", code => {
- console.warn(`child process exited with code ${code}`);
-
- if ( code === 0 ) {
- const path = config.cacheDir+"/fzblocks/"+zblock;
- console.log(path)
- try {
- if(fs.existsSync(path)){
- var buffer = fs.readFileSync(path);
- res.writeHead(200, {'Content-Type': 'application/json'});
- res.end(JSON.stringify(JSON.parse(buffer)));
- }
- } catch (error) {
- res.writeHead(404, {'Content-Type': 'application/json'});
- res.end({"error":error.message});
- }
- } else if ( code === 2){
- res.writeHead(404, {'Content-Type': 'application/json'});
- res.end({"error":"The roof is on fire"});
- } else {
- res.writeHead(404, {'Content-Type': 'application/json'});
- res.end({"error":"invalid or unreachable"});
- }
- });
- } else {
- res.writeHead(404, {'Content-Type': 'application/json'});
- res.end(JSON.stringify({error:"Invalid data: regexp failed to pass"}));
- }
-};
-
-module.exports = (req, res) => {
- var args = req.url.split("/");
- if ( (args[2] === 'zblock') && args[3] && typeof args[3] === "string" && args[3].length === 46 ){
- regex= /Qm[A-Za-z0-9]{44}/;
- if (regex.test(args[3])){
- if (args[3] === "QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH" ){
- res.writeHead(404, {'Content-Type': 'application/json'});
- res.end(JSON.stringify({error:"Genesis block"}));
- } else {
- fetchZblock(args[3],res);
- }
- } else {
- res.writeHead(404, {'Content-Type': 'application/json'});
- res.end(JSON.stringify({error:"Invalid data: regexp failed to pass"}));
- }
- } else {
- res.writeHead(404, {'Content-Type': 'application/json'});
- res.end(JSON.stringify({error:"Invalid data: no valid zblock was provided"}));
- }
-}