-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathssh_client.go
213 lines (193 loc) · 8.52 KB
/
ssh_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
MIT License
Copyright (c) 2022-2024 The Trzsz Authors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package main
import (
"fmt"
"io"
"net"
"os"
"os/signal"
"path/filepath"
"runtime"
"time"
"github.com/kevinburke/ssh_config"
"github.com/trzsz/trzsz-go/trzsz"
"golang.org/x/crypto/ssh"
"golang.org/x/term"
)
func main() {
// parse ssh alias
if len(os.Args) != 2 {
fmt.Printf("Usage: %s ssh_alias\n", os.Args[0])
return
}
alias := os.Args[1]
host := ssh_config.Get(alias, "HostName")
port := ssh_config.Get(alias, "Port")
user := ssh_config.Get(alias, "User")
if host == "" || port == "" || user == "" {
fmt.Printf("ssh alias [%s] invalid: host=[%s] port=[%s] user=[%s]\n", alias, host, port, user)
return
}
// read private key
home, err := os.UserHomeDir()
if err != nil {
fmt.Printf("user home dir failed: %s\n", err)
return
}
var signers []ssh.Signer
for _, name := range []string{"id_rsa", "id_ecdsa", "id_ecdsa_sk", "id_ed25519", "id_ed25519_sk", "id_dsa"} {
path := filepath.Join(home, ".ssh", name)
if _, err := os.Stat(path); os.IsNotExist(err) {
continue
}
privateKey, err := os.ReadFile(path)
if err != nil {
fmt.Printf("read private key [%s] failed: %s\n", path, err)
return
}
signer, err := ssh.ParsePrivateKey(privateKey)
if err != nil {
fmt.Printf("parse private key [%s] failed: %s\n", path, err)
return
}
signers = append(signers, signer)
}
// ssh login
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{ssh.PublicKeys(signers...)},
Timeout: 3 * time.Second,
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // TODO should not be used for production code
}
client, err := ssh.Dial("tcp", net.JoinHostPort(host, port), config)
if err != nil {
fmt.Printf("ssh dial tcp [%s:%s] failed: %s\n", host, port, err)
return
}
defer client.Close()
session, err := client.NewSession()
if err != nil {
fmt.Printf("ssh new session failed: %s\n", err)
return
}
defer session.Close()
// make stdin to raw
fd := int(os.Stdin.Fd())
state, err := term.MakeRaw(fd)
if err != nil {
fmt.Printf("term make raw failed: %s\n", err)
return
}
defer term.Restore(fd, state) // nolint:all
// request a pty session
width, height, err := term.GetSize(fd)
if err != nil {
if runtime.GOOS != "windows" {
fmt.Printf("term get size failed: %s\n", err)
return
}
// TODO find another way to get size on Windows
width, height = 80, 40
}
if err := session.RequestPty("xterm-256color", height, width, ssh.TerminalModes{}); err != nil {
fmt.Printf("session request pty failed: %s\n", err)
return
}
// session input and output
serverIn, err := session.StdinPipe()
if err != nil {
fmt.Printf("session stdin pipe failed: %s\n", err)
return
}
serverOut, err := session.StdoutPipe()
if err != nil {
fmt.Printf("session stdout pipe failed: %s\n", err)
return
}
var trzszFilter *trzsz.TrzszFilter
// want to do something with stdin and stdout ?
var wantToDoSomethingWithStdinAndStdout bool
if !wantToDoSomethingWithStdinAndStdout {
// create a TrzszFilter to support trzsz, no need to control stdin and stdout.
//
// os.Stdin ┌────────┐ os.Stdin ┌─────────────┐ ServerIn ┌────────┐
// ───────────►│ ├─────────────►│ ├─────────────►│ │
// │ │ │ TrzszFilter │ │ │
// ◄───────────│ Client │◄─────────────┤ │◄─────────────┤ Server │
// os.Stdout │ │ os.Stdout └─────────────┘ ServerOut │ │
// ◄───────────│ │◄──────────────────────────────────────────┤ │
// os.Stderr └────────┘ stderr └────────┘
trzszFilter = trzsz.NewTrzszFilter(os.Stdin, os.Stdout, serverIn, serverOut,
trzsz.TrzszOptions{TerminalColumns: int32(width)})
session.Stderr = os.Stderr
} else {
// create a TrzszFilter to support trzsz, with stdin and stdout controllable.
//
// ┌──────────────────────────────────────────┐
// │ Client │
// │ │
// os.Stdin │ ┌──────────┐ stdinPipe ┌───────────┐ │ ClientIn ┌─────────────┐ ServerIn ┌────────┐
// ────────────┼►│ ├────────────►│ ├───┼───────────►│ ├─────────────►│ │
// │ │ Custom │ │ io.Pipe │ │ │ TrzszFilter │ │ │
// ◄───────────┼─┤ │◄────────────┤ │◄──┼────────────┤ │◄─────────────┤ Server │
// os.Stdout │ └──────────┘ stdoutPipe └───────────┘ │ ClientOut └─────────────┘ ServerOut │ │
// ◄───────────│ │◄────────────────────────────────────────┤ │
// os.Stderr └──────────────────────────────────────────┘ stderr └────────┘
clientIn, stdinPipe := io.Pipe() // You can treat stdinPipe as session.StdinPipe()
stdoutPipe, clientOut := io.Pipe() // You can treat stdoutPipe as session.StdoutPipe()
trzszFilter = trzsz.NewTrzszFilter(clientIn, clientOut, serverIn, serverOut,
trzsz.TrzszOptions{TerminalColumns: int32(width)})
// TODO implement your function with stdin, stdout and stderr
go io.Copy(stdinPipe, os.Stdin) // nolint:all
go io.Copy(os.Stdout, stdoutPipe) // nolint:all
session.Stderr = os.Stderr
}
// connect to linux directly is not affected by Windows
trzsz.SetAffectedByWindows(false)
// reset terminal columns on resize
ch := make(chan os.Signal, 1)
// signal.Notify(ch, syscall.SIGWINCH) // TODO find another way to do this on Windows
go func() {
for range ch {
width, height, err := term.GetSize(fd)
if err != nil {
fmt.Printf("term get size failed: %s\n", err)
continue
}
_ = session.WindowChange(height, width)
trzszFilter.SetTerminalColumns(int32(width))
}
}()
defer func() { signal.Stop(ch); close(ch) }()
go func() {
// call TrzszFilter to upload some files and directories as you want
// trzszFilter.UploadFiles([]string{"/path/to/file", "/path/to/directory"})
// tell TrzszFilter to stop transferring files if necessary
// trzszFilter.StopTransferringFiles()
}()
// start shell
if err := session.Shell(); err != nil {
fmt.Printf("session shell failed: %s\n", err)
return
}
// wait for exit
session.Wait() // nolint:all
}