From f57178513c77fe925f3cca6c2c9802a790ace42b Mon Sep 17 00:00:00 2001 From: OldCat <924417424@qq.com> Date: Mon, 14 Dec 2020 23:18:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=BF=9E=E6=8E=A5=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E4=B8=AD=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/aes.go | 23 +++++++++++++++-------- common/core/ssh_shell_conn.go | 24 +++++++++++++++++------- model/Apiform/Api.go | 5 ++++- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/common/aes.go b/common/aes.go index 8a8d85b..1783af0 100644 --- a/common/aes.go +++ b/common/aes.go @@ -7,6 +7,7 @@ import ( "crypto/md5" "encoding/base64" "encoding/hex" + "errors" ) func AesEncryptCBC(origData []byte, key []byte) string { @@ -23,16 +24,19 @@ func AesEncryptCBC(origData []byte, key []byte) string { blockMode.CryptBlocks(encrypted, origData) // 加密 return base64.StdEncoding.EncodeToString(encrypted) } -func AesDecryptCBC(enc string, key []byte) string { +func AesDecryptCBC(enc string, key []byte) (string, error) { key = Md5(key)[:24] - encrypted,_ := base64.StdEncoding.DecodeString(enc) + encrypted, _ := base64.StdEncoding.DecodeString(enc) block, _ := aes.NewCipher(key) // 分组秘钥 blockSize := block.BlockSize() // 获取秘钥块的长度 blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) // 加密模式 - decrypted := make([]byte, len(encrypted)) // 创建数组 - blockMode.CryptBlocks(decrypted, encrypted) // 解密 - decrypted = pkcs7UnPadding(decrypted) // 去除补全码 - return string(decrypted) + decrypted := make([]byte, len(encrypted)) // 创建数组 + blockMode.CryptBlocks(decrypted, encrypted) // 解密 + decrypted, err := pkcs7UnPadding(decrypted) // 去除补全码 + if err != nil { + return "", err + } + return string(decrypted), nil } func pkcs5Padding(ciphertext []byte, blockSize int) []byte { padding := blockSize - len(ciphertext)%blockSize @@ -51,10 +55,13 @@ func pkcs7Padding(ciphertext []byte, blockSize int) []byte { return append(ciphertext, padtext...) } -func pkcs7UnPadding(origData []byte) []byte { +func pkcs7UnPadding(origData []byte) ([]byte, error) { length := len(origData) unpadding := int(origData[length-1]) - return origData[:(length - unpadding)] + if length-unpadding < 0 { + return []byte{}, errors.New("解密失败") + } + return origData[:(length - unpadding)], nil } func Md5(str []byte) []byte { diff --git a/common/core/ssh_shell_conn.go b/common/core/ssh_shell_conn.go index 357ec63..7671843 100644 --- a/common/core/ssh_shell_conn.go +++ b/common/core/ssh_shell_conn.go @@ -130,7 +130,7 @@ func (ssConn *SshConn) ReceiveWsMsg(wsConn *websocket.Conn, exitCh chan bool) { Cols: 180, } if err := json.Unmarshal(wsData, &msgObj); err != nil { - log.Println("unmarshal websocket message failed:",string(wsData)) + log.Println("unmarshal websocket message failed:", string(wsData)) continue } switch msgObj.Type { @@ -171,9 +171,6 @@ func (ssConn *SshConn) SendComboOutput(wsConn *websocket.Conn, exitCh chan bool) case <-tick.C: //write combine output bytes into websocket response if err := flushComboOutput(ssConn.ComboOutput, wsConn); err != nil { - if err == io.EOF{ - log.Println("Exit") - } log.Println(err.Error()) //logrus.WithError(err).Error("ssh sending combo output to webSocket failed") return @@ -185,9 +182,22 @@ func (ssConn *SshConn) SendComboOutput(wsConn *websocket.Conn, exitCh chan bool) } func (ssConn *SshConn) SessionWait(quitChan chan bool) { - if err := ssConn.Session.Wait(); err != nil { - log.Println("ssh session wait failed") - setQuit(quitChan) + //if err := ssConn.Session.Wait(); err != nil { + // log.Println("ssh session wait failed") + // setQuit(quitChan) + //} + timer := time.NewTicker(time.Second * 3) + defer timer.Stop() + for { + select { + case <-timer.C: + { + if _, err := ssConn.StdinPipe.Write([]byte{32,127}); err != nil { + log.Println("ws cmd bytes write to ssh.stdin pipe failed") + return + } + } + } } } diff --git a/model/Apiform/Api.go b/model/Apiform/Api.go index f38da6c..8886c7c 100644 --- a/model/Apiform/Api.go +++ b/model/Apiform/Api.go @@ -100,7 +100,10 @@ func (l *Login) Verify() (key, code string) { func (t *GetTerm) Decode(server model.Server) (sid string, err error) { sid = uuid.Must(uuid.NewV4(), nil).String() //log.Println(server) - s_pass := common.AesDecryptCBC(server.Password, []byte(t.Password)) + s_pass,err := common.AesDecryptCBC(server.Password, []byte(t.Password)) + if err != nil{ + return "",err + } if s_pass == "" { return "", errors.New("秘钥验证失败") } else {