11import { Injectable , Logger , OnApplicationBootstrap , OnModuleInit } from '@nestjs/common' ;
22import { InjectXtls } from '@remnawave/xtls-sdk-nestjs' ;
3+ import { ConfigService } from '@nestjs/config' ;
34import { XtlsApi } from '@remnawave/xtls-sdk' ;
45import { execa } from '@cjs-exporter/execa' ;
56import objectHash from 'object-hash' ;
@@ -22,6 +23,7 @@ import { InternalService } from '../internal/internal.service';
2223@Injectable ( )
2324export 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