修复连接自动中断
This commit is contained in:
parent
da166c7394
commit
f57178513c
@ -7,6 +7,7 @@ import (
|
|||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AesEncryptCBC(origData []byte, key []byte) string {
|
func AesEncryptCBC(origData []byte, key []byte) string {
|
||||||
@ -23,7 +24,7 @@ func AesEncryptCBC(origData []byte, key []byte) string {
|
|||||||
blockMode.CryptBlocks(encrypted, origData) // 加密
|
blockMode.CryptBlocks(encrypted, origData) // 加密
|
||||||
return base64.StdEncoding.EncodeToString(encrypted)
|
return base64.StdEncoding.EncodeToString(encrypted)
|
||||||
}
|
}
|
||||||
func AesDecryptCBC(enc string, key []byte) string {
|
func AesDecryptCBC(enc string, key []byte) (string, error) {
|
||||||
key = Md5(key)[:24]
|
key = Md5(key)[:24]
|
||||||
encrypted, _ := base64.StdEncoding.DecodeString(enc)
|
encrypted, _ := base64.StdEncoding.DecodeString(enc)
|
||||||
block, _ := aes.NewCipher(key) // 分组秘钥
|
block, _ := aes.NewCipher(key) // 分组秘钥
|
||||||
@ -31,8 +32,11 @@ func AesDecryptCBC(enc string, key []byte) string {
|
|||||||
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) // 加密模式
|
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) // 加密模式
|
||||||
decrypted := make([]byte, len(encrypted)) // 创建数组
|
decrypted := make([]byte, len(encrypted)) // 创建数组
|
||||||
blockMode.CryptBlocks(decrypted, encrypted) // 解密
|
blockMode.CryptBlocks(decrypted, encrypted) // 解密
|
||||||
decrypted = pkcs7UnPadding(decrypted) // 去除补全码
|
decrypted, err := pkcs7UnPadding(decrypted) // 去除补全码
|
||||||
return string(decrypted)
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(decrypted), nil
|
||||||
}
|
}
|
||||||
func pkcs5Padding(ciphertext []byte, blockSize int) []byte {
|
func pkcs5Padding(ciphertext []byte, blockSize int) []byte {
|
||||||
padding := blockSize - len(ciphertext)%blockSize
|
padding := blockSize - len(ciphertext)%blockSize
|
||||||
@ -51,10 +55,13 @@ func pkcs7Padding(ciphertext []byte, blockSize int) []byte {
|
|||||||
return append(ciphertext, padtext...)
|
return append(ciphertext, padtext...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func pkcs7UnPadding(origData []byte) []byte {
|
func pkcs7UnPadding(origData []byte) ([]byte, error) {
|
||||||
length := len(origData)
|
length := len(origData)
|
||||||
unpadding := int(origData[length-1])
|
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 {
|
func Md5(str []byte) []byte {
|
||||||
|
|||||||
@ -171,9 +171,6 @@ func (ssConn *SshConn) SendComboOutput(wsConn *websocket.Conn, exitCh chan bool)
|
|||||||
case <-tick.C:
|
case <-tick.C:
|
||||||
//write combine output bytes into websocket response
|
//write combine output bytes into websocket response
|
||||||
if err := flushComboOutput(ssConn.ComboOutput, wsConn); err != nil {
|
if err := flushComboOutput(ssConn.ComboOutput, wsConn); err != nil {
|
||||||
if err == io.EOF{
|
|
||||||
log.Println("Exit")
|
|
||||||
}
|
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
//logrus.WithError(err).Error("ssh sending combo output to webSocket failed")
|
//logrus.WithError(err).Error("ssh sending combo output to webSocket failed")
|
||||||
return
|
return
|
||||||
@ -185,9 +182,22 @@ func (ssConn *SshConn) SendComboOutput(wsConn *websocket.Conn, exitCh chan bool)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ssConn *SshConn) SessionWait(quitChan chan bool) {
|
func (ssConn *SshConn) SessionWait(quitChan chan bool) {
|
||||||
if err := ssConn.Session.Wait(); err != nil {
|
//if err := ssConn.Session.Wait(); err != nil {
|
||||||
log.Println("ssh session wait failed")
|
// log.Println("ssh session wait failed")
|
||||||
setQuit(quitChan)
|
// 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) {
|
func (t *GetTerm) Decode(server model.Server) (sid string, err error) {
|
||||||
sid = uuid.Must(uuid.NewV4(), nil).String()
|
sid = uuid.Must(uuid.NewV4(), nil).String()
|
||||||
//log.Println(server)
|
//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 == "" {
|
if s_pass == "" {
|
||||||
return "", errors.New("秘钥验证失败")
|
return "", errors.New("秘钥验证失败")
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user