Skip to content

Commit c260b85

Browse files
authored
feat: 增加主机工具箱管理 (#3001)
1 parent 6c7e9d0 commit c260b85

File tree

28 files changed

+2165
-366
lines changed

28 files changed

+2165
-366
lines changed

backend/app/api/v1/device.go

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package v1
2+
3+
import (
4+
"encoding/base64"
5+
6+
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
7+
"github.com/1Panel-dev/1Panel/backend/app/dto"
8+
"github.com/1Panel-dev/1Panel/backend/constant"
9+
"github.com/gin-gonic/gin"
10+
)
11+
12+
// @Tags Device
13+
// @Summary Load device base info
14+
// @Description 获取设备基础信息
15+
// @Success 200 {object} dto.DeviceBaseInfo
16+
// @Security ApiKeyAuth
17+
// @Router /toolbox/device/base [get]
18+
func (b *BaseApi) LoadDeviceBaseInfo(c *gin.Context) {
19+
data, err := deviceService.LoadBaseInfo()
20+
if err != nil {
21+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
22+
return
23+
}
24+
25+
helper.SuccessWithData(c, data)
26+
}
27+
28+
// @Tags Device
29+
// @Summary list time zone options
30+
// @Description 获取系统可用时区选项
31+
// @Accept json
32+
// @Success 200 {Array} string
33+
// @Security ApiKeyAuth
34+
// @Router /toolbox/device/zone/options [get]
35+
func (b *BaseApi) LoadTimeOption(c *gin.Context) {
36+
list, err := deviceService.LoadTimeZone()
37+
if err != nil {
38+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
39+
return
40+
}
41+
42+
helper.SuccessWithData(c, list)
43+
}
44+
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
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+
89+
// @Tags Device
90+
// @Summary Update device
91+
// @Description 修改系统参数
92+
// @Accept json
93+
// @Param request body dto.SettingUpdate true "request"
94+
// @Success 200
95+
// @Security ApiKeyAuth
96+
// @Router /toolbox/device/update/conf [post]
97+
// @x-panel-log {"bodyKeys":["key","value"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改主机参数 [key] => [value]","formatEN":"update device conf [key] => [value]"}
98+
func (b *BaseApi) UpdateDeviceConf(c *gin.Context) {
99+
var req dto.SettingUpdate
100+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
101+
return
102+
}
103+
104+
if err := deviceService.Update(req.Key, req.Value); err != nil {
105+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
106+
return
107+
}
108+
109+
helper.SuccessWithData(c, nil)
110+
}
111+
112+
// @Tags Device
113+
// @Summary Update device hosts
114+
// @Description 修改系统 hosts
115+
// @Success 200
116+
// @Security ApiKeyAuth
117+
// @Router /toolbox/device/update/host [post]
118+
// @x-panel-log {"bodyKeys":["key","value"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改主机 Host [key] => [value]","formatEN":"update device host [key] => [value]"}
119+
func (b *BaseApi) UpdateDeviceHost(c *gin.Context) {
120+
var req []dto.HostHelper
121+
if err := helper.CheckBind(&req, c); err != nil {
122+
return
123+
}
124+
125+
if err := deviceService.UpdateHosts(req); err != nil {
126+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
127+
return
128+
}
129+
130+
helper.SuccessWithData(c, nil)
131+
}
132+
133+
// @Tags Device
134+
// @Summary Update device passwd
135+
// @Description 修改系统密码
136+
// @Accept json
137+
// @Param request body dto.ChangePasswd true "request"
138+
// @Success 200
139+
// @Security ApiKeyAuth
140+
// @Router /toolbox/device/update/passwd [post]
141+
func (b *BaseApi) UpdateDevicPasswd(c *gin.Context) {
142+
var req dto.ChangePasswd
143+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
144+
return
145+
}
146+
if len(req.Passwd) != 0 {
147+
password, err := base64.StdEncoding.DecodeString(req.Passwd)
148+
if err != nil {
149+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
150+
return
151+
}
152+
req.Passwd = string(password)
153+
}
154+
if err := deviceService.UpdatePasswd(req); err != nil {
155+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
156+
return
157+
}
158+
159+
helper.SuccessWithData(c, nil)
160+
}
161+
162+
// @Tags Device
163+
// @Summary Check device DNS conf
164+
// @Description 检查系统 DNS 配置可用性
165+
// @Accept json
166+
// @Param request body dto.SettingUpdate true "request"
167+
// @Success 200
168+
// @Security ApiKeyAuth
169+
// @Router /toolbox/device/check/dns [post]
170+
func (b *BaseApi) CheckDNS(c *gin.Context) {
171+
var req dto.SettingUpdate
172+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
173+
return
174+
}
175+
176+
data, err := deviceService.CheckDNS(req.Key, req.Value)
177+
if err != nil {
178+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
179+
return
180+
}
181+
182+
helper.SuccessWithData(c, data)
183+
}

backend/app/api/v1/entry.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var (
3333
sshService = service.NewISSHService()
3434
firewallService = service.NewIFirewallService()
3535

36+
deviceService = service.NewIDeviceService()
3637
fail2banService = service.NewIFail2BanService()
3738

3839
settingService = service.NewISettingService()

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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package dto
2+
3+
type DeviceBaseInfo struct {
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"`
11+
}
12+
13+
type HostHelper struct {
14+
IP string `json:"ip"`
15+
Host string `json:"host"`
16+
}
17+
18+
type TimeZoneOptions struct {
19+
From string `json:"from"`
20+
Zones []string `json:"zones"`
21+
}
22+
23+
type ChangePasswd struct {
24+
User string `json:"user"`
25+
Passwd string `json:"passwd"`
26+
}

0 commit comments

Comments
 (0)