thelounge/server/helper.ts

201 lines
4.5 KiB
TypeScript
Raw Normal View History

import pkg from "../package.json";
import _ from "lodash";
import path from "path";
import os from "os";
import fs from "fs";
import net from "net";
import bcrypt from "bcryptjs";
import crypto from "crypto";
export type Hostmask = {
nick: string;
ident: string;
hostname: string;
};
2014-10-04 23:22:23 +00:00
const Helper = {
expandHome,
getVersion,
getVersionCacheBust,
2020-08-21 11:26:35 +00:00
getVersionNumber,
getGitCommit,
ip2hex,
parseHostmask,
compareHostmask,
compareWithWildcard,
catch_to_error,
2016-10-21 19:00:43 +00:00
password: {
hash: passwordHash,
compare: passwordCompare,
requiresUpdate: passwordRequiresUpdate,
},
};
export default Helper;
2016-05-09 16:19:16 +00:00
function getVersion() {
const gitCommit = getGitCommit();
const version = `v${pkg.version}`;
return gitCommit ? `source (${gitCommit} / ${version})` : version;
}
2020-08-21 11:26:35 +00:00
function getVersionNumber() {
return pkg.version;
}
let _fetchedGitCommit = false;
let _gitCommit: string | null = null;
function getGitCommit() {
if (_fetchedGitCommit) {
return _gitCommit;
}
_fetchedGitCommit = true;
// --git-dir ".git" makes git only check current directory for `.git`, and not travel upwards
// We set cwd to the location of `index.js` as soon as the process is started
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
_gitCommit = require("child_process")
.execSync(
'git --git-dir ".git" rev-parse --short HEAD', // Returns hash of current commit
{stdio: ["ignore", "pipe", "ignore"]}
)
.toString()
.trim();
return _gitCommit;
} catch (e: any) {
// Not a git repository or git is not installed
_gitCommit = null;
return null;
}
}
function getVersionCacheBust() {
const hash = crypto.createHash("sha256").update(Helper.getVersion()).digest("hex");
return hash.substring(0, 10);
}
function ip2hex(address: string) {
2016-11-19 18:32:47 +00:00
// no ipv6 support
if (!net.isIPv4(address)) {
return "00000000";
}
2019-07-17 09:33:59 +00:00
return address
.split(".")
.map(function (octet) {
2019-07-17 09:33:59 +00:00
let hex = parseInt(octet, 10).toString(16);
2016-11-19 18:32:47 +00:00
2019-07-17 09:33:59 +00:00
if (hex.length === 1) {
hex = "0" + hex;
}
2016-11-19 18:32:47 +00:00
2019-07-17 09:33:59 +00:00
return hex;
})
.join("");
2016-11-19 18:32:47 +00:00
}
// Expand ~ into the current user home dir.
// This does *not* support `~other_user/tmp` => `/home/other_user/tmp`.
function expandHome(shortenedPath: string) {
if (!shortenedPath) {
return "";
}
2017-08-16 07:06:20 +00:00
const home = os.homedir().replace("$", "$$$$");
return path.resolve(shortenedPath.replace(/^~($|\/|\\)/, home + "$1"));
}
2016-10-21 19:00:43 +00:00
function passwordRequiresUpdate(password: string) {
2016-10-21 19:00:43 +00:00
return bcrypt.getRounds(password) !== 11;
}
function passwordHash(password: string) {
2016-10-21 19:00:43 +00:00
return bcrypt.hashSync(password, bcrypt.genSaltSync(11));
}
function passwordCompare(password: string, expected: string) {
return bcrypt.compare(password, expected);
2016-10-21 19:00:43 +00:00
}
function parseHostmask(hostmask: string): Hostmask {
let nick = "";
let ident = "*";
let hostname = "*";
let parts: string[] = [];
// Parse hostname first, then parse the rest
parts = hostmask.split("@");
if (parts.length >= 2) {
hostname = parts[1] || "*";
hostmask = parts[0];
}
hostname = hostname.toLowerCase();
parts = hostmask.split("!");
if (parts.length >= 2) {
ident = parts[1] || "*";
hostmask = parts[0];
}
ident = ident.toLowerCase();
nick = hostmask.toLowerCase() || "*";
const result = {
nick: nick,
ident: ident,
hostname: hostname,
};
return result;
}
function compareHostmask(a: Hostmask, b: Hostmask) {
2019-07-17 09:33:59 +00:00
return (
compareWithWildcard(a.nick, b.nick) &&
compareWithWildcard(a.ident, b.ident) &&
compareWithWildcard(a.hostname, b.hostname)
2019-07-17 09:33:59 +00:00
);
}
function compareWithWildcard(a: string, b: string) {
// we allow '*' and '?' wildcards in our comparison.
// this is mostly aligned with https://modern.ircdocs.horse/#wildcard-expressions
// but we do not support the escaping. The ABNF does not seem to be clear as to
// how to escape the escape char '\', which is valid in a nick,
// whereas the wildcards tend not to be (as per RFC1459).
// The "*" wildcard is ".*" in regex, "?" is "."
// so we tokenize and join with the proper char back together,
// escaping any other regex modifier
const wildmany_split = a.split("*").map((sub) => {
const wildone_split = sub.split("?").map((p) => _.escapeRegExp(p));
return wildone_split.join(".");
});
const user_regex = wildmany_split.join(".*");
const re = new RegExp(`^${user_regex}$`, "i"); // case insensitive
return re.test(b);
}
function catch_to_error(prefix: string, err: any): Error {
let msg: string;
if (err instanceof Error) {
msg = err.message;
} else if (typeof err === "string") {
msg = err;
} else {
msg = err.toString();
}
return new Error(`${prefix}: ${msg}`);
}