1
0

utils.js 12 KB

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