utils.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. const { differenceInDays, differenceInHours, differenceInMonths, differenceInMilliseconds, addDays, subHours, subDays, subMonths, subYears, format } = require("date-fns");
  2. const { customAlphabet } = require("nanoid");
  3. const JWT = require("jsonwebtoken");
  4. const path = require("node:path");
  5. const fs = require("node:fs");
  6. const hbs = require("hbs");
  7. const ms = require("ms");
  8. const { ROLES } = require("../consts");
  9. const knexUtils = require("./knex");
  10. const knex = require("../knex");
  11. const env = require("../env");
  12. const nanoid = customAlphabet(env.LINK_CUSTOM_ALPHABET, env.LINK_LENGTH);
  13. class CustomError extends Error {
  14. constructor(message, statusCode, data) {
  15. super(message);
  16. this.name = this.constructor.name;
  17. this.statusCode = statusCode ?? 500;
  18. this.data = data;
  19. }
  20. }
  21. const urlRegex = /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i;
  22. const charsNeedEscapeInRegExp = ".$*+?()[]{}|^-";
  23. const customAlphabetEscaped = env.LINK_CUSTOM_ALPHABET
  24. .split("").map(c => charsNeedEscapeInRegExp.includes(c) ? "\\" + c : c).join("");
  25. const customAlphabetRegex = new RegExp(`^[${customAlphabetEscaped}_-]+$`);
  26. const customAddressRegex = new RegExp("^[a-zA-Z0-9-_]+$");
  27. function isAdmin(user) {
  28. return user.role === ROLES.ADMIN;
  29. }
  30. function signToken(user) {
  31. return JWT.sign(
  32. {
  33. iss: "ApiAuth",
  34. sub: user.id,
  35. iat: parseInt((new Date().getTime() / 1000).toFixed(0)),
  36. exp: parseInt((addDays(new Date(), 7).getTime() / 1000).toFixed(0))
  37. },
  38. env.JWT_SECRET
  39. )
  40. }
  41. function setToken(res, token) {
  42. res.cookie("token", token, {
  43. maxAge: 1000 * 60 * 60 * 24 * 7, // expire after seven days
  44. httpOnly: true,
  45. secure: env.isProd
  46. });
  47. }
  48. function deleteCurrentToken(res) {
  49. res.clearCookie("token", { httpOnly: true, secure: env.isProd });
  50. }
  51. async function generateId(query, domain_id) {
  52. const address = nanoid();
  53. const link = await query.link.find({ address, domain_id });
  54. if (link) {
  55. return generateId(query, domain_id)
  56. };
  57. return address;
  58. }
  59. function addProtocol(url) {
  60. const hasProtocol = /^(\w+:|\/\/)/.test(url);
  61. return hasProtocol ? url : "http://" + url;
  62. }
  63. function getShortURL(address, domain) {
  64. const protocol = (env.CUSTOM_DOMAIN_USE_HTTPS || !domain) && !env.isDev ? "https://" : "http://";
  65. const link = `${domain || env.DEFAULT_DOMAIN}/${address}`;
  66. const url = `${protocol}${link}`;
  67. return { address, link, url };
  68. }
  69. function statsObjectToArray(obj) {
  70. const objToArr = (key) =>
  71. Array.from(Object.keys(obj[key]))
  72. .map((name) => ({
  73. name,
  74. value: obj[key][name]
  75. }))
  76. .sort((a, b) => b.value - a.value);
  77. return {
  78. browser: objToArr("browser"),
  79. os: objToArr("os"),
  80. country: objToArr("country"),
  81. referrer: objToArr("referrer")
  82. };
  83. }
  84. function getDifferenceFunction(type) {
  85. if (type === "lastDay") return differenceInHours;
  86. if (type === "lastWeek") return differenceInDays;
  87. if (type === "lastMonth") return differenceInDays;
  88. if (type === "lastYear") return differenceInMonths;
  89. throw new Error("Unknown type.");
  90. }
  91. function parseDatetime(date) {
  92. // because postgres and mysql return date, sqlite returns formatted iso 8601 string in utc
  93. return date instanceof Date ? date : new Date(date + "Z");
  94. }
  95. function parseTimestamps(item) {
  96. return {
  97. created_at: parseDatetime(item.created_at),
  98. updated_at: parseDatetime(item.updated_at),
  99. }
  100. }
  101. function dateToUTC(date) {
  102. const dateUTC = date instanceof Date ? date.toISOString() : new Date(date).toISOString();
  103. // format the utc date in 'YYYY-MM-DD hh:mm:ss' for SQLite
  104. if (knex.isSQLite) {
  105. return dateUTC.substring(0, 10) + " " + dateUTC.substring(11, 19);
  106. }
  107. // mysql doesn't save time in utc, so format the date in local timezone instead
  108. if (knex.isMySQL) {
  109. return format(new Date(date), "yyyy-MM-dd HH:mm:ss");
  110. }
  111. // return unformatted utc string for postgres
  112. return dateUTC;
  113. }
  114. function getStatsPeriods(now) {
  115. return [
  116. ["lastDay", subHours(now, 24)],
  117. ["lastWeek", subDays(now, 7)],
  118. ["lastMonth", subDays(now, 30)],
  119. ["lastYear", subMonths(now, 12)],
  120. ]
  121. }
  122. const preservedURLs = [
  123. "login",
  124. "logout",
  125. "create-admin",
  126. "404",
  127. "settings",
  128. "admin",
  129. "stats",
  130. "signup",
  131. "banned",
  132. "report",
  133. "reset-password",
  134. "resetpassword",
  135. "verify-email",
  136. "verifyemail",
  137. "verify",
  138. "terms",
  139. "confirm-link-delete",
  140. "confirm-link-ban",
  141. "confirm-user-delete",
  142. "confirm-user-ban",
  143. "create-user",
  144. "confirm-domain-delete-admin",
  145. "confirm-domain-ban",
  146. "add-domain-form",
  147. "confirm-domain-delete",
  148. "get-report-email",
  149. "get-support-email",
  150. "link",
  151. "admin",
  152. "url-password",
  153. "url-info",
  154. "api",
  155. "static",
  156. "images",
  157. "privacy",
  158. "protected",
  159. "css",
  160. "fonts",
  161. "libs",
  162. "pricing"
  163. ];
  164. function parseBooleanQuery(query) {
  165. if (query === "true" || query === true) return true;
  166. if (query === "false" || query === false) return false;
  167. return undefined;
  168. }
  169. function getInitStats() {
  170. return Object.create({
  171. browser: {
  172. chrome: 0,
  173. edge: 0,
  174. firefox: 0,
  175. ie: 0,
  176. opera: 0,
  177. other: 0,
  178. safari: 0
  179. },
  180. os: {
  181. android: 0,
  182. ios: 0,
  183. linux: 0,
  184. macos: 0,
  185. other: 0,
  186. windows: 0
  187. },
  188. country: {},
  189. referrer: {}
  190. });
  191. }
  192. // format date to relative date
  193. const MINUTE = 60,
  194. HOUR = MINUTE * 60,
  195. DAY = HOUR * 24,
  196. WEEK = DAY * 7,
  197. MONTH = DAY * 30,
  198. YEAR = DAY * 365;
  199. function getTimeAgo(dateString) {
  200. const date = new Date(dateString);
  201. const secondsAgo = Math.round((Date.now() - Number(date)) / 1000);
  202. if (secondsAgo < MINUTE) {
  203. return `${secondsAgo} second${secondsAgo !== 1 ? "s" : ""} ago`;
  204. }
  205. let divisor;
  206. let unit = "";
  207. if (secondsAgo < HOUR) {
  208. [divisor, unit] = [MINUTE, "minute"];
  209. } else if (secondsAgo < DAY) {
  210. [divisor, unit] = [HOUR, "hour"];
  211. } else if (secondsAgo < WEEK) {
  212. [divisor, unit] = [DAY, "day"];
  213. } else if (secondsAgo < MONTH) {
  214. [divisor, unit] = [WEEK, "week"];
  215. } else if (secondsAgo < YEAR) {
  216. [divisor, unit] = [MONTH, "month"];
  217. } else {
  218. [divisor, unit] = [YEAR, "year"];
  219. }
  220. const count = Math.floor(secondsAgo / divisor);
  221. return `${count} ${unit}${count > 1 ? "s" : ""} ago`;
  222. }
  223. const sanitize = {
  224. domain: domain => ({
  225. ...domain,
  226. ...parseTimestamps(domain),
  227. id: domain.uuid,
  228. banned: !!domain.banned,
  229. homepage: domain.homepage || env.DEFAULT_DOMAIN,
  230. uuid: undefined,
  231. user_id: undefined,
  232. banned_by_id: undefined
  233. }),
  234. link: link => {
  235. const timestamps = parseTimestamps(link);
  236. return {
  237. ...link,
  238. ...timestamps,
  239. banned_by_id: undefined,
  240. domain_id: undefined,
  241. user_id: undefined,
  242. uuid: undefined,
  243. banned: !!link.banned,
  244. id: link.uuid,
  245. password: !!link.password,
  246. }
  247. },
  248. link_html: link => {
  249. const timestamps = parseTimestamps(link);
  250. return {
  251. ...link,
  252. ...timestamps,
  253. banned_by_id: undefined,
  254. domain_id: undefined,
  255. user_id: undefined,
  256. uuid: undefined,
  257. banned: !!link.banned,
  258. id: link.uuid,
  259. relative_created_at: getTimeAgo(timestamps.created_at),
  260. relative_expire_in: link.expire_in && ms(differenceInMilliseconds(parseDatetime(link.expire_in), new Date()), { long: true }),
  261. password: !!link.password,
  262. visit_count: link.visit_count.toLocaleString("en-US"),
  263. link: getShortURL(link.address, link.domain),
  264. }
  265. },
  266. link_admin: link => {
  267. const timestamps = parseTimestamps(link);
  268. return {
  269. ...link,
  270. ...timestamps,
  271. domain: link.domain || env.DEFAULT_DOMAIN,
  272. id: link.uuid,
  273. relative_created_at: getTimeAgo(timestamps.created_at),
  274. relative_expire_in: link.expire_in && ms(differenceInMilliseconds(parseDatetime(link.expire_in), new Date()), { long: true }),
  275. password: !!link.password,
  276. visit_count: link.visit_count.toLocaleString("en-US"),
  277. link: getShortURL(link.address, link.domain)
  278. }
  279. },
  280. user_admin: user => {
  281. const timestamps = parseTimestamps(user);
  282. return {
  283. ...user,
  284. ...timestamps,
  285. links_count: (user.links_count ?? 0).toLocaleString("en-US"),
  286. relative_created_at: getTimeAgo(timestamps.created_at),
  287. relative_updated_at: getTimeAgo(timestamps.updated_at),
  288. }
  289. },
  290. domain_admin: domain => {
  291. const timestamps = parseTimestamps(domain);
  292. return {
  293. ...domain,
  294. ...timestamps,
  295. links_count: (domain.links_count ?? 0).toLocaleString("en-US"),
  296. relative_created_at: getTimeAgo(timestamps.created_at),
  297. relative_updated_at: getTimeAgo(timestamps.updated_at),
  298. }
  299. }
  300. };
  301. function sleep(ms) {
  302. return new Promise(resolve => setTimeout(resolve, ms));
  303. }
  304. function removeWww(host) {
  305. return host.replace("www.", "");
  306. };
  307. function registerHandlebarsHelpers() {
  308. hbs.registerHelper("ifEquals", function(arg1, arg2, options) {
  309. return (arg1 === arg2) ? options.fn(this) : options.inverse(this);
  310. });
  311. hbs.registerHelper("json", function(context) {
  312. return JSON.stringify(context);
  313. });
  314. const blocks = {};
  315. hbs.registerHelper("extend", function(name, context) {
  316. let block = blocks[name];
  317. if (!block) {
  318. block = blocks[name] = [];
  319. }
  320. block.push(context.fn(this));
  321. });
  322. hbs.registerHelper("block", function(name) {
  323. const val = (blocks[name] || []).join('\n');
  324. blocks[name] = [];
  325. return val;
  326. });
  327. hbs.registerPartials(path.join(__dirname, "../views/partials"), function (err) {});
  328. const customPartialsPath = path.join(__dirname, "../../custom/views/partials");
  329. const customPartialsExist = fs.existsSync(customPartialsPath);
  330. if (customPartialsExist) {
  331. hbs.registerPartials(customPartialsPath, function (err) {});
  332. }
  333. }
  334. // grab custom styles file name from the custom/css folder
  335. const custom_css_file_names = [];
  336. const customCSSPath = path.join(__dirname, "../../custom/css");
  337. const customCSSExists = fs.existsSync(customCSSPath);
  338. if (customCSSExists) {
  339. fs.readdir(customCSSPath, function(error, files) {
  340. if (error) {
  341. console.warn("Could not read the custom CSS folder:", error);
  342. } else {
  343. files.forEach(function(file_name) {
  344. custom_css_file_names.push(file_name);
  345. });
  346. }
  347. })
  348. }
  349. function getCustomCSSFileNames() {
  350. return custom_css_file_names;
  351. }
  352. module.exports = {
  353. addProtocol,
  354. customAddressRegex,
  355. customAlphabetRegex,
  356. CustomError,
  357. dateToUTC,
  358. deleteCurrentToken,
  359. generateId,
  360. getCustomCSSFileNames,
  361. getDifferenceFunction,
  362. getInitStats,
  363. getShortURL,
  364. getStatsPeriods,
  365. isAdmin,
  366. parseBooleanQuery,
  367. parseDatetime,
  368. parseTimestamps,
  369. preservedURLs,
  370. registerHandlebarsHelpers,
  371. removeWww,
  372. sanitize,
  373. setToken,
  374. signToken,
  375. sleep,
  376. statsObjectToArray,
  377. urlRegex,
  378. ...knexUtils,
  379. }