feat(dcrouter): add managed local dcrouter mode with status controls and gateway integration

This commit is contained in:
2026-05-09 20:04:02 +00:00
parent 595e84cdb6
commit dc37a71802
19 changed files with 873 additions and 31 deletions
+37 -12
View File
@@ -3,6 +3,7 @@ import { logger } from '../logging.ts';
import { getErrorMessage } from '../utils/error.ts';
import { OneboxDatabase } from './database.ts';
import type { IDomain, IService } from '../types.ts';
import type { TDcRouterMode } from './managed-dcrouter.ts';
type TWorkHosterType = 'onebox';
@@ -124,6 +125,9 @@ export class ExternalGatewayManager {
}
public async isConfigured(): Promise<boolean> {
if (this.getMode() === 'disabled') {
return false;
}
const config = await this.getConfig({ requireTarget: false });
return Boolean(config);
}
@@ -336,13 +340,24 @@ export class ExternalGatewayManager {
}
private async getConfig(options: { requireTarget?: boolean } = {}): Promise<IExternalGatewayConfig | null> {
const url = this.normalizeUrl(this.database.getSetting('dcrouterGatewayUrl') || '');
const apiToken = await this.database.getSecretSetting('dcrouterGatewayApiToken');
const mode = this.getMode();
if (mode === 'disabled') {
return null;
}
const url = mode === 'managed'
? this.oneboxRef.managedDcRouter.getGatewayUrl()
: this.normalizeUrl(this.database.getSetting('dcrouterGatewayUrl') || '');
const apiToken = mode === 'managed'
? await this.oneboxRef.managedDcRouter.getAdminToken()
: await this.database.getSecretSetting('dcrouterGatewayApiToken');
if (!url || !apiToken) {
return null;
}
const gatewayClientId = this.ensureGatewayClientId();
const gatewayClientId = mode === 'managed'
? this.oneboxRef.managedDcRouter.ensureGatewayClientId()
: this.ensureGatewayClientId();
const config: IExternalGatewayConfig = {
url,
apiToken,
@@ -351,15 +366,21 @@ export class ExternalGatewayManager {
};
if (options.requireTarget !== false) {
config.targetHost = this.database.getSetting('dcrouterTargetHost')
|| this.database.getSetting('serverIP')
|| undefined;
const targetPort = this.parsePort(
this.database.getSetting('dcrouterTargetPort')
|| this.database.getSetting('httpPort')
|| '80',
);
config.targetPort = targetPort;
if (mode === 'managed') {
const target = this.oneboxRef.managedDcRouter.getRouteTarget();
config.targetHost = target.host;
config.targetPort = target.port;
} else {
config.targetHost = this.database.getSetting('dcrouterTargetHost')
|| this.database.getSetting('serverIP')
|| undefined;
const targetPort = this.parsePort(
this.database.getSetting('dcrouterTargetPort')
|| this.database.getSetting('httpPort')
|| '80',
);
config.targetPort = targetPort;
}
if (!config.targetHost) {
throw new Error('dcrouterTargetHost or serverIP must be configured for external gateway route sync');
@@ -369,6 +390,10 @@ export class ExternalGatewayManager {
return config;
}
private getMode(): TDcRouterMode {
return this.oneboxRef.managedDcRouter?.getMode?.() || 'external';
}
private async requireConfig(options: { requireTarget?: boolean } = {}): Promise<IExternalGatewayConfig> {
const config = await this.getConfig(options);
if (!config) {