Skip to content

Commit cedc7f0

Browse files
committed
chore: refactoring RandomUtil class
now we use window.crypto.getRandomValues to generate random values.
1 parent 64fa0e9 commit cedc7f0

File tree

3 files changed

+27
-38
lines changed

3 files changed

+27
-38
lines changed

web/assets/js/model/inbound.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ class Allocate extends XrayCommonClass {
10501050

10511051
class Inbound extends XrayCommonClass {
10521052
constructor(
1053-
port = RandomUtil.randomIntRange(10000, 60000),
1053+
port = RandomUtil.randomInteger(10000, 60000),
10541054
listen = '',
10551055
protocol = Protocols.VLESS,
10561056
settings = null,
@@ -1226,7 +1226,7 @@ class Inbound extends XrayCommonClass {
12261226
}
12271227

12281228
reset() {
1229-
this.port = RandomUtil.randomIntRange(10000, 60000);
1229+
this.port = RandomUtil.randomInteger(10000, 60000);
12301230
this.listen = '';
12311231
this.protocol = Protocols.VMESS;
12321232
this.settings = Inbound.Settings.getSettings(Protocols.VMESS);

web/assets/js/util/index.js

+24-35
Original file line numberDiff line numberDiff line change
@@ -80,59 +80,48 @@ class PromiseUtil {
8080
}
8181
}
8282

83-
const seq = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
84-
8583
class RandomUtil {
86-
static randomIntRange(min, max) {
87-
return Math.floor(Math.random() * (max - min) + min);
84+
static getSeq({ hasNumbers = true, hasLowercase = true, hasUppercase = true } = {}) {
85+
let seq = '';
86+
if (hasNumbers) seq += "0123456789";
87+
if (hasLowercase) seq += "abcdefghijklmnopqrstuvwxyz";
88+
if (hasUppercase) seq += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
89+
return seq;
8890
}
8991

90-
static randomInt(n) {
91-
return this.randomIntRange(0, n);
92+
static randomInteger(min, max) {
93+
const range = max - min + 1;
94+
const randomBuffer = new Uint32Array(1);
95+
window.crypto.getRandomValues(randomBuffer);
96+
return Math.floor((randomBuffer[0] / (0xFFFFFFFF + 1)) * range) + min;
9297
}
9398

94-
static randomSeq(count) {
95-
let str = '';
96-
for (let i = 0; i < count; ++i) {
97-
str += seq[this.randomInt(62)];
98-
}
99-
return str;
99+
static randomSeq(count, options = {}) {
100+
const seq = this.getSeq(options);
101+
const seqLength = seq.length;
102+
const randomValues = new Uint32Array(count);
103+
window.crypto.getRandomValues(randomValues);
104+
return Array.from(randomValues, v => seq[v % seqLength]).join('');
100105
}
101106

102107
static randomShortIds() {
103-
const lengths = [2, 4, 6, 8, 10, 12, 14, 16];
104-
for (let i = lengths.length - 1; i > 0; i--) {
105-
const j = Math.floor(Math.random() * (i + 1));
106-
[lengths[i], lengths[j]] = [lengths[j], lengths[i]];
107-
}
108-
109-
let shortIds = [];
110-
for (let length of lengths) {
111-
let shortId = '';
112-
for (let i = 0; i < length; i++) {
113-
shortId += seq[this.randomInt(16)];
114-
}
115-
shortIds.push(shortId);
116-
}
117-
return shortIds.join(',');
108+
const lengths = [2, 4, 6, 8, 10, 12, 14, 16].sort(() => Math.random() - 0.5);
109+
const seq = this.getSeq();
110+
return lengths.map(len => this.randomSeq(len)).join(',');
118111
}
119112

120113
static randomLowerAndNum(len) {
121-
let str = '';
122-
for (let i = 0; i < len; ++i) {
123-
str += seq[this.randomInt(36)];
124-
}
125-
return str;
114+
return this.randomSeq(len, { hasUppercase: false });
126115
}
127116

128117
static randomUUID() {
129-
return window.crypto.randomUUID()
118+
return window.crypto.randomUUID();
130119
}
131120

132121
static randomShadowsocksPassword() {
133-
let array = new Uint8Array(32);
122+
const array = new Uint8Array(32);
134123
window.crypto.getRandomValues(array);
135-
return Base64.encode(String.fromCharCode.apply(null, array));
124+
return Base64.encode(String.fromCharCode(...array));
136125
}
137126
}
138127

web/html/xui/inbounds.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@
927927
expiryTime: dbInbound.expiryTime,
928928

929929
listen: '',
930-
port: RandomUtil.randomIntRange(10000, 60000),
930+
port: RandomUtil.randomInteger(10000, 60000),
931931
protocol: baseInbound.protocol,
932932
settings: Inbound.Settings.getSettings(baseInbound.protocol).toString(),
933933
streamSettings: baseInbound.stream.toString(),

0 commit comments

Comments
 (0)