utils.js 11 KB

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