common.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. const ONE_KB = 1024;
  2. const units = ["B", "KB", "MB", "GB", "TB", "PB"];
  3. function sizeFormat(size) {
  4. if (size < 0) {
  5. return "0 B";
  6. }
  7. let index = 0;
  8. while (size >= ONE_KB && index < units.length - 1) {
  9. size /= ONE_KB;
  10. index++;
  11. }
  12. return `${size.toFixed(index === 0 ? 0 : 2)} ${units[index]}`;
  13. }
  14. function cpuSpeedFormat(speed) {
  15. if (speed > 1000) {
  16. const GHz = speed / 1000;
  17. return GHz.toFixed(2) + " GHz";
  18. } else {
  19. return speed.toFixed(2) + " MHz";
  20. }
  21. }
  22. function cpuCoreFormat(cores) {
  23. if (cores === 1) {
  24. return "1 Core";
  25. } else {
  26. return cores + " Cores";
  27. }
  28. }
  29. function base64(str) {
  30. return Base64.encode(str);
  31. }
  32. function safeBase64(str) {
  33. return base64(str)
  34. .replace(/\+/g, '-')
  35. .replace(/=/g, '')
  36. .replace(/\//g, '_');
  37. }
  38. function formatSecond(second) {
  39. if (second < 60) {
  40. return second.toFixed(0) + 's';
  41. } else if (second < 3600) {
  42. return (second / 60).toFixed(0) + 'm';
  43. } else if (second < 3600 * 24) {
  44. return (second / 3600).toFixed(0) + 'h';
  45. } else {
  46. day = Math.floor(second / 3600 / 24);
  47. remain = ((second / 3600) - (day * 24)).toFixed(0);
  48. return day + 'd' + (remain > 0 ? ' ' + remain + 'h' : '');
  49. }
  50. }
  51. function addZero(num) {
  52. if (num < 10) {
  53. return "0" + num;
  54. } else {
  55. return num;
  56. }
  57. }
  58. function toFixed(num, n) {
  59. n = Math.pow(10, n);
  60. return Math.floor(num * n) / n;
  61. }
  62. function debounce(fn, delay) {
  63. var timeoutID = null;
  64. return function () {
  65. clearTimeout(timeoutID);
  66. var args = arguments;
  67. var that = this;
  68. timeoutID = setTimeout(function () {
  69. fn.apply(that, args);
  70. }, delay);
  71. };
  72. }
  73. function getCookie(cname) {
  74. let name = cname + '=';
  75. let ca = document.cookie.split(';');
  76. for (let i = 0; i < ca.length; i++) {
  77. let c = ca[i];
  78. while (c.charAt(0) == ' ') {
  79. c = c.substring(1);
  80. }
  81. if (c.indexOf(name) == 0) {
  82. // decode cookie value only
  83. return decodeURIComponent(c.substring(name.length, c.length));
  84. }
  85. }
  86. return '';
  87. }
  88. function setCookie(cname, cvalue, exdays) {
  89. const d = new Date();
  90. d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
  91. let expires = 'expires=' + d.toUTCString();
  92. // encode cookie value
  93. document.cookie = cname + '=' + encodeURIComponent(cvalue) + ';' + expires + ';path=/';
  94. }
  95. function usageColor(data, threshold, total) {
  96. switch (true) {
  97. case data === null:
  98. return "purple";
  99. case total < 0:
  100. return "green";
  101. case total == 0:
  102. return "purple";
  103. case data < total - threshold:
  104. return "green";
  105. case data < total:
  106. return "orange";
  107. default:
  108. return "red";
  109. }
  110. }
  111. function clientUsageColor(clientStats, trafficDiff) {
  112. switch (true) {
  113. case !clientStats || clientStats.total == 0:
  114. return "#7a316f"; // purple
  115. case clientStats.up + clientStats.down < clientStats.total - trafficDiff:
  116. return "#008771"; // Green
  117. case clientStats.up + clientStats.down < clientStats.total:
  118. return "#f37b24"; // Orange
  119. default:
  120. return "#cf3c3c"; // Red
  121. }
  122. }
  123. function userExpiryColor(threshold, client, isDark = false) {
  124. if (!client.enable) {
  125. return isDark ? '#2c3950' : '#bcbcbc';
  126. }
  127. now = new Date().getTime(),
  128. expiry = client.expiryTime;
  129. switch (true) {
  130. case expiry === null:
  131. return "#7a316f"; // purple
  132. case expiry < 0:
  133. return "#008771"; // Green
  134. case expiry == 0:
  135. return "#7a316f"; // purple
  136. case now < expiry - threshold:
  137. return "#008771"; // Green
  138. case now < expiry:
  139. return "#f37b24"; // Orange
  140. default:
  141. return "#cf3c3c"; // Red
  142. }
  143. }
  144. function doAllItemsExist(array1, array2) {
  145. for (let i = 0; i < array1.length; i++) {
  146. if (!array2.includes(array1[i])) {
  147. return false;
  148. }
  149. }
  150. return true;
  151. }
  152. function buildURL({ host, port, isTLS, base, path }) {
  153. if (!host || host.length === 0) host = window.location.hostname;
  154. if (!port || port.length === 0) port = window.location.port;
  155. if (isTLS === undefined) isTLS = window.location.protocol === "https:";
  156. const protocol = isTLS ? "https:" : "http:";
  157. port = String(port);
  158. if (port === "" || (isTLS && port === "443") || (!isTLS && port === "80")) {
  159. port = "";
  160. } else {
  161. port = `:${port}`;
  162. }
  163. return `${protocol}//${host}${port}${base}${path}`;
  164. }