MCPcopy
hub / github.com/Eugeny/tabby / ForwardedPort

Class ForwardedPort

tabby-ssh/src/session/forwards.ts:6–65  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4import { ForwardedPortConfig, PortForwardType } from '../api'
5
6export class ForwardedPort implements ForwardedPortConfig {
7 type: PortForwardType
8 host = '127.0.0.1'
9 port: number
10 targetAddress: string
11 targetPort: number
12 description: string
13
14 private listener: Server|null = null
15
16 async startLocalListener (callback: (accept: () => Socket, reject: () => void, sourceAddress: string|null, sourcePort: number|null, targetAddress: string, targetPort: number) => void): Promise<void> {
17 if (this.type === PortForwardType.Local) {
18 const listener = this.listener = createServer(s => callback(
19 () => s,
20 () => s.destroy(),
21 s.remoteAddress ?? null,
22 s.remotePort ?? null,
23 this.targetAddress,
24 this.targetPort,
25 ))
26 return new Promise((resolve, reject) => {
27 listener.listen(this.port, this.host)
28 listener.on('error', reject)
29 listener.on('listening', resolve)
30 })
31 } else if (this.type === PortForwardType.Dynamic) {
32 return new Promise((resolve, reject) => {
33 this.listener = socksv5.createServer((info, acceptConnection, rejectConnection) => {
34 callback(
35 () => acceptConnection(true),
36 () => rejectConnection(),
37 null,
38 null,
39 info.dstAddr,
40 info.dstPort,
41 )
42 }) as Server
43 this.listener.on('error', reject)
44 this.listener.listen(this.port, this.host, resolve)
45 this.listener['useAuth'](socksv5.auth.None())
46 })
47 } else {
48 throw new Error('Invalid forward type for a local listener')
49 }
50 }
51
52 stopLocalListener (): void {
53 this.listener?.close()
54 }
55
56 toString (): string {
57 if (this.type === PortForwardType.Local) {
58 return `(local) ${this.host}:${this.port} → (remote) ${this.targetAddress}:${this.targetPort}`
59 } if (this.type === PortForwardType.Remote) {
60 return `(remote) ${this.host}:${this.port} → (local) ${this.targetAddress}:${this.targetPort}`
61 } else {
62 return `(dynamic) ${this.host}:${this.port}`
63 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected