Skip to content

Commit 60f6e36

Browse files
committed
refactor: streamline config profile update process
1 parent b32da76 commit 60f6e36

File tree

2 files changed

+54
-37
lines changed

2 files changed

+54
-37
lines changed

src/modules/config-profiles/config-profile.service.ts

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { GetConfigProfilesResponseModel } from './models/get-config-profiles.res
1818
import { ConfigProfileInboundEntity } from './entities/config-profile-inbound.entity';
1919
import { ConfigProfileRepository } from './repositories/config-profile.repository';
2020
import { ConfigProfileEntity } from './entities/config-profile.entity';
21+
import { ConfigProfileWithInboundsAndNodesEntity } from './entities';
2122
import { GetSnippetsQuery } from './queries/get-snippets';
2223

2324
@Injectable()
@@ -233,7 +234,6 @@ export class ConfigProfileService {
233234
}
234235
}
235236

236-
@Transactional()
237237
public async updateConfigProfile(
238238
uuid: string,
239239
name?: string,
@@ -257,42 +257,7 @@ export class ConfigProfileService {
257257
};
258258
}
259259

260-
const configProfileEntity = new ConfigProfileEntity({
261-
uuid,
262-
});
263-
264-
if (name) {
265-
configProfileEntity.name = name;
266-
}
267-
268-
if (config) {
269-
const existingInbounds = existingConfigProfile.inbounds;
270-
271-
const validatedConfig = new XRayConfig(config);
272-
validatedConfig.cleanClients();
273-
validatedConfig.fixIncorrectServerNames();
274-
const sortedConfig = validatedConfig.getSortedConfig();
275-
const inbounds = validatedConfig.getAllInbounds();
276-
277-
const inboundsEntities = inbounds.map(
278-
(inbound) =>
279-
new ConfigProfileInboundEntity({
280-
profileUuid: existingConfigProfile.uuid,
281-
tag: inbound.tag,
282-
type: inbound.type,
283-
network: inbound.network,
284-
security: inbound.security,
285-
port: inbound.port,
286-
rawInbound: inbound.rawInbound as unknown as object,
287-
}),
288-
);
289-
290-
await this.syncInbounds(existingInbounds, inboundsEntities);
291-
292-
configProfileEntity.config = sortedConfig as object;
293-
}
294-
295-
await this.configProfileRepository.update(configProfileEntity);
260+
await this.updateConfigProfileTransactional(existingConfigProfile, uuid, name, config);
296261

297262
if (config) {
298263
// No need for now
@@ -338,6 +303,54 @@ export class ConfigProfileService {
338303
}
339304
}
340305

306+
@Transactional()
307+
public async updateConfigProfileTransactional(
308+
existingConfigProfile: ConfigProfileWithInboundsAndNodesEntity,
309+
uuid: string,
310+
name?: string,
311+
config?: object,
312+
): Promise<boolean> {
313+
try {
314+
const configProfileEntity = new ConfigProfileEntity({
315+
uuid,
316+
name,
317+
});
318+
319+
if (config) {
320+
const existingInbounds = existingConfigProfile.inbounds;
321+
322+
const validatedConfig = new XRayConfig(config);
323+
validatedConfig.cleanClients();
324+
validatedConfig.fixIncorrectServerNames();
325+
const sortedConfig = validatedConfig.getSortedConfig();
326+
const inbounds = validatedConfig.getAllInbounds();
327+
328+
const inboundsEntities = inbounds.map(
329+
(inbound) =>
330+
new ConfigProfileInboundEntity({
331+
profileUuid: existingConfigProfile.uuid,
332+
tag: inbound.tag,
333+
type: inbound.type,
334+
network: inbound.network,
335+
security: inbound.security,
336+
port: inbound.port,
337+
rawInbound: inbound.rawInbound as unknown as object,
338+
}),
339+
);
340+
341+
await this.syncInbounds(existingInbounds, inboundsEntities);
342+
343+
configProfileEntity.config = sortedConfig as object;
344+
}
345+
346+
await this.configProfileRepository.update(configProfileEntity);
347+
348+
return true;
349+
} catch (error) {
350+
throw error;
351+
}
352+
}
353+
341354
public async getInboundsByProfileUuid(
342355
profileUuid: string,
343356
): Promise<ICommandResponse<GetAllInboundsResponseModel>> {

src/queue/start-all-nodes-by-profile/start-all-nodes-by-profile.processor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ export class StartAllNodesByProfileQueueProcessor extends WorkerHost {
101101
continue;
102102
}
103103

104+
this.logger.log(
105+
`Node ${node.uuid} has ${node.activeInbounds.length} active inbounds.`,
106+
);
107+
104108
await this.nodesRepository.update({
105109
uuid: node.uuid,
106110
isConnecting: true,

0 commit comments

Comments
 (0)