From 61e46fdc08170e0c630ff9983b87fde6890e3caf Mon Sep 17 00:00:00 2001 From: kaotisk Date: Sat, 7 Dec 2024 03:29:33 +0200 Subject: New routes and fixes --- api/config.js | 1 + api/index.js | 8 +++- api/routes/getIPFSHash/index.js | 71 +++++++++++++++++++++++++++++++++++ api/routes/getRemoteNodeInfo/index.js | 46 +++++++++++++++++++++++ api/routes/getRemotePeers/index.js | 54 ++++++++++++++++++++++++++ api/routes/getZblock/index.js | 66 -------------------------------- 6 files changed, 178 insertions(+), 68 deletions(-) create mode 100644 api/routes/getIPFSHash/index.js create mode 100644 api/routes/getRemoteNodeInfo/index.js create mode 100644 api/routes/getRemotePeers/index.js delete mode 100644 api/routes/getZblock/index.js diff --git a/api/config.js b/api/config.js index 41eb9dd..fc944e1 100644 --- a/api/config.js +++ b/api/config.js @@ -19,6 +19,7 @@ const config = { pairsFile : env.AK_ZPAIRSFILE, peersFile : env.AK_ZPEERSFILE, cacheDir : env.AK_CACHEDIR, + ipfsArtifactsDir: `${env.AK_WORKDIR}/ipfs_artifacts`, minedBlocksDir: env.AK_MINEDBLOCKSDIR, chunksDir: env.AK_CHUNKSDIR, leafsDir: env.AK_LEAFSDIR, diff --git a/api/index.js b/api/index.js index d1a18a6..2646a9c 100755 --- a/api/index.js +++ b/api/index.js @@ -3,13 +3,15 @@ const http = require("node:http"); const welcomeMessage = require("./routes/default/index.js"); const getNodeInfo = require('./routes/getNodeInfo/index.js'); const getPeers = require('./routes/getPeers/index.js'); -const getZblock = require('./routes/getZblock/index.js'); +const getIPFSHash = require('./routes/getIPFSHash/index.js'); const getZlatest = require('./routes/getZLatest/index.js'); const getSblock = require('./routes/getSBlock/index.js'); const getChunk = require('./routes/getChunk/index.js'); const getLeaf = require('./routes/getLeaf/index.js'); const getMap = require('./routes/getMap/index.js'); const getSlatest = require('./routes/getSLatest/index.js'); +const getRemoteNodeInfo = require('./routes/getRemoteNodeInfo/index.js'); +const getRemotePeers = require('./routes/getRemotePeers/index.js'); const akLogMessage = require('./lib/akLogMessage'); @@ -51,13 +53,15 @@ function getRoutes(req, res) case 'root': testRootRoute(req, res); break; case 'peers': getPeers(req, res); break; case 'node_info': getNodeInfo(req, res); break; - case 'zblock': getZblock(req, res); break; + case 'ipfs_hash': getIPFSHash(req, res); break; case 'zlatest': getZlatest(req, res); break; case 'sblock': getSblock(req, res); break; case 'slatest': getSlatest(req, res); break; case 'chunk': getChunk(req, res); break; case 'leaf': getLeaf(req, res); break; case 'map': getMap(req, res); break; + case 'remote_node_info': getRemoteNodeInfo(req, res); break; + case 'remote_peers': getRemotePeers(req, res); break; default: notImplemented(req, res); } } 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"})); - } -} -- cgit v1.2.3