utils.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. const seq = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
  77. class RandomUtil {
  78. static randomIntRange(min, max) {
  79. return Math.floor(Math.random() * (max - min) + min);
  80. }
  81. static randomInt(n) {
  82. return this.randomIntRange(0, n);
  83. }
  84. static randomSeq(count) {
  85. let str = '';
  86. for (let i = 0; i < count; ++i) {
  87. str += seq[this.randomInt(62)];
  88. }
  89. return str;
  90. }
  91. static randomShortIds() {
  92. const lengths = [2, 4, 6, 8, 10, 12, 14, 16];
  93. for (let i = lengths.length - 1; i > 0; i--) {
  94. const j = Math.floor(Math.random() * (i + 1));
  95. [lengths[i], lengths[j]] = [lengths[j], lengths[i]];
  96. }
  97. let shortIds = [];
  98. for (let length of lengths) {
  99. let shortId = '';
  100. for (let i = 0; i < length; i++) {
  101. shortId += seq[this.randomInt(16)];
  102. }
  103. shortIds.push(shortId);
  104. }
  105. return shortIds.join(',');
  106. }
  107. static randomLowerAndNum(len) {
  108. let str = '';
  109. for (let i = 0; i < len; ++i) {
  110. str += seq[this.randomInt(36)];
  111. }
  112. return str;
  113. }
  114. static randomUUID() {
  115. const template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
  116. return template.replace(/[xy]/g, function (c) {
  117. const randomValues = new Uint8Array(1);
  118. 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. static randomShadowsocksPassword() {
  125. let array = new Uint8Array(32);
  126. window.crypto.getRandomValues(array);
  127. return btoa(String.fromCharCode.apply(null, array));
  128. }
  129. }
  130. class ObjectUtil {
  131. static getPropIgnoreCase(obj, prop) {
  132. for (const name in obj) {
  133. if (!obj.hasOwnProperty(name)) {
  134. continue;
  135. }
  136. if (name.toLowerCase() === prop.toLowerCase()) {
  137. return obj[name];
  138. }
  139. }
  140. return undefined;
  141. }
  142. static deepSearch(obj, key) {
  143. if (obj instanceof Array) {
  144. for (let i = 0; i < obj.length; ++i) {
  145. if (this.deepSearch(obj[i], key)) {
  146. return true;
  147. }
  148. }
  149. } else if (obj instanceof Object) {
  150. for (let name in obj) {
  151. if (!obj.hasOwnProperty(name)) {
  152. continue;
  153. }
  154. if (this.deepSearch(obj[name], key)) {
  155. return true;
  156. }
  157. }
  158. } else {
  159. return this.isEmpty(obj) ? false : obj.toString().toLowerCase().indexOf(key.toLowerCase()) >= 0;
  160. }
  161. return false;
  162. }
  163. static isEmpty(obj) {
  164. return obj === null || obj === undefined || obj === '';
  165. }
  166. static isArrEmpty(arr) {
  167. return !this.isEmpty(arr) && arr.length === 0;
  168. }
  169. static copyArr(dest, src) {
  170. dest.splice(0);
  171. for (const item of src) {
  172. dest.push(item);
  173. }
  174. }
  175. static clone(obj) {
  176. let newObj;
  177. if (obj instanceof Array) {
  178. newObj = [];
  179. this.copyArr(newObj, obj);
  180. } else if (obj instanceof Object) {
  181. newObj = {};
  182. for (const key of Object.keys(obj)) {
  183. newObj[key] = obj[key];
  184. }
  185. } else {
  186. newObj = obj;
  187. }
  188. return newObj;
  189. }
  190. static deepClone(obj) {
  191. let newObj;
  192. if (obj instanceof Array) {
  193. newObj = [];
  194. for (const item of obj) {
  195. newObj.push(this.deepClone(item));
  196. }
  197. } else if (obj instanceof Object) {
  198. newObj = {};
  199. for (const key of Object.keys(obj)) {
  200. newObj[key] = this.deepClone(obj[key]);
  201. }
  202. } else {
  203. newObj = obj;
  204. }
  205. return newObj;
  206. }
  207. static cloneProps(dest, src, ...ignoreProps) {
  208. if (dest == null || src == null) {
  209. return;
  210. }
  211. const ignoreEmpty = this.isArrEmpty(ignoreProps);
  212. for (const key of Object.keys(src)) {
  213. if (!src.hasOwnProperty(key)) {
  214. continue;
  215. } else if (!dest.hasOwnProperty(key)) {
  216. continue;
  217. } else if (src[key] === undefined) {
  218. continue;
  219. }
  220. if (ignoreEmpty) {
  221. dest[key] = src[key];
  222. } else {
  223. let ignore = false;
  224. for (let i = 0; i < ignoreProps.length; ++i) {
  225. if (key === ignoreProps[i]) {
  226. ignore = true;
  227. break;
  228. }
  229. }
  230. if (!ignore) {
  231. dest[key] = src[key];
  232. }
  233. }
  234. }
  235. }
  236. static delProps(obj, ...props) {
  237. for (const prop of props) {
  238. if (prop in obj) {
  239. delete obj[prop];
  240. }
  241. }
  242. }
  243. static execute(func, ...args) {
  244. if (!this.isEmpty(func) && typeof func === 'function') {
  245. func(...args);
  246. }
  247. }
  248. static orDefault(obj, defaultValue) {
  249. if (obj == null) {
  250. return defaultValue;
  251. }
  252. return obj;
  253. }
  254. static equals(a, b) {
  255. for (const key in a) {
  256. if (!a.hasOwnProperty(key)) {
  257. continue;
  258. }
  259. if (!b.hasOwnProperty(key)) {
  260. return false;
  261. } else if (a[key] !== b[key]) {
  262. return false;
  263. }
  264. }
  265. return true;
  266. }
  267. }
  268. class Wireguard {
  269. static gf(init) {
  270. var r = new Float64Array(16);
  271. if (init) {
  272. for (var i = 0; i < init.length; ++i)
  273. r[i] = init[i];
  274. }
  275. return r;
  276. }
  277. static pack(o, n) {
  278. var b, m = this.gf(), t = this.gf();
  279. for (var i = 0; i < 16; ++i)
  280. t[i] = n[i];
  281. this.carry(t);
  282. this.carry(t);
  283. this.carry(t);
  284. for (var j = 0; j < 2; ++j) {
  285. m[0] = t[0] - 0xffed;
  286. for (var i = 1; i < 15; ++i) {
  287. m[i] = t[i] - 0xffff - ((m[i - 1] >> 16) & 1);
  288. m[i - 1] &= 0xffff;
  289. }
  290. m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1);
  291. b = (m[15] >> 16) & 1;
  292. m[14] &= 0xffff;
  293. this.cswap(t, m, 1 - b);
  294. }
  295. for (var i = 0; i < 16; ++i) {
  296. o[2 * i] = t[i] & 0xff;
  297. o[2 * i + 1] = t[i] >> 8;
  298. }
  299. }
  300. static carry(o) {
  301. var c;
  302. for (var i = 0; i < 16; ++i) {
  303. o[(i + 1) % 16] += (i < 15 ? 1 : 38) * Math.floor(o[i] / 65536);
  304. o[i] &= 0xffff;
  305. }
  306. }
  307. static cswap(p, q, b) {
  308. var t, c = ~(b - 1);
  309. for (var i = 0; i < 16; ++i) {
  310. t = c & (p[i] ^ q[i]);
  311. p[i] ^= t;
  312. q[i] ^= t;
  313. }
  314. }
  315. static add(o, a, b) {
  316. for (var i = 0; i < 16; ++i)
  317. o[i] = (a[i] + b[i]) | 0;
  318. }
  319. static subtract(o, a, b) {
  320. for (var i = 0; i < 16; ++i)
  321. o[i] = (a[i] - b[i]) | 0;
  322. }
  323. static multmod(o, a, b) {
  324. var t = new Float64Array(31);
  325. for (var i = 0; i < 16; ++i) {
  326. for (var j = 0; j < 16; ++j)
  327. t[i + j] += a[i] * b[j];
  328. }
  329. for (var i = 0; i < 15; ++i)
  330. t[i] += 38 * t[i + 16];
  331. for (var i = 0; i < 16; ++i)
  332. o[i] = t[i];
  333. this.carry(o);
  334. this.carry(o);
  335. }
  336. static invert(o, i) {
  337. var c = this.gf();
  338. for (var a = 0; a < 16; ++a)
  339. c[a] = i[a];
  340. for (var a = 253; a >= 0; --a) {
  341. this.multmod(c, c, c);
  342. if (a !== 2 && a !== 4)
  343. this.multmod(c, c, i);
  344. }
  345. for (var a = 0; a < 16; ++a)
  346. o[a] = c[a];
  347. }
  348. static clamp(z) {
  349. z[31] = (z[31] & 127) | 64;
  350. z[0] &= 248;
  351. }
  352. static generatePublicKey(privateKey) {
  353. var r, z = new Uint8Array(32);
  354. var a = this.gf([1]),
  355. b = this.gf([9]),
  356. c = this.gf(),
  357. d = this.gf([1]),
  358. e = this.gf(),
  359. f = this.gf(),
  360. _121665 = this.gf([0xdb41, 1]),
  361. _9 = this.gf([9]);
  362. for (var i = 0; i < 32; ++i)
  363. z[i] = privateKey[i];
  364. this.clamp(z);
  365. for (var i = 254; i >= 0; --i) {
  366. r = (z[i >>> 3] >>> (i & 7)) & 1;
  367. this.cswap(a, b, r);
  368. this.cswap(c, d, r);
  369. this.add(e, a, c);
  370. this.subtract(a, a, c);
  371. this.add(c, b, d);
  372. this.subtract(b, b, d);
  373. this.multmod(d, e, e);
  374. this.multmod(f, a, a);
  375. this.multmod(a, c, a);
  376. this.multmod(c, b, e);
  377. this.add(e, a, c);
  378. this.subtract(a, a, c);
  379. this.multmod(b, a, a);
  380. this.subtract(c, d, f);
  381. this.multmod(a, c, _121665);
  382. this.add(a, a, d);
  383. this.multmod(c, c, a);
  384. this.multmod(a, d, f);
  385. this.multmod(d, b, _9);
  386. this.multmod(b, e, e);
  387. this.cswap(a, b, r);
  388. this.cswap(c, d, r);
  389. }
  390. this.invert(c, c);
  391. this.multmod(a, a, c);
  392. this.pack(z, a);
  393. return z;
  394. }
  395. static generatePresharedKey() {
  396. var privateKey = new Uint8Array(32);
  397. window.crypto.getRandomValues(privateKey);
  398. return privateKey;
  399. }
  400. static generatePrivateKey() {
  401. var privateKey = this.generatePresharedKey();
  402. this.clamp(privateKey);
  403. return privateKey;
  404. }
  405. static encodeBase64(dest, src) {
  406. 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]);
  407. for (var i = 0; i < 4; ++i)
  408. dest[i] = input[i] + 65 +
  409. (((25 - input[i]) >> 8) & 6) -
  410. (((51 - input[i]) >> 8) & 75) -
  411. (((61 - input[i]) >> 8) & 15) +
  412. (((62 - input[i]) >> 8) & 3);
  413. }
  414. static keyToBase64(key) {
  415. var i, base64 = new Uint8Array(44);
  416. for (i = 0; i < 32 / 3; ++i)
  417. this.encodeBase64(base64.subarray(i * 4), key.subarray(i * 3));
  418. this.encodeBase64(base64.subarray(i * 4), Uint8Array.from([key[i * 3 + 0], key[i * 3 + 1], 0]));
  419. base64[43] = 61;
  420. return String.fromCharCode.apply(null, base64);
  421. }
  422. static keyFromBase64(encoded) {
  423. const binaryStr = atob(encoded);
  424. const bytes = new Uint8Array(binaryStr.length);
  425. for (let i = 0; i < binaryStr.length; i++) {
  426. bytes[i] = binaryStr.charCodeAt(i);
  427. }
  428. return bytes;
  429. }
  430. static generateKeypair(secretKey = '') {
  431. var privateKey = secretKey.length > 0 ? this.keyFromBase64(secretKey) : this.generatePrivateKey();
  432. var publicKey = this.generatePublicKey(privateKey);
  433. return {
  434. publicKey: this.keyToBase64(publicKey),
  435. privateKey: secretKey.length > 0 ? secretKey : this.keyToBase64(privateKey)
  436. };
  437. }
  438. }