index.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. class Msg {
  2. constructor(success = false, msg = "", obj = null) {
  3. this.success = success;
  4. this.msg = msg;
  5. this.obj = obj;
  6. }
  7. }
  8. class HttpUtil {
  9. static _handleMsg(msg) {
  10. if (!(msg instanceof Msg) || msg.msg === "") {
  11. return;
  12. }
  13. const messageType = msg.success ? 'success' : 'error';
  14. Vue.prototype.$message[messageType](msg.msg);
  15. }
  16. static _respToMsg(resp) {
  17. if (!resp || !resp.data) {
  18. return new Msg(false, 'No response data');
  19. }
  20. const { data } = resp;
  21. if (data == null) {
  22. return new Msg(true);
  23. }
  24. if (typeof data === 'object' && 'success' in data) {
  25. return new Msg(data.success, data.msg, data.obj);
  26. }
  27. return typeof data === 'object' ? data : new Msg(false, 'unknown data:', data);
  28. }
  29. static async get(url, params, options = {}) {
  30. try {
  31. const resp = await axios.get(url, { params, ...options });
  32. const msg = this._respToMsg(resp);
  33. this._handleMsg(msg);
  34. return msg;
  35. } catch (error) {
  36. console.error('GET request failed:', error);
  37. const errorMsg = new Msg(false, error.response?.data?.message || error.message || 'Request failed');
  38. this._handleMsg(errorMsg);
  39. return errorMsg;
  40. }
  41. }
  42. static async post(url, data, options = {}) {
  43. try {
  44. const resp = await axios.post(url, data, options);
  45. const msg = this._respToMsg(resp);
  46. this._handleMsg(msg);
  47. return msg;
  48. } catch (error) {
  49. console.error('POST request failed:', error);
  50. const errorMsg = new Msg(false, error.response?.data?.message || error.message || 'Request failed');
  51. this._handleMsg(errorMsg);
  52. return errorMsg;
  53. }
  54. }
  55. static async postWithModal(url, data, modal) {
  56. if (modal) {
  57. modal.loading(true);
  58. }
  59. const msg = await this.post(url, data);
  60. if (modal) {
  61. modal.loading(false);
  62. if (msg instanceof Msg && msg.success) {
  63. modal.close();
  64. }
  65. }
  66. return msg;
  67. }
  68. }
  69. class PromiseUtil {
  70. static async sleep(timeout) {
  71. await new Promise(resolve => {
  72. setTimeout(resolve, timeout)
  73. });
  74. }
  75. }
  76. class RandomUtil {
  77. static getSeq({ type = "default", hasNumbers = true, hasLowercase = true, hasUppercase = true } = {}) {
  78. let seq = '';
  79. switch (type) {
  80. case "hex":
  81. seq += "0123456789abcdef";
  82. break;
  83. default:
  84. if (hasNumbers) seq += "0123456789";
  85. if (hasLowercase) seq += "abcdefghijklmnopqrstuvwxyz";
  86. if (hasUppercase) seq += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  87. break;
  88. }
  89. return seq;
  90. }
  91. static randomInteger(min, max) {
  92. const range = max - min + 1;
  93. const randomBuffer = new Uint32Array(1);
  94. window.crypto.getRandomValues(randomBuffer);
  95. return Math.floor((randomBuffer[0] / (0xFFFFFFFF + 1)) * range) + min;
  96. }
  97. static randomSeq(count, options = {}) {
  98. const seq = this.getSeq(options);
  99. const seqLength = seq.length;
  100. const randomValues = new Uint32Array(count);
  101. window.crypto.getRandomValues(randomValues);
  102. return Array.from(randomValues, v => seq[v % seqLength]).join('');
  103. }
  104. static randomShortIds() {
  105. const lengths = [2, 4, 6, 8, 10, 12, 14, 16].sort(() => Math.random() - 0.5);
  106. return lengths.map(len => this.randomSeq(len, { type: "hex" })).join(',');
  107. }
  108. static randomLowerAndNum(len) {
  109. return this.randomSeq(len, { hasUppercase: false });
  110. }
  111. static randomUUID() {
  112. if (window.location.protocol === "https:") {
  113. return window.crypto.randomUUID();
  114. } else {
  115. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
  116. .replace(/[xy]/g, function (c) {
  117. const randomValues = new Uint8Array(1);
  118. window.crypto.getRandomValues(randomValues);
  119. let randomValue = randomValues[0] % 16;
  120. let calculatedValue = (c === 'x') ? randomValue : (randomValue & 0x3 | 0x8);
  121. return calculatedValue.toString(16);
  122. });
  123. }
  124. }
  125. static randomShadowsocksPassword() {
  126. const array = new Uint8Array(32);
  127. window.crypto.getRandomValues(array);
  128. return Base64.alternativeEncode(String.fromCharCode(...array));
  129. }
  130. static randomBase32String(length = 16) {
  131. const array = new Uint8Array(length);
  132. window.crypto.getRandomValues(array);
  133. const base32Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
  134. let result = '';
  135. let bits = 0;
  136. let buffer = 0;
  137. for (let i = 0; i < array.length; i++) {
  138. buffer = (buffer << 8) | array[i];
  139. bits += 8;
  140. while (bits >= 5) {
  141. bits -= 5;
  142. result += base32Chars[(buffer >>> bits) & 0x1F];
  143. }
  144. }
  145. if (bits > 0) {
  146. result += base32Chars[(buffer << (5 - bits)) & 0x1F];
  147. }
  148. return result;
  149. }
  150. }
  151. class ObjectUtil {
  152. static getPropIgnoreCase(obj, prop) {
  153. for (const name in obj) {
  154. if (!obj.hasOwnProperty(name)) {
  155. continue;
  156. }
  157. if (name.toLowerCase() === prop.toLowerCase()) {
  158. return obj[name];
  159. }
  160. }
  161. return undefined;
  162. }
  163. static deepSearch(obj, key) {
  164. if (obj instanceof Array) {
  165. for (let i = 0; i < obj.length; ++i) {
  166. if (this.deepSearch(obj[i], key)) {
  167. return true;
  168. }
  169. }
  170. } else if (obj instanceof Object) {
  171. for (let name in obj) {
  172. if (!obj.hasOwnProperty(name)) {
  173. continue;
  174. }
  175. if (this.deepSearch(obj[name], key)) {
  176. return true;
  177. }
  178. }
  179. } else {
  180. return this.isEmpty(obj) ? false : obj.toString().toLowerCase().indexOf(key.toLowerCase()) >= 0;
  181. }
  182. return false;
  183. }
  184. static isEmpty(obj) {
  185. return obj === null || obj === undefined || obj === '';
  186. }
  187. static isArrEmpty(arr) {
  188. return !this.isEmpty(arr) && arr.length === 0;
  189. }
  190. static copyArr(dest, src) {
  191. dest.splice(0);
  192. for (const item of src) {
  193. dest.push(item);
  194. }
  195. }
  196. static clone(obj) {
  197. let newObj;
  198. if (obj instanceof Array) {
  199. newObj = [];
  200. this.copyArr(newObj, obj);
  201. } else if (obj instanceof Object) {
  202. newObj = {};
  203. for (const key of Object.keys(obj)) {
  204. newObj[key] = obj[key];
  205. }
  206. } else {
  207. newObj = obj;
  208. }
  209. return newObj;
  210. }
  211. static deepClone(obj) {
  212. let newObj;
  213. if (obj instanceof Array) {
  214. newObj = [];
  215. for (const item of obj) {
  216. newObj.push(this.deepClone(item));
  217. }
  218. } else if (obj instanceof Object) {
  219. newObj = {};
  220. for (const key of Object.keys(obj)) {
  221. newObj[key] = this.deepClone(obj[key]);
  222. }
  223. } else {
  224. newObj = obj;
  225. }
  226. return newObj;
  227. }
  228. static cloneProps(dest, src, ...ignoreProps) {
  229. if (dest == null || src == null) {
  230. return;
  231. }
  232. const ignoreEmpty = this.isArrEmpty(ignoreProps);
  233. for (const key of Object.keys(src)) {
  234. if (!src.hasOwnProperty(key)) {
  235. continue;
  236. } else if (!dest.hasOwnProperty(key)) {
  237. continue;
  238. } else if (src[key] === undefined) {
  239. continue;
  240. }
  241. if (ignoreEmpty) {
  242. dest[key] = src[key];
  243. } else {
  244. let ignore = false;
  245. for (let i = 0; i < ignoreProps.length; ++i) {
  246. if (key === ignoreProps[i]) {
  247. ignore = true;
  248. break;
  249. }
  250. }
  251. if (!ignore) {
  252. dest[key] = src[key];
  253. }
  254. }
  255. }
  256. }
  257. static delProps(obj, ...props) {
  258. for (const prop of props) {
  259. if (prop in obj) {
  260. delete obj[prop];
  261. }
  262. }
  263. }
  264. static execute(func, ...args) {
  265. if (!this.isEmpty(func) && typeof func === 'function') {
  266. func(...args);
  267. }
  268. }
  269. static orDefault(obj, defaultValue) {
  270. if (obj == null) {
  271. return defaultValue;
  272. }
  273. return obj;
  274. }
  275. static equals(a, b) {
  276. for (const key in a) {
  277. if (!a.hasOwnProperty(key)) {
  278. continue;
  279. }
  280. if (!b.hasOwnProperty(key)) {
  281. return false;
  282. } else if (a[key] !== b[key]) {
  283. return false;
  284. }
  285. }
  286. return true;
  287. }
  288. }
  289. class Wireguard {
  290. static gf(init) {
  291. var r = new Float64Array(16);
  292. if (init) {
  293. for (var i = 0; i < init.length; ++i)
  294. r[i] = init[i];
  295. }
  296. return r;
  297. }
  298. static pack(o, n) {
  299. var b, m = this.gf(), t = this.gf();
  300. for (var i = 0; i < 16; ++i)
  301. t[i] = n[i];
  302. this.carry(t);
  303. this.carry(t);
  304. this.carry(t);
  305. for (var j = 0; j < 2; ++j) {
  306. m[0] = t[0] - 0xffed;
  307. for (var i = 1; i < 15; ++i) {
  308. m[i] = t[i] - 0xffff - ((m[i - 1] >> 16) & 1);
  309. m[i - 1] &= 0xffff;
  310. }
  311. m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1);
  312. b = (m[15] >> 16) & 1;
  313. m[14] &= 0xffff;
  314. this.cswap(t, m, 1 - b);
  315. }
  316. for (var i = 0; i < 16; ++i) {
  317. o[2 * i] = t[i] & 0xff;
  318. o[2 * i + 1] = t[i] >> 8;
  319. }
  320. }
  321. static carry(o) {
  322. var c;
  323. for (var i = 0; i < 16; ++i) {
  324. o[(i + 1) % 16] += (i < 15 ? 1 : 38) * Math.floor(o[i] / 65536);
  325. o[i] &= 0xffff;
  326. }
  327. }
  328. static cswap(p, q, b) {
  329. var t, c = ~(b - 1);
  330. for (var i = 0; i < 16; ++i) {
  331. t = c & (p[i] ^ q[i]);
  332. p[i] ^= t;
  333. q[i] ^= t;
  334. }
  335. }
  336. static add(o, a, b) {
  337. for (var i = 0; i < 16; ++i)
  338. o[i] = (a[i] + b[i]) | 0;
  339. }
  340. static subtract(o, a, b) {
  341. for (var i = 0; i < 16; ++i)
  342. o[i] = (a[i] - b[i]) | 0;
  343. }
  344. static multmod(o, a, b) {
  345. var t = new Float64Array(31);
  346. for (var i = 0; i < 16; ++i) {
  347. for (var j = 0; j < 16; ++j)
  348. t[i + j] += a[i] * b[j];
  349. }
  350. for (var i = 0; i < 15; ++i)
  351. t[i] += 38 * t[i + 16];
  352. for (var i = 0; i < 16; ++i)
  353. o[i] = t[i];
  354. this.carry(o);
  355. this.carry(o);
  356. }
  357. static invert(o, i) {
  358. var c = this.gf();
  359. for (var a = 0; a < 16; ++a)
  360. c[a] = i[a];
  361. for (var a = 253; a >= 0; --a) {
  362. this.multmod(c, c, c);
  363. if (a !== 2 && a !== 4)
  364. this.multmod(c, c, i);
  365. }
  366. for (var a = 0; a < 16; ++a)
  367. o[a] = c[a];
  368. }
  369. static clamp(z) {
  370. z[31] = (z[31] & 127) | 64;
  371. z[0] &= 248;
  372. }
  373. static generatePublicKey(privateKey) {
  374. var r, z = new Uint8Array(32);
  375. var a = this.gf([1]),
  376. b = this.gf([9]),
  377. c = this.gf(),
  378. d = this.gf([1]),
  379. e = this.gf(),
  380. f = this.gf(),
  381. _121665 = this.gf([0xdb41, 1]),
  382. _9 = this.gf([9]);
  383. for (var i = 0; i < 32; ++i)
  384. z[i] = privateKey[i];
  385. this.clamp(z);
  386. for (var i = 254; i >= 0; --i) {
  387. r = (z[i >>> 3] >>> (i & 7)) & 1;
  388. this.cswap(a, b, r);
  389. this.cswap(c, d, r);
  390. this.add(e, a, c);
  391. this.subtract(a, a, c);
  392. this.add(c, b, d);
  393. this.subtract(b, b, d);
  394. this.multmod(d, e, e);
  395. this.multmod(f, a, a);
  396. this.multmod(a, c, a);
  397. this.multmod(c, b, e);
  398. this.add(e, a, c);
  399. this.subtract(a, a, c);
  400. this.multmod(b, a, a);
  401. this.subtract(c, d, f);
  402. this.multmod(a, c, _121665);
  403. this.add(a, a, d);
  404. this.multmod(c, c, a);
  405. this.multmod(a, d, f);
  406. this.multmod(d, b, _9);
  407. this.multmod(b, e, e);
  408. this.cswap(a, b, r);
  409. this.cswap(c, d, r);
  410. }
  411. this.invert(c, c);
  412. this.multmod(a, a, c);
  413. this.pack(z, a);
  414. return z;
  415. }
  416. static generatePresharedKey() {
  417. var privateKey = new Uint8Array(32);
  418. window.crypto.getRandomValues(privateKey);
  419. return privateKey;
  420. }
  421. static generatePrivateKey() {
  422. var privateKey = this.generatePresharedKey();
  423. this.clamp(privateKey);
  424. return privateKey;
  425. }
  426. static encodeBase64(dest, src) {
  427. var input = Uint8Array.from([(src[0] >> 2) & 63, ((src[0] << 4) | (src[1] >> 4)) & 63, ((src[1] << 2) | (src[2] >> 6)) & 63, src[2] & 63]);
  428. for (var i = 0; i < 4; ++i)
  429. dest[i] = input[i] + 65 +
  430. (((25 - input[i]) >> 8) & 6) -
  431. (((51 - input[i]) >> 8) & 75) -
  432. (((61 - input[i]) >> 8) & 15) +
  433. (((62 - input[i]) >> 8) & 3);
  434. }
  435. static keyToBase64(key) {
  436. var i, base64 = new Uint8Array(44);
  437. for (i = 0; i < 32 / 3; ++i)
  438. this.encodeBase64(base64.subarray(i * 4), key.subarray(i * 3));
  439. this.encodeBase64(base64.subarray(i * 4), Uint8Array.from([key[i * 3 + 0], key[i * 3 + 1], 0]));
  440. base64[43] = 61;
  441. return String.fromCharCode.apply(null, base64);
  442. }
  443. static keyFromBase64(encoded) {
  444. const binaryStr = atob(encoded);
  445. const bytes = new Uint8Array(binaryStr.length);
  446. for (let i = 0; i < binaryStr.length; i++) {
  447. bytes[i] = binaryStr.charCodeAt(i);
  448. }
  449. return bytes;
  450. }
  451. static generateKeypair(secretKey = '') {
  452. var privateKey = secretKey.length > 0 ? this.keyFromBase64(secretKey) : this.generatePrivateKey();
  453. var publicKey = this.generatePublicKey(privateKey);
  454. return {
  455. publicKey: this.keyToBase64(publicKey),
  456. privateKey: secretKey.length > 0 ? secretKey : this.keyToBase64(privateKey)
  457. };
  458. }
  459. }
  460. class ClipboardManager {
  461. static copyText(content = "") {
  462. // !! here old way of copying is used because not everyone can afford https connection
  463. return new Promise((resolve) => {
  464. try {
  465. const textarea = window.document.createElement('textarea');
  466. textarea.style.fontSize = '12pt';
  467. textarea.style.border = '0';
  468. textarea.style.padding = '0';
  469. textarea.style.margin = '0';
  470. textarea.style.position = 'absolute';
  471. textarea.style.left = '-9999px';
  472. textarea.style.top = `${window.pageYOffset || document.documentElement.scrollTop}px`;
  473. textarea.setAttribute('readonly', '');
  474. textarea.value = content;
  475. window.document.body.appendChild(textarea);
  476. textarea.select();
  477. window.document.execCommand("copy");
  478. window.document.body.removeChild(textarea);
  479. resolve(true)
  480. } catch {
  481. resolve(false)
  482. }
  483. })
  484. }
  485. }
  486. class Base64 {
  487. static encode(content = "", safe = false) {
  488. if (safe) {
  489. return Base64.encode(content)
  490. .replace(/\+/g, '-')
  491. .replace(/=/g, '')
  492. .replace(/\//g, '_')
  493. }
  494. return window.btoa(
  495. String.fromCharCode(...new TextEncoder().encode(content))
  496. )
  497. }
  498. static alternativeEncode(content) {
  499. return window.btoa(
  500. content
  501. )
  502. }
  503. static decode(content = "") {
  504. return new TextDecoder()
  505. .decode(
  506. Uint8Array.from(window.atob(content), c => c.charCodeAt(0))
  507. )
  508. }
  509. }
  510. class SizeFormatter {
  511. static ONE_KB = 1024;
  512. static ONE_MB = this.ONE_KB * 1024;
  513. static ONE_GB = this.ONE_MB * 1024;
  514. static ONE_TB = this.ONE_GB * 1024;
  515. static ONE_PB = this.ONE_TB * 1024;
  516. static sizeFormat(size) {
  517. if (size <= 0) return "0 B";
  518. if (size < this.ONE_KB) return size.toFixed(0) + " B";
  519. if (size < this.ONE_MB) return (size / this.ONE_KB).toFixed(2) + " KB";
  520. if (size < this.ONE_GB) return (size / this.ONE_MB).toFixed(2) + " MB";
  521. if (size < this.ONE_TB) return (size / this.ONE_GB).toFixed(2) + " GB";
  522. if (size < this.ONE_PB) return (size / this.ONE_TB).toFixed(2) + " TB";
  523. return (size / this.ONE_PB).toFixed(2) + " PB";
  524. }
  525. }
  526. class CPUFormatter {
  527. static cpuSpeedFormat(speed) {
  528. return speed > 1000 ? (speed / 1000).toFixed(2) + " GHz" : speed.toFixed(2) + " MHz";
  529. }
  530. static cpuCoreFormat(cores) {
  531. return cores === 1 ? "1 Core" : cores + " Cores";
  532. }
  533. }
  534. class TimeFormatter {
  535. static formatSecond(second) {
  536. if (second < 60) return second.toFixed(0) + 's';
  537. if (second < 3600) return (second / 60).toFixed(0) + 'm';
  538. if (second < 3600 * 24) return (second / 3600).toFixed(0) + 'h';
  539. let day = Math.floor(second / 3600 / 24);
  540. let remain = ((second / 3600) - (day * 24)).toFixed(0);
  541. return day + 'd' + (remain > 0 ? ' ' + remain + 'h' : '');
  542. }
  543. }
  544. class NumberFormatter {
  545. static addZero(num) {
  546. return num < 10 ? "0" + num : num;
  547. }
  548. static toFixed(num, n) {
  549. n = Math.pow(10, n);
  550. return Math.floor(num * n) / n;
  551. }
  552. }
  553. class Utils {
  554. static debounce(fn, delay) {
  555. let timeoutID = null;
  556. return function () {
  557. clearTimeout(timeoutID);
  558. let args = arguments;
  559. let that = this;
  560. timeoutID = setTimeout(() => fn.apply(that, args), delay);
  561. };
  562. }
  563. }
  564. class CookieManager {
  565. static getCookie(cname) {
  566. let name = cname + '=';
  567. let ca = document.cookie.split(';');
  568. for (let c of ca) {
  569. c = c.trim();
  570. if (c.indexOf(name) === 0) {
  571. return decodeURIComponent(c.substring(name.length, c.length));
  572. }
  573. }
  574. return '';
  575. }
  576. static setCookie(cname, cvalue, exdays) {
  577. const d = new Date();
  578. d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
  579. let expires = 'expires=' + d.toUTCString();
  580. document.cookie = cname + '=' + encodeURIComponent(cvalue) + ';' + expires + ';path=/';
  581. }
  582. }
  583. class ColorUtils {
  584. static usageColor(data, threshold, total) {
  585. switch (true) {
  586. case data === null: return "purple";
  587. case total < 0: return "green";
  588. case total == 0: return "purple";
  589. case data < total - threshold: return "green";
  590. case data < total: return "orange";
  591. default: return "red";
  592. }
  593. }
  594. static clientUsageColor(clientStats, trafficDiff) {
  595. switch (true) {
  596. case !clientStats || clientStats.total == 0: return "#7a316f";
  597. case clientStats.up + clientStats.down < clientStats.total - trafficDiff: return "#008771";
  598. case clientStats.up + clientStats.down < clientStats.total: return "#f37b24";
  599. default: return "#cf3c3c";
  600. }
  601. }
  602. static userExpiryColor(threshold, client, isDark = false) {
  603. if (!client.enable) return isDark ? '#2c3950' : '#bcbcbc';
  604. let now = new Date().getTime(), expiry = client.expiryTime;
  605. switch (true) {
  606. case expiry === null: return "#7a316f";
  607. case expiry < 0: return "#008771";
  608. case expiry == 0: return "#7a316f";
  609. case now < expiry - threshold: return "#008771";
  610. case now < expiry: return "#f37b24";
  611. default: return "#cf3c3c";
  612. }
  613. }
  614. }
  615. class ArrayUtils {
  616. static doAllItemsExist(array1, array2) {
  617. return array1.every(item => array2.includes(item));
  618. }
  619. }
  620. class URLBuilder {
  621. static buildURL({ host, port, isTLS, base, path }) {
  622. if (!host || host.length === 0) host = window.location.hostname;
  623. if (!port || port.length === 0) port = window.location.port;
  624. if (isTLS === undefined) isTLS = window.location.protocol === "https:";
  625. const protocol = isTLS ? "https:" : "http:";
  626. port = String(port);
  627. if (port === "" || (isTLS && port === "443") || (!isTLS && port === "80")) {
  628. port = "";
  629. } else {
  630. port = `:${port}`;
  631. }
  632. return `${protocol}//${host}${port}${base}${path}`;
  633. }
  634. }
  635. class LanguageManager {
  636. static supportedLanguages = [
  637. {
  638. name: "العربية",
  639. value: "ar-EG",
  640. icon: "🇪🇬",
  641. },
  642. {
  643. name: "English",
  644. value: "en-US",
  645. icon: "🇺🇸",
  646. },
  647. {
  648. name: "فارسی",
  649. value: "fa-IR",
  650. icon: "🇮🇷",
  651. },
  652. {
  653. name: "简体中文",
  654. value: "zh-CN",
  655. icon: "🇨🇳",
  656. },
  657. {
  658. name: "繁體中文",
  659. value: "zh-TW",
  660. icon: "🇹🇼",
  661. },
  662. {
  663. name: "日本語",
  664. value: "ja-JP",
  665. icon: "🇯🇵",
  666. },
  667. {
  668. name: "Русский",
  669. value: "ru-RU",
  670. icon: "🇷🇺",
  671. },
  672. {
  673. name: "Tiếng Việt",
  674. value: "vi-VN",
  675. icon: "🇻🇳",
  676. },
  677. {
  678. name: "Español",
  679. value: "es-ES",
  680. icon: "🇪🇸",
  681. },
  682. {
  683. name: "Indonesian",
  684. value: "id-ID",
  685. icon: "🇮🇩",
  686. },
  687. {
  688. name: "Український",
  689. value: "uk-UA",
  690. icon: "🇺🇦",
  691. },
  692. {
  693. name: "Türkçe",
  694. value: "tr-TR",
  695. icon: "🇹🇷",
  696. },
  697. {
  698. name: "Português",
  699. value: "pt-BR",
  700. icon: "🇧🇷",
  701. }
  702. ]
  703. static getLanguage() {
  704. let lang = CookieManager.getCookie("lang");
  705. if (!lang) {
  706. if (window.navigator) {
  707. lang = window.navigator.language || window.navigator.userLanguage;
  708. if (LanguageManager.isSupportLanguage(lang)) {
  709. CookieManager.setCookie("lang", lang, 150);
  710. } else {
  711. CookieManager.setCookie("lang", "en-US", 150);
  712. window.location.reload();
  713. }
  714. } else {
  715. CookieManager.setCookie("lang", "en-US", 150);
  716. window.location.reload();
  717. }
  718. }
  719. return lang;
  720. }
  721. static setLanguage(language) {
  722. if (!LanguageManager.isSupportLanguage(language)) {
  723. language = "en-US";
  724. }
  725. CookieManager.setCookie("lang", language, 150);
  726. window.location.reload();
  727. }
  728. static isSupportLanguage(language) {
  729. const languageFilter = LanguageManager.supportedLanguages.filter((lang) => {
  730. return lang.value === language
  731. })
  732. return languageFilter.length > 0;
  733. }
  734. }
  735. const MediaQueryMixin = {
  736. data() {
  737. return {
  738. isMobile: window.innerWidth <= 768,
  739. };
  740. },
  741. methods: {
  742. updateDeviceType() {
  743. this.isMobile = window.innerWidth <= 768;
  744. },
  745. },
  746. mounted() {
  747. window.addEventListener('resize', this.updateDeviceType);
  748. },
  749. beforeDestroy() {
  750. window.removeEventListener('resize', this.updateDeviceType);
  751. },
  752. }
  753. class FileManager {
  754. static downloadTextFile(content, filename = 'file.txt', options = { type: "text/plain" }) {
  755. let link = window.document.createElement('a');
  756. link.download = filename;
  757. link.style.border = '0';
  758. link.style.padding = '0';
  759. link.style.margin = '0';
  760. link.style.position = 'absolute';
  761. link.style.left = '-9999px';
  762. link.style.top = `${window.pageYOffset || window.document.documentElement.scrollTop}px`;
  763. link.href = URL.createObjectURL(new Blob([content], options));
  764. link.click();
  765. URL.revokeObjectURL(link.href);
  766. link.remove();
  767. }
  768. }