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(),