Files

311 lines
8.1 KiB
Go
Raw Permalink Normal View History

package v2
2024-07-23 14:48:37 +08:00
import (
2025-08-01 10:43:20 +08:00
"encoding/base64"
"github.com/1Panel-dev/1Panel/agent/app/api/v2/helper"
2024-07-23 14:48:37 +08:00
"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/gin-gonic/gin"
)
// @Tags SSH
// @Summary Load host SSH setting info
// @Success 200 {object} dto.SSHInfo
// @Security ApiKeyAuth
2025-01-21 18:32:14 +08:00
// @Security Timestamp
2025-06-17 11:56:53 +08:00
// @Router /hosts/ssh/search [post]
2024-07-23 14:48:37 +08:00
func (b *BaseApi) GetSSHInfo(c *gin.Context) {
info, err := sshService.GetSSHInfo()
if err != nil {
2024-10-29 18:42:12 +08:00
helper.InternalServer(c, err)
2024-07-23 14:48:37 +08:00
return
}
helper.SuccessWithData(c, info)
}
// @Tags SSH
// @Summary Operate SSH
// @Accept json
// @Param request body dto.Operate true "request"
2025-01-21 18:32:14 +08:00
// @Success 200
2024-07-23 14:48:37 +08:00
// @Security ApiKeyAuth
2025-01-21 18:32:14 +08:00
// @Security Timestamp
2025-06-17 11:56:53 +08:00
// @Router /hosts/ssh/operate [post]
2024-07-23 14:48:37 +08:00
// @x-panel-log {"bodyKeys":["operation"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"[operation] SSH ","formatEN":"[operation] SSH"}
func (b *BaseApi) OperateSSH(c *gin.Context) {
var req dto.Operate
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
if err := sshService.OperateSSH(req.Operation); err != nil {
2024-10-29 18:42:12 +08:00
helper.InternalServer(c, err)
2024-07-23 14:48:37 +08:00
return
}
helper.Success(c)
2024-07-23 14:48:37 +08:00
}
// @Tags SSH
// @Summary Update host SSH setting
// @Accept json
// @Param request body dto.SSHUpdate true "request"
// @Success 200
// @Security ApiKeyAuth
2025-01-21 18:32:14 +08:00
// @Security Timestamp
2025-06-17 11:56:53 +08:00
// @Router /hosts/ssh/update [post]
// @x-panel-log {"bodyKeys":["key","newValue"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改 SSH 配置 [key] => [newValue]","formatEN":"update SSH setting [key] => [newValue]"}
2024-07-23 14:48:37 +08:00
func (b *BaseApi) UpdateSSH(c *gin.Context) {
var req dto.SSHUpdate
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
if err := sshService.Update(req); err != nil {
2024-10-29 18:42:12 +08:00
helper.InternalServer(c, err)
2024-07-23 14:48:37 +08:00
return
}
helper.Success(c)
2024-07-23 14:48:37 +08:00
}
// @Tags SSH
// @Summary Generate host SSH secret
// @Accept json
// @Param request body dto.RootCertOperate true "request"
2024-07-23 14:48:37 +08:00
// @Success 200
// @Security ApiKeyAuth
2025-01-21 18:32:14 +08:00
// @Security Timestamp
2025-08-01 10:43:20 +08:00
// @Router /hosts/ssh/cert [post]
2024-07-23 14:48:37 +08:00
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"生成 SSH 密钥 ","formatEN":"generate SSH secret"}
2025-08-01 10:43:20 +08:00
func (b *BaseApi) CreateRootCert(c *gin.Context) {
var req dto.RootCertOperate
2024-07-23 14:48:37 +08:00
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
if err := loadCertAfterDecrypt(&req); err != nil {
helper.BadRequest(c, err)
return
2025-08-01 10:43:20 +08:00
}
if err := sshService.CreateRootCert(req); err != nil {
helper.InternalServer(c, err)
return
}
helper.Success(c)
}
// @Tags SSH
// @Summary Update host SSH secret
// @Accept json
// @Param request body dto.RootCertOperate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /hosts/ssh/cert/update [post]
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"生成 SSH 密钥 ","formatEN":"generate SSH secret"}
func (b *BaseApi) EditRootCert(c *gin.Context) {
var req dto.RootCertOperate
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
if err := loadCertAfterDecrypt(&req); err != nil {
helper.BadRequest(c, err)
return
}
if err := sshService.EditRootCert(req); err != nil {
helper.InternalServer(c, err)
return
}
helper.Success(c)
}
2025-08-01 10:43:20 +08:00
// @Tags SSH
// @Summary Sycn host SSH secret
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /hosts/ssh/cert/sync [post]
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"同步 SSH 密钥 ","formatEN":"sync SSH secret"}
func (b *BaseApi) SyncRootCert(c *gin.Context) {
if err := sshService.SyncRootCert(); err != nil {
2024-10-29 18:42:12 +08:00
helper.InternalServer(c, err)
2024-07-23 14:48:37 +08:00
return
}
helper.Success(c)
2024-07-23 14:48:37 +08:00
}
// @Tags SSH
// @Summary Load host SSH secret
// @Accept json
2025-08-01 10:43:20 +08:00
// @Param request body dto.SearchWithPage true "request"
// @Success 200 {object} dto.PageResult
2024-07-23 14:48:37 +08:00
// @Security ApiKeyAuth
2025-01-21 18:32:14 +08:00
// @Security Timestamp
2025-08-01 10:43:20 +08:00
// @Router /hosts/ssh/cert/search [post]
func (b *BaseApi) SearchRootCert(c *gin.Context) {
var req dto.SearchWithPage
2024-07-23 14:48:37 +08:00
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
2025-08-01 10:43:20 +08:00
total, data, err := sshService.SearchRootCerts(req)
2024-07-23 14:48:37 +08:00
if err != nil {
2024-10-29 18:42:12 +08:00
helper.InternalServer(c, err)
2024-07-23 14:48:37 +08:00
return
}
2025-08-01 10:43:20 +08:00
helper.SuccessWithData(c, dto.PageResult{
Total: total,
Items: data,
})
}
// @Tags SSH
// @Summary Delete host SSH secret
// @Accept json
// @Param request body dto.ForceDelete true "request"
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /hosts/ssh/cert/delete [post]
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"删除 SSH 密钥 ","formatEN":"delete SSH secret"}
func (b *BaseApi) DeleteRootCert(c *gin.Context) {
var req dto.ForceDelete
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
if err := sshService.DeleteRootCerts(req); err != nil {
helper.InternalServer(c, err)
return
}
helper.Success(c)
2024-07-23 14:48:37 +08:00
}
// @Tags SSH
// @Summary Load host SSH logs
// @Accept json
// @Param request body dto.SearchSSHLog true "request"
2025-08-07 21:40:39 +08:00
// @Success 200 {object} dto.PageResult
2024-07-23 14:48:37 +08:00
// @Security ApiKeyAuth
2025-01-21 18:32:14 +08:00
// @Security Timestamp
2025-06-17 11:56:53 +08:00
// @Router /hosts/ssh/log [post]
2024-07-23 14:48:37 +08:00
func (b *BaseApi) LoadSSHLogs(c *gin.Context) {
var req dto.SearchSSHLog
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
2025-08-07 21:40:39 +08:00
total, data, err := sshService.LoadLog(c, req)
2024-07-23 14:48:37 +08:00
if err != nil {
2024-10-29 18:42:12 +08:00
helper.InternalServer(c, err)
2024-07-23 14:48:37 +08:00
return
}
2025-08-07 21:40:39 +08:00
helper.SuccessWithData(c, dto.PageResult{
Total: total,
Items: data,
})
}
// @Tags SSH
// @Summary Export host SSH logs
// @Accept json
// @Param request body dto.SearchSSHLog true "request"
// @Success 200 {string} path
2025-08-07 21:40:39 +08:00
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /hosts/ssh/log/export [post]
func (b *BaseApi) ExportSSHLogs(c *gin.Context) {
var req dto.SearchSSHLog
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
tmpFile, err := sshService.ExportLog(c, req)
if err != nil {
helper.InternalServer(c, err)
return
}
helper.SuccessWithData(c, tmpFile)
2024-07-23 14:48:37 +08:00
}
2026-07-23 14:53:26 +08:00
// @Tags SSH
// @Summary Clean host SSH logs
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /hosts/ssh/log/clean [post]
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"清空 SSH 登录日志","formatEN":"clean SSH login logs"}
func (b *BaseApi) CleanSSHLogs(c *gin.Context) {
if err := sshService.CleanLog(); err != nil {
helper.InternalServer(c, err)
return
}
helper.Success(c)
}
2024-07-23 14:48:37 +08:00
// @Tags SSH
// @Summary Load host SSH conf
2025-09-25 15:26:10 +08:00
// @Accept json
// @Param request body dto.OperationWithName true "request"
2025-01-21 18:32:14 +08:00
// @Success 200 {string} conf
2024-07-23 14:48:37 +08:00
// @Security ApiKeyAuth
2025-01-21 18:32:14 +08:00
// @Security Timestamp
2025-09-25 15:26:10 +08:00
// @Router /hosts/ssh/file [post]
func (b *BaseApi) LoadSSHFile(c *gin.Context) {
var req dto.OperationWithName
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
data, err := sshService.LoadSSHFile(req.Name)
2024-07-23 14:48:37 +08:00
if err != nil {
2024-10-29 18:42:12 +08:00
helper.InternalServer(c, err)
2024-07-23 14:48:37 +08:00
return
}
helper.SuccessWithData(c, data)
}
2025-09-25 15:26:10 +08:00
// @Tags SSH
// @Summary Update host SSH setting by file
// @Accept json
// @Param request body dto.SSHConfUpdate true "request"
2025-09-25 15:26:10 +08:00
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /hosts/ssh/file/update [post]
// @x-panel-log {"bodyKeys":["key"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改 SSH 配置文件 [key]","formatEN":"update SSH conf [key]"}
func (b *BaseApi) UpdateSSHByFile(c *gin.Context) {
var req dto.SSHConfUpdate
2025-09-25 15:26:10 +08:00
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
if err := sshService.UpdateByFile(req); err != nil {
helper.InternalServer(c, err)
return
}
helper.Success(c)
}
func loadCertAfterDecrypt(req *dto.RootCertOperate) error {
if len(req.PassPhrase) != 0 {
passPhrase, err := base64.StdEncoding.DecodeString(req.PassPhrase)
if err != nil {
return err
}
req.PassPhrase = string(passPhrase)
}
if len(req.PrivateKey) != 0 {
privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey)
if err != nil {
return err
}
req.PrivateKey = string(privateKey)
}
if len(req.PublicKey) != 0 {
publicKey, err := base64.StdEncoding.DecodeString(req.PublicKey)
if err != nil {
return err
}
req.PublicKey = string(publicKey)
}
return nil
}