27 lines
844 B
TypeScript
27 lines
844 B
TypeScript
|
|
import { SshClient } from './classes.sshclient.js';
|
||
|
|
import type { IProxyRequest } from './types.js';
|
||
|
|
|
||
|
|
export class PortProxy {
|
||
|
|
constructor(private sshClient = new SshClient()) {}
|
||
|
|
|
||
|
|
public buildArgs(request: IProxyRequest): string[] {
|
||
|
|
this.validateLocalForward(request.localForward);
|
||
|
|
return this.sshClient.buildSshArgs(request.host, {
|
||
|
|
localForwards: [request.localForward],
|
||
|
|
extraArgs: ['-N'],
|
||
|
|
exitOnForwardFailure: true,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
public async start(request: IProxyRequest): Promise<number> {
|
||
|
|
return this.sshClient.spawnInteractive('ssh', this.buildArgs(request));
|
||
|
|
}
|
||
|
|
|
||
|
|
private validateLocalForward(localForward: string): void {
|
||
|
|
const parts = localForward.split(':');
|
||
|
|
if (parts.length < 3) {
|
||
|
|
throw new Error('Local forward must look like <localPort>:<remoteHost>:<remotePort>');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|