utils.js 13 KB

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