AAAAAAAAA FUUUUUCK!!! KILL ME, PLEASE!!!
This commit is contained in:
parent
49334760e9
commit
d7685f56a3
@ -13,8 +13,12 @@ class APIMethods {
|
|||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
info (_, ___, __, cb) {
|
info (con, req, cb) {
|
||||||
cb({ result: require("./server-info") });
|
cb({ result: require("./server-info"), trace_id: req.trace_id, ended: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
authed (con, req, cb) {
|
||||||
|
cb({ result: require("./server-info"), trace_id: req.trace_id, ended: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
/*setSession (isEncrypted, address, { key, counter }, cb) {
|
/*setSession (isEncrypted, address, { key, counter }, cb) {
|
||||||
@ -34,11 +38,12 @@ class APIMethods {
|
|||||||
class API {
|
class API {
|
||||||
constructor () {
|
constructor () {
|
||||||
this.methods = new APIMethods(this);
|
this.methods = new APIMethods(this);
|
||||||
this.sessions = {};
|
this.sessions = new WeakMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
decrypt (address, bytes) {
|
decrypt (address, bytes) {
|
||||||
try {
|
throw new Error("Deprecated. Use TLS. If you see this error, please, add issue into: https://git.fullgream.tech/fullgream/ai-adventure-labs/issues");
|
||||||
|
/*try {
|
||||||
if (!global.config.server.secureMode)
|
if (!global.config.server.secureMode)
|
||||||
// return Buffer.from([]);
|
// return Buffer.from([]);
|
||||||
return undefined;
|
return undefined;
|
||||||
@ -48,10 +53,10 @@ class API {
|
|||||||
this.sessions[address].decrypt(bytes);
|
this.sessions[address].decrypt(bytes);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
async exec (address, bytes, cb) {
|
async exec (connection, bytes, cb) {
|
||||||
const request = parseRq(bytes);
|
const request = parseRq(bytes);
|
||||||
|
|
||||||
if (request === undefined)
|
if (request === undefined)
|
||||||
@ -61,9 +66,18 @@ class API {
|
|||||||
|
|
||||||
if (typeof request === "object" && request !== null) {
|
if (typeof request === "object" && request !== null) {
|
||||||
const {
|
const {
|
||||||
method
|
method,
|
||||||
|
trace_id
|
||||||
} = request;
|
} = request;
|
||||||
this.methods[method]?.(false, address, request, cb);
|
if (!method)
|
||||||
|
return cb({
|
||||||
|
error: "method missed"
|
||||||
|
});
|
||||||
|
if (!trace_id)
|
||||||
|
return cb({
|
||||||
|
error: "trace_id missed"
|
||||||
|
});
|
||||||
|
this.methods[method]?.(connection, request, cb);
|
||||||
} else {
|
} else {
|
||||||
return cb({
|
return cb({
|
||||||
error: "required JSON-object based request"
|
error: "required JSON-object based request"
|
||||||
|
@ -1,28 +1,79 @@
|
|||||||
import { ServerAuth } from "/js/connect/auth.js";
|
import { ServerAuth } from "/js/connect/auth.js";
|
||||||
|
|
||||||
class ApiMethods {
|
function randint(min, max) {
|
||||||
|
return Math.ceil((Math.random() * (max - min)) + min);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTraceId() {
|
||||||
|
const dict = "1234567890abcdefABCDEF";
|
||||||
|
return [...new Array(16)]
|
||||||
|
.map(() => dict[randint(0, dict.length - 1)])
|
||||||
|
.join("");
|
||||||
|
}
|
||||||
|
|
||||||
|
class ProtoApiMethods {
|
||||||
constructor (api) {
|
constructor (api) {
|
||||||
this.api = api;
|
this.api = api;
|
||||||
}
|
}
|
||||||
|
|
||||||
async info () {
|
async _protoMethod (rqdata, threadcb) {
|
||||||
const socket = this.api.socket;
|
const socket = this.api.socket;
|
||||||
|
const trace_id = getTraceId()
|
||||||
|
|
||||||
const promise = new Promise((rs, rj) => {
|
const promise = new Promise((rs, rj) => {
|
||||||
socket.onmessage = (ev) => {
|
socket.onmessage = (ev) => {
|
||||||
const data = JSON.parse(ev.data).result;
|
ev = JSON.parse(ev.data);
|
||||||
rs(data);
|
const data = ev.result;
|
||||||
|
return rs(data);
|
||||||
|
if (ev.trace_id === trace_id)
|
||||||
|
if (!!ev.ended)
|
||||||
|
return rs(data);
|
||||||
|
threadcb(data);
|
||||||
};
|
};
|
||||||
socket.onerror = (err) => rj(err);
|
socket.onerror = (err) => rj(err);
|
||||||
});
|
});
|
||||||
socket.send(
|
socket.send(
|
||||||
JSON.stringify({
|
JSON.stringify({ trace_id, ...rqdata }),
|
||||||
method: "info",
|
|
||||||
}),
|
|
||||||
);
|
);
|
||||||
return await promise;
|
return await promise;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ApiMethods extends ProtoApiMethods {
|
||||||
|
async _protoMethod (rqdata) {
|
||||||
|
throw new Error("_protoMethod allowed only into abstract class");
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor (api) {
|
||||||
|
super(api);
|
||||||
|
}
|
||||||
|
|
||||||
|
async info () {
|
||||||
|
return await super._protoMethod({
|
||||||
|
method: "info",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async authed () {
|
||||||
|
return await super._protoMethod({
|
||||||
|
method: "authed",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ApiHTML {
|
||||||
|
constructor (api) {
|
||||||
|
this.api = api;
|
||||||
|
}
|
||||||
|
|
||||||
|
async renderAuth (authMode) {
|
||||||
|
switch (authMode) {
|
||||||
|
default:
|
||||||
|
document.getElementById("server.area").innerHTML = '<div class="auth-window"><button>connect</button></div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class ApiSocket {
|
export class ApiSocket {
|
||||||
constructor ({
|
constructor ({
|
||||||
isTLSmode, address, port
|
isTLSmode, address, port
|
||||||
@ -33,6 +84,8 @@ export class ApiSocket {
|
|||||||
|
|
||||||
this.socket = new WebSocket(`${!isTLSmode ? "ws" : "wss"}://${address}:${port}`);
|
this.socket = new WebSocket(`${!isTLSmode ? "ws" : "wss"}://${address}:${port}`);
|
||||||
this.methods = new ApiMethods(this);
|
this.methods = new ApiMethods(this);
|
||||||
|
|
||||||
|
this.html = new ApiHTML(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
async run () {
|
async run () {
|
||||||
|
@ -45,10 +45,11 @@ const socket = new ApiSocket({
|
|||||||
isTLSmode, address, port
|
isTLSmode, address, port
|
||||||
});
|
});
|
||||||
socket.run()
|
socket.run()
|
||||||
.then(data => {
|
.then(async (data) => {
|
||||||
console.log("socket sends:", data);
|
console.log("socket sends:", data);
|
||||||
document.getElementById("server-name").innerHTML = data.name;
|
|
||||||
document.title = data.name;
|
document.title = data.name;
|
||||||
|
document.getElementById("server-name").innerText = data.name;
|
||||||
|
await socket.html.renderAuth(data.authMode);
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
Loading…
Reference in New Issue
Block a user