MCPcopy Create free account
hub / github.com/coder/wush / TailnetSSH

Function TailnetSSH

xssh/client.go:17–105  ·  view source on GitHub ↗
(ctx context.Context, inv *serpent.Invocation, ts *tsnet.Server, addr string, stdio bool)

Source from the content-addressed store, hash-verified

15)
16
17func TailnetSSH(ctx context.Context, inv *serpent.Invocation, ts *tsnet.Server, addr string, stdio bool) error {
18 conn, err := ts.Dial(ctx, "tcp", addr)
19 if err != nil {
20 return err
21 }
22
23 // if stdio {
24 // gnConn, ok := conn.(*gonet.TCPConn)
25 // if !ok {
26 // panic("ssh tcp conn is not *gonet.TCPConn")
27 // }
28 // }
29
30 sshConn, channels, requests, err := ssh.NewClientConn(conn, "127.0.0.1:22", &ssh.ClientConfig{
31 HostKeyCallback: ssh.InsecureIgnoreHostKey(),
32 })
33 if err != nil {
34 return err
35 }
36
37 sshClient := ssh.NewClient(sshConn, channels, requests)
38 sshSession, err := sshClient.NewSession()
39 if err != nil {
40 return err
41 }
42
43 sshSession.Stdin = inv.Stdin
44 sshSession.Stdout = inv.Stdout
45 sshSession.Stderr = inv.Stderr
46
47 if len(inv.Args) > 1 {
48 return sshSession.Run(strings.Join(inv.Args, " "))
49 }
50
51 stdinFile, validIn := inv.Stdin.(*os.File)
52 stdoutFile, validOut := inv.Stdout.(*os.File)
53 if validIn && validOut && isatty.IsTerminal(stdinFile.Fd()) && isatty.IsTerminal(stdoutFile.Fd()) {
54 inState, err := pty.MakeInputRaw(stdinFile.Fd())
55 if err != nil {
56 return err
57 }
58 defer func() {
59 _ = pty.RestoreTerminal(stdinFile.Fd(), inState)
60 }()
61 outState, err := pty.MakeOutputRaw(stdoutFile.Fd())
62 if err != nil {
63 return err
64 }
65 defer func() {
66 _ = pty.RestoreTerminal(stdoutFile.Fd(), outState)
67 }()
68
69 windowChange := ListenWindowSize(ctx)
70 go func() {
71 for {
72 select {
73 case <-ctx.Done():
74 return

Callers 1

sshCmdFunction · 0.92

Calls 3

RunMethod · 0.80
ListenWindowSizeFunction · 0.70
DialMethod · 0.45

Tested by

no test coverage detected