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
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 => {

View File

@ -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;
module.exports.GetCpuCoreCount = GetCpuCoreCount;

View File

@ -1,7 +1,52 @@
// External User ip
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 => {
@ -49,46 +94,40 @@ 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,
}
LocalInterfaces,
GetExternalPublicAddress,
host: null,
GetHost
}