diff --git a/cmd/tsconnect/src/lib/ssh.ts b/cmd/tsconnect/src/lib/ssh.ts index 424687909..c1308b3d5 100644 --- a/cmd/tsconnect/src/lib/ssh.ts +++ b/cmd/tsconnect/src/lib/ssh.ts @@ -27,26 +27,39 @@ export function runSSHSession( term.focus() - const sshSession = ipn.ssh(def.hostname, def.username, { - writeFn: (input) => term.write(input), - setReadFn: (hook) => (onDataHook = hook), + let resizeObserver: ResizeObserver | undefined + let handleBeforeUnload: ((e: BeforeUnloadEvent) => void) | undefined + + const sshSession = ipn.ssh(def.hostname + "2", def.username, { + writeFn(input) { + term.write(input) + }, + writeErrorFn(err) { + console.error(err) + term.write(err) + }, + setReadFn(hook) { + onDataHook = hook + }, rows: term.rows, cols: term.cols, - onDone: () => { - resizeObserver.disconnect() + onDone() { + resizeObserver?.disconnect() term.dispose() - window.removeEventListener("beforeunload", handleBeforeUnload) + if (handleBeforeUnload) { + window.removeEventListener("beforeunload", handleBeforeUnload) + } onDone() }, }) // Make terminal and SSH session track the size of the containing DOM node. - const resizeObserver = new ResizeObserver(() => fitAddon.fit()) + resizeObserver = new ResizeObserver(() => fitAddon.fit()) resizeObserver.observe(termContainerNode) term.onResize(({ rows, cols }) => sshSession.resize(rows, cols)) // Close the session if the user closes the window without an explicit // exit. - const handleBeforeUnload = () => sshSession.close() + handleBeforeUnload = () => sshSession.close() window.addEventListener("beforeunload", handleBeforeUnload) } diff --git a/cmd/tsconnect/src/types/wasm_js.d.ts b/cmd/tsconnect/src/types/wasm_js.d.ts index 9d6622a7c..b7ae9c793 100644 --- a/cmd/tsconnect/src/types/wasm_js.d.ts +++ b/cmd/tsconnect/src/types/wasm_js.d.ts @@ -19,6 +19,7 @@ declare global { username: string, termConfig: { writeFn: (data: string) => void + writeErrorFn: (err: string) => void setReadFn: (readFn: (data: string) => void) => void rows: number cols: number diff --git a/cmd/tsconnect/wasm/wasm_js.go b/cmd/tsconnect/wasm/wasm_js.go index 18abef044..e4cf762bf 100644 --- a/cmd/tsconnect/wasm/wasm_js.go +++ b/cmd/tsconnect/wasm/wasm_js.go @@ -347,6 +347,7 @@ type jsSSHSession struct { func (s *jsSSHSession) Run() { writeFn := s.termConfig.Get("writeFn") + writeErrorFn := s.termConfig.Get("writeErrorFn") setReadFn := s.termConfig.Get("setReadFn") rows := s.termConfig.Get("rows").Int() cols := s.termConfig.Get("cols").Int() @@ -357,7 +358,7 @@ func (s *jsSSHSession) Run() { writeFn.Invoke(s) } writeError := func(label string, err error) { - write(fmt.Sprintf("%s Error: %v\r\n", label, err)) + writeErrorFn.Invoke(fmt.Sprintf("%s Error: %v\r\n", label, err)) } ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)