A Node.js server that create a tunnel to the Internet

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite
1












I'm creating a Node.js app that allows the user to create their own social network. This code doesn't do anything special yet except creating a basic server and making it available on the Internet via a persistent address. As the address can be assigned to any person at any time, I make the server check for available addresses in the form of [device uuid]-[discriminator], where the discriminator is any number between 0 and 29. So the client then connect to the server by probing 30 possible addresses (sounds awful ;P) until the correct address is found. Anyway here is the code, please review it for me, I want this thing to be as perfect as possible.



Server



const localtunnel = require("localtunnel");
const portscanner = require("portscanner");
const express = require("express");
const low = require("lowdb");
const FileSync = require("lowdb/adapters/FileSync");
const uuidv4 = require("uuid/v4");

const getSubdomainFromURL = url => new URL(url).hostname.split(".")[0];
const constructSubdomain = (uuid, discriminator) => uuid + "-" + discriminator;
const createTunnel = (subdomain, port) => new Promise((resolve, reject) =>
localtunnel(port, subdomain: subdomain , (error, tunnel) =>
if (error)
reject(error);
else if (getSubdomainFromURL(tunnel.url) === subdomain)
resolve(tunnel);
else
tunnel.close();
reject(tunnel);

)
);

const adapter = new FileSync("db.json");
const db = low(adapter);
db
.defaults(
deviceUUID: uuidv4()
)
.write();

const defaultPorts = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020];
portscanner.findAPortNotInUse(defaultPorts)
.then(async port =>
const numberOfAttempts = 30;
let subdomain;
for (let i = 0; i < numberOfAttempts; i++)
try
subdomain = constructSubdomain(db.get("deviceUUID"), i);
await createTunnel(subdomain, port);
break;
catch (error)
console.error(error);
if (i === numberOfAttempts - 1)
console.log("Sorry, we are giving up. We can't create a tunnel for you after " + numberOfAttempts + " unsuccessful attempts.");
process.exit();

continue;


console.log("Here is your subdomain: " + subdomain);
const app = express();
app.get("/", (req, res) => res.send("LOCAL_GROUP_NETWORK_INSTANCE"));
app.listen(port, () => console.log("Application server listening on port " + port));
);


Client



const axios = require("axios");
const numberOfAttempts = 30;
const deviceID = process.argv[2];
const constructURL = (deviceID, discriminator) => `https://$deviceID-$discriminator.localtunnel.me/`;
const request = (deviceID, i = 0) =>
console.log(`Establishing connection: $i + 1/30`);
if (i === 30) return console.log("Finally giving up...");
axios.get(constructURL(deviceID, i), timeout: 3000 )
.then(response =>
response === "LOCAL_GROUP_NETWORK_INSTANCE"
? request(deviceID, i + 1)
: console.log("We are the world!")
)
.catch(error => request(deviceID, i + 1))
;
request(deviceID);


I know the way my app works sound ridiculous but it's just an experiment, I don't really care much about it. What I need is a code review.







share|improve this question



























    up vote
    2
    down vote

    favorite
    1












    I'm creating a Node.js app that allows the user to create their own social network. This code doesn't do anything special yet except creating a basic server and making it available on the Internet via a persistent address. As the address can be assigned to any person at any time, I make the server check for available addresses in the form of [device uuid]-[discriminator], where the discriminator is any number between 0 and 29. So the client then connect to the server by probing 30 possible addresses (sounds awful ;P) until the correct address is found. Anyway here is the code, please review it for me, I want this thing to be as perfect as possible.



    Server



    const localtunnel = require("localtunnel");
    const portscanner = require("portscanner");
    const express = require("express");
    const low = require("lowdb");
    const FileSync = require("lowdb/adapters/FileSync");
    const uuidv4 = require("uuid/v4");

    const getSubdomainFromURL = url => new URL(url).hostname.split(".")[0];
    const constructSubdomain = (uuid, discriminator) => uuid + "-" + discriminator;
    const createTunnel = (subdomain, port) => new Promise((resolve, reject) =>
    localtunnel(port, subdomain: subdomain , (error, tunnel) =>
    if (error)
    reject(error);
    else if (getSubdomainFromURL(tunnel.url) === subdomain)
    resolve(tunnel);
    else
    tunnel.close();
    reject(tunnel);

    )
    );

    const adapter = new FileSync("db.json");
    const db = low(adapter);
    db
    .defaults(
    deviceUUID: uuidv4()
    )
    .write();

    const defaultPorts = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020];
    portscanner.findAPortNotInUse(defaultPorts)
    .then(async port =>
    const numberOfAttempts = 30;
    let subdomain;
    for (let i = 0; i < numberOfAttempts; i++)
    try
    subdomain = constructSubdomain(db.get("deviceUUID"), i);
    await createTunnel(subdomain, port);
    break;
    catch (error)
    console.error(error);
    if (i === numberOfAttempts - 1)
    console.log("Sorry, we are giving up. We can't create a tunnel for you after " + numberOfAttempts + " unsuccessful attempts.");
    process.exit();

    continue;


    console.log("Here is your subdomain: " + subdomain);
    const app = express();
    app.get("/", (req, res) => res.send("LOCAL_GROUP_NETWORK_INSTANCE"));
    app.listen(port, () => console.log("Application server listening on port " + port));
    );


    Client



    const axios = require("axios");
    const numberOfAttempts = 30;
    const deviceID = process.argv[2];
    const constructURL = (deviceID, discriminator) => `https://$deviceID-$discriminator.localtunnel.me/`;
    const request = (deviceID, i = 0) =>
    console.log(`Establishing connection: $i + 1/30`);
    if (i === 30) return console.log("Finally giving up...");
    axios.get(constructURL(deviceID, i), timeout: 3000 )
    .then(response =>
    response === "LOCAL_GROUP_NETWORK_INSTANCE"
    ? request(deviceID, i + 1)
    : console.log("We are the world!")
    )
    .catch(error => request(deviceID, i + 1))
    ;
    request(deviceID);


    I know the way my app works sound ridiculous but it's just an experiment, I don't really care much about it. What I need is a code review.







    share|improve this question























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I'm creating a Node.js app that allows the user to create their own social network. This code doesn't do anything special yet except creating a basic server and making it available on the Internet via a persistent address. As the address can be assigned to any person at any time, I make the server check for available addresses in the form of [device uuid]-[discriminator], where the discriminator is any number between 0 and 29. So the client then connect to the server by probing 30 possible addresses (sounds awful ;P) until the correct address is found. Anyway here is the code, please review it for me, I want this thing to be as perfect as possible.



      Server



      const localtunnel = require("localtunnel");
      const portscanner = require("portscanner");
      const express = require("express");
      const low = require("lowdb");
      const FileSync = require("lowdb/adapters/FileSync");
      const uuidv4 = require("uuid/v4");

      const getSubdomainFromURL = url => new URL(url).hostname.split(".")[0];
      const constructSubdomain = (uuid, discriminator) => uuid + "-" + discriminator;
      const createTunnel = (subdomain, port) => new Promise((resolve, reject) =>
      localtunnel(port, subdomain: subdomain , (error, tunnel) =>
      if (error)
      reject(error);
      else if (getSubdomainFromURL(tunnel.url) === subdomain)
      resolve(tunnel);
      else
      tunnel.close();
      reject(tunnel);

      )
      );

      const adapter = new FileSync("db.json");
      const db = low(adapter);
      db
      .defaults(
      deviceUUID: uuidv4()
      )
      .write();

      const defaultPorts = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020];
      portscanner.findAPortNotInUse(defaultPorts)
      .then(async port =>
      const numberOfAttempts = 30;
      let subdomain;
      for (let i = 0; i < numberOfAttempts; i++)
      try
      subdomain = constructSubdomain(db.get("deviceUUID"), i);
      await createTunnel(subdomain, port);
      break;
      catch (error)
      console.error(error);
      if (i === numberOfAttempts - 1)
      console.log("Sorry, we are giving up. We can't create a tunnel for you after " + numberOfAttempts + " unsuccessful attempts.");
      process.exit();

      continue;


      console.log("Here is your subdomain: " + subdomain);
      const app = express();
      app.get("/", (req, res) => res.send("LOCAL_GROUP_NETWORK_INSTANCE"));
      app.listen(port, () => console.log("Application server listening on port " + port));
      );


      Client



      const axios = require("axios");
      const numberOfAttempts = 30;
      const deviceID = process.argv[2];
      const constructURL = (deviceID, discriminator) => `https://$deviceID-$discriminator.localtunnel.me/`;
      const request = (deviceID, i = 0) =>
      console.log(`Establishing connection: $i + 1/30`);
      if (i === 30) return console.log("Finally giving up...");
      axios.get(constructURL(deviceID, i), timeout: 3000 )
      .then(response =>
      response === "LOCAL_GROUP_NETWORK_INSTANCE"
      ? request(deviceID, i + 1)
      : console.log("We are the world!")
      )
      .catch(error => request(deviceID, i + 1))
      ;
      request(deviceID);


      I know the way my app works sound ridiculous but it's just an experiment, I don't really care much about it. What I need is a code review.







      share|improve this question













      I'm creating a Node.js app that allows the user to create their own social network. This code doesn't do anything special yet except creating a basic server and making it available on the Internet via a persistent address. As the address can be assigned to any person at any time, I make the server check for available addresses in the form of [device uuid]-[discriminator], where the discriminator is any number between 0 and 29. So the client then connect to the server by probing 30 possible addresses (sounds awful ;P) until the correct address is found. Anyway here is the code, please review it for me, I want this thing to be as perfect as possible.



      Server



      const localtunnel = require("localtunnel");
      const portscanner = require("portscanner");
      const express = require("express");
      const low = require("lowdb");
      const FileSync = require("lowdb/adapters/FileSync");
      const uuidv4 = require("uuid/v4");

      const getSubdomainFromURL = url => new URL(url).hostname.split(".")[0];
      const constructSubdomain = (uuid, discriminator) => uuid + "-" + discriminator;
      const createTunnel = (subdomain, port) => new Promise((resolve, reject) =>
      localtunnel(port, subdomain: subdomain , (error, tunnel) =>
      if (error)
      reject(error);
      else if (getSubdomainFromURL(tunnel.url) === subdomain)
      resolve(tunnel);
      else
      tunnel.close();
      reject(tunnel);

      )
      );

      const adapter = new FileSync("db.json");
      const db = low(adapter);
      db
      .defaults(
      deviceUUID: uuidv4()
      )
      .write();

      const defaultPorts = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020];
      portscanner.findAPortNotInUse(defaultPorts)
      .then(async port =>
      const numberOfAttempts = 30;
      let subdomain;
      for (let i = 0; i < numberOfAttempts; i++)
      try
      subdomain = constructSubdomain(db.get("deviceUUID"), i);
      await createTunnel(subdomain, port);
      break;
      catch (error)
      console.error(error);
      if (i === numberOfAttempts - 1)
      console.log("Sorry, we are giving up. We can't create a tunnel for you after " + numberOfAttempts + " unsuccessful attempts.");
      process.exit();

      continue;


      console.log("Here is your subdomain: " + subdomain);
      const app = express();
      app.get("/", (req, res) => res.send("LOCAL_GROUP_NETWORK_INSTANCE"));
      app.listen(port, () => console.log("Application server listening on port " + port));
      );


      Client



      const axios = require("axios");
      const numberOfAttempts = 30;
      const deviceID = process.argv[2];
      const constructURL = (deviceID, discriminator) => `https://$deviceID-$discriminator.localtunnel.me/`;
      const request = (deviceID, i = 0) =>
      console.log(`Establishing connection: $i + 1/30`);
      if (i === 30) return console.log("Finally giving up...");
      axios.get(constructURL(deviceID, i), timeout: 3000 )
      .then(response =>
      response === "LOCAL_GROUP_NETWORK_INSTANCE"
      ? request(deviceID, i + 1)
      : console.log("We are the world!")
      )
      .catch(error => request(deviceID, i + 1))
      ;
      request(deviceID);


      I know the way my app works sound ridiculous but it's just an experiment, I don't really care much about it. What I need is a code review.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 17 at 22:34









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Jun 10 at 6:06









      continued-

      563




      563

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196210%2fa-node-js-server-that-create-a-tunnel-to-the-internet%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196210%2fa-node-js-server-that-create-a-tunnel-to-the-internet%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods