Skip to content

Commit b642732

Browse files
rootssongliu
authored andcommitted
feat: 增加主机工具箱
1 parent dc9fa1f commit b642732

File tree

8 files changed

+271
-123
lines changed

8 files changed

+271
-123
lines changed

backend/app/api/v1/device.go

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package v1
22

33
import (
4+
"encoding/base64"
5+
46
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
57
"github.com/1Panel-dev/1Panel/backend/app/dto"
68
"github.com/1Panel-dev/1Panel/backend/constant"
@@ -27,10 +29,10 @@ func (b *BaseApi) LoadDeviceBaseInfo(c *gin.Context) {
2729
// @Summary list time zone options
2830
// @Description 获取系统可用时区选项
2931
// @Accept json
30-
// @Success 200 {Array} dto.TimeZoneOptions
32+
// @Success 200 {Array} string
3133
// @Security ApiKeyAuth
3234
// @Router /toolbox/device/zone/options [get]
33-
func (b *BaseApi) SearchTimeOption(c *gin.Context) {
35+
func (b *BaseApi) LoadTimeOption(c *gin.Context) {
3436
list, err := deviceService.LoadTimeZone()
3537
if err != nil {
3638
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
@@ -40,14 +42,58 @@ func (b *BaseApi) SearchTimeOption(c *gin.Context) {
4042
helper.SuccessWithData(c, list)
4143
}
4244

45+
// @Tags Device
46+
// @Summary load conf
47+
// @Description 获取系统配置文件
48+
// @Accept json
49+
// @Param request body dto.OperationWithName true "request"
50+
// @Success 200
51+
// @Security ApiKeyAuth
52+
// @Router /toolbox/device/conf [post]
53+
func (b *BaseApi) LoadDeviceConf(c *gin.Context) {
54+
var req dto.OperationWithName
55+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
56+
return
57+
}
58+
59+
list, err := deviceService.LoadConf(req.Name)
60+
if err != nil {
61+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
62+
return
63+
}
64+
65+
helper.SuccessWithData(c, list)
66+
}
67+
68+
// @Tags Device
69+
// @Summary Update device conf by file
70+
// @Description 通过文件修改配置
71+
// @Accept json
72+
// @Param request body dto.UpdateByNameAndFile true "request"
73+
// @Success 200 {object}
74+
// @Security ApiKeyAuth
75+
// @Router /toolbox/device/update/byconf [post]
76+
func (b *BaseApi) UpdateDevicByFile(c *gin.Context) {
77+
var req dto.UpdateByNameAndFile
78+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
79+
return
80+
}
81+
if err := deviceService.UpdateByConf(req); err != nil {
82+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
83+
return
84+
}
85+
86+
helper.SuccessWithData(c, nil)
87+
}
88+
4389
// @Tags Device
4490
// @Summary Update device
4591
// @Description 修改系统参数
4692
// @Accept json
4793
// @Param request body dto.SettingUpdate true "request"
48-
// @Success 200 {object} dto.PageResult
94+
// @Success 200
4995
// @Security ApiKeyAuth
50-
// @Router /toolbox/device/conf [post]
96+
// @Router /toolbox/device/update/conf [post]
5197
// @x-panel-log {"bodyKeys":["key","value"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改主机参数 [key] => [value]","formatEN":"update device conf [key] => [value]"}
5298
func (b *BaseApi) UpdateDeviceConf(c *gin.Context) {
5399
var req dto.SettingUpdate
@@ -70,11 +116,11 @@ func (b *BaseApi) UpdateDeviceConf(c *gin.Context) {
70116
// @Param request body {Array} true "request"
71117
// @Success 200 {object} dto.PageResult
72118
// @Security ApiKeyAuth
73-
// @Router /toolbox/device/host [post]
119+
// @Router /toolbox/device/update/host [post]
74120
// @x-panel-log {"bodyKeys":["key","value"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改主机 Host [key] => [value]","formatEN":"update device host [key] => [value]"}
75121
func (b *BaseApi) UpdateDeviceHost(c *gin.Context) {
76122
var req []dto.HostHelper
77-
if err := helper.CheckBindAndValidate(&req, c); err != nil {
123+
if err := helper.CheckBind(&req, c); err != nil {
78124
return
79125
}
80126

@@ -85,3 +131,55 @@ func (b *BaseApi) UpdateDeviceHost(c *gin.Context) {
85131

86132
helper.SuccessWithData(c, nil)
87133
}
134+
135+
// @Tags Device
136+
// @Summary Update device passwd
137+
// @Description 修改系统密码
138+
// @Accept json
139+
// @Param request body dto.ChangePasswd true "request"
140+
// @Success 200
141+
// @Security ApiKeyAuth
142+
// @Router /toolbox/device/update/passwd [post]
143+
func (b *BaseApi) UpdateDevicPasswd(c *gin.Context) {
144+
var req dto.ChangePasswd
145+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
146+
return
147+
}
148+
if len(req.Passwd) != 0 {
149+
password, err := base64.StdEncoding.DecodeString(req.Passwd)
150+
if err != nil {
151+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
152+
return
153+
}
154+
req.Passwd = string(password)
155+
}
156+
if err := deviceService.UpdatePasswd(req); err != nil {
157+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
158+
return
159+
}
160+
161+
helper.SuccessWithData(c, nil)
162+
}
163+
164+
// @Tags Device
165+
// @Summary Check device DNS conf
166+
// @Description 检查系统 DNS 配置可用性
167+
// @Accept json
168+
// @Param request body dto.SettingUpdate true "request"
169+
// @Success 200
170+
// @Security ApiKeyAuth
171+
// @Router /toolbox/device/test/dns [post]
172+
func (b *BaseApi) CheckDNS(c *gin.Context) {
173+
var req dto.SettingUpdate
174+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
175+
return
176+
}
177+
178+
data, err := deviceService.CheckDNS(req.Key, req.Value)
179+
if err != nil {
180+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
181+
return
182+
}
183+
184+
helper.SuccessWithData(c, data)
185+
}

backend/app/api/v1/setting.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -217,43 +217,6 @@ func (b *BaseApi) HandlePasswordExpired(c *gin.Context) {
217217
helper.SuccessWithData(c, nil)
218218
}
219219

220-
// @Tags System Setting
221-
// @Summary Load time zone options
222-
// @Description 加载系统可用时区
223-
// @Success 200
224-
// @Security ApiKeyAuth
225-
// @Router /settings/time/option [get]
226-
func (b *BaseApi) LoadTimeZone(c *gin.Context) {
227-
zones, err := settingService.LoadTimeZone()
228-
if err != nil {
229-
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
230-
return
231-
}
232-
helper.SuccessWithData(c, zones)
233-
}
234-
235-
// @Tags System Setting
236-
// @Summary Sync system time
237-
// @Description 系统时间同步
238-
// @Accept json
239-
// @Param request body dto.SyncTime true "request"
240-
// @Success 200 {string} ntime
241-
// @Security ApiKeyAuth
242-
// @Router /settings/time/sync [post]
243-
// @x-panel-log {"bodyKeys":["ntpSite"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"系统时间同步[ntpSite]","formatEN":"sync system time [ntpSite]"}
244-
func (b *BaseApi) SyncTime(c *gin.Context) {
245-
var req dto.SyncTime
246-
if err := helper.CheckBindAndValidate(&req, c); err != nil {
247-
return
248-
}
249-
250-
if err := settingService.SyncTime(req); err != nil {
251-
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
252-
return
253-
}
254-
helper.SuccessWithData(c, nil)
255-
}
256-
257220
// @Tags System Setting
258221
// @Summary Load local backup dir
259222
// @Description 获取安装根目录

backend/app/dto/common_req.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ type UpdateByFile struct {
4545
File string `json:"file"`
4646
}
4747

48+
type UpdateByNameAndFile struct {
49+
Name string `json:"name"`
50+
File string `json:"file"`
51+
}
52+
4853
type OperationWithNameAndType struct {
4954
Name string `json:"name"`
5055
Type string `json:"type" validate:"required"`

backend/app/dto/device.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package dto
22

33
type DeviceBaseInfo struct {
4-
TimeZone string `json:"timeZone"`
5-
LocalTime string `json:"localTime"`
6-
NameServers []string `json:"nameServer"`
7-
Hosts []HostHelper `json:"hosts"`
4+
DNS []string `json:"dns"`
5+
Hosts []HostHelper `json:"hosts"`
6+
Hostname string `json:"hostname"`
7+
TimeZone string `json:"timeZone"`
8+
LocalTime string `json:"localTime"`
9+
Ntp string `json:"ntp"`
10+
User string `json:"user"`
811
}
912

1013
type HostHelper struct {
@@ -16,3 +19,8 @@ type TimeZoneOptions struct {
1619
From string `json:"from"`
1720
Zones []string `json:"zones"`
1821
}
22+
23+
type ChangePasswd struct {
24+
User string `json:"user"`
25+
Passwd string `json:"passwd"`
26+
}

0 commit comments

Comments
 (0)