-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.go
218 lines (181 loc) · 3.93 KB
/
cli.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
214
215
216
217
218
package isle
import (
"encoding/json"
"fmt"
"io"
"net"
"os"
"os/signal"
"os/user"
"strconv"
"strings"
"time"
"github.com/creack/pty"
"github.com/hashicorp/go-hclog"
"github.com/lab47/isle/pkg/crypto/ssh"
"github.com/lab47/isle/pkg/crypto/ssh/terminal"
"github.com/lab47/isle/types"
"github.com/morikuni/aec"
"github.com/oleksandr/bonjour"
"golang.org/x/sys/unix"
)
type CLI struct {
Token string
Name string
Image string
Dir string
AsRoot bool
IsTerm bool
Console bool
LocalKey []byte
ConnectTo *bonjour.ServiceEntry
DirectIP string
L hclog.Logger
Path string
}
func (c *CLI) Shell(cmd string, stdin io.Reader, stdout io.Writer) error {
var cfg ssh.ClientConfig
cfg.HostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
}
if c.LocalKey != nil {
sig, err := ssh.ParsePrivateKey(c.LocalKey)
if err != nil {
return err
}
cfg.Auth = append(cfg.Auth, ssh.PublicKeys(sig))
}
cfg.Auth = append(cfg.Auth,
ssh.Password(c.Token),
)
cfg.SetDefaults()
var (
sconn *ssh.Client
err error
)
if c.DirectIP != "" {
sconn, err = ssh.Dial("tcp", c.DirectIP, &cfg)
if err != nil {
return err
}
} else if c.ConnectTo != nil {
addr := fmt.Sprintf("[%s]:%d", c.ConnectTo.AddrIPv6.String(), c.ConnectTo.Port)
c.L.Info("connecting to bonjour located instance", "addr", addr)
sconn, err = ssh.Dial("tcp", addr, &cfg)
if err != nil {
addr := fmt.Sprintf("%s:%d", c.ConnectTo.AddrIPv4.String(), c.ConnectTo.Port)
c.L.Info("connecting to bonjour located instance", "addr", addr)
sconn, err = ssh.Dial("tcp", addr, &cfg)
}
if err != nil {
return err
}
} else {
c.L.Info("connecting to local socket")
for i := 0; i < 100; i++ {
if c.IsTerm {
fmt.Printf("🐚 Connecting...%s",
aec.EmptyBuilder.Column(0).ANSI.String(),
)
}
sconn, err = ssh.Dial("unix", c.Path, &cfg)
if err == nil {
break
}
c.L.Error("error connecting to unixsocket", "error", err)
time.Sleep(time.Second)
}
}
sess, err := sconn.NewSession()
if err != nil {
c.L.Error("error creating new session", "error", err)
return err
}
sess.Stdout = stdout
sess.Stderr = stdout
sess.Stdin = stdin
setup := sess.Extended(2)
hostname, err := os.Hostname()
if err != nil {
hostname = "isle"
} else {
idx := strings.IndexByte(hostname, '.')
if idx != -1 {
hostname = hostname[:idx]
}
}
u, err := user.Current()
if err == nil {
uid, err := strconv.Atoi(u.Uid)
if err != nil {
return err
}
data, err := json.Marshal(&types.MSLInfo{
Name: c.Name,
Image: c.Image,
Dir: c.Dir,
AsRoot: c.AsRoot,
Hostname: hostname,
UserName: u.Username,
UserId: uid,
})
if err != nil {
return err
}
sess.Setenv("_MSL_INFO", string(data))
}
if c.Console {
sess.Setenv("ISLE_CONSOLE", "1")
}
forwardVars := []string{
"LANG",
"COLORTERM",
"TERM_PROGRAM",
"TERM_PROGRAM_VERSION",
}
for _, v := range forwardVars {
if val := os.Getenv(v); val != "" {
sess.Setenv(v, val)
}
}
rows, cols, err := pty.Getsize(os.Stdout)
if err == nil {
err = sess.RequestPty(os.Getenv("TERM"), rows, cols, nil)
if err != nil {
return err
}
}
if c.IsTerm {
fmt.Print(aec.EmptyBuilder.Column(0).EraseLine(aec.EraseModes.All).ANSI.String())
}
sigWin := make(chan os.Signal, 1)
go func() {
for {
select {
case <-sigWin:
rows, cols, err := pty.Getsize(os.Stdout)
if err == nil {
sess.WindowChange(rows, cols)
}
}
}
}()
signal.Notify(sigWin, unix.SIGWINCH)
if cmd == "" {
c.L.Info("running shell")
state, err := terminal.MakeRaw(int(os.Stdout.Fd()))
if err == nil {
defer terminal.Restore(int(os.Stdout.Fd()), state)
}
go io.Copy(os.Stderr, setup)
err = sess.Shell()
} else {
go io.Copy(io.Discard, setup)
c.L.Info("running command", "command", cmd)
err = sess.Start(cmd)
}
if err != nil {
return err
}
return sess.Wait()
}