2023-05-11 10:09:42 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// @Tags SSH
|
2023-09-20 06:18:20 +00:00
|
|
|
// @Summary Load host SSH setting info
|
2023-05-11 10:09:42 +00:00
|
|
|
// @Description 加载 SSH 配置信息
|
|
|
|
// @Success 200 {object} dto.SSHInfo
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /host/ssh/search [post]
|
|
|
|
func (b *BaseApi) GetSSHInfo(c *gin.Context) {
|
|
|
|
info, err := sshService.GetSSHInfo()
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, info)
|
|
|
|
}
|
|
|
|
|
2023-05-17 10:45:48 +00:00
|
|
|
// @Tags SSH
|
2023-09-20 06:18:20 +00:00
|
|
|
// @Summary Operate SSH
|
2023-05-17 10:45:48 +00:00
|
|
|
// @Description 修改 SSH 服务状态
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.Operate true "request"
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /host/ssh/operate [post]
|
2023-10-07 07:46:44 +00:00
|
|
|
// @x-panel-log {"bodyKeys":["operation"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"[operation] SSH ","formatEN":"[operation] SSH"}
|
2023-05-17 10:45:48 +00:00
|
|
|
func (b *BaseApi) OperateSSH(c *gin.Context) {
|
|
|
|
var req dto.Operate
|
2023-10-27 06:19:26 +00:00
|
|
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
2023-05-17 10:45:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sshService.OperateSSH(req.Operation); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2023-05-11 10:09:42 +00:00
|
|
|
// @Tags SSH
|
2023-09-20 06:18:20 +00:00
|
|
|
// @Summary Update host SSH setting
|
2023-05-11 10:09:42 +00:00
|
|
|
// @Description 更新 SSH 配置
|
|
|
|
// @Accept json
|
2023-11-01 09:12:01 +00:00
|
|
|
// @Param request body dto.SSHUpdate true "request"
|
2023-05-11 10:09:42 +00:00
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /host/ssh/update [post]
|
2023-10-07 07:46:44 +00:00
|
|
|
// @x-panel-log {"bodyKeys":["key","value"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改 SSH 配置 [key] => [value]","formatEN":"update SSH setting [key] => [value]"}
|
2023-05-11 10:09:42 +00:00
|
|
|
func (b *BaseApi) UpdateSSH(c *gin.Context) {
|
2023-11-01 09:12:01 +00:00
|
|
|
var req dto.SSHUpdate
|
2023-10-27 06:19:26 +00:00
|
|
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
2023-05-11 10:09:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-01 09:12:01 +00:00
|
|
|
if err := sshService.Update(req); err != nil {
|
2023-05-11 10:09:42 +00:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
2023-05-15 11:00:40 +00:00
|
|
|
|
2023-05-17 06:45:48 +00:00
|
|
|
// @Tags SSH
|
2023-09-20 06:18:20 +00:00
|
|
|
// @Summary Update host SSH setting by file
|
2023-05-17 06:45:48 +00:00
|
|
|
// @Description 上传文件更新 SSH 配置
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.SSHConf true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /host/conffile/update [post]
|
2023-10-07 07:46:44 +00:00
|
|
|
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改 SSH 配置文件","formatEN":"update SSH conf"}
|
2023-05-17 06:45:48 +00:00
|
|
|
func (b *BaseApi) UpdateSSHByfile(c *gin.Context) {
|
|
|
|
var req dto.SSHConf
|
2023-10-27 06:19:26 +00:00
|
|
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
2023-05-17 06:45:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sshService.UpdateByFile(req.File); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2023-05-15 11:00:40 +00:00
|
|
|
// @Tags SSH
|
2023-09-20 06:18:20 +00:00
|
|
|
// @Summary Generate host SSH secret
|
|
|
|
// @Description 生成 SSH 密钥
|
2023-05-15 11:00:40 +00:00
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.GenerateSSH true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /host/ssh/generate [post]
|
2023-10-07 07:46:44 +00:00
|
|
|
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"生成 SSH 密钥 ","formatEN":"generate SSH secret"}
|
2023-05-15 11:00:40 +00:00
|
|
|
func (b *BaseApi) GenerateSSH(c *gin.Context) {
|
|
|
|
var req dto.GenerateSSH
|
2023-10-27 06:19:26 +00:00
|
|
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
2023-05-15 11:00:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sshService.GenerateSSH(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Tags SSH
|
2023-09-20 06:18:20 +00:00
|
|
|
// @Summary Load host SSH secret
|
|
|
|
// @Description 获取 SSH 密钥
|
2023-05-15 11:00:40 +00:00
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.GenerateLoad true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /host/ssh/secret [post]
|
|
|
|
func (b *BaseApi) LoadSSHSecret(c *gin.Context) {
|
|
|
|
var req dto.GenerateLoad
|
2023-10-27 06:19:26 +00:00
|
|
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
2023-05-15 11:00:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := sshService.LoadSSHSecret(req.EncryptionMode)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, data)
|
|
|
|
}
|
2023-05-16 10:49:27 +00:00
|
|
|
|
|
|
|
// @Tags SSH
|
2023-09-20 06:18:20 +00:00
|
|
|
// @Summary Analysis host SSH logs
|
|
|
|
// @Description 分析 SSH 登录日志
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.SearchForAnalysis true "request"
|
|
|
|
// @Success 200 {array} dto.SSHLogAnalysis
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /host/ssh/log/analysis [post]
|
|
|
|
func (b *BaseApi) AnalysisLog(c *gin.Context) {
|
|
|
|
var req dto.SearchForAnalysis
|
2023-10-27 06:19:26 +00:00
|
|
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
2023-09-20 06:18:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := sshService.AnalysisLog(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Tags SSH
|
|
|
|
// @Summary Load host SSH logs
|
|
|
|
// @Description 获取 SSH 登录日志
|
2023-05-16 10:49:27 +00:00
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.SearchSSHLog true "request"
|
|
|
|
// @Success 200 {object} dto.SSHLog
|
|
|
|
// @Security ApiKeyAuth
|
2023-09-20 06:18:20 +00:00
|
|
|
// @Router /host/ssh/log [post]
|
2023-05-16 10:49:27 +00:00
|
|
|
func (b *BaseApi) LoadSSHLogs(c *gin.Context) {
|
|
|
|
var req dto.SearchSSHLog
|
2023-10-27 06:19:26 +00:00
|
|
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
2023-05-16 10:49:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := sshService.LoadLog(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, data)
|
|
|
|
}
|
2023-08-02 08:47:30 +00:00
|
|
|
|
|
|
|
// @Tags SSH
|
2023-09-20 06:18:20 +00:00
|
|
|
// @Summary Load host SSH conf
|
|
|
|
// @Description 获取 SSH 配置文件
|
2023-08-02 08:47:30 +00:00
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /host/ssh/conf [get]
|
|
|
|
func (b *BaseApi) LoadSSHConf(c *gin.Context) {
|
|
|
|
data, err := sshService.LoadSSHConf()
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, data)
|
|
|
|
}
|