utils.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. link: getShortURL(link.address, link.domain).url,
  247. }
  248. },
  249. link_html: link => {
  250. const timestamps = parseTimestamps(link);
  251. return {
  252. ...link,
  253. ...timestamps,
  254. banned_by_id: undefined,
  255. domain_id: undefined,
  256. user_id: undefined,
  257. uuid: undefined,
  258. banned: !!link.banned,
  259. id: link.uuid,
  260. relative_created_at: getTimeAgo(timestamps.created_at),
  261. relative_expire_in: link.expire_in && ms(differenceInMilliseconds(parseDatetime(link.expire_in), new Date()), { long: true }),
  262. password: !!link.password,
  263. visit_count: link.visit_count.toLocaleString("en-US"),
  264. link: getShortURL(link.address, link.domain),
  265. }
  266. },
  267. link_admin: link => {
  268. const timestamps = parseTimestamps(link);
  269. return {
  270. ...link,
  271. ...timestamps,
  272. domain: link.domain || env.DEFAULT_DOMAIN,
  273. id: link.uuid,
  274. relative_created_at: getTimeAgo(timestamps.created_at),
  275. relative_expire_in: link.expire_in && ms(differenceInMilliseconds(parseDatetime(link.expire_in), new Date()), { long: true }),
  276. password: !!link.password,
  277. visit_count: link.visit_count.toLocaleString("en-US"),
  278. link: getShortURL(link.address, link.domain)
  279. }
  280. },
  281. user_admin: user => {
  282. const timestamps = parseTimestamps(user);
  283. return {
  284. ...user,
  285. ...timestamps,
  286. links_count: (user.links_count ?? 0).toLocaleString("en-US"),
  287. relative_created_at: getTimeAgo(timestamps.created_at),
  288. relative_updated_at: getTimeAgo(timestamps.updated_at),
  289. }
  290. },
  291. domain_admin: domain => {
  292. const timestamps = parseTimestamps(domain);
  293. return {
  294. ...domain,
  295. ...timestamps,
  296. links_count: (domain.links_count ?? 0).toLocaleString("en-US"),
  297. relative_created_at: getTimeAgo(timestamps.created_at),
  298. relative_updated_at: getTimeAgo(timestamps.updated_at),
  299. }
  300. }
  301. };
  302. function sleep(ms) {
  303. return new Promise(resolve => setTimeout(resolve, ms));
  304. }
  305. function removeWww(host) {
  306. return host.replace("www.", "");
  307. };
  308. function registerHandlebarsHelpers() {
  309. hbs.registerHelper("ifEquals", function(arg1, arg2, options) {
  310. return (arg1 === arg2) ? options.fn(this) : options.inverse(this);
  311. });
  312. hbs.registerHelper("json", function(context) {
  313. return JSON.stringify(context);
  314. });
  315. const blocks = {};
  316. hbs.registerHelper("extend", function(name, context) {
  317. let block = blocks[name];
  318. if (!block) {
  319. block = blocks[name] = [];
  320. }
  321. block.push(context.fn(this));
  322. });
  323. hbs.registerHelper("block", function(name) {
  324. const val = (blocks[name] || []).join('\n');
  325. blocks[name] = [];
  326. return val;
  327. });
  328. hbs.registerPartials(path.join(__dirname, "../views/partials"), function (err) {});
  329. const customPartialsPath = path.join(__dirname, "../../custom/views/partials");
  330. const customPartialsExist = fs.existsSync(customPartialsPath);
  331. if (customPartialsExist) {
  332. hbs.registerPartials(customPartialsPath, function (err) {});
  333. }
  334. }
  335. // grab custom styles file name from the custom/css folder
  336. const custom_css_file_names = [];
  337. const customCSSPath = path.join(__dirname, "../../custom/css");
  338. const customCSSExists = fs.existsSync(customCSSPath);
  339. if (customCSSExists) {
  340. fs.readdir(customCSSPath, function(error, files) {
  341. if (error) {
  342. console.warn("Could not read the custom CSS folder:", error);
  343. } else {
  344. files.forEach(function(file_name) {
  345. custom_css_file_names.push(file_name);
  346. });
  347. }
  348. })
  349. }
  350. function getCustomCSSFileNames() {
  351. return custom_css_file_names;
  352. }
  353. module.exports = {
  354. addProtocol,
  355. customAddressRegex,
  356. customAlphabetRegex,
  357. CustomError,
  358. dateToUTC,
  359. deleteCurrentToken,
  360. generateId,
  361. getCustomCSSFileNames,
  362. getDifferenceFunction,
  363. getInitStats,
  364. getShortURL,
  365. getStatsPeriods,
  366. isAdmin,
  367. parseBooleanQuery,
  368. parseDatetime,
  369. parseTimestamps,
  370. preservedURLs,
  371. registerHandlebarsHelpers,
  372. removeWww,
  373. sanitize,
  374. setToken,
  375. signToken,
  376. sleep,
  377. statsObjectToArray,
  378. urlRegex,
  379. ...knexUtils,
  380. }