utils.js 12 KB

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