added createInterface & localOrigin set

This commit is contained in:
srgooglo 2021-06-21 15:46:43 +02:00
parent ce332cd8ef
commit 51ef45613c
5 changed files with 60 additions and 32 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@corenode/cloudlink-addon", "name": "@corenode/cloudlink-addon",
"version": "0.5.6", "version": "0.5.11",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
"publishConfig": { "publishConfig": {
@ -14,6 +14,6 @@
], ],
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@ragestudio/cloudlink": "^0.5.6" "@ragestudio/cloudlink": "^0.5.11"
} }
} }

View File

@ -2,7 +2,10 @@ const cloudlink = require("../dist")
const random = require("corenode/dist/libs/random") const random = require("corenode/dist/libs/random")
// create server // create server
new cloudlink.Server({ const server = new cloudlink.Server({
autoInit: true, autoInit: true,
id: runtime.args.id ?? random.generateName() id: runtime.args.id ?? random.generateName()
}) })
// connect
const client = cloudlink.Client.createInterface(server.localOrigin)

View File

@ -1,37 +1,48 @@
const axios = require("axios") const axios = require("axios")
const wsClient = require('websocket').client const wsClient = require('websocket').client
const defaultRelicOrigin = global.DEFAULT_RELIC_ORIGIN const RELIC_ORIGIN = global.RELIC_ORIGIN
let sockets = {}
function registerNewBridge() {
}
function resolveOrigin(origin) { function resolveOrigin(origin) {
} }
function connectToOrigin(origin) { class Bridge {
if (typeof origin === "undefined") {
origin = defaultRelicOrigin
}
return new DefaultBridge({
origin: origin
})
}
class DefaultBridge {
constructor(params) { constructor(params) {
this.params = params this.params = params
this.origin = this.params.origin ?? "https://relic.ragestudio.net" this.origin = this.params.origin
this.headers = { ...this.params.headers }
this.instance = axios.create({
baseURL: this.origin,
headers: this.headers
})
this.map = null
}
async connect() {
//get map
const req = await this.instance.get("/map")
this.map = req.data
} }
} }
module.exports = { function createInterface(address) {
defaultRelicOrigin,
registerNewBridge, const bridge = new Bridge({
resolveOrigin, origin: address
connectToOrigin })
bridge.connect()
.then(() => {
console.log(bridge.map)
})
}
module.exports = {
Bridge,
resolveOrigin,
createInterface,
} }

View File

@ -3,7 +3,7 @@ const path = require('path')
//* set globals //* set globals
global.runtime = runtime global.runtime = runtime
global.IS_DEV = runtime.helpers.isDevMode() global.IS_DEV = runtime.helpers.isDevMode()
global.DEFAULT_RELIC_ORIGIN = require('./relicOrigin.json') global.RELIC_ORIGIN = require('./relicOrigin.json')
global.SERVER_VERSION = runtime.helpers.getVersion() global.SERVER_VERSION = runtime.helpers.getVersion()
global.SERVER_MANIFEST = "server.manifest" global.SERVER_MANIFEST = "server.manifest"
global.SERVER_MANIFEST_PATH = path.resolve(process.cwd(), SERVER_MANIFEST) global.SERVER_MANIFEST_PATH = path.resolve(process.cwd(), SERVER_MANIFEST)

View File

@ -1,5 +1,18 @@
const fs = require("fs") const fs = require("fs")
const express = require("express") const express = require("express")
function getIPAddress() {
var interfaces = require('os').networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
return alias.address;
}
}
return '0.0.0.0';
}
const { objectToArrayMap } = require("@corenode/utils") const { objectToArrayMap } = require("@corenode/utils")
const tokenizer = require("corenode/dist/libs/tokenizer") const tokenizer = require("corenode/dist/libs/tokenizer")
@ -25,6 +38,7 @@ const defaultHeaders = {
class Server { class Server {
constructor(params, endpoints, middlewares) { constructor(params, endpoints, middlewares) {
this.params = params ?? {} this.params = params ?? {}
this.port = this.params.port ?? 3010
// handle endpoints && middlewares // handle endpoints && middlewares
const localEndpoints = getLocalEndpoints() const localEndpoints = getLocalEndpoints()
@ -59,9 +73,9 @@ class Server {
this._everyRequest = null this._everyRequest = null
this._onRequest = {} this._onRequest = {}
if (typeof this.params.port === "undefined") {
this.params.port = 3010 this.localOrigin = `http://${getIPAddress()}:${this.port}`
} this.nethubOrigin = ""
if (this.params.autoInit) { if (this.params.autoInit) {
this.init() this.init()
@ -236,13 +250,13 @@ class Server {
}) })
}) })
this.httpServer.listen(this.params.port, () => { this.httpServer.listen(this.port, () => {
//? register to nethub //? register to nethub
if (this.params.onlineNethub) { if (this.params.onlineNethub) {
nethub.registerOrigin({ entry: "/", oskid: this.oskid, id: this.id }) nethub.registerOrigin({ entry: "/", oskid: this.oskid, id: this.id })
} }
console.log(`✅ Ready on port ${this.params.port}!`) console.log(`✅ Ready on port ${this.port}!`)
}) })
} }
} }