自动续期
This commit is contained in:
parent
784e8b9b2f
commit
75a8fd1bcf
2
main.go
2
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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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{
|
||||
|
|
74
src/task.go
74
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)
|
||||
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue
Block a user