Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 证书增加下载功能 #3090

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions backend/app/api/v1/website_ssl.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package v1

import (
"net/http"
"net/url"
"reflect"
"strconv"

"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/backend/app/dto"
Expand Down Expand Up @@ -213,3 +216,32 @@ func (b *BaseApi) UploadWebsiteSSL(c *gin.Context) {
}
helper.SuccessWithData(c, nil)
}

// @Tags Website SSL
// @Summary Download SSL file
// @Description 下载证书文件
// @Accept json
// @Param request body request.WebsiteResourceReq true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /websites/ssl/download [post]
// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"website_ssls","output_column":"primary_domain","output_value":"domain"}],"formatZH":"下载证书文件 [domain]","formatEN":"download ssl file [domain]"}
func (b *BaseApi) DownloadWebsiteSSL(c *gin.Context) {
var req request.WebsiteResourceReq
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
file, err := websiteSSLService.DownloadFile(req.ID)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
info, err := file.Stat()
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
c.Header("Content-Length", strconv.FormatInt(info.Size(), 10))
c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(info.Name()))
http.ServeContent(c.Writer, c.Request, info.Name(), info.ModTime(), file)
}
30 changes: 30 additions & 0 deletions backend/app/service/website_ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/repo"
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/i18n"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/files"
Expand Down Expand Up @@ -41,6 +42,7 @@ type IWebsiteSSLService interface {
Upload(req request.WebsiteSSLUpload) error
ObtainSSL(apply request.WebsiteSSLApply) error
SyncForRestart() error
DownloadFile(id uint) (*os.File, error)
}

func NewIWebsiteSSLService() IWebsiteSSLService {
Expand Down Expand Up @@ -425,6 +427,34 @@ func (w WebsiteSSLService) Upload(req request.WebsiteSSLUpload) error {
return websiteSSLRepo.Create(context.Background(), websiteSSL)
}

func (w WebsiteSSLService) DownloadFile(id uint) (*os.File, error) {
websiteSSL, err := websiteSSLRepo.GetFirst(commonRepo.WithByID(id))
if err != nil {
return nil, err
}
fileOp := files.NewFileOp()
dir := path.Join(global.CONF.System.BaseDir, "1panel/tmp/ssl", websiteSSL.PrimaryDomain)
if fileOp.Stat(dir) {
if err = fileOp.DeleteDir(dir); err != nil {
return nil, err
}
}
if err = fileOp.CreateDir(dir, 0666); err != nil {
return nil, err
}
if err = fileOp.WriteFile(path.Join(dir, "fullchain.pem"), strings.NewReader(websiteSSL.Pem), 0644); err != nil {
return nil, err
}
if err = fileOp.WriteFile(path.Join(dir, "privkey.pem"), strings.NewReader(websiteSSL.PrivateKey), 0644); err != nil {
return nil, err
}
fileName := websiteSSL.PrimaryDomain + ".zip"
if err = fileOp.Compress([]string{path.Join(dir, "fullchain.pem"), path.Join(dir, "privkey.pem")}, dir, fileName, files.SdkZip); err != nil {
return nil, err
}
return os.Open(path.Join(dir, fileName))
}

func (w WebsiteSSLService) SyncForRestart() error {
sslList, err := websiteSSLRepo.List()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions backend/router/ro_website_ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ func (a *WebsiteSSLRouter) InitWebsiteSSLRouter(Router *gin.RouterGroup) {
groupRouter.POST("/update", baseApi.UpdateWebsiteSSL)
groupRouter.POST("/upload", baseApi.UploadWebsiteSSL)
groupRouter.POST("/obtain", baseApi.ApplyWebsiteSSL)
groupRouter.POST("/download", baseApi.DownloadWebsiteSSL)
}
}
78 changes: 77 additions & 1 deletion cmd/server/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12879,6 +12879,57 @@ const docTemplate = `{
}
}
},
"/websites/ssl/download": {
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "下载证书文件",
"consumes": [
"application/json"
],
"tags": [
"Website SSL"
],
"summary": "Download SSL file",
"parameters": [
{
"description": "request",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/request.WebsiteResourceReq"
}
}
],
"responses": {
"200": {
"description": "OK"
}
},
"x-panel-log": {
"BeforeFunctions": [
{
"db": "website_ssls",
"input_column": "id",
"input_value": "id",
"isList": false,
"output_column": "primary_domain",
"output_value": "domain"
}
],
"bodyKeys": [
"id"
],
"formatEN": "download ssl file [domain]",
"formatZH": "下载证书文件 [domain]",
"paramKeys": []
}
}
},
"/websites/ssl/obtain": {
"post": {
"security": [
Expand Down Expand Up @@ -17478,6 +17529,9 @@ const docTemplate = `{
"createdAt": {
"type": "string"
},
"description": {
"type": "string"
},
"dir": {
"type": "string"
},
Expand Down Expand Up @@ -19522,14 +19576,33 @@ const docTemplate = `{
"request.WebsiteSSLUpdate": {
"type": "object",
"required": [
"id"
"id",
"type"
],
"properties": {
"autoRenew": {
"type": "boolean"
},
"certificate": {
"type": "string"
},
"description": {
"type": "string"
},
"id": {
"type": "integer"
},
"privateKey": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"autoRenew",
"description",
"certificate",
"privateKey"
]
}
}
},
Expand All @@ -19551,6 +19624,9 @@ const docTemplate = `{
"privateKeyPath": {
"type": "string"
},
"sslID": {
"type": "integer"
},
"type": {
"type": "string",
"enum": [
Expand Down
78 changes: 77 additions & 1 deletion cmd/server/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -12872,6 +12872,57 @@
}
}
},
"/websites/ssl/download": {
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "下载证书文件",
"consumes": [
"application/json"
],
"tags": [
"Website SSL"
],
"summary": "Download SSL file",
"parameters": [
{
"description": "request",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/request.WebsiteResourceReq"
}
}
],
"responses": {
"200": {
"description": "OK"
}
},
"x-panel-log": {
"BeforeFunctions": [
{
"db": "website_ssls",
"input_column": "id",
"input_value": "id",
"isList": false,
"output_column": "primary_domain",
"output_value": "domain"
}
],
"bodyKeys": [
"id"
],
"formatEN": "download ssl file [domain]",
"formatZH": "下载证书文件 [domain]",
"paramKeys": []
}
}
},
"/websites/ssl/obtain": {
"post": {
"security": [
Expand Down Expand Up @@ -17471,6 +17522,9 @@
"createdAt": {
"type": "string"
},
"description": {
"type": "string"
},
"dir": {
"type": "string"
},
Expand Down Expand Up @@ -19515,14 +19569,33 @@
"request.WebsiteSSLUpdate": {
"type": "object",
"required": [
"id"
"id",
"type"
],
"properties": {
"autoRenew": {
"type": "boolean"
},
"certificate": {
"type": "string"
},
"description": {
"type": "string"
},
"id": {
"type": "integer"
},
"privateKey": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"autoRenew",
"description",
"certificate",
"privateKey"
]
}
}
},
Expand All @@ -19544,6 +19617,9 @@
"privateKeyPath": {
"type": "string"
},
"sslID": {
"type": "integer"
},
"type": {
"type": "string",
"enum": [
Expand Down
Loading
Loading