Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShishkevich D. <135337715+shishkevichd@users.noreply.github.com>2025-03-09 09:37:05 +0300
committerShishkevich D. <135337715+shishkevichd@users.noreply.github.com>2025-03-09 09:37:05 +0300
commitcedc7f0fb8b30009a9526053be3dc3857af8bdcb (patch)
tree83cbdbdfd314b0c313682e074966d1fac265eb15
parent64fa0e97a30eb8f21b37f358b6382abcb4e4d9f2 (diff)
chore: refactoring `RandomUtil` class
now we use window.crypto.getRandomValues to generate random values.
-rw-r--r--web/assets/js/model/inbound.js4
-rw-r--r--web/assets/js/util/index.js59
-rw-r--r--web/html/xui/inbounds.html2
3 files changed, 27 insertions, 38 deletions
diff --git a/web/assets/js/model/inbound.js b/web/assets/js/model/inbound.js
index 5c3235e1..2a5fb6d6 100644
--- a/web/assets/js/model/inbound.js
+++ b/web/assets/js/model/inbound.js
@@ -1050,7 +1050,7 @@ class Allocate extends XrayCommonClass {
class Inbound extends XrayCommonClass {
constructor(
- port = RandomUtil.randomIntRange(10000, 60000),
+ port = RandomUtil.randomInteger(10000, 60000),
listen = '',
protocol = Protocols.VLESS,
settings = null,
@@ -1226,7 +1226,7 @@ class Inbound extends XrayCommonClass {
}
reset() {
- this.port = RandomUtil.randomIntRange(10000, 60000);
+ this.port = RandomUtil.randomInteger(10000, 60000);
this.listen = '';
this.protocol = Protocols.VMESS;
this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
diff --git a/web/assets/js/util/index.js b/web/assets/js/util/index.js
index 0fea0592..c3bf68e7 100644
--- a/web/assets/js/util/index.js
+++ b/web/assets/js/util/index.js
@@ -80,59 +80,48 @@ class PromiseUtil {
}
}
-const seq = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
-
class RandomUtil {
- static randomIntRange(min, max) {
- return Math.floor(Math.random() * (max - min) + min);
+ static getSeq({ hasNumbers = true, hasLowercase = true, hasUppercase = true } = {}) {
+ let seq = '';
+ if (hasNumbers) seq += "0123456789";
+ if (hasLowercase) seq += "abcdefghijklmnopqrstuvwxyz";
+ if (hasUppercase) seq += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ return seq;
}
- static randomInt(n) {
- return this.randomIntRange(0, n);
+ static randomInteger(min, max) {
+ const range = max - min + 1;
+ const randomBuffer = new Uint32Array(1);
+ window.crypto.getRandomValues(randomBuffer);
+ return Math.floor((randomBuffer[0] / (0xFFFFFFFF + 1)) * range) + min;
}
- static randomSeq(count) {
- let str = '';
- for (let i = 0; i < count; ++i) {
- str += seq[this.randomInt(62)];
- }
- return str;
+ static randomSeq(count, options = {}) {
+ const seq = this.getSeq(options);
+ const seqLength = seq.length;
+ const randomValues = new Uint32Array(count);
+ window.crypto.getRandomValues(randomValues);
+ return Array.from(randomValues, v => seq[v % seqLength]).join('');
}
static randomShortIds() {
- const lengths = [2, 4, 6, 8, 10, 12, 14, 16];
- for (let i = lengths.length - 1; i > 0; i--) {
- const j = Math.floor(Math.random() * (i + 1));
- [lengths[i], lengths[j]] = [lengths[j], lengths[i]];
- }
-
- let shortIds = [];
- for (let length of lengths) {
- let shortId = '';
- for (let i = 0; i < length; i++) {
- shortId += seq[this.randomInt(16)];
- }
- shortIds.push(shortId);
- }
- return shortIds.join(',');
+ const lengths = [2, 4, 6, 8, 10, 12, 14, 16].sort(() => Math.random() - 0.5);
+ const seq = this.getSeq();
+ return lengths.map(len => this.randomSeq(len)).join(',');
}
static randomLowerAndNum(len) {
- let str = '';
- for (let i = 0; i < len; ++i) {
- str += seq[this.randomInt(36)];
- }
- return str;
+ return this.randomSeq(len, { hasUppercase: false });
}
static randomUUID() {
- return window.crypto.randomUUID()
+ return window.crypto.randomUUID();
}
static randomShadowsocksPassword() {
- let array = new Uint8Array(32);
+ const array = new Uint8Array(32);
window.crypto.getRandomValues(array);
- return Base64.encode(String.fromCharCode.apply(null, array));
+ return Base64.encode(String.fromCharCode(...array));
}
}
diff --git a/web/html/xui/inbounds.html b/web/html/xui/inbounds.html
index 677a3074..fe5d7e3a 100644
--- a/web/html/xui/inbounds.html
+++ b/web/html/xui/inbounds.html
@@ -927,7 +927,7 @@
expiryTime: dbInbound.expiryTime,
listen: '',
- port: RandomUtil.randomIntRange(10000, 60000),
+ port: RandomUtil.randomInteger(10000, 60000),
protocol: baseInbound.protocol,
settings: Inbound.Settings.getSettings(baseInbound.protocol).toString(),
streamSettings: baseInbound.stream.toString(),