common.js 5.0 KB

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