Skip to content

Commit 02fe636

Browse files
committed
feat: add configurable config checksum checking for Xray service
- Introduce CONFIG_EQUAL_CHECKING environment variable to toggle config checksum verification - Update config schema to include new configuration option - Modify XrayService to conditionally perform config checksum checks based on configuration
1 parent 1114612 commit 02fe636

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/common/config/app-config/config.schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export const configSchema = z.object({
99
SSL_CERT: z.string(),
1010
XTLS_IP: z.string().default('127.0.0.1'),
1111
XTLS_PORT: z.string().default('61000'),
12+
CONFIG_EQUAL_CHECKING: z
13+
.string()
14+
.default('true')
15+
.transform((val) => val === 'true'),
1216
});
1317

1418
export type ConfigSchema = z.infer<typeof configSchema>;

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

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Injectable, Logger, OnApplicationBootstrap, OnModuleInit } from '@nestjs/common';
22
import { InjectXtls } from '@remnawave/xtls-sdk-nestjs';
3+
import { ConfigService } from '@nestjs/config';
34
import { XtlsApi } from '@remnawave/xtls-sdk';
45
import { execa } from '@cjs-exporter/execa';
56
import objectHash from 'object-hash';
@@ -22,6 +23,7 @@ import { InternalService } from '../internal/internal.service';
2223
@Injectable()
2324
export class XrayService implements OnApplicationBootstrap, OnModuleInit {
2425
private readonly logger = new Logger(XrayService.name);
26+
private readonly configEqualChecking: boolean;
2527

2628
private readonly xrayPath: string;
2729

@@ -34,11 +36,13 @@ export class XrayService implements OnApplicationBootstrap, OnModuleInit {
3436
constructor(
3537
@InjectXtls() private readonly xtlsSdk: XtlsApi,
3638
private readonly internalService: InternalService,
39+
private readonly configService: ConfigService,
3740
) {
3841
this.xrayPath = '/usr/local/bin/xray';
3942
this.xrayVersion = null;
4043
this.systemStats = null;
4144
this.isXrayStartedProccesing = false;
45+
this.configEqualChecking = this.configService.getOrThrow<boolean>('CONFIG_EQUAL_CHECKING');
4246
}
4347

4448
async onModuleInit() {
@@ -82,34 +86,45 @@ export class XrayService implements OnApplicationBootstrap, OnModuleInit {
8286

8387
this.logger.debug(JSON.stringify(fullConfig, null, 2));
8488

85-
const newChecksum = this.getConfigChecksum(fullConfig);
89+
if (this.configEqualChecking) {
90+
this.logger.log('Getting config checksum...');
91+
const newChecksum = this.getConfigChecksum(fullConfig);
8692

87-
if (this.isXrayOnline) {
88-
this.logger.warn(
89-
`Xray process is already running with checksum ${this.configChecksum}`,
90-
);
93+
if (this.isXrayOnline) {
94+
this.logger.warn(
95+
`Xray process is already running with checksum ${this.configChecksum}`,
96+
);
9197

92-
const oldChecksum = this.configChecksum;
93-
const isXrayOnline = await this.getXrayInternalStatus();
98+
const oldChecksum = this.configChecksum;
99+
const isXrayOnline = await this.getXrayInternalStatus();
94100

95-
this.logger.debug(`
101+
this.logger.debug(`
96102
oldChecksum: ${oldChecksum}
97103
newChecksum: ${newChecksum}
98104
isXrayOnline: ${isXrayOnline}
99105
`);
100106

101-
if (oldChecksum === newChecksum && isXrayOnline) {
102-
this.logger.error('Xray is already online with the same config. Skipping...');
103-
104-
return {
105-
isOk: true,
106-
response: new StartXrayResponseModel(true, this.xrayVersion, null, null),
107-
};
107+
if (oldChecksum === newChecksum && isXrayOnline) {
108+
this.logger.error(
109+
'Xray is already online with the same config. Skipping...',
110+
);
111+
112+
return {
113+
isOk: true,
114+
response: new StartXrayResponseModel(
115+
true,
116+
this.xrayVersion,
117+
null,
118+
null,
119+
),
120+
};
121+
}
108122
}
123+
124+
this.configChecksum = newChecksum;
109125
}
110126

111127
this.internalService.setXrayConfig(fullConfig);
112-
this.configChecksum = newChecksum;
113128

114129
this.logger.log(`XTLS config generated in ${performance.now() - tm}ms`);
115130

0 commit comments

Comments
 (0)