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