utils.js 12 KB

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