diff --git a/main.go b/main.go index 474ca41..7b91a2b 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,7 @@ func testParseCert() { // 读取 dir := src.GetAppConfig().CertDir dir = filepath.Join(dir, "acme.zzzykj.cn") - certFile := path.Join(dir, "cert.crt") + certFile := path.Join(dir, src.CertFileName) certBytes, err := os.ReadFile(certFile) if err != nil { panic(err) diff --git a/src/acme-client.go b/src/acme-client.go index 0c45dfc..3d1db80 100644 --- a/src/acme-client.go +++ b/src/acme-client.go @@ -99,12 +99,12 @@ func saveCertFile(cert *certificate.Resource, name string) { } certBytes := cert.Certificate - err = os.WriteFile(path.Join(dir, "cert.crt"), certBytes, 0755) + err = os.WriteFile(path.Join(dir, CertFileName), certBytes, 0755) if err != nil { log.Fatalf("Failed to save certificate: %v", err) } - err = os.WriteFile(path.Join(dir, "cert.key"), cert.PrivateKey, 0755) + err = os.WriteFile(path.Join(dir, KeyFileName), cert.PrivateKey, 0755) if err != nil { log.Fatalf("Failed to save private key: %v", err) } @@ -128,7 +128,7 @@ func saveCertFile(cert *certificate.Resource, name string) { if err != nil { log.Fatalf("Failed to marshal certificate: %v", err) } - err = os.WriteFile(path.Join(dir, "info.json"), certJson, 0644) + err = os.WriteFile(path.Join(dir, CertInfoFileName), certJson, 0644) if err != nil { log.Fatalf("Failed to save certificate info: %v", err) } diff --git a/src/http.go b/src/http.go index 871a6ff..8eb4ec1 100644 --- a/src/http.go +++ b/src/http.go @@ -43,7 +43,7 @@ func getCert(c *gin.Context) { }) return } - crtFilePath := path.Join(dir, "cert.crt") + crtFilePath := path.Join(dir, CertFileName) crtContent, err := os.ReadFile(crtFilePath) if err != nil { c.JSON(200, gin.H{ @@ -54,7 +54,7 @@ func getCert(c *gin.Context) { } crt := string(crtContent) - keyFilePath := path.Join(dir, "cert.key") + keyFilePath := path.Join(dir, KeyFileName) keyContent, err := os.ReadFile(keyFilePath) if err != nil { c.JSON(200, gin.H{ diff --git a/src/task.go b/src/task.go index 57256b0..15abff1 100644 --- a/src/task.go +++ b/src/task.go @@ -1,7 +1,79 @@ package src -import "log" +import ( + "encoding/json" + "log" + "os" + "path" + "time" +) + +var AutoRefreshCertTicker = time.NewTicker(time.Hour) func AutoRefreshCert() { log.Println("Start auto refresh cert") + defer AutoRefreshCertTicker.Stop() + for { + select { + case <-AutoRefreshCertTicker.C: + doRefreshCert() + } + } +} + +func doRefreshCert() { + domains := GetAppConfig().Domains + for _, domain := range domains { + doRefreshCertOnce(domain) + } +} + +func doRefreshCertOnce(domain Domain) { + name := domain.Name + dir := GetAppConfig().CertDir + certDir := path.Join(dir, name) + // 判断文件夹和证书文件是否存在 + _, err := os.Stat(certDir) + if os.IsNotExist(err) { + log.Println("Applying for a certificate, Domain: {} certificate directory does not exist!", name) + Apply(domain) + return + } + if existFile(certDir, CertFileName) { + log.Println("Applying for a certificate, Domain: {} {} does not exist!", name, CertFileName) + Apply(domain) + return + } + if existFile(certDir, KeyFileName) { + log.Println("Applying for a certificate, Domain: {} {} does not exist!", name, KeyFileName) + Apply(domain) + return + } + if existFile(certDir, CertInfoFileName) { + log.Println("Applying for a certificate, Domain: {} {} does not exist!", name, CertInfoFileName) + Apply(domain) + return + } + infoFile := path.Join(dir, CertInfoFileName) + infoBytes, err := os.ReadFile(infoFile) + if err != nil { + log.Println("Failed to read cert info file, Domain: {}", name) + } + var certInfo CertInfo + err = json.Unmarshal(infoBytes, &certInfo) + if err != nil { + log.Println("Failed to parse cert info file, Domain: {}", name) + } + log.Println("Checking if the certificate is expired, Domain: {}", name) + if certInfo.Info.NotAfter.Sub(time.Now()) < 7*24*time.Hour { + log.Println("Apply for a certificate that is about to expire, domain name:", name) + Apply(domain) + } +} + +func existFile(dir string, fileName string) bool { + f := path.Join(dir, fileName) + _, err := os.Stat(f) + return !os.IsNotExist(err) + } diff --git a/src/variable.go b/src/variable.go index cfa704e..02a1069 100644 --- a/src/variable.go +++ b/src/variable.go @@ -3,6 +3,10 @@ package src const PidFile = "acme-mana.pid" const SocketFile = "acme-mana.sock" +const CertFileName = "fullchain.pem" +const KeyFileName = "privkey.pem" +const CertInfoFileName = "info.json" + var appConfig AppConfig = ReadConfig() func GetAppConfig() AppConfig {