Compare commits
5 Commits
956435ba40
...
904090f51e
Author | SHA1 | Date | |
---|---|---|---|
904090f51e | |||
0b4c2cc7b8 | |||
6add83075c | |||
cc2ed1c277 | |||
c7fe3b36c5 |
@ -116,6 +116,7 @@ class APIMethods {
|
|||||||
|
|
||||||
// getting characters
|
// getting characters
|
||||||
const user = global.authed.get(con);
|
const user = global.authed.get(con);
|
||||||
|
const fields = req.fields.split(/\s*,\s*/g);
|
||||||
const result = (await user.getCharacters())
|
const result = (await user.getCharacters())
|
||||||
.map(character => {
|
.map(character => {
|
||||||
return character;
|
return character;
|
||||||
|
@ -15,12 +15,28 @@ const curUrl = new URL(location.href);
|
|||||||
const [address, port] = curUrl.searchParams.get("server").split(/:/);
|
const [address, port] = curUrl.searchParams.get("server").split(/:/);
|
||||||
const isTLSmode = curUrl.searchParams.get("encrypted") === "true";
|
const isTLSmode = curUrl.searchParams.get("encrypted") === "true";
|
||||||
|
|
||||||
|
const apiMethodsProtected = new WeakMap();
|
||||||
class ProtoApiMethods {
|
class ProtoApiMethods {
|
||||||
constructor (api) {
|
constructor (api) {
|
||||||
this.api = api;
|
//this.api = api;
|
||||||
|
if (!(api instanceof ApiSocket))
|
||||||
|
throw new Error("api must be instance of ApiSocket class");
|
||||||
|
apiMethodsProtected.set(this, api);
|
||||||
|
}
|
||||||
|
|
||||||
|
set api (_) {
|
||||||
|
throw new Error("api is not writeable");
|
||||||
|
}
|
||||||
|
|
||||||
|
get api () {
|
||||||
|
return apiMethodsProtected.get(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _protoMethod (rqdata, threadcb = null) {
|
async _protoMethod (rqdata, threadcb = null) {
|
||||||
|
if (!(this instanceof ProtoApiMethods)) {
|
||||||
|
throw new Error("_protoMethod allowed only into original abstract class");
|
||||||
|
}
|
||||||
|
|
||||||
const socket = this.api.socket;
|
const socket = this.api.socket;
|
||||||
const trace_id = getTraceId()
|
const trace_id = getTraceId()
|
||||||
|
|
||||||
@ -48,9 +64,9 @@ class ProtoApiMethods {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class ApiMethods extends ProtoApiMethods {
|
class ApiMethods extends ProtoApiMethods {
|
||||||
async _protoMethod (rqdata) {
|
/*async _protoMethod (rqdata) {
|
||||||
throw new Error("_protoMethod allowed only into abstract class");
|
throw new Error("_protoMethod allowed only into abstract class");
|
||||||
}
|
}*/
|
||||||
|
|
||||||
constructor (api) {
|
constructor (api) {
|
||||||
super(api);
|
super(api);
|
||||||
@ -84,16 +100,28 @@ class ApiMethods extends ProtoApiMethods {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async characters () {
|
async characters (fields = "*") {
|
||||||
return await super._protoMethod({
|
return await super._protoMethod({
|
||||||
method: "characters"
|
method: "characters", fields
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const apiHTMLProtected = new WeakMap();
|
||||||
class ApiHTML {
|
class ApiHTML {
|
||||||
constructor (api) {
|
constructor (api) {
|
||||||
this.api = api;
|
//this.api = api;
|
||||||
|
if (!(api instanceof ApiSocket))
|
||||||
|
throw new Error("api must be instance of ApiSocket class");
|
||||||
|
apiHTMLProtected.set(this, api);
|
||||||
|
}
|
||||||
|
|
||||||
|
set api (_) {
|
||||||
|
throw new Error("api is not writeable");
|
||||||
|
}
|
||||||
|
|
||||||
|
get api () {
|
||||||
|
return apiHTMLProtected.get(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
async renderMainMenu (user, bgUrl = null, favicon = null) {
|
async renderMainMenu (user, bgUrl = null, favicon = null) {
|
||||||
@ -109,8 +137,30 @@ class ApiHTML {
|
|||||||
document.getElementById("server.area").innerHTML = '';
|
document.getElementById("server.area").innerHTML = '';
|
||||||
$(document.getElementById("server.area")).append(ServerAuth.mainMenuForm);
|
$(document.getElementById("server.area")).append(ServerAuth.mainMenuForm);
|
||||||
// JS-Events
|
// JS-Events
|
||||||
const charactersEvents = await this.api.methods.characters();
|
this.api.methods.characters("*")
|
||||||
console.debug({ charactersEvents });
|
.then(characters => {
|
||||||
|
const addCharBtn = document.createElement("button");
|
||||||
|
addCharBtn.setAttribute("type", "button");
|
||||||
|
["btn", "btn-success", "btn-outline-light"].forEach(c =>
|
||||||
|
addCharBtn.classList.add(c));
|
||||||
|
addCharBtn.innerText = "Add character";
|
||||||
|
|
||||||
|
const charlist = $.find("#user-charlist")[0];
|
||||||
|
for (let character of characters) {
|
||||||
|
// TODO: Create add chars to charlist
|
||||||
|
}
|
||||||
|
charlist.append(addCharBtn);
|
||||||
|
|
||||||
|
// Display
|
||||||
|
$.find("#mainmenu-load")[0].hidden = true;
|
||||||
|
$.find("#main-panel")[0].hidden = false;
|
||||||
|
}).catch(err => {
|
||||||
|
// Logging error
|
||||||
|
console.error(err.stack);
|
||||||
|
// Display
|
||||||
|
$.find("#mainmenu-load")[0].hidden = true;
|
||||||
|
$.find("#mainmenu-error")[0].hidden = false;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async renderAuth (authMode, bgUrl = null, favicon = null) {
|
async renderAuth (authMode, bgUrl = null, favicon = null) {
|
||||||
@ -165,6 +215,7 @@ class ApiHTML {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const apiSocketsProtected = new WeakMap();
|
||||||
export class ApiSocket {
|
export class ApiSocket {
|
||||||
constructor ({
|
constructor ({
|
||||||
isTLSmode, address, port
|
isTLSmode, address, port
|
||||||
@ -173,13 +224,23 @@ export class ApiSocket {
|
|||||||
this.address = address;
|
this.address = address;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
|
|
||||||
this.socket = new WebSocket(`${!isTLSmode ? "ws" : "wss"}://${address}:${port}`);
|
//this.socket = new WebSocket(`${!isTLSmode ? "ws" : "wss"}://${address}:${port}`);
|
||||||
|
const socket = new WebSocket(`${!isTLSmode ? "ws" : "wss"}://${address}:${port}`);
|
||||||
|
apiSocketsProtected.set(this, socket);
|
||||||
this.methods = new ApiMethods(this);
|
this.methods = new ApiMethods(this);
|
||||||
this.user = null;
|
this.user = null;
|
||||||
|
|
||||||
this.html = new ApiHTML(this);
|
this.html = new ApiHTML(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set socket (_) {
|
||||||
|
throw new Error("api is not writeable");
|
||||||
|
}
|
||||||
|
|
||||||
|
get socket () {
|
||||||
|
return apiSocketsProtected.get(this);
|
||||||
|
}
|
||||||
|
|
||||||
async run () {
|
async run () {
|
||||||
const socket = this.socket;
|
const socket = this.socket;
|
||||||
const promise = new Promise((rs, rj) => {
|
const promise = new Promise((rs, rj) => {
|
||||||
|
@ -101,17 +101,17 @@ ServerAuth.mainMenuForm.innerHTML = `
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="main-panel" >
|
<div id="main-panel" hidden>
|
||||||
<div id="main-content">
|
<div id="main-content">
|
||||||
<center><h2>Your Profile</h2></center><hr/>
|
<center><h2>Your Profile</h2></center><hr/>
|
||||||
<div class="flex-box">
|
<div class="flex-box">
|
||||||
<div class="characters-menu">
|
<div class="characters-menu">
|
||||||
<center><h4>Characters</h4></center>
|
<center><h4>Characters</h4></center>
|
||||||
<div class="user-character-list btn-group-vertical charlist-item" role="group" aria-label="Characters">
|
<div id="user-charlist" class="user-character-list btn-group-vertical charlist-item" role="group" aria-label="Characters">
|
||||||
<button type="button" class="btn btn-dark btn-outline-light">
|
<!--<button type="button" class="btn btn-dark btn-outline-light">
|
||||||
<div class="flex-box"><img src="https://fullgream.tech/assets/contacts-image.jpg" class="avatar-on-charlist" /><h5>John Doe</h5></div>
|
<div class="flex-box"><img src="https://fullgream.tech/assets/contacts-image.jpg" class="avatar-on-charlist" /><h5>John Doe</h5></div>
|
||||||
</button>
|
</button>
|
||||||
<button type="button" class="btn btn-success btn-outline-light">Add character</button>
|
<button type="button" class="btn btn-success btn-outline-light">Add character</button>-->
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="character-menu">
|
<div class="character-menu">
|
||||||
@ -123,5 +123,7 @@ ServerAuth.mainMenuForm.innerHTML = `
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="mainmenu-load" hidden><center>Loading...</center></div>
|
<div id="mainmenu-load"><center>Loading...</center></div>
|
||||||
|
|
||||||
|
<div id="mainmenu-error"><center>Throwed error</center></div>
|
||||||
`;
|
`;
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
class Logger {
|
class Logger {
|
||||||
static log (...p) {
|
static log(...p) {
|
||||||
console.log("[", new Date(), "]:", ...p);
|
console.log("[", new Date(), "]:", ...p);
|
||||||
}
|
}
|
||||||
static warn (...p) {
|
static warn(...p) {
|
||||||
console.warn("[", new Date(), "]:", ...p);
|
console.warn("[", new Date(), "]:", ...p);
|
||||||
}
|
}
|
||||||
static debug (...p) {
|
static debug(...p) {
|
||||||
console.debug("[", new Date(), "]:", ...p);
|
console.debug("[", new Date(), "]:", ...p);
|
||||||
}
|
}
|
||||||
static wslog (...p) {
|
static wslog(...p) {
|
||||||
// return;
|
// return;
|
||||||
Logger.log("[WebSocket]:", ...p);
|
Logger.log("[WebSocket]:", ...p);
|
||||||
}
|
}
|
||||||
static error (msg, error, level) {
|
static error(msg, error, level) {
|
||||||
console.error("[", new Date(), "]:", msg, `\n${error.stack}`);
|
console.error("[", new Date(), "]:", msg, `\n${error.stack}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,33 +1,38 @@
|
|||||||
function simpleParseHeaders (packet) {
|
function simpleParseHeaders(packet) {
|
||||||
return Object.fromEntries(packet.match(/[a-z\-]{1,}:\s{0,}.{1,}(\n|\r){1,}/gmiu)?.map(i => {
|
return Object.fromEntries(
|
||||||
const [key, value] = i.split(/:\s{0,}/miu);
|
packet.match(/[a-z\-]{1,}:\s{0,}.{1,}(\n|\r){1,}/gimu)?.map((i) => {
|
||||||
return [
|
const [key, value] = i.split(/:\s{0,}/imu);
|
||||||
key.trim(),
|
return [key.trim(), value.trim()];
|
||||||
value.trim()
|
}) ?? [],
|
||||||
];
|
);
|
||||||
}) ?? []);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class PacketParseResult {
|
class PacketParseResult {
|
||||||
constructor (result, isEncrypted = false) {
|
constructor(result, isEncrypted = false) {
|
||||||
this.result = result;
|
this.result = result;
|
||||||
this.isEncrypted = isEncrypted;
|
this.isEncrypted = isEncrypted;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = async (bytes) => {
|
module.exports = async (bytes) => {
|
||||||
//console.log("((('", bytes.toString("utf8"), "')))");
|
//console.log("((('", bytes.toString("utf8"), "')))");
|
||||||
const packet = bytes.toString("utf8");
|
const packet = bytes.toString("utf8");
|
||||||
|
|
||||||
const isHTTP = packet.match(/(GET|POST|PUT|DELETE|OPTIONS|HEAD|PATCH|TRACE|CONNECT)\s{1,}\/[a-zA-Z0-9_\-\.\/?=&%:]{0,}\s{1,}HTTP(S|)\/(1\.0|1\.1|2\.0)(\n|\r)/gmu)?.length > 0 ?? false;
|
const isHTTP =
|
||||||
|
packet.match(
|
||||||
|
/(GET|POST|PUT|DELETE|OPTIONS|HEAD|PATCH|TRACE|CONNECT)\s{1,}\/[a-zA-Z0-9_\-\.\/?=&%:]{0,}\s{1,}HTTP(S|)\/(1\.0|1\.1|2\.0)(\n|\r)/gmu,
|
||||||
|
)?.length > 0 ?? false;
|
||||||
//console.log({ isHTTP });
|
//console.log({ isHTTP });
|
||||||
let isWebSocket = false;
|
let isWebSocket = false;
|
||||||
if (isHTTP) {
|
if (isHTTP) {
|
||||||
const headers = simpleParseHeaders(packet);
|
const headers = simpleParseHeaders(packet);
|
||||||
if (headers.Upgrade === "websocket")
|
if (headers.Upgrade === "websocket") isWebSocket = true;
|
||||||
isWebSocket = true;
|
}
|
||||||
}
|
return new PacketParseResult(
|
||||||
return new PacketParseResult({
|
{
|
||||||
isHTTP, isWebSocket
|
isHTTP,
|
||||||
}, false);
|
isWebSocket,
|
||||||
}
|
},
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user