aboutsummaryrefslogtreecommitdiff
path: root/api/routes/getRemotePeers/index.js
blob: 78dbba4838da5323a50f3b2b35f55275fa6a174d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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"}));
    }
}