Skip to content

Commit 59c77d9

Browse files
committed
refactor: optimize XRay configuration processing and Prometheus reporting
- Simplify XRay config validation by removing redundant commented code - Update Prometheus reporter labels to use 'remnawave-scheduler' prefix - Modify XRay config service to directly use repository for config retrieval - Remove unused commented methods in XRay config controller
1 parent b370bd8 commit 59c77d9

File tree

4 files changed

+8
-107
lines changed

4 files changed

+8
-107
lines changed

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

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ export class XRayConfig {
8383
}
8484

8585
// console.log(`Inbound ${inbound.tag} network: ${network || 'not set'}`);
86-
}
8786

88-
for (const inbound of this.config.inbounds) {
8987
if (!inbound.tag) {
9088
throw new Error('All inbounds must have a unique tag.');
9189
}
@@ -212,44 +210,6 @@ export class XRayConfig {
212210
}));
213211
}
214212

215-
// private includeUsers(users: UserForConfigEntity[]): IXrayConfig {
216-
// let config: IXrayConfig | null = null;
217-
// try {
218-
// config = JSON.parse(JSON.stringify(this.config)) as IXrayConfig;
219-
220-
// const inboundMap = new Map(config.inbounds.map((inbound) => [inbound.tag, inbound]));
221-
222-
// const usersByTag = new Map<string, UserForConfigEntity[]>();
223-
// for (const user of users) {
224-
// if (!usersByTag.has(user.tag)) {
225-
// usersByTag.set(user.tag, []);
226-
// }
227-
// usersByTag.get(user.tag)!.push(user);
228-
// }
229-
230-
// for (const [tag, tagUsers] of usersByTag) {
231-
// const inbound = inboundMap.get(tag);
232-
// if (!inbound) continue;
233-
234-
// inbound.settings ??= {} as InboundSettings;
235-
236-
// this.addUsersToInbound(inbound, tagUsers);
237-
// }
238-
239-
// usersByTag.clear();
240-
241-
// return config;
242-
// } catch (error) {
243-
// throw error;
244-
// } finally {
245-
// config = null;
246-
// }
247-
// }
248-
249-
// public prepareConfigForNode(users: UserForConfigEntity[]): IXrayConfig {
250-
// return this.processCertificates(this.includeUsers(users));
251-
// }
252-
253213
public getSortedConfig(): IXrayConfig {
254214
return this.sortObjectByKeys<IXrayConfig>(this.config);
255215
}
@@ -290,31 +250,6 @@ export class XRayConfig {
290250
}
291251
}
292252

293-
// public includeUserBatch(users: UserForConfigEntity[]): IXrayConfig {
294-
// const usersByTag = new Map<string, UserForConfigEntity[]>();
295-
// for (const user of users) {
296-
// if (!usersByTag.has(user.tag)) {
297-
// usersByTag.set(user.tag, []);
298-
// }
299-
// usersByTag.get(user.tag)!.push(user);
300-
// }
301-
302-
// const inboundMap = new Map(this.config.inbounds.map((inbound) => [inbound.tag, inbound]));
303-
304-
// for (const [tag, tagUsers] of usersByTag) {
305-
// const inbound = inboundMap.get(tag);
306-
// if (!inbound) continue;
307-
308-
// inbound.settings ??= {} as InboundSettings;
309-
310-
// this.addUsersToInbound(inbound, tagUsers);
311-
// }
312-
313-
// usersByTag.clear();
314-
315-
// return this.config;
316-
// }
317-
318253
public includeUserBatch(users: UserForConfigEntity[]): IXrayConfig {
319254
const usersByTag = new Map<string, UserForConfigEntity[]>();
320255
for (const user of users) {
@@ -341,30 +276,4 @@ export class XRayConfig {
341276

342277
return this.config;
343278
}
344-
345-
// public includeUserBatch(users: UserForConfigEntity[]): IXrayConfig {
346-
// console.time('indexing');
347-
348-
// for (const inbound of this.config.inbounds) {
349-
// // Подготавливаем инбаунд
350-
// inbound.settings ??= {} as InboundSettings;
351-
352-
// // Находим пользователей для этого инбаунда вручную
353-
// const usersForThisInbound: UserForConfigEntity[] = [];
354-
// for (const user of users) {
355-
// if (user.tags.includes(inbound.tag)) {
356-
// usersForThisInbound.push(user);
357-
// }
358-
// }
359-
360-
// // Если есть пользователи для этого инбаунда, добавляем их
361-
// if (usersForThisInbound.length > 0) {
362-
// this.addUsersToInbound(inbound, usersForThisInbound);
363-
// }
364-
// }
365-
366-
// console.timeEnd('indexing');
367-
368-
// return this.config;
369-
// }
370279
}

src/intergration-modules/prometheus-reporter/prometheus-reporter.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import { BasicStrategy } from './strategies';
1616
controller: PrometheusReporterController,
1717
useFactory: () => ({
1818
defaultLabels: {
19-
app: 'remnawave',
19+
app: 'remnawave-scheduler',
2020
},
21-
customMetricPrefix: 'remnawave',
21+
customMetricPrefix: 'remnawave-scheduler',
2222
}),
2323
}),
2424
],

src/modules/xray-config/xray-config.controller.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,4 @@ export class XrayConfigController {
8484
// response: new KeygenResponseModel(data),
8585
// };
8686
// }
87-
88-
// @Put(':uuid')
89-
// @ApiOperation({ summary: 'Update Xray Config', description: 'Update xray configuration' })
90-
// @ApiOkResponse({ description: 'Configuration updated successfully' })
91-
// @Roles(ROLE.ADMIN)
92-
// async updateConfig(@Param('uuid') uuid: string, @Body() config: object) {
93-
// const result = await this.xrayConfigService.updateConfig(uuid, config);
94-
// return errorHandler(result);
95-
// }
9687
}

src/modules/xray-config/xray-config.service.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,16 @@ export class XrayConfigService implements OnApplicationBootstrap {
134134
}
135135

136136
const validatedConfig = new XRayConfig(config);
137+
const sortedConfig = validatedConfig.getSortedConfig();
137138

138-
const writeDBConfig = await this.updateConfig(validatedConfig.getSortedConfig());
139+
const writeDBConfig = await this.updateConfig(sortedConfig);
139140
if (!writeDBConfig.isOk) {
140141
throw new Error('Failed to write config to DB');
141142
}
142143

143144
return {
144145
isOk: true,
145-
response: validatedConfig.getSortedConfig(),
146+
response: sortedConfig,
146147
};
147148
} catch (error) {
148149
if (error instanceof Error) {
@@ -175,13 +176,13 @@ export class XrayConfigService implements OnApplicationBootstrap {
175176

176177
public async syncInbounds(): Promise<void> {
177178
try {
178-
const config = await this.getConfig();
179+
const config = await this.xrayConfigRepository.findFirst();
179180

180-
if (!config.isOk || !config.response) {
181+
if (!config) {
181182
throw new Error('Failed to get config');
182183
}
183184

184-
const parsedConf = new XRayConfig(config.response);
185+
const parsedConf = new XRayConfig(config);
185186
const configInbounds = parsedConf.getAllInbounds();
186187

187188
const existingInbounds = await this.getAllInbounds();

0 commit comments

Comments
 (0)