@@ -4,30 +4,35 @@ import (
44 "bytes"
55 json2 "encoding/json"
66 "fmt"
7- "github.com/gorilla/websocket"
8- "github.com/sirupsen/logrus"
9- "golang.org/x/crypto/ssh"
107 "io"
118 "regexp"
129 "strconv"
1310 "sync"
1411 "time"
12+
13+ "github.com/gorilla/websocket"
14+ "github.com/sirupsen/logrus"
15+ "golang.org/x/crypto/ssh"
1516)
1617
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+
1823 config := & ssh.ClientConfig {
1924 Timeout : time .Second * 5 ,
20- User : "root" ,
25+ User : user ,
2126 HostKeyCallback : ssh .InsecureIgnoreHostKey (),
2227 //HostKeyCallback: ,
2328 //HostKeyCallback: hostKeyCallBackFunc(h.Host),
2429 }
2530 //if h.Type == "password" {
26- config .Auth = []ssh.AuthMethod {ssh .Password ("123456" )}
31+ config .Auth = []ssh.AuthMethod {ssh .Password (password )}
2732 //} else {
2833 // config.Auth = []ssh.AuthMethod{publicKeyAuthFunc(h.Key)}
2934 //}
30- addr := fmt .Sprintf ("%s:%d" , "192.168.2.142 " , 22 )
35+ addr := fmt .Sprintf ("%s:%d" , "127.0.0.1 " , 22 )
3136 c , err := ssh .Dial ("tcp" , addr , config )
3237 if err != nil {
3338 return nil , err
@@ -98,6 +103,98 @@ const (
98103 wsMsgResize = "resize"
99104)
100105
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+
101198//ReceiveWsMsg receive websocket msg do some handling then write into ssh.session.stdin
102199func (ssConn * SshConn ) ReceiveWsMsg (wsConn * websocket.Conn , logBuff * bytes.Buffer , exitCh chan bool ) {
103200 //tells other go routine quit
@@ -187,6 +284,64 @@ func flushComboOutput(w *wsBufferWriter, wsConn *websocket.Conn) error {
187284 }
188285 return nil
189286}
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+ }
190345func (ssConn * SshConn ) SessionWait (quitChan chan bool ) {
191346 if err := ssConn .Session .Wait (); err != nil {
192347 logrus .WithError (err ).Error ("ssh session wait failed" )
@@ -241,7 +396,7 @@ func WsReaderCopy(reader *websocket.Conn, writer io.Writer) {
241396 if err = json2 .Unmarshal (p , & msgObj ); err != nil {
242397 writer .Write (p )
243398 } 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 " ))
245400 }
246401 }
247402 }
0 commit comments