Skip to content

Commit

Permalink
refactor: acme.sh exec
Browse files Browse the repository at this point in the history
  • Loading branch information
pupilcc committed Jan 10, 2024
1 parent e813cd5 commit cf6d38a
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 139 deletions.
53 changes: 53 additions & 0 deletions infrastructure/acme/issue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package acme

import (
"autossl/internal/service"
"fmt"
"go.uber.org/zap"
"os"
"os/exec"
"path/filepath"
"strings"
)

func Issue(name string) error {
dns := os.Getenv("ACME_DNS")
alias := os.Getenv("ACME_ALIAS")

cmd := exec.Command(filepath.Join(usr.HomeDir, ".acme.sh/acme.sh"), "--issue", "--dns", dns, "-d", name, "--challenge-alias", alias, "--keylength", "2048")
return execIssue(cmd)
}

func Install(name string, id string) error {
cmd := exec.Command(filepath.Join(usr.HomeDir, ".acme.sh/acme.sh"), "--install-cert", "-d", name, "--key-file", filepath.Join(service.CertPath, id+".key"), "--fullchain-file", filepath.Join(service.CertPath, id+".crt"))
logger.Info("command", zap.String("Running command:", strings.Join(cmd.Args, " ")))
return execIssue(cmd)
}

func Remove(name string) error {
cmd := exec.Command(filepath.Join(usr.HomeDir, ".acme.sh/acme.sh"), "--remove", "--domain", name)
logger.Info("command", zap.String("Running command:", strings.Join(cmd.Args, " ")))
return execIssue(cmd)
}

func execIssue(cmd *exec.Cmd) error {
stdout, err := cmd.StdoutPipe()
if err != nil {
logger.Error("cmd.StdoutPipe() running command failed", zap.String("error:", err.Error()))
}
if err = cmd.Start(); err != nil {
logger.Error("cmd.Start() running command failed", zap.String("error:", err.Error()))
}
for {
tmp := make([]byte, 1024)
_, err := stdout.Read(tmp)
fmt.Print(string(tmp))
if err != nil {
break
}
}
if err := cmd.Wait(); err != nil {
logger.Error("cmd.Wait() running command failed", zap.String("error:", err.Error()))
}
return err
}
45 changes: 45 additions & 0 deletions infrastructure/acme/setting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package acme

import (
"autossl/middleware"
"go.uber.org/zap"
"os"
"os/exec"
"os/user"
"path/filepath"
)

var usr, _ = user.Current()
var logger = middleware.GetLogger()

func InitAcme() {
ca()
email()
upgrade()
}

func ca() {
ca := os.Getenv("ACME_CA")
cmd := exec.Command(filepath.Join(usr.HomeDir, ".acme.sh/acme.sh"), "--set-default-ca", "--server", ca)
execSetting(cmd)
}

func email() {
email := os.Getenv("ACME_EMAIL")
cmd := exec.Command(filepath.Join(usr.HomeDir, ".acme.sh/acme.sh"), "--update-account", "--email", email)
execSetting(cmd)
}

func upgrade() {
cmd := exec.Command(filepath.Join(usr.HomeDir, ".acme.sh/acme.sh"), "--upgrade", "--auto-upgrade")
execSetting(cmd)
}

func execSetting(cmd *exec.Cmd) {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
err := cmd.Start()
if err != nil {
logger.Error("cmd.Start() failed: ", zap.String("error", err.Error()))
}
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"autossl/infrastructure/acme"
"autossl/middleware"
"autossl/web"
"github.com/golang-jwt/jwt/v5"
Expand Down Expand Up @@ -33,7 +34,7 @@ func main() {
e.Use(middleware.RequestLogger())

// Init acme.sh
middleware.InitAcme()
acme.InitAcme()

// Start the service
e.Logger.Fatal(e.Start(":1323"))
Expand Down
134 changes: 0 additions & 134 deletions middleware/acme.go

This file was deleted.

8 changes: 4 additions & 4 deletions web/ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"autossl/common/exception"
"autossl/common/response"
"autossl/common/util"
"autossl/infrastructure/acme"
"autossl/internal/domain"
"autossl/internal/service"
"autossl/middleware"
"fmt"
"github.com/labstack/echo/v4"
"net/http"
Expand Down Expand Up @@ -110,12 +110,12 @@ func generate(c echo.Context) error {
fmt.Println("错误:", err)
}

err = middleware.Issue(certCommand.Domain)
err = acme.Issue(certCommand.Domain)
if err != nil {
return err
}

err = middleware.Install(certCommand.Domain, id)
err = acme.Install(certCommand.Domain, id)
if err != nil {
return err
}
Expand Down Expand Up @@ -147,7 +147,7 @@ func remove(c echo.Context) error {
certMap[cert.Id] = cert
}
if cert, exists := certMap[uuid]; exists {
if err := middleware.Remove(cert.Name); err != nil {
if err := acme.Remove(cert.Name); err != nil {
return c.JSON(http.StatusBadRequest, response.Message(err.Error()))
}
}
Expand Down

0 comments on commit cf6d38a

Please sign in to comment.