测试
This commit is contained in:
parent
75a8fd1bcf
commit
a90cc4eb1b
56
main.go
56
main.go
|
@ -1,55 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"acme-mana/src"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"github.com/go-acme/lego/v4/certificate"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
)
|
||||
import "acme-mana/src"
|
||||
|
||||
func main() {
|
||||
//src.Start()
|
||||
testParseCert()
|
||||
}
|
||||
|
||||
func testParseCert() {
|
||||
// 读取
|
||||
dir := src.GetAppConfig().CertDir
|
||||
dir = filepath.Join(dir, "acme.zzzykj.cn")
|
||||
certFile := path.Join(dir, src.CertFileName)
|
||||
certBytes, err := os.ReadFile(certFile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
block, _ := pem.Decode(certBytes)
|
||||
if block == nil {
|
||||
log.Fatalf("Failed to decode PEM block")
|
||||
return
|
||||
}
|
||||
|
||||
certParse, err := x509.ParseCertificate(block.Bytes)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse certificate: %v", err)
|
||||
}
|
||||
//info, err := json.Marshal(certParse)
|
||||
//if err != nil {
|
||||
// log.Fatalf("Failed to marshal certificate: %v", err)
|
||||
//}
|
||||
certInfo := src.CertInfo{
|
||||
Cert: certificate.Resource{},
|
||||
Info: *certParse,
|
||||
}
|
||||
info, err := json.Marshal(certInfo)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to marshal certificate: %v", err)
|
||||
}
|
||||
log.Println(string(info))
|
||||
|
||||
//log.Println(string(info))
|
||||
//log.Println(certParse)
|
||||
src.Start()
|
||||
//test.TestParseCert()
|
||||
//test.TestValidExist()
|
||||
//test.TestParseCertInfo()
|
||||
|
||||
}
|
||||
|
|
69
src/task.go
69
src/task.go
|
@ -32,38 +32,11 @@ 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)
|
||||
if !ValidExist(certDir, domain) {
|
||||
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)
|
||||
}
|
||||
infoFile := path.Join(certDir, CertInfoFileName)
|
||||
certInfo := ParseCertInfo(infoFile, domain)
|
||||
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)
|
||||
|
@ -71,7 +44,41 @@ func doRefreshCertOnce(domain Domain) {
|
|||
}
|
||||
}
|
||||
|
||||
func existFile(dir string, fileName string) bool {
|
||||
func ValidExist(certDir string, domain Domain) bool {
|
||||
_, err := os.Stat(certDir)
|
||||
if os.IsNotExist(err) {
|
||||
log.Printf("Applying for a certificate, Domain: %s certificate directory does not exist!", domain.Name)
|
||||
return false
|
||||
}
|
||||
if !ExistFile(certDir, CertFileName) {
|
||||
log.Printf("Applying for a certificate, Domain: %s %s does not exist!", domain.Name, CertFileName)
|
||||
return false
|
||||
}
|
||||
if !ExistFile(certDir, KeyFileName) {
|
||||
log.Printf("Applying for a certificate, Domain: %s %s does not exist!", domain.Name, KeyFileName)
|
||||
return false
|
||||
}
|
||||
if !ExistFile(certDir, CertInfoFileName) {
|
||||
log.Printf("Applying for a certificate, Domain: %s %s does not exist!", domain.Name, CertInfoFileName)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func ParseCertInfo(infoFile string, domain Domain) CertInfo {
|
||||
infoBytes, err := os.ReadFile(infoFile)
|
||||
if err != nil {
|
||||
log.Println("Failed to read cert info file, Domain: {}", domain.Name)
|
||||
}
|
||||
var certInfo CertInfo
|
||||
err = json.Unmarshal(infoBytes, &certInfo)
|
||||
if err != nil {
|
||||
log.Println("Failed to parse cert info file, Domain: {}", domain.Name)
|
||||
}
|
||||
return certInfo
|
||||
}
|
||||
|
||||
func ExistFile(dir string, fileName string) bool {
|
||||
f := path.Join(dir, fileName)
|
||||
_, err := os.Stat(f)
|
||||
return !os.IsNotExist(err)
|
||||
|
|
73
test/task.go
Normal file
73
test/task.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"acme-mana/src"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"github.com/go-acme/lego/v4/certificate"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestValidExist() {
|
||||
domain := src.GetAppConfig().Domains[0]
|
||||
name := domain.Name
|
||||
dir := src.GetAppConfig().CertDir
|
||||
certDir := path.Join(dir, name)
|
||||
log.Println(src.ValidExist(certDir, domain))
|
||||
|
||||
}
|
||||
|
||||
func TestParseCertInfo() {
|
||||
domain := src.GetAppConfig().Domains[0]
|
||||
name := domain.Name
|
||||
dir := src.GetAppConfig().CertDir
|
||||
certDir := path.Join(dir, name)
|
||||
infoFile := path.Join(certDir, src.CertInfoFileName)
|
||||
certInfo := src.ParseCertInfo(infoFile, domain)
|
||||
log.Println(certInfo)
|
||||
log.Println(certInfo.Info.NotAfter)
|
||||
log.Println(certInfo.Info.NotAfter.Sub(time.Now()))
|
||||
log.Println(certInfo.Info.NotAfter.Sub(time.Now()) < 7*24*time.Hour)
|
||||
}
|
||||
|
||||
func TestParseCert() {
|
||||
// 读取
|
||||
dir := src.GetAppConfig().CertDir
|
||||
dir = filepath.Join(dir, "acme.zzzykj.cn")
|
||||
certFile := path.Join(dir, src.CertFileName)
|
||||
certBytes, err := os.ReadFile(certFile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
block, _ := pem.Decode(certBytes)
|
||||
if block == nil {
|
||||
log.Fatalf("Failed to decode PEM block")
|
||||
return
|
||||
}
|
||||
|
||||
certParse, err := x509.ParseCertificate(block.Bytes)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse certificate: %v", err)
|
||||
}
|
||||
//info, err := json.Marshal(certParse)
|
||||
//if err != nil {
|
||||
// log.Fatalf("Failed to marshal certificate: %v", err)
|
||||
//}
|
||||
certInfo := src.CertInfo{
|
||||
Cert: certificate.Resource{},
|
||||
Info: *certParse,
|
||||
}
|
||||
info, err := json.Marshal(certInfo)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to marshal certificate: %v", err)
|
||||
}
|
||||
log.Println(string(info))
|
||||
|
||||
//log.Println(string(info))
|
||||
//log.Println(certParse)
|
||||
}
|
Loading…
Reference in New Issue
Block a user