修复连接自动中断
This commit is contained in:
parent
da166c7394
commit
f57178513c
@ -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, 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 {
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user