Update Get Temp Host #229

Merged
Sirherobrine23 merged 4 commits from UpdateGetHost into main 2021-10-26 13:14:59 +00:00
3 changed files with 103 additions and 46 deletions

View File

@ -11,6 +11,7 @@ const ProcessArgs = require("minimist")(process.argv.slice(2));
// Import Bds Core // Import Bds Core
const BdsCore = require("../index"); const BdsCore = require("../index");
const BdsNetwork = require("../src/BdsNetwork");
const commandExits = require("../lib/commandExist"); const commandExits = require("../lib/commandExist");
const readline = require("readline"); const readline = require("readline");
// const BdsMenus = require("./bds_maneger/menus"); // const BdsMenus = require("./bds_maneger/menus");
@ -71,6 +72,7 @@ async function help() {
"* -i, --info Print info about Bds Maneger Core and Platforms", "* -i, --info Print info about Bds Maneger Core and Platforms",
"* -d, --download Download a server", "* -d, --download Download a server",
"* -s, --start Start 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", "* -p, --platform Change the platform",
"* -n, --no-api Don't start the Bds Maneger API Rest", "* -n, --no-api Don't start the Bds Maneger API Rest",
"*", "*",
@ -116,9 +118,24 @@ async function Runner() {
// Download // Download
if (ProcessArgs.download || ProcessArgs.d) await DownloadServer(); if (ProcessArgs.download || ProcessArgs.d) await DownloadServer();
// Start Server
if (!(ProcessArgs.start || ProcessArgs.s)) return; 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(); const BdsCoreStart = BdsCore.start();
BdsCoreStart.log(data => console.log(cli_color.blueBright(data.replace(/\n$/gi, "")))); BdsCoreStart.log(data => console.log(cli_color.blueBright(data.replace(/\n$/gi, ""))));
BdsCoreStart.exit(code => { BdsCoreStart.exit(code => {

View File

@ -27,7 +27,7 @@ async function CheckSystemAsync() {
if ((PHPBin[process.platform] || {})[arch]) valid_platform["pocketmine"] = true; if ((PHPBin[process.platform] || {})[arch]) valid_platform["pocketmine"] = true;
else valid_platform["pocketmine"] = false; else valid_platform["pocketmine"] = false;
// // Check for Dragonfly
if (!(Servers.dragonfly.versions[Servers.dragonfly.latest][process.platform][bds.arch])) valid_platform["dragonfly"] = false; if (!(Servers.dragonfly.versions[Servers.dragonfly.latest][process.platform][bds.arch])) valid_platform["dragonfly"] = false;
// SoSystem X // SoSystem X
@ -44,15 +44,16 @@ async function CheckSystemAsync() {
if (valid_platform["bedrock"] === false) { if (valid_platform["bedrock"] === false) {
if (commadExist("qemu-x86_64-static")) { 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 valid_platform["bedrock"] = true
require_qemu = true require_qemu = true;
} }
} }
} else if (process.platform == "darwin") { } else if (process.platform == "darwin") {
system = "MacOS"; system = "MacOS";
} else if (process.platform === "android") { } else if (process.platform === "android") {
system = "Android"; system = "Android";
if (valid_platform["bedrock"]) valid_platform["bedrock"] = false;
} else { } else {
throw new Error(`The Bds Maneger Core does not support ${process.platform} systems, as no tests have been done.`); throw new Error(`The Bds Maneger Core does not support ${process.platform} systems, as no tests have been done.`);
} }

View File

@ -1,7 +1,52 @@
// External User ip // External User ip
const Request = require("../lib/Requests"); const Request = require("../lib/Requests");
const os = require("os"); 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://api.ipify.org").then(external_ipv4 => {
Request.TEXT("https://api64.ipify.org/").then(external_ipv6 => { Request.TEXT("https://api64.ipify.org/").then(external_ipv6 => {
@ -49,46 +94,40 @@ const Interfaces = Object.getOwnPropertyNames(interfaces).map(inter => {
} }
}).filter(a=>a); }).filter(a=>a);
// Temp Host async function GetHost() {
var host = null, const MacAddr = LocalInterfaces().map(Int => Int.mac);
HostResponse = null; 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 (GetTempHost()){ if (!RequestUpstream.ok) {
// Get HOST throw {
Request.JSON("https://bds-core-back-end.vercel.app/Gethost", { Backend: await RequestUpstream.json()
method: "POST", }
}
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", mode: "cors",
body: JSON.stringify({ body: JSON.stringify({
mac: Interfaces[0].MAC, UUID: HostInfo.ID
ip: external_ip.ipv4, })
}), });
headers: { return await RequestDeleteHost.json();
"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 => { _toReturn["host"] = HostInfo.host.host
if (deleted_host.error) console.log(deleted_host.error) _toReturn["UpstreamID"] = HostInfo.ID
}); return _toReturn;
}); }
});
} else module.exports.host = null
module.exports = { module.exports = {
internal_ip, internal_ip,
Interfaces, Interfaces,
HostResponse, LocalInterfaces,
host, GetExternalPublicAddress,
host: null,
GetHost
} }