Skip to content

Commit 2d576fa

Browse files
committed
refactor: optimize XRay config generation
1 parent ae4083f commit 2d576fa

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

src/common/helpers/xray-config/xray-config.validator.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,7 @@ export class XRayConfig {
155155
}
156156

157157
private processCertificates(config: IXrayConfig): IXrayConfig {
158-
const newConfig = config;
159-
160-
for (const inbound of newConfig.inbounds) {
158+
for (const inbound of config.inbounds) {
161159
const tlsSettings = inbound?.streamSettings?.tlsSettings;
162160
if (!tlsSettings?.certificates) continue;
163161

@@ -194,7 +192,7 @@ export class XRayConfig {
194192
});
195193
}
196194

197-
return newConfig;
195+
return config;
198196
}
199197

200198
public getAllInbounds(): InboundsWithTagsAndType[] {
@@ -206,34 +204,40 @@ export class XRayConfig {
206204
}));
207205
}
208206

209-
public includeUsers(users: UserForConfigEntity[]): IXrayConfig {
210-
const config = JSON.parse(JSON.stringify(this.config)) as IXrayConfig;
207+
private includeUsers(users: UserForConfigEntity[]): IXrayConfig {
208+
let config: IXrayConfig | null = null;
209+
try {
210+
config = JSON.parse(JSON.stringify(this.config)) as IXrayConfig;
211211

212-
const inboundMap = new Map(config.inbounds.map((inbound) => [inbound.tag, inbound]));
212+
const inboundMap = new Map(config.inbounds.map((inbound) => [inbound.tag, inbound]));
213213

214-
const usersByTag = new Map<string, UserForConfigEntity[]>();
215-
for (const user of users) {
216-
if (!usersByTag.has(user.tag)) {
217-
usersByTag.set(user.tag, []);
214+
const usersByTag = new Map<string, UserForConfigEntity[]>();
215+
for (const user of users) {
216+
if (!usersByTag.has(user.tag)) {
217+
usersByTag.set(user.tag, []);
218+
}
219+
usersByTag.get(user.tag)!.push(user);
218220
}
219-
usersByTag.get(user.tag)!.push(user);
220-
}
221221

222-
for (const [tag, tagUsers] of usersByTag) {
223-
const inbound = inboundMap.get(tag);
224-
if (!inbound) continue;
222+
for (const [tag, tagUsers] of usersByTag) {
223+
const inbound = inboundMap.get(tag);
224+
if (!inbound) continue;
225225

226-
inbound.settings ??= {} as InboundSettings;
226+
inbound.settings ??= {} as InboundSettings;
227227

228-
this.addUsersToInbound(inbound, tagUsers);
229-
}
228+
this.addUsersToInbound(inbound, tagUsers);
229+
}
230230

231-
return config;
231+
return config;
232+
} catch (error) {
233+
throw error;
234+
} finally {
235+
config = null;
236+
}
232237
}
233238

234239
public prepareConfigForNode(users: UserForConfigEntity[]): IXrayConfig {
235-
const configWithUsers = this.includeUsers(users);
236-
return this.processCertificates(configWithUsers);
240+
return this.processCertificates(this.includeUsers(users));
237241
}
238242

239243
public getSortedConfig(): IXrayConfig {

0 commit comments

Comments
 (0)