From c6766b5cf1d390ca56dee3f61734eb7d36e2d2dc Mon Sep 17 00:00:00 2001 From: Matheus Sampaio Queiroga Date: Mon, 25 Oct 2021 22:17:58 -0300 Subject: [PATCH 1/4] Update Get Temp Host --- src/BdsNetwork.js | 107 +++++++++++++++++++++++++++++++--------------- 1 file changed, 73 insertions(+), 34 deletions(-) diff --git a/src/BdsNetwork.js b/src/BdsNetwork.js index fe7ed70..0f9cdbe 100644 --- a/src/BdsNetwork.js +++ b/src/BdsNetwork.js @@ -3,6 +3,52 @@ const Request = require("../lib/Requests"); const os = require("os"); const { GetTempHost } = require("../lib/BdsSettings"); +function LocalInterfaces() { + const interfaces = os.networkInterfaces(); + const localInterfaces = []; + for (const name of Object.getOwnPropertyNames(interfaces)) { + const Inter = { + interfaceName: name, + mac: "", + v4: { + addresses: "", + netmask: "", + cidr: "" + }, + v6: { + addresses: "", + netmask: "", + cidr: "" + }, + } + for (let iface of interfaces[name]) { + if (!Inter.mac && iface.mac) Inter.mac = iface.mac; + if (iface.family === "IPv4") { + Inter.v4.addresses = iface.address; + Inter.v4.netmask = iface.netmask; + Inter.v4.cidr = iface.cidr; + } else if (iface.family === "IPv6") { + Inter.v6.addresses = iface.address; + Inter.v6.netmask = iface.netmask; + Inter.v6.cidr = iface.cidr; + } + } + if (!(interfaces[name][0].internal)) localInterfaces.push(Inter); + } + return localInterfaces; +} + +async function GetExternalPublicAddress() { + const ExternlIPs = { + ipv4: null, + ipv6: null + } + ExternlIPs["ipv4"] = (await Request.TEXT("https://api.ipify.org")).replace("\n", "") + ExternlIPs["ipv6"] = (await Request.TEXT("https://api64.ipify.org/")).replace("\n", "") + if (ExternlIPs["ipv6"] === ExternlIPs["ipv4"]) ExternlIPs["ipv6"] = null; + return ExternlIPs; +} + Request.TEXT("https://api.ipify.org").then(external_ipv4 => { Request.TEXT("https://api64.ipify.org/").then(external_ipv6 => { const externalIP = { @@ -49,46 +95,39 @@ const Interfaces = Object.getOwnPropertyNames(interfaces).map(inter => { } }).filter(a=>a); -// Temp Host -var host = null, - HostResponse = null; - -if (GetTempHost()){ - // Get HOST - Request.JSON("https://bds-core-back-end.vercel.app/Gethost", { - method: "POST", +async function GetHost() { + const MacAddr = LocalInterfaces().map(Int => Int.mac); + const ExternalAddress = (await GetExternalPublicAddress()).ipv4; + const RequestUpstream = await fetch(`https://upstream.bdsmaneger.com/v1/public_domain?MacAddress=${JSON.stringify(MacAddr)}&ExternalAdress=${ExternalAddress}`, {mode: "cors"}); + if (!RequestUpstream.ok) { + throw { + Backend: await RequestUpstream.json() + } + } + const HostInfo = await RequestUpstream.json(); + const _toReturn = { + host: "", + UpstreamID: "", + delete_host: async () => { + const RequestDeleteHost = await fetch("https://upstream.bdsmaneger.com/v1/public_domain", { + method: "DELETE", mode: "cors", body: JSON.stringify({ - mac: Interfaces[0].MAC, - ip: external_ip.ipv4, - }), - headers: { - "Content-Type": "application/json" - } - }).then(HostResponse => { - global.BdsTempHost = HostResponse.user.host.host - host = HostResponse.user.host.host - console.log(`Bds Maneger Core Temp Host ID: ${HostResponse.user.ID}`) - // Delete Host - process.on("exit", function () { - Request.JSON("https://bds-core-back-end.vercel.app/DeleteHost", { - method: "post", - body: JSON.stringify({ - "ID": HostResponse.user.ID - }), - headers: { - "Content-Type": "application/json" - } - }).then(deleted_host => { - if (deleted_host.error) console.log(deleted_host.error) - }); - }); - }); -} else module.exports.host = null + UUID: HostInfo.ID + }) + }); + return await RequestDeleteHost.json(); + } + } + _toReturn["host"] = HostInfo.host.host + _toReturn["UpstreamID"] = HostInfo.ID + return _toReturn; +} module.exports = { internal_ip, Interfaces, HostResponse, host, + GetHost } \ No newline at end of file -- 2.49.0 From 027dcee150442a57de7ab8973f7cadbca47b7fa5 Mon Sep 17 00:00:00 2001 From: Matheus Sampaio Queiroga Date: Mon, 25 Oct 2021 23:52:15 -0300 Subject: [PATCH 2/4] Update BdsNetwork.js --- src/BdsNetwork.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/BdsNetwork.js b/src/BdsNetwork.js index 0f9cdbe..13fca41 100644 --- a/src/BdsNetwork.js +++ b/src/BdsNetwork.js @@ -127,7 +127,8 @@ async function GetHost() { module.exports = { internal_ip, Interfaces, - HostResponse, - host, + LocalInterfaces, + GetExternalPublicAddress, + host: null, GetHost -} \ No newline at end of file +} -- 2.49.0 From 65a23aa0fe2ea6e53943f35ed24b21532ed33c82 Mon Sep 17 00:00:00 2001 From: Matheus Sampaio Queiroga Date: Tue, 26 Oct 2021 07:07:57 -0300 Subject: [PATCH 3/4] Edit cli to use GetHost the cli now has a new arg (--get_domain) for the user to get a temporary domain in Bds Maneger Upstream to Bds Maneger CLI --- bin/bds_maneger.js | 21 +++++++++++++++++++-- lib/BdsSystemInfo.js | 13 +++++++------ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/bin/bds_maneger.js b/bin/bds_maneger.js index 9dad6e9..8087394 100755 --- a/bin/bds_maneger.js +++ b/bin/bds_maneger.js @@ -11,6 +11,7 @@ const ProcessArgs = require("minimist")(process.argv.slice(2)); // Import Bds Core const BdsCore = require("../index"); +const BdsNetwork = require("../src/BdsNetwork"); const commandExits = require("../lib/commandExist"); const readline = require("readline"); // const BdsMenus = require("./bds_maneger/menus"); @@ -71,6 +72,7 @@ async function help() { "* -i, --info Print info about Bds Maneger Core and Platforms", "* -d, --download Download a server", "* -s, --start Start a server", + "* --get_domain Get temporary public domain to connect in to server or API", "* -p, --platform Change the platform", "* -n, --no-api Don't start the Bds Maneger API Rest", "*", @@ -107,7 +109,7 @@ async function Runner() { await info(); return; } - + // Help if (ProcessArgs.help || ProcessArgs.h) { await help(); @@ -116,9 +118,24 @@ async function Runner() { // Download if (ProcessArgs.download || ProcessArgs.d) await DownloadServer(); + + // Start Server if (!(ProcessArgs.start || ProcessArgs.s)) return; - // Start + // Get Temporary External Domain + if (ProcessArgs.get_domain) { + try { + const HostInfo = await BdsNetwork.GetHost(); + console.log("Domain:", HostInfo.host); + process.on("exit", async () => { + await HostInfo.delete_host(); + console.log("Sucess remove host"); + }); + } catch (err) { + console.log("Cannot get domain"); + } + } + const BdsCoreStart = BdsCore.start(); BdsCoreStart.log(data => console.log(cli_color.blueBright(data.replace(/\n$/gi, "")))); BdsCoreStart.exit(code => { diff --git a/lib/BdsSystemInfo.js b/lib/BdsSystemInfo.js index 8b085d7..aaf64d9 100644 --- a/lib/BdsSystemInfo.js +++ b/lib/BdsSystemInfo.js @@ -14,7 +14,7 @@ async function CheckSystemAsync() { spigot: await PlatformVersionsV2("spigot"), dragonfly: await PlatformVersionsV2("dragonfly"), } - + let system, require_qemu = false; const valid_platform = { bedrock: true, @@ -27,7 +27,7 @@ async function CheckSystemAsync() { if ((PHPBin[process.platform] || {})[arch]) valid_platform["pocketmine"] = true; else valid_platform["pocketmine"] = false; - // + // Check for Dragonfly if (!(Servers.dragonfly.versions[Servers.dragonfly.latest][process.platform][bds.arch])) valid_platform["dragonfly"] = false; // SoSystem X @@ -35,7 +35,7 @@ async function CheckSystemAsync() { system = "Windows"; } else if (process.platform == "linux") { system = "Linux"; - + // Bedrock Check if (Servers.bedrock.versions[Servers.bedrock.latest][process.platform]) { if (Servers.bedrock.versions[Servers.bedrock.latest][process.platform][arch]) valid_platform["bedrock"] = true; @@ -44,15 +44,16 @@ async function CheckSystemAsync() { if (valid_platform["bedrock"] === false) { if (commadExist("qemu-x86_64-static")) { - // console.warn("The Minecraft Bedrock Server is only being validated because you can use 'qemu-x86_64-static'"); + // console.warn("The Minecraft Bedrock Server is only being validated because you can use 'qemu-x86_64-static'");; valid_platform["bedrock"] = true - require_qemu = true + require_qemu = true; } } } else if (process.platform == "darwin") { system = "MacOS"; } else if (process.platform === "android") { system = "Android"; + if (valid_platform["bedrock"]) valid_platform["bedrock"] = false; } else { throw new Error(`The Bds Maneger Core does not support ${process.platform} systems, as no tests have been done.`); } @@ -121,4 +122,4 @@ function GetCpuCoreCount() { else if (process.platform === "darwin") return require("os").cpus().length; else return 1; } -module.exports.GetCpuCoreCount = GetCpuCoreCount; \ No newline at end of file +module.exports.GetCpuCoreCount = GetCpuCoreCount; -- 2.49.0 From fbbada5da8c1655caa5494b6b735b16bf12ec956 Mon Sep 17 00:00:00 2001 From: Matheus Sampaio Queiroga Date: Tue, 26 Oct 2021 07:41:52 -0300 Subject: [PATCH 4/4] Remove import --- src/BdsNetwork.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/BdsNetwork.js b/src/BdsNetwork.js index 13fca41..856bbbb 100644 --- a/src/BdsNetwork.js +++ b/src/BdsNetwork.js @@ -1,7 +1,6 @@ // External User ip const Request = require("../lib/Requests"); const os = require("os"); -const { GetTempHost } = require("../lib/BdsSettings"); function LocalInterfaces() { const interfaces = os.networkInterfaces(); -- 2.49.0