mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-19 16:29:17 +08:00
fix: 解决主机密钥无法正常连接终端的问题 (#1675)
This commit is contained in:
parent
e555dcb903
commit
dac0c81d96
@ -1,8 +1,6 @@
|
|||||||
package v1
|
package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
|
|
||||||
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
||||||
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
||||||
"github.com/1Panel-dev/1Panel/backend/constant"
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
||||||
@ -30,44 +28,6 @@ func (b *BaseApi) CreateHost(c *gin.Context) {
|
|||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if req.AuthMode == "password" && len(req.Password) != 0 {
|
|
||||||
password, err := base64.StdEncoding.DecodeString(req.Password)
|
|
||||||
if err != nil {
|
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
passwordItem, err := encrypt.StringEncrypt(string(password))
|
|
||||||
if err != nil {
|
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
req.Password = passwordItem
|
|
||||||
req.PrivateKey = ""
|
|
||||||
req.PassPhrase = ""
|
|
||||||
}
|
|
||||||
if req.AuthMode == "key" && len(req.PrivateKey) != 0 {
|
|
||||||
privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey)
|
|
||||||
if err != nil {
|
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
keyItem, err := encrypt.StringEncrypt(string(privateKey))
|
|
||||||
if err != nil {
|
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
req.Password = keyItem
|
|
||||||
|
|
||||||
if len(req.PassPhrase) != 0 {
|
|
||||||
pass, err := encrypt.StringEncrypt(req.PassPhrase)
|
|
||||||
if err != nil {
|
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
req.PassPhrase = pass
|
|
||||||
}
|
|
||||||
req.Password = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
host, err := hostService.Create(req)
|
host, err := hostService.Create(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -216,40 +176,30 @@ func (b *BaseApi) UpdateHost(c *gin.Context) {
|
|||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if req.AuthMode == "password" && len(req.Password) != 0 {
|
var err error
|
||||||
password, err := base64.StdEncoding.DecodeString(req.Password)
|
if len(req.Password) != 0 && req.AuthMode == "password" {
|
||||||
|
req.Password, err = hostService.EncryptHost(req.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
passwordItem, err := encrypt.StringEncrypt(string(password))
|
req.PrivateKey = ""
|
||||||
if err != nil {
|
req.PassPhrase = ""
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
req.Password = passwordItem
|
|
||||||
}
|
}
|
||||||
if req.AuthMode == "key" && len(req.PrivateKey) != 0 {
|
if len(req.PrivateKey) != 0 && req.AuthMode == "key" {
|
||||||
privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey)
|
req.PrivateKey, err = hostService.EncryptHost(req.PrivateKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
keyItem, err := encrypt.StringEncrypt(string(privateKey))
|
|
||||||
if err != nil {
|
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
req.PrivateKey = keyItem
|
|
||||||
|
|
||||||
if len(req.PassPhrase) != 0 {
|
if len(req.PassPhrase) != 0 {
|
||||||
pass, err := encrypt.StringEncrypt(req.PassPhrase)
|
req.PassPhrase, err = encrypt.StringEncrypt(req.PassPhrase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
req.PassPhrase = pass
|
|
||||||
}
|
}
|
||||||
|
req.Password = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
upMap := make(map[string]interface{})
|
upMap := make(map[string]interface{})
|
||||||
|
@ -24,6 +24,8 @@ type IHostService interface {
|
|||||||
Create(hostDto dto.HostOperate) (*dto.HostInfo, error)
|
Create(hostDto dto.HostOperate) (*dto.HostInfo, error)
|
||||||
Update(id uint, upMap map[string]interface{}) error
|
Update(id uint, upMap map[string]interface{}) error
|
||||||
Delete(id []uint) error
|
Delete(id []uint) error
|
||||||
|
|
||||||
|
EncryptHost(itemVal string) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIHostService() IHostService {
|
func NewIHostService() IHostService {
|
||||||
@ -220,6 +222,28 @@ func (u *HostService) SearchForTree(search dto.SearchForTree) ([]dto.HostTree, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *HostService) Create(req dto.HostOperate) (*dto.HostInfo, error) {
|
func (u *HostService) Create(req dto.HostOperate) (*dto.HostInfo, error) {
|
||||||
|
var err error
|
||||||
|
if len(req.Password) != 0 && req.AuthMode == "password" {
|
||||||
|
req.Password, err = u.EncryptHost(req.Password)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.PrivateKey = ""
|
||||||
|
req.PassPhrase = ""
|
||||||
|
}
|
||||||
|
if len(req.PrivateKey) != 0 && req.AuthMode == "key" {
|
||||||
|
req.PrivateKey, err = u.EncryptHost(req.PrivateKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(req.PassPhrase) != 0 {
|
||||||
|
req.PassPhrase, err = encrypt.StringEncrypt(req.PassPhrase)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
req.Password = ""
|
||||||
|
}
|
||||||
var host model.Host
|
var host model.Host
|
||||||
if err := copier.Copy(&host, &req); err != nil {
|
if err := copier.Copy(&host, &req); err != nil {
|
||||||
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
||||||
@ -290,3 +314,12 @@ func (u *HostService) Delete(ids []uint) error {
|
|||||||
func (u *HostService) Update(id uint, upMap map[string]interface{}) error {
|
func (u *HostService) Update(id uint, upMap map[string]interface{}) error {
|
||||||
return hostRepo.Update(id, upMap)
|
return hostRepo.Update(id, upMap)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *HostService) EncryptHost(itemVal string) (string, error) {
|
||||||
|
privateKey, err := base64.StdEncoding.DecodeString(itemVal)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
keyItem, err := encrypt.StringEncrypt(string(privateKey))
|
||||||
|
return keyItem, err
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user