summaryrefslogtreecommitdiffstats
path: root/lib/xdsserver/terminal-ssh.go
blob: 3f4a344aee97cf1b299a1c509cd15e4a334bbc37 (plain)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * Copyright (C) 2018 "IoT.bzh"
 * Author Sebastien Douheret <sebastien@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package xdsserver

import (
	"fmt"
	"strings"
	"time"

	"gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib/eows"
	"gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
	socketio "github.com/googollee/go-socket.io"
	uuid "github.com/satori/go.uuid"
)

// ITARGET interface implementation for standard targets

// TermSSH .
type TermSSH struct {
	*Context
	termCfg  xsapiv1.TerminalConfig
	targetID string
	sshWS    *eows.ExecOverWS
}

// NewTermSSH Create a new instance of TermSSH
func NewTermSSH(ctx *Context, cfg xsapiv1.TerminalConfig, targetID string) *TermSSH {

	// Allocate and set default settings
	t := TermSSH{
		Context: ctx,
		termCfg: xsapiv1.TerminalConfig{
			ID:      cfg.ID,
			Name:    "ssh",
			Type:    xsapiv1.TypeTermSSH,
			Status:  xsapiv1.StatusTermClose,
			User:    "",
			Options: []string{""},
			Cols:    80,
			Rows:    24,
		},
		targetID: targetID,
	}

	t.UpdateConfig(cfg)
	return &t
}

// NewUID Get a UUID
func (t *TermSSH) _NewUID(suffix string) string {
	uuid := uuid.NewV1().String()
	if len(suffix) > 0 {
		uuid += "_" + suffix
	}
	return uuid
}

// GetConfig Get public part of terminal config
func (t *TermSSH) GetConfig() xsapiv1.TerminalConfig {
	return t.termCfg
}

// UpdateConfig Update terminal config
func (t *TermSSH) UpdateConfig(newCfg xsapiv1.TerminalConfig) *xsapiv1.TerminalConfig {

	if t.termCfg.ID == "" {
		if newCfg.ID != "" {
			t.termCfg.ID = newCfg.ID
		} else {
			t.termCfg.ID = t._NewUID("")
		}
	}
	if newCfg.Name != "" {
		t.termCfg.Name = newCfg.Name
	}
	if newCfg.User != "" {
		t.termCfg.User = newCfg.User
	}
	if len(newCfg.Options) > 0 {
		t.termCfg.Options = newCfg.Options
	}

	// Adjust terminal size
	t.Resize(newCfg.Cols, newCfg.Rows)

	return &t.termCfg
}

// Open a new terminal - execute ssh command and bind stdin/stdout to WebSocket
func (t *TermSSH) Open(sock *socketio.Socket, sessID string) (*xsapiv1.TerminalConfig, error) {

	// Get target info to retrieve IP
	tgt := t.targets.Get(t.targetID)
	if tgt == nil {
		return nil, fmt.Errorf("Cannot retrieve target definition")
	}
	tgtCfg := (*tgt).GetConfig()

	// Sanity check
	if tgtCfg.IP == "" {
		return nil, fmt.Errorf("null target IP")
	}
	userStr := ""
	if t.termCfg.User != "" {
		userStr = t.termCfg.User + "@"
	}

	// Compute ssh command
	cmd := "ssh"
	cmdID := "ssh_term_" + t.termCfg.ID
	args := t.termCfg.Options
	args = append(args, userStr+tgtCfg.IP)

	t.sshWS = eows.New(cmd, args, sock, sessID, cmdID)
	t.sshWS.Log = t.Log
	t.sshWS.OutSplit = eows.SplitChar
	t.sshWS.PtsMode = true

	// Define callback for input (stdin)
	t.sshWS.InputEvent = xsapiv1.TerminalInEvent
	t.sshWS.InputCB = func(e *eows.ExecOverWS, stdin string) (string, error) {
		t.Log.Debugf("STDIN <<%v>>", strings.Replace(stdin, "\n", "\\n", -1))

		// Handle Ctrl-D
		if len(stdin) == 1 && stdin == "\x04" {
			// Close stdin
			errMsg := fmt.Errorf("close stdin: %v", stdin)
			return "", errMsg
		}

		return stdin, nil
	}

	// Define callback for output (stdout+stderr)
	t.sshWS.OutputCB = func(e *eows.ExecOverWS, stdout, stderr string) {
		// IO socket can be nil when disconnected
		so := t.sessions.IOSocketGet(e.Sid)
		if so == nil {
			t.Log.Infof("%s not emitted: WS closed (sid:%s, CmdID:%s)", xsapiv1.TerminalOutEvent, e.Sid, e.CmdID)
			return
		}

		// Retrieve project ID and RootPath
		data := e.UserData
		termID := (*data)["ID"].(string)

		t.Log.Debugf("%s emitted - WS sid[4:] %s - id:%s - termID:%s", xsapiv1.TerminalOutEvent, e.Sid[4:], e.CmdID, termID)
		if stdout != "" {
			t.Log.Debugf("STDOUT <<%v>>", strings.Replace(stdout, "\n", "\\n", -1))
		}
		if stderr != "" {
			t.Log.Debugf("STDERR <<%v>>", strings.Replace(stderr, "\n", "\\n", -1))
		}

		// FIXME replace by .BroadcastTo a room
		err := (*so).Emit(xsapiv1.TerminalOutEvent, xsapiv1.TerminalOutMsg{
			TermID:    termID,
			Timestamp: time.Now().String(),
			Stdout:    stdout,
			Stderr:    stderr,
		})
		if err != nil {
			t.Log.Errorf("WS Emit : %v", err)
		}
	}

	// Define callback for output
	t.sshWS.ExitCB = func(e *eows.ExecOverWS, code int, err error) {
		t.Log.Debugf("Command [Cmd ID %s] exited: code %d, error: %v", e.CmdID, code, err)

		// IO socket can be nil when disconnected
		so := t.sessions.IOSocketGet(e.Sid)
		if so == nil {
			t.Log.Infof("%s not emitted - WS closed (id:%s)", xsapiv1.TerminalExitEvent, e.CmdID)
			return
		}

		// Retrieve project ID and RootPath
		data := e.UserData
		termID := (*data)["ID"].(string)

		// FIXME replace by .BroadcastTo a room
		errSoEmit := (*so).Emit(xsapiv1.TerminalExitEvent, xsapiv1.TerminalExitMsg{
			TermID:    termID,
			Timestamp: time.Now().String(),
			Code:      code,
			Error:     err,
		})
		if errSoEmit != nil {
			t.Log.Errorf("WS Emit : %v", errSoEmit)
		}

		t.termCfg.Status = xsapiv1.StatusTermClose
		t.sshWS = nil
	}

	// data (used within callbacks)
	data := make(map[string]interface{})
	data["ID"] = t.termCfg.ID
	t.sshWS.UserData = &data

	// Start ssh command
	t.Log.Infof("Execute [Cmd ID %s]: %v %v", t.sshWS.CmdID, t.sshWS.Cmd, t.sshWS.Args)

	if err := t.sshWS.Start(); err != nil {
		return &t.termCfg, err
	}

	t.termCfg.Status = xsapiv1.StatusTermOpen

	return &t.termCfg, nil
}

// Close a terminal
func (t *TermSSH) Close() (*xsapiv1.TerminalConfig, error) {
	// nothing to do when not open
	if t.termCfg.Status != xsapiv1.StatusTermOpen {
		return &t.termCfg, nil
	}

	err := t.sshWS.Signal("SIGTERM")

	return &t.termCfg, err
}

// Resize a terminal
func (t *TermSSH) Resize(cols, rows uint16) (*xsapiv1.TerminalConfig, error) {
	if t.sshWS == nil {
		return &t.termCfg, fmt.Errorf("ssh session not initialized")
	}

	if cols > 0 {
		t.termCfg.Cols = cols
	}
	if rows > 0 {
		t.termCfg.Rows = rows
	}

	err := t.sshWS.TerminalSetSize(t.termCfg.Rows, t.termCfg.Cols)
	if err != nil {
		t.Log.Errorf("Error ssh TerminalSetSize: %v", err)
	}

	return &t.termCfg, err
}

// Signal Send a signal to a terminal
func (t *TermSSH) Signal(sigName string) error {
	return t.sshWS.Signal(sigName)
}