174 lines
5.0 KiB
TypeScript
174 lines
5.0 KiB
TypeScript
import * as plugins from './plugins.js';
|
|
import { EventEmitter } from 'events';
|
|
import { decodeConnectionToken } from './classes.token.js';
|
|
|
|
// Command map for the edge side of remoteingress-bin
|
|
type TEdgeCommands = {
|
|
ping: {
|
|
params: Record<string, never>;
|
|
result: { pong: boolean };
|
|
};
|
|
startEdge: {
|
|
params: {
|
|
hubHost: string;
|
|
hubPort: number;
|
|
edgeId: string;
|
|
secret: string;
|
|
};
|
|
result: { started: boolean };
|
|
};
|
|
stopEdge: {
|
|
params: Record<string, never>;
|
|
result: { stopped: boolean; wasRunning?: boolean };
|
|
};
|
|
getEdgeStatus: {
|
|
params: Record<string, never>;
|
|
result: {
|
|
running: boolean;
|
|
connected: boolean;
|
|
publicIp: string | null;
|
|
activeStreams: number;
|
|
listenPorts: number[];
|
|
};
|
|
};
|
|
};
|
|
|
|
export interface IEdgeConfig {
|
|
hubHost: string;
|
|
hubPort?: number;
|
|
edgeId: string;
|
|
secret: string;
|
|
}
|
|
|
|
export class RemoteIngressEdge extends EventEmitter {
|
|
private bridge: InstanceType<typeof plugins.smartrust.RustBridge<TEdgeCommands>>;
|
|
private started = false;
|
|
private statusInterval: ReturnType<typeof setInterval> | undefined;
|
|
|
|
constructor() {
|
|
super();
|
|
|
|
const packageDir = plugins.path.resolve(
|
|
plugins.path.dirname(new URL(import.meta.url).pathname),
|
|
'..',
|
|
);
|
|
|
|
this.bridge = new plugins.smartrust.RustBridge<TEdgeCommands>({
|
|
binaryName: 'remoteingress-bin',
|
|
cliArgs: ['--management'],
|
|
requestTimeoutMs: 30_000,
|
|
readyTimeoutMs: 10_000,
|
|
localPaths: [
|
|
// Platform-suffixed binary in dist_rust (production)
|
|
plugins.path.join(packageDir, 'dist_rust', `remoteingress-bin_${process.platform === 'win32' ? 'windows' : 'linux'}_${process.arch === 'x64' ? 'amd64' : process.arch}`),
|
|
// Exact binaryName fallback in dist_rust
|
|
plugins.path.join(packageDir, 'dist_rust', 'remoteingress-bin'),
|
|
// Development build paths (cargo output uses exact name)
|
|
plugins.path.join(packageDir, 'rust', 'target', 'release', 'remoteingress-bin'),
|
|
plugins.path.join(packageDir, 'rust', 'target', 'debug', 'remoteingress-bin'),
|
|
],
|
|
searchSystemPath: false,
|
|
});
|
|
|
|
// Forward events from Rust binary
|
|
this.bridge.on('management:tunnelConnected', () => {
|
|
this.emit('tunnelConnected');
|
|
});
|
|
this.bridge.on('management:tunnelDisconnected', () => {
|
|
this.emit('tunnelDisconnected');
|
|
});
|
|
this.bridge.on('management:publicIpDiscovered', (data: { ip: string }) => {
|
|
this.emit('publicIpDiscovered', data);
|
|
});
|
|
this.bridge.on('management:portsAssigned', (data: { listenPorts: number[] }) => {
|
|
console.log(`[RemoteIngressEdge] Ports assigned by hub: ${data.listenPorts.join(', ')}`);
|
|
this.emit('portsAssigned', data);
|
|
});
|
|
this.bridge.on('management:portsUpdated', (data: { listenPorts: number[] }) => {
|
|
console.log(`[RemoteIngressEdge] Ports updated by hub: ${data.listenPorts.join(', ')}`);
|
|
this.emit('portsUpdated', data);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Start the edge — spawns the Rust binary and connects to the hub.
|
|
* Accepts either a connection token or an explicit IEdgeConfig.
|
|
*/
|
|
public async start(config: { token: string } | IEdgeConfig): Promise<void> {
|
|
let edgeConfig: IEdgeConfig;
|
|
|
|
if ('token' in config) {
|
|
const decoded = decodeConnectionToken(config.token);
|
|
edgeConfig = {
|
|
hubHost: decoded.hubHost,
|
|
hubPort: decoded.hubPort,
|
|
edgeId: decoded.edgeId,
|
|
secret: decoded.secret,
|
|
};
|
|
} else {
|
|
edgeConfig = config;
|
|
}
|
|
|
|
const spawned = await this.bridge.spawn();
|
|
if (!spawned) {
|
|
throw new Error('Failed to spawn remoteingress-bin');
|
|
}
|
|
|
|
await this.bridge.sendCommand('startEdge', {
|
|
hubHost: edgeConfig.hubHost,
|
|
hubPort: edgeConfig.hubPort ?? 8443,
|
|
edgeId: edgeConfig.edgeId,
|
|
secret: edgeConfig.secret,
|
|
});
|
|
|
|
this.started = true;
|
|
|
|
// Start periodic status logging
|
|
this.statusInterval = setInterval(async () => {
|
|
try {
|
|
const status = await this.getStatus();
|
|
console.log(
|
|
`[RemoteIngressEdge] Status: connected=${status.connected}, ` +
|
|
`streams=${status.activeStreams}, ports=[${status.listenPorts.join(',')}], ` +
|
|
`publicIp=${status.publicIp ?? 'unknown'}`
|
|
);
|
|
} catch {
|
|
// Bridge may be shutting down
|
|
}
|
|
}, 60_000);
|
|
}
|
|
|
|
/**
|
|
* Stop the edge and kill the Rust process.
|
|
*/
|
|
public async stop(): Promise<void> {
|
|
if (this.statusInterval) {
|
|
clearInterval(this.statusInterval);
|
|
this.statusInterval = undefined;
|
|
}
|
|
if (this.started) {
|
|
try {
|
|
await this.bridge.sendCommand('stopEdge', {} as Record<string, never>);
|
|
} catch {
|
|
// Process may already be dead
|
|
}
|
|
this.bridge.kill();
|
|
this.started = false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the current edge status.
|
|
*/
|
|
public async getStatus() {
|
|
return this.bridge.sendCommand('getEdgeStatus', {} as Record<string, never>);
|
|
}
|
|
|
|
/**
|
|
* Check if the bridge is running.
|
|
*/
|
|
public get running(): boolean {
|
|
return this.bridge.running;
|
|
}
|
|
}
|