fix: Fix terminal connection exception issues (#10301)

This commit is contained in:
ssongliu 2025-09-08 22:08:42 +08:00 committed by GitHub
parent a099308a82
commit 80e233c4a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 38 additions and 38 deletions

View file

@ -75,16 +75,16 @@ func (b *BaseApi) LoadBaseDir(c *gin.Context) {
// @Success 200 {object} dto.SSHConnData
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /settings/ssh [get]
// @Router /settings/ssh/conn [get]
func (b *BaseApi) LoadLocalConn(c *gin.Context) {
connInfoInDB, err := settingService.GetSSHInfo()
if err != nil {
helper.InternalServer(c, err)
connInfoInDB := settingService.GetSettingByKey("LocalSSHConn")
if len(connInfoInDB) == 0 {
helper.Success(c)
return
}
var data dto.SSHConnData
if err := json.Unmarshal([]byte(connInfoInDB), &data); err != nil {
helper.InternalServer(c, err)
helper.Success(c)
return
}
if len(data.Password) != 0 {
@ -139,10 +139,7 @@ func (b *BaseApi) SaveLocalConn(c *gin.Context) {
}
func loadLocalConn() (*ssh.SSHClient, error) {
connInfoInDB, err := settingService.GetSSHInfo()
if err != nil {
return nil, err
}
connInfoInDB := settingService.GetSettingByKey("LocalSSHConn")
if len(connInfoInDB) == 0 {
return nil, errors.New("no such ssh conn info in db!")
}

View file

@ -7,6 +7,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/utils/encrypt"
"github.com/1Panel-dev/1Panel/agent/utils/ssh"
"github.com/jinzhu/copier"
@ -18,7 +19,6 @@ type ISettingService interface {
GetSettingInfo() (*dto.SettingInfo, error)
Update(key, value string) error
GetSSHInfo() (string, error)
TestConnByInfo(req dto.SSHConnData) bool
SaveConnInfo(req dto.SSHConnData) error
GetSystemProxy() (*dto.SystemProxy, error)
@ -55,14 +55,6 @@ func (u *SettingService) Update(key, value string) error {
return settingRepo.UpdateOrCreate(key, value)
}
func (u *SettingService) GetSSHInfo() (string, error) {
conn, err := settingRepo.GetValueByKey("LocalSSHConn")
if err != nil || len(conn) == 0 {
return "", err
}
return encrypt.StringDecrypt(conn)
}
func (u *SettingService) TestConnByInfo(req dto.SSHConnData) bool {
if req.AuthMode == "password" && len(req.Password) != 0 {
password, err := base64.StdEncoding.DecodeString(req.Password)
@ -124,6 +116,7 @@ func (u *SettingService) SaveConnInfo(req dto.SSHConnData) error {
localConn, _ := json.Marshal(&connInfo)
connAfterEncrypt, _ := encrypt.StringEncrypt(string(localConn))
_ = settingRepo.Update("LocalSSHConn", connAfterEncrypt)
_ = settingRepo.Update("LocalSSHConnShow", constant.StatusEnable)
return nil
}
@ -140,17 +133,15 @@ func (u *SettingService) GetSystemProxy() (*dto.SystemProxy, error) {
func (u *SettingService) GetSettingByKey(key string) string {
switch key {
case "SystemIP":
value, _ := settingRepo.GetValueByKey(key)
return value
case "LocalSSHConn":
value, _ := settingRepo.GetValueByKey(key)
if len(value) == 0 {
return ""
}
itemStr, _ := encrypt.StringDecryptWithBase64(value)
itemStr, _ := encrypt.StringDecrypt(value)
return itemStr
default:
return ""
value, _ := settingRepo.GetValueByKey(key)
return value
}
}

View file

@ -40,6 +40,7 @@ func InitAgentDB() {
migrations.AddQuickJump,
migrations.UpdateMcpServerAddType,
migrations.InitLocalSSHConn,
migrations.InitLocalSSHShow,
})
if err := m.Migrate(); err != nil {
global.LOG.Error(err)

View file

@ -553,3 +553,13 @@ var InitLocalSSHConn = &gormigrate.Migration{
return nil
},
}
var InitLocalSSHShow = &gormigrate.Migration{
ID: "20250908-init-local-ssh-show",
Migrate: func(tx *gorm.DB) error {
if err := tx.Create(&model.Setting{Key: "LocalSSHConnShow", Value: constant.StatusEnable}).Error; err != nil {
return err
}
return nil
},
}

View file

@ -47,6 +47,7 @@ import { Rules } from '@/global/form-rules';
import { addHost, loadLocalConn, testByInfo } from '@/api/modules/terminal';
import i18n from '@/lang';
import { MsgError, MsgSuccess } from '@/utils/message';
import { Base64 } from 'js-base64';
const loading = ref();
const isOK = ref(false);
@ -81,21 +82,21 @@ const rules = reactive({
privateKey: [Rules.requiredInput],
});
const acceptParams = (init: boolean): void => {
if (!init) {
search();
}
const acceptParams = (): void => {
search();
drawerVisible.value = true;
};
const search = async () => {
await loadLocalConn().then((res) => {
form.addr = res.data.addr;
form.port = res.data.port;
form.authMode = res.data.authMode;
form.password = res.data.password;
form.privateKey = res.data.privateKey;
form.passPhrase = res.data.passPhrase;
if (res.data) {
form.addr = res.data.addr;
form.port = res.data.port;
form.authMode = res.data.authMode;
form.password = Base64.decode(res.data.password);
form.privateKey = res.data.privateKey;
form.passPhrase = res.data.passPhrase;
}
});
};

View file

@ -166,8 +166,8 @@ const search = async (withReset?: boolean) => {
};
const loadConnShow = async () => {
await getAgentSettingByKey('LocalSSHConn').then((res) => {
form.showDefaultConn = res.data.length !== 0;
await getAgentSettingByKey('LocalSSHConnShow').then((res) => {
form.showDefaultConn = res.data === 'Enable';
});
};
@ -176,7 +176,7 @@ const changeShow = async () => {
dialogRef.value.acceptParams(true);
return;
}
await updateAgentSetting({ key: 'LocalSSHConn', value: '' }).then(() => {
await updateAgentSetting({ key: 'LocalSSHConnShow', value: 'Disable' }).then(() => {
MsgSuccess(i18n.global.t('commons.msg.operationSuccess'));
});
};

View file

@ -210,8 +210,8 @@ const acceptParams = async () => {
loadCommandTree();
loadHostTree();
if (terminalTabs.value.length === 0) {
await getAgentSettingByKey('LocalSSHConn').then((res) => {
if (res.data.length !== 0) {
await getAgentSettingByKey('LocalSSHConnShow').then((res) => {
if (res.data === 'Enable') {
onNewLocal();
}
});