Skip to content

Commit c3bd196

Browse files
authored
fix: Fix the problem of terminal saving failure (#8043)
1 parent 5963e28 commit c3bd196

File tree

15 files changed

+205
-134
lines changed

15 files changed

+205
-134
lines changed

agent/app/api/v2/dashboard.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -137,24 +137,3 @@ func (b *BaseApi) LoadDashboardCurrentInfo(c *gin.Context) {
137137
data := dashboardService.LoadCurrentInfo(ioOption, netOption)
138138
helper.SuccessWithData(c, data)
139139
}
140-
141-
// @Tags Dashboard
142-
// @Summary System restart
143-
// @Accept json
144-
// @Param operation path string true "request"
145-
// @Success 200
146-
// @Security ApiKeyAuth
147-
// @Security Timestamp
148-
// @Router /dashboard/system/restart/:operation [post]
149-
func (b *BaseApi) SystemRestart(c *gin.Context) {
150-
operation, ok := c.Params.Get("operation")
151-
if !ok {
152-
helper.BadRequest(c, errors.New("error operation in path"))
153-
return
154-
}
155-
if err := dashboardService.Restart(operation); err != nil {
156-
helper.InternalServer(c, err)
157-
return
158-
}
159-
helper.SuccessWithOutData(c)
160-
}

agent/app/service/dashboard.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ type IDashboardService interface {
4242

4343
LoadAppLauncher() ([]dto.AppLauncher, error)
4444
ListLauncherOption(filter string) ([]dto.LauncherOption, error)
45-
Restart(operation string) error
4645
}
4746

4847
func NewIDashboardService() IDashboardService {
@@ -78,23 +77,6 @@ func (u *DashboardService) Sync(req dto.SyncFromMaster) error {
7877
}
7978
}
8079

81-
func (u *DashboardService) Restart(operation string) error {
82-
if operation != "1panel" && operation != "system" {
83-
return fmt.Errorf("handle restart operation %s failed, err: nonsupport such operation", operation)
84-
}
85-
itemCmd := fmt.Sprintf("%s systemctl restart 1panel-agent.service", cmd.SudoHandleCmd())
86-
if operation == "system" {
87-
itemCmd = fmt.Sprintf("%s reboot", cmd.SudoHandleCmd())
88-
}
89-
go func() {
90-
stdout, err := cmd.Exec(itemCmd)
91-
if err != nil {
92-
global.LOG.Errorf("handle %s failed, err: %v", itemCmd, stdout)
93-
}
94-
}()
95-
return nil
96-
}
97-
9880
func (u *DashboardService) LoadOsInfo() (*dto.OsInfo, error) {
9981
var baseInfo dto.OsInfo
10082
hostInfo, err := host.Info()

agent/router/ro_dashboard.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,5 @@ func (s *DashboardRouter) InitRouter(Router *gin.RouterGroup) {
1818
cmdRouter.GET("/base/:ioOption/:netOption", baseApi.LoadDashboardBaseInfo)
1919
cmdRouter.GET("/current/node", baseApi.LoadCurrentInfoForNode)
2020
cmdRouter.GET("/current/:ioOption/:netOption", baseApi.LoadDashboardCurrentInfo)
21-
cmdRouter.POST("/system/restart/:operation", baseApi.SystemRestart)
2221
}
2322
}

core/app/api/v2/host.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/1Panel-dev/1Panel/core/app/api/v2/helper"
1111
"github.com/1Panel-dev/1Panel/core/app/dto"
12+
"github.com/1Panel-dev/1Panel/core/app/service"
1213
"github.com/1Panel-dev/1Panel/core/global"
1314
"github.com/1Panel-dev/1Panel/core/utils/copier"
1415
"github.com/1Panel-dev/1Panel/core/utils/encrypt"
@@ -159,7 +160,7 @@ func (b *BaseApi) DeleteHost(c *gin.Context) {
159160
// @Summary Update host
160161
// @Accept json
161162
// @Param request body dto.HostOperate true "request"
162-
// @Success 200
163+
// @Success 200 {object} dto.HostInfo
163164
// @Security ApiKeyAuth
164165
// @Security Timestamp
165166
// @Router /core/hosts/update [post]
@@ -214,11 +215,12 @@ func (b *BaseApi) UpdateHost(c *gin.Context) {
214215
upMap["pass_phrase"] = req.PassPhrase
215216
}
216217
upMap["description"] = req.Description
217-
if err := hostService.Update(req.ID, upMap); err != nil {
218+
hostItem, err := hostService.Update(req.ID, upMap)
219+
if err != nil {
218220
helper.InternalServer(c, err)
219221
return
220222
}
221-
helper.SuccessWithOutData(c)
223+
helper.SuccessWithData(c, hostItem)
222224
}
223225

224226
// @Tags Host
@@ -238,13 +240,34 @@ func (b *BaseApi) UpdateHostGroup(c *gin.Context) {
238240

239241
upMap := make(map[string]interface{})
240242
upMap["group_id"] = req.GroupID
241-
if err := hostService.Update(req.ID, upMap); err != nil {
243+
if _, err := hostService.Update(req.ID, upMap); err != nil {
242244
helper.InternalServer(c, err)
243245
return
244246
}
245247
helper.SuccessWithOutData(c)
246248
}
247249

250+
// @Tags Host
251+
// @Summary Get host info
252+
// @Accept json
253+
// @Param request body dto.OperateByID true "request"
254+
// @Success 200 {object} dto.HostInfo
255+
// @Security ApiKeyAuth
256+
// @Security Timestamp
257+
// @Router /core/hosts/info [post]
258+
func (b *BaseApi) GetHostByID(c *gin.Context) {
259+
var req dto.OperateByID
260+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
261+
return
262+
}
263+
info, err := hostService.GetHostByID(req.ID)
264+
if err != nil {
265+
helper.InternalServer(c, err)
266+
return
267+
}
268+
helper.SuccessWithData(c, info)
269+
}
270+
248271
func (b *BaseApi) WsSsh(c *gin.Context) {
249272
wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil)
250273
if err != nil {
@@ -265,7 +288,7 @@ func (b *BaseApi) WsSsh(c *gin.Context) {
265288
if wshandleError(wsConn, errors.WithMessage(err, "invalid param rows in request")) {
266289
return
267290
}
268-
host, err := hostService.GetHostInfo(uint(id))
291+
host, err := service.GetHostInfo(uint(id))
269292
if wshandleError(wsConn, errors.WithMessage(err, "load host info by id failed")) {
270293
return
271294
}

core/app/service/host.go

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ type HostService struct{}
2020
type IHostService interface {
2121
TestLocalConn(id uint) bool
2222
TestByInfo(req dto.HostConnTest) bool
23-
GetHostInfo(id uint) (*model.Host, error)
23+
GetHostByID(id uint) (*dto.HostInfo, error)
2424
SearchForTree(search dto.SearchForTree) ([]dto.HostTree, error)
2525
SearchWithPage(search dto.SearchHostWithPage) (int64, interface{}, error)
2626
Create(hostDto dto.HostOperate) (*dto.HostInfo, error)
27-
Update(id uint, upMap map[string]interface{}) error
27+
Update(id uint, upMap map[string]interface{}) (*dto.HostInfo, error)
2828
Delete(id []uint) error
2929

3030
EncryptHost(itemVal string) (string, error)
@@ -124,33 +124,6 @@ func (u *HostService) TestLocalConn(id uint) bool {
124124
return true
125125
}
126126

127-
func (u *HostService) GetHostInfo(id uint) (*model.Host, error) {
128-
host, err := hostRepo.Get(repo.WithByID(id))
129-
if err != nil {
130-
return nil, buserr.New("ErrRecordNotFound")
131-
}
132-
if len(host.Password) != 0 {
133-
host.Password, err = encrypt.StringDecrypt(host.Password)
134-
if err != nil {
135-
return nil, err
136-
}
137-
}
138-
if len(host.PrivateKey) != 0 {
139-
host.PrivateKey, err = encrypt.StringDecrypt(host.PrivateKey)
140-
if err != nil {
141-
return nil, err
142-
}
143-
}
144-
145-
if len(host.PassPhrase) != 0 {
146-
host.PassPhrase, err = encrypt.StringDecrypt(host.PassPhrase)
147-
if err != nil {
148-
return nil, err
149-
}
150-
}
151-
return &host, err
152-
}
153-
154127
func (u *HostService) SearchWithPage(req dto.SearchHostWithPage) (int64, interface{}, error) {
155128
var options []global.DBOption
156129
if len(req.Info) != 0 {
@@ -230,6 +203,48 @@ func (u *HostService) SearchForTree(search dto.SearchForTree) ([]dto.HostTree, e
230203
return datas, err
231204
}
232205

206+
func (u *HostService) GetHostByID(id uint) (*dto.HostInfo, error) {
207+
var item dto.HostInfo
208+
var host model.Host
209+
if id == 0 {
210+
host, _ = hostRepo.Get(repo.WithByName("local"))
211+
} else {
212+
host, _ = hostRepo.Get(repo.WithByID(id))
213+
}
214+
if host.ID == 0 {
215+
return nil, buserr.New("ErrRecordNotFound")
216+
}
217+
if err := copier.Copy(&item, &host); err != nil {
218+
return nil, buserr.WithDetail("ErrStructTransform", err.Error(), nil)
219+
}
220+
if !item.RememberPassword {
221+
item.Password = ""
222+
item.PrivateKey = ""
223+
item.PassPhrase = ""
224+
return &item, nil
225+
}
226+
var err error
227+
if len(host.Password) != 0 {
228+
item.Password, err = encrypt.StringDecrypt(host.Password)
229+
if err != nil {
230+
return nil, err
231+
}
232+
}
233+
if len(host.PrivateKey) != 0 {
234+
item.PrivateKey, err = encrypt.StringDecrypt(host.PrivateKey)
235+
if err != nil {
236+
return nil, err
237+
}
238+
}
239+
if len(host.PassPhrase) != 0 {
240+
item.PassPhrase, err = encrypt.StringDecrypt(host.PassPhrase)
241+
if err != nil {
242+
return nil, err
243+
}
244+
}
245+
return &item, err
246+
}
247+
233248
func (u *HostService) Create(req dto.HostOperate) (*dto.HostInfo, error) {
234249
if req.Name == "local" {
235250
return nil, buserr.New("ErrRecordExist")
@@ -297,8 +312,16 @@ func (u *HostService) Delete(ids []uint) error {
297312
return hostRepo.Delete(repo.WithByIDs(ids))
298313
}
299314

300-
func (u *HostService) Update(id uint, upMap map[string]interface{}) error {
301-
return hostRepo.Update(id, upMap)
315+
func (u *HostService) Update(id uint, upMap map[string]interface{}) (*dto.HostInfo, error) {
316+
if err := hostRepo.Update(id, upMap); err != nil {
317+
return nil, err
318+
}
319+
hostItem, _ := hostRepo.Get(repo.WithByID(id))
320+
var hostinfo dto.HostInfo
321+
if err := copier.Copy(&hostinfo, &hostItem); err != nil {
322+
return nil, buserr.WithDetail("ErrStructTransform", err.Error(), nil)
323+
}
324+
return &hostinfo, nil
302325
}
303326

304327
func (u *HostService) EncryptHost(itemVal string) (string, error) {
@@ -309,3 +332,30 @@ func (u *HostService) EncryptHost(itemVal string) (string, error) {
309332
keyItem, err := encrypt.StringEncrypt(string(privateKey))
310333
return keyItem, err
311334
}
335+
336+
func GetHostInfo(id uint) (*model.Host, error) {
337+
host, err := hostRepo.Get(repo.WithByID(id))
338+
if err != nil {
339+
return nil, buserr.New("ErrRecordNotFound")
340+
}
341+
if len(host.Password) != 0 {
342+
host.Password, err = encrypt.StringDecrypt(host.Password)
343+
if err != nil {
344+
return nil, err
345+
}
346+
}
347+
if len(host.PrivateKey) != 0 {
348+
host.PrivateKey, err = encrypt.StringDecrypt(host.PrivateKey)
349+
if err != nil {
350+
return nil, err
351+
}
352+
}
353+
354+
if len(host.PassPhrase) != 0 {
355+
host.PassPhrase, err = encrypt.StringDecrypt(host.PassPhrase)
356+
if err != nil {
357+
return nil, err
358+
}
359+
}
360+
return &host, err
361+
}

core/constant/common.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,16 @@ var WebUrlMap = map[string]struct{}{
5151
"/ai/model": {},
5252
"/ai/gpu": {},
5353

54-
"/containers": {},
55-
"/containers/container": {},
56-
"containers/container/operate": {},
57-
"/containers/image": {},
58-
"/containers/network": {},
59-
"/containers/volume": {},
60-
"/containers/repo": {},
61-
"/containers/compose": {},
62-
"/containers/template": {},
63-
"/containers/setting": {},
64-
"/containers/dashboard": {},
54+
"/containers": {},
55+
"/containers/container": {},
56+
"/containers/image": {},
57+
"/containers/network": {},
58+
"/containers/volume": {},
59+
"/containers/repo": {},
60+
"/containers/compose": {},
61+
"/containers/template": {},
62+
"/containers/setting": {},
63+
"/containers/dashboard": {},
6564

6665
"/cronjobs": {},
6766

@@ -147,6 +146,7 @@ var WebUrlMap = map[string]struct{}{
147146
}
148147

149148
var DynamicRoutes = []string{
149+
`^/containers/container/operate/[^/]+$`,
150150
`^/containers/composeDetail/[^/]+$`,
151151
`^/databases/mysql/setting/[^/]+/[^/]+$`,
152152
`^/databases/postgresql/setting/[^/]+/[^/]+$`,

core/router/ro_host.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func (s *HostRouter) InitRouter(Router *gin.RouterGroup) {
1616
baseApi := v2.ApiGroupApp.BaseApi
1717
{
1818
hostRouter.POST("", baseApi.CreateHost)
19+
hostRouter.POST("/info", baseApi.GetHostByID)
1920
hostRouter.POST("/del", baseApi.DeleteHost)
2021
hostRouter.POST("/update", baseApi.UpdateHost)
2122
hostRouter.POST("/update/group", baseApi.UpdateHostGroup)

core/utils/ssh/ssh.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ func (c *SSHClient) Run(shell string) (string, error) {
7171
return string(buf), err
7272
}
7373

74+
func (c *SSHClient) SudoHandleCmd() string {
75+
if _, err := c.Run("sudo -n ls"); err == nil {
76+
return "sudo "
77+
}
78+
return ""
79+
}
80+
7481
func (c *SSHClient) Runf(shell string, args ...interface{}) (string, error) {
7582
session, err := c.Client.NewSession()
7683
if err != nil {

frontend/src/api/modules/terminal.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { deepCopy } from '@/utils/util';
77
export const searchHosts = (params: Host.SearchWithPage) => {
88
return http.post<ResPage<Host.Host>>(`/core/hosts/search`, params);
99
};
10+
export const getHostByID = (id: number) => {
11+
return http.post<Host.Host>(`/core/hosts/info`, { id: id });
12+
};
1013
export const getHostTree = (params: Host.ReqSearch) => {
1114
return http.post<Array<Host.HostTree>>(`/core/hosts/tree`, params);
1215
};

0 commit comments

Comments
 (0)