@@ -4,30 +4,35 @@ import (
4
4
"bytes"
5
5
json2 "encoding/json"
6
6
"fmt"
7
- "github.com/gorilla/websocket"
8
- "github.com/sirupsen/logrus"
9
- "golang.org/x/crypto/ssh"
10
7
"io"
11
8
"regexp"
12
9
"strconv"
13
10
"sync"
14
11
"time"
12
+
13
+ "github.com/gorilla/websocket"
14
+ "github.com/sirupsen/logrus"
15
+ "golang.org/x/crypto/ssh"
15
16
)
16
17
17
- func NewSshClient () (* ssh.Client , error ) {
18
+ func NewSshClient (user , password string ) (* ssh.Client , error ) {
19
+
20
+ // connet to ssh
21
+ // addr = fmt.Sprintf("%s:%d", host, port)
22
+
18
23
config := & ssh.ClientConfig {
19
24
Timeout : time .Second * 5 ,
20
- User : "root" ,
25
+ User : user ,
21
26
HostKeyCallback : ssh .InsecureIgnoreHostKey (),
22
27
//HostKeyCallback: ,
23
28
//HostKeyCallback: hostKeyCallBackFunc(h.Host),
24
29
}
25
30
//if h.Type == "password" {
26
- config .Auth = []ssh.AuthMethod {ssh .Password ("123456" )}
31
+ config .Auth = []ssh.AuthMethod {ssh .Password (password )}
27
32
//} else {
28
33
// config.Auth = []ssh.AuthMethod{publicKeyAuthFunc(h.Key)}
29
34
//}
30
- addr := fmt .Sprintf ("%s:%d" , "192.168.2.142 " , 22 )
35
+ addr := fmt .Sprintf ("%s:%d" , "127.0.0.1 " , 22 )
31
36
c , err := ssh .Dial ("tcp" , addr , config )
32
37
if err != nil {
33
38
return nil , err
@@ -98,6 +103,98 @@ const (
98
103
wsMsgResize = "resize"
99
104
)
100
105
106
+ //ReceiveWsMsg receive websocket msg do some handling then write into ssh.session.stdin
107
+ func ReceiveWsMsgUser (wsConn * websocket.Conn , logBuff * bytes.Buffer ) string {
108
+ //tells other go routine quit
109
+ username := ""
110
+ for {
111
+
112
+ //read websocket msg
113
+ _ , wsData , err := wsConn .ReadMessage ()
114
+ if err != nil {
115
+
116
+ return ""
117
+ }
118
+
119
+ msgObj := wsMsg {}
120
+ if err := json2 .Unmarshal (wsData , & msgObj ); err != nil {
121
+ msgObj .Type = "cmd"
122
+ msgObj .Cmd = string (wsData )
123
+ }
124
+ //if err := json.Unmarshal(wsData, &msgObj); err != nil {
125
+ // logrus.WithError(err).WithField("wsData", string(wsData)).Error("unmarshal websocket message failed")
126
+ //}
127
+ switch msgObj .Type {
128
+ case wsMsgCmd :
129
+ //handle xterm.js stdin
130
+ //decodeBytes, err := base64.StdEncoding.DecodeString(msgObj.Cmd)
131
+ decodeBytes := []byte (msgObj .Cmd )
132
+ if msgObj .Cmd == "\u007f " {
133
+ if len (username ) == 0 {
134
+ continue
135
+ }
136
+ wsConn .WriteMessage (websocket .TextMessage , []byte ("\b \x1b [K" ))
137
+ username = username [:len (username )- 1 ]
138
+ continue
139
+ }
140
+ if msgObj .Cmd == "\r " {
141
+ return username
142
+ }
143
+ username += msgObj .Cmd
144
+
145
+ if err := wsConn .WriteMessage (websocket .TextMessage , decodeBytes ); err != nil {
146
+ logrus .WithError (err ).Error ("ws cmd bytes write to ssh.stdin pipe failed" )
147
+ }
148
+ //write input cmd to log buffer
149
+ if _ , err := logBuff .Write (decodeBytes ); err != nil {
150
+ logrus .WithError (err ).Error ("write received cmd into log buffer failed" )
151
+ }
152
+ }
153
+
154
+ }
155
+ }
156
+
157
+ func ReceiveWsMsgPassword (wsConn * websocket.Conn , logBuff * bytes.Buffer ) string {
158
+ //tells other go routine quit
159
+ password := ""
160
+ for {
161
+
162
+ //read websocket msg
163
+ _ , wsData , err := wsConn .ReadMessage ()
164
+ if err != nil {
165
+ logrus .WithError (err ).Error ("reading webSocket message failed" )
166
+ return ""
167
+ }
168
+
169
+ msgObj := wsMsg {}
170
+ if err := json2 .Unmarshal (wsData , & msgObj ); err != nil {
171
+ msgObj .Type = "cmd"
172
+ msgObj .Cmd = string (wsData )
173
+ }
174
+ //if err := json.Unmarshal(wsData, &msgObj); err != nil {
175
+ // logrus.WithError(err).WithField("wsData", string(wsData)).Error("unmarshal websocket message failed")
176
+ //}
177
+ switch msgObj .Type {
178
+ case wsMsgCmd :
179
+ //handle xterm.js stdin
180
+ //decodeBytes, err := base64.StdEncoding.DecodeString(msgObj.Cmd)
181
+ if msgObj .Cmd == "\r " {
182
+ return password
183
+ }
184
+
185
+ if msgObj .Cmd == "\u007f " {
186
+ if len (password ) == 0 {
187
+ continue
188
+ }
189
+ password = password [:len (password )- 1 ]
190
+ continue
191
+ }
192
+ password += msgObj .Cmd
193
+ }
194
+
195
+ }
196
+ }
197
+
101
198
//ReceiveWsMsg receive websocket msg do some handling then write into ssh.session.stdin
102
199
func (ssConn * SshConn ) ReceiveWsMsg (wsConn * websocket.Conn , logBuff * bytes.Buffer , exitCh chan bool ) {
103
200
//tells other go routine quit
@@ -187,6 +284,64 @@ func flushComboOutput(w *wsBufferWriter, wsConn *websocket.Conn) error {
187
284
}
188
285
return nil
189
286
}
287
+
288
+ //ReceiveWsMsg receive websocket msg do some handling then write into ssh.session.stdin
289
+ func (ssConn * SshConn ) Login (wsConn * websocket.Conn , logBuff * bytes.Buffer , exitCh chan bool ) {
290
+ //tells other go routine quit
291
+ defer setQuit (exitCh )
292
+ for {
293
+ select {
294
+ case <- exitCh :
295
+ return
296
+ default :
297
+ //read websocket msg
298
+ _ , wsData , err := wsConn .ReadMessage ()
299
+ if err != nil {
300
+ logrus .WithError (err ).Error ("reading webSocket message failed" )
301
+ return
302
+ }
303
+ //unmashal bytes into struct
304
+ //msgObj := wsMsg{
305
+ // Type: "cmd",
306
+ // Cmd: "",
307
+ // Rows: 50,
308
+ // Cols: 180,
309
+ //}
310
+ msgObj := wsMsg {}
311
+ if err := json2 .Unmarshal (wsData , & msgObj ); err != nil {
312
+ msgObj .Type = "cmd"
313
+ msgObj .Cmd = string (wsData )
314
+ }
315
+ //if err := json.Unmarshal(wsData, &msgObj); err != nil {
316
+ // logrus.WithError(err).WithField("wsData", string(wsData)).Error("unmarshal websocket message failed")
317
+ //}
318
+ switch msgObj .Type {
319
+
320
+ case wsMsgResize :
321
+ //handle xterm.js size change
322
+ if msgObj .Cols > 0 && msgObj .Rows > 0 {
323
+ if err := ssConn .Session .WindowChange (msgObj .Rows , msgObj .Cols ); err != nil {
324
+ logrus .WithError (err ).Error ("ssh pty change windows size failed" )
325
+ }
326
+ }
327
+ case wsMsgCmd :
328
+ //handle xterm.js stdin
329
+ //decodeBytes, err := base64.StdEncoding.DecodeString(msgObj.Cmd)
330
+ decodeBytes := []byte (msgObj .Cmd )
331
+ if err != nil {
332
+ logrus .WithError (err ).Error ("websock cmd string base64 decoding failed" )
333
+ }
334
+ if _ , err := ssConn .StdinPipe .Write (decodeBytes ); err != nil {
335
+ logrus .WithError (err ).Error ("ws cmd bytes write to ssh.stdin pipe failed" )
336
+ }
337
+ //write input cmd to log buffer
338
+ if _ , err := logBuff .Write (decodeBytes ); err != nil {
339
+ logrus .WithError (err ).Error ("write received cmd into log buffer failed" )
340
+ }
341
+ }
342
+ }
343
+ }
344
+ }
190
345
func (ssConn * SshConn ) SessionWait (quitChan chan bool ) {
191
346
if err := ssConn .Session .Wait (); err != nil {
192
347
logrus .WithError (err ).Error ("ssh session wait failed" )
@@ -241,7 +396,7 @@ func WsReaderCopy(reader *websocket.Conn, writer io.Writer) {
241
396
if err = json2 .Unmarshal (p , & msgObj ); err != nil {
242
397
writer .Write (p )
243
398
} else if msgObj .Type == wsMsgResize {
244
- writer .Write ([]byte ("stty rows " + strconv .Itoa (msgObj .Rows ) + " && stty cols " + strconv .Itoa (msgObj .Cols ) + " \r " ))
399
+ writer .Write ([]byte ("stty rows " + strconv .Itoa (msgObj .Rows ) + " && stty cols " + strconv .Itoa (msgObj .Cols ) + " \r " ))
245
400
}
246
401
}
247
402
}
0 commit comments