1
0
mirror of https://github.com/hack-chat/main.git synced 2024-03-22 13:20:33 +08:00
hack-chat-main/commands/core/session.js

201 lines
4.6 KiB
JavaScript
Raw Normal View History

2022-06-23 00:32:51 +08:00
/* eslint import/no-cycle: [0, { ignoreExternal: true }] */
2020-09-17 13:44:32 +08:00
2022-06-23 00:32:51 +08:00
/**
* @author Marzavec ( https://github.com/marzavec )
2023-12-22 15:14:03 +08:00
* @summary Create or restore session
2022-06-23 00:32:51 +08:00
* @version 1.0.0
2023-12-22 15:14:03 +08:00
* @description Restore previous state by session or create new session
2022-06-23 00:32:51 +08:00
* @module session
*/
2020-03-13 02:28:20 +08:00
import {
readFileSync,
} from 'node:fs';
2022-06-23 00:32:51 +08:00
import jsonwebtoken from 'jsonwebtoken';
import {
2023-12-27 16:38:18 +08:00
isModerator,
2022-06-23 00:32:51 +08:00
verifyNickname,
} from '../utility/_UAC.js';
import {
Errors,
} from '../utility/_Constants.js';
import {
restoreJoin,
} from './join.js';
2020-09-08 12:51:47 +08:00
2022-06-23 00:32:51 +08:00
const SessionLocation = './session.key';
/**
2023-12-22 15:14:03 +08:00
* Get a new json web token for the provided socket
* @param {*} socket
* @param {*} core
2022-06-23 00:32:51 +08:00
* @returns {object}
*/
export function getSession(socket, core) {
return jsonwebtoken.sign({
channel: socket.channel,
channels: socket.channels,
color: socket.color,
isBot: socket.isBot,
level: socket.level,
nick: socket.nick,
trip: socket.trip,
userid: socket.userid,
2023-12-22 15:14:03 +08:00
uType: socket.uType,
2022-06-23 00:32:51 +08:00
muzzled: socket.muzzled || false,
banned: socket.banned || false,
}, core.sessionKey, {
expiresIn: '7 days',
2020-09-08 12:51:47 +08:00
});
2022-06-23 00:32:51 +08:00
}
2020-09-08 12:51:47 +08:00
2022-06-23 00:32:51 +08:00
/**
* Reply to target socket with session failure notice
2023-12-22 15:14:03 +08:00
* @param {*} server
* @param {*} socket
2022-06-23 00:32:51 +08:00
* @returns {boolean}
*/
function notifyFailure(server, socket) {
server.reply({
cmd: 'error',
id: Errors.Session.BAD_SESSION,
text: 'Invalid session',
}, socket);
2020-09-08 12:51:47 +08:00
2022-06-23 00:32:51 +08:00
return false;
}
/**
* Executes when invoked by a remote client
* @param {Object} env - Environment object with references to core, server, socket & payload
2022-06-23 00:32:51 +08:00
* @public
* @return {void}
*/
export async function run({
core, server, socket, payload,
}) {
if (typeof payload.token === 'undefined') {
return notifyFailure(server, socket);
}
let session = false;
try {
session = jsonwebtoken.verify(payload.token, core.sessionKey);
} catch (err) {
return notifyFailure(server, socket);
}
// validate session
if (typeof session.channel !== 'string') {
return notifyFailure(server, socket);
}
if (Array.isArray(session.channels) === false) {
return notifyFailure(server, socket);
}
if (typeof session.color !== 'string' && typeof session.color !== 'boolean') {
return notifyFailure(server, socket);
}
if (typeof session.isBot !== 'boolean') {
return notifyFailure(server, socket);
}
if (typeof session.level !== 'number') {
return notifyFailure(server, socket);
}
if (verifyNickname(session.nick) === false) {
return notifyFailure(server, socket);
}
if (typeof session.trip !== 'string') {
return notifyFailure(server, socket);
}
if (typeof session.userid !== 'number') {
return notifyFailure(server, socket);
}
if (typeof session.uType !== 'string') {
return notifyFailure(server, socket);
}
if (typeof session.muzzled !== 'boolean') {
return notifyFailure(server, socket);
}
if (typeof session.banned !== 'boolean') {
return notifyFailure(server, socket);
}
// populate socket info with validated session
socket.channels = [];
socket.color = session.color;
socket.isBot = session.isBot;
socket.level = session.level;
socket.nick = session.nick;
socket.trip = session.trip;
socket.userid = session.userid;
2023-12-22 15:14:03 +08:00
socket.uType = session.uType;
2022-06-23 00:32:51 +08:00
socket.muzzled = session.muzzled;
socket.banned = session.banned;
2020-09-08 12:51:47 +08:00
2023-12-27 16:38:18 +08:00
// global mod perks
if (isModerator(socket.level)) {
socket.ratelimitImmune = true;
}
socket.hash = server.getSocketHash(socket);
2022-06-23 00:32:51 +08:00
socket.hcProtocol = 2;
2023-12-22 15:14:03 +08:00
// dispatch info
server.reply({
cmd: 'session',
restored: true,
token: getSession(socket, core),
channels: socket.channels,
}, socket);
2022-06-23 00:32:51 +08:00
for (let i = 0, j = session.channels.length; i < j; i += 1) {
restoreJoin({
core,
server,
socket,
channel: session.channels[i],
}, true);
}
2020-09-08 12:51:47 +08:00
2022-06-23 00:32:51 +08:00
return true;
}
/**
* Automatically executes once after server is ready
* @param {Object} core - Reference to core environment object
2022-06-23 00:32:51 +08:00
* @public
* @return {void}
*/
export function init(core) {
// load the encryption key if required
if (typeof core.sessionKey === 'undefined') {
core.sessionKey = readFileSync(SessionLocation);
2022-06-23 00:32:51 +08:00
}
2020-09-08 12:51:47 +08:00
}
2020-03-13 02:28:20 +08:00
2022-06-23 00:32:51 +08:00
/**
* Module meta information
* @public
* @typedef {Object} session/info
* @property {string} name - Module command name
* @property {string} category - Module category name
* @property {string} description - Information about module
* @property {string} usage - Information about module usage
*/
2020-03-13 02:28:20 +08:00
export const info = {
name: 'session',
2022-06-23 00:32:51 +08:00
category: 'core',
description: 'Restore previous state by session or create new session',
usage: "API: { cmd: 'session', id: '<previous session>' }",
2020-03-13 02:28:20 +08:00
};