-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh.go
More file actions
171 lines (135 loc) · 3.43 KB
/
ssh.go
File metadata and controls
171 lines (135 loc) · 3.43 KB
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
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"golang.org/x/crypto/ssh"
"golang.org/x/term"
)
const (
// maxKeyFileSize = 2 * 1024 * 1024 // 2 megabytes
sshBaudSpeed = 14400 // 14400,
// sshBaudSpeed = 38400 // 14400,
)
/*
func loadKeyFile(filepath string) ssh.Signer {
key, err := os.ReadFile(filepath)
if err != nil {
return nil
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil
}
return signer
}
func loadLocalKeys(options RemoteShellOptions) []ssh.Signer {
if options.forceKeyFile != "" {
return []ssh.Signer{loadKeyFile(options.forceKeyFile)}
}
homeDir, err := os.UserHomeDir()
if err != nil {
panic("Unable to determine user home directory")
}
sshDir := fmt.Sprintf("%s/.ssh", homeDir)
files, err := os.ReadDir(sshDir)
if err != nil {
panic("unable to read keys from ssh directory")
}
var signers []ssh.Signer
for _, file := range files {
if file.Type().IsRegular() {
info, err := file.Info()
if err != nil {
continue
}
if info.Size() <= maxKeyFileSize {
keyFile := fmt.Sprintf("%s/%s", sshDir, file.Name())
signer := loadKeyFile(keyFile)
if signer != nil {
log.Println("Loaded key", keyFile)
signers = append(signers, signer)
}
}
}
}
return signers
}
*/
func launchSSHSession(options RemoteShellOptions, containerHost string, port int32) {
// sshKeys := loadLocalKeys(options)
// if len(sshKeys) == 0 {
// log.Panicln("Could not load any keys!")
// }
config := &ssh.ClientConfig{
User: "cloud87",
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
ClientVersion: fmt.Sprintf("SSH-2.0-cloud87-client-%s", buildVersion), // MUST start with SSH-2.0-
Timeout: 30 * time.Second,
Auth: []ssh.AuthMethod{
// ssh.PublicKeys(sshKeys...),
ssh.PublicKeys(options.privateKey),
},
}
signalChan := make(chan os.Signal)
signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGWINCH)
containerAddress := fmt.Sprintf("%s:%d", containerHost, port)
client, err := ssh.Dial("tcp", containerAddress, config)
check(err)
defer client.Close()
session, err := client.NewSession()
check(err)
defer session.Close()
termFd := int(os.Stdin.Fd())
width, height, err := term.GetSize(termFd)
if err != nil {
log.Println("Failed to get terminal size")
height = 24
width = 80
}
log.Printf("Terminal Size: %d rows x %d cols\n", height, width)
fmt.Println("")
state, err := term.MakeRaw(termFd)
if err != nil {
panic(err)
}
defer term.Restore(termFd, state)
modes := ssh.TerminalModes{
ssh.ECHO: 0,
ssh.TTY_OP_ISPEED: sshBaudSpeed,
ssh.TTY_OP_OSPEED: sshBaudSpeed,
}
session.Stdin = os.Stdin
session.Stderr = os.Stderr
session.Stdout = os.Stdout
go func() {
for value := range signalChan {
if value == syscall.SIGINT {
session.Signal(ssh.SIGINT)
} else if value == syscall.SIGTERM {
session.Signal(ssh.SIGTERM)
} else if value == syscall.SIGWINCH {
width, height, err := term.GetSize(termFd)
if err == nil {
_ = session.WindowChange(height, width)
}
} else {
// uhhh?
}
}
}()
termName := fetchEnvValue("TERM", "xterm")
if err := session.RequestPty(termName, int(height), int(width), modes); err != nil {
log.Panicln("Unable to request PTY session: ", err)
}
if err := session.Shell(); err != nil {
log.Panicln("Unable to shell: ", err)
}
if err := session.Wait(); err != nil {
panic(err)
}
close(signalChan)
}