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 // @Success 200 {object} dto.SSHConnData
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Security Timestamp // @Security Timestamp
// @Router /settings/ssh [get] // @Router /settings/ssh/conn [get]
func (b *BaseApi) LoadLocalConn(c *gin.Context) { func (b *BaseApi) LoadLocalConn(c *gin.Context) {
connInfoInDB, err := settingService.GetSSHInfo() connInfoInDB := settingService.GetSettingByKey("LocalSSHConn")
if err != nil { if len(connInfoInDB) == 0 {
helper.InternalServer(c, err) helper.Success(c)
return return
} }
var data dto.SSHConnData var data dto.SSHConnData
if err := json.Unmarshal([]byte(connInfoInDB), &data); err != nil { if err := json.Unmarshal([]byte(connInfoInDB), &data); err != nil {
helper.InternalServer(c, err) helper.Success(c)
return return
} }
if len(data.Password) != 0 { if len(data.Password) != 0 {
@ -139,10 +139,7 @@ func (b *BaseApi) SaveLocalConn(c *gin.Context) {
} }
func loadLocalConn() (*ssh.SSHClient, error) { func loadLocalConn() (*ssh.SSHClient, error) {
connInfoInDB, err := settingService.GetSSHInfo() connInfoInDB := settingService.GetSettingByKey("LocalSSHConn")
if err != nil {
return nil, err
}
if len(connInfoInDB) == 0 { if len(connInfoInDB) == 0 {
return nil, errors.New("no such ssh conn info in db!") 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/app/dto"
"github.com/1Panel-dev/1Panel/agent/buserr" "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/encrypt"
"github.com/1Panel-dev/1Panel/agent/utils/ssh" "github.com/1Panel-dev/1Panel/agent/utils/ssh"
"github.com/jinzhu/copier" "github.com/jinzhu/copier"
@ -18,7 +19,6 @@ type ISettingService interface {
GetSettingInfo() (*dto.SettingInfo, error) GetSettingInfo() (*dto.SettingInfo, error)
Update(key, value string) error Update(key, value string) error
GetSSHInfo() (string, error)
TestConnByInfo(req dto.SSHConnData) bool TestConnByInfo(req dto.SSHConnData) bool
SaveConnInfo(req dto.SSHConnData) error SaveConnInfo(req dto.SSHConnData) error
GetSystemProxy() (*dto.SystemProxy, error) GetSystemProxy() (*dto.SystemProxy, error)
@ -55,14 +55,6 @@ func (u *SettingService) Update(key, value string) error {
return settingRepo.UpdateOrCreate(key, value) 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 { func (u *SettingService) TestConnByInfo(req dto.SSHConnData) bool {
if req.AuthMode == "password" && len(req.Password) != 0 { if req.AuthMode == "password" && len(req.Password) != 0 {
password, err := base64.StdEncoding.DecodeString(req.Password) password, err := base64.StdEncoding.DecodeString(req.Password)
@ -124,6 +116,7 @@ func (u *SettingService) SaveConnInfo(req dto.SSHConnData) error {
localConn, _ := json.Marshal(&connInfo) localConn, _ := json.Marshal(&connInfo)
connAfterEncrypt, _ := encrypt.StringEncrypt(string(localConn)) connAfterEncrypt, _ := encrypt.StringEncrypt(string(localConn))
_ = settingRepo.Update("LocalSSHConn", connAfterEncrypt) _ = settingRepo.Update("LocalSSHConn", connAfterEncrypt)
_ = settingRepo.Update("LocalSSHConnShow", constant.StatusEnable)
return nil return nil
} }
@ -140,17 +133,15 @@ func (u *SettingService) GetSystemProxy() (*dto.SystemProxy, error) {
func (u *SettingService) GetSettingByKey(key string) string { func (u *SettingService) GetSettingByKey(key string) string {
switch key { switch key {
case "SystemIP":
value, _ := settingRepo.GetValueByKey(key)
return value
case "LocalSSHConn": case "LocalSSHConn":
value, _ := settingRepo.GetValueByKey(key) value, _ := settingRepo.GetValueByKey(key)
if len(value) == 0 { if len(value) == 0 {
return "" return ""
} }
itemStr, _ := encrypt.StringDecryptWithBase64(value) itemStr, _ := encrypt.StringDecrypt(value)
return itemStr return itemStr
default: default:
return "" value, _ := settingRepo.GetValueByKey(key)
return value
} }
} }

View file

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

View file

@ -553,3 +553,13 @@ var InitLocalSSHConn = &gormigrate.Migration{
return nil 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 { addHost, loadLocalConn, testByInfo } from '@/api/modules/terminal';
import i18n from '@/lang'; import i18n from '@/lang';
import { MsgError, MsgSuccess } from '@/utils/message'; import { MsgError, MsgSuccess } from '@/utils/message';
import { Base64 } from 'js-base64';
const loading = ref(); const loading = ref();
const isOK = ref(false); const isOK = ref(false);
@ -81,21 +82,21 @@ const rules = reactive({
privateKey: [Rules.requiredInput], privateKey: [Rules.requiredInput],
}); });
const acceptParams = (init: boolean): void => { const acceptParams = (): void => {
if (!init) {
search(); search();
}
drawerVisible.value = true; drawerVisible.value = true;
}; };
const search = async () => { const search = async () => {
await loadLocalConn().then((res) => { await loadLocalConn().then((res) => {
if (res.data) {
form.addr = res.data.addr; form.addr = res.data.addr;
form.port = res.data.port; form.port = res.data.port;
form.authMode = res.data.authMode; form.authMode = res.data.authMode;
form.password = res.data.password; form.password = Base64.decode(res.data.password);
form.privateKey = res.data.privateKey; form.privateKey = res.data.privateKey;
form.passPhrase = res.data.passPhrase; form.passPhrase = res.data.passPhrase;
}
}); });
}; };

View file

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

View file

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