Compare commits

..

No commits in common. "ee6ef8dde3f0877e6db6dc6f5c7c105e56d6440d" and "75a8fd1bcfd62cd6bc117622371dc2f60ecdc728" have entirely different histories.

7 changed files with 97 additions and 196 deletions

56
main.go
View File

@ -1,11 +1,55 @@
package main
import "acme-mana/src"
import (
"acme-mana/src"
"crypto/x509"
"encoding/json"
"encoding/pem"
"github.com/go-acme/lego/v4/certificate"
"log"
"os"
"path"
"path/filepath"
)
func main() {
src.Start()
//test.TestParseCert()
//test.TestValidExist()
//test.TestParseCertInfo()
//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)
}

View File

@ -26,8 +26,6 @@ func Start() {
}
command := args[1]
switch command {
case "block":
doTask()
case "start":
daemonStart()
case "stop":
@ -38,8 +36,6 @@ func Start() {
dumpConfig()
case "domains":
showDomains()
case "pubkey":
showPubkey()
case "apply":
applyOnce()
case "-s":
@ -194,11 +190,6 @@ func showDomains() {
log.Println(string(config))
}
func showPubkey() {
key := GetAppConfig().Encrypt.PubKey
log.Println(key)
}
/*
守护进程接收名称
*/

View File

@ -7,8 +7,6 @@ import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"github.com/gin-gonic/gin"
"io"
@ -25,28 +23,12 @@ func InitHttpServer(host string, port int) {
h := gin.Default()
h.GET("/api/v1/refresh", refreshCert)
h.GET("/api/v1/cert", getCert)
h.GET("/api/v1/domain/list", domainList)
err := h.Run(host + ":" + strconv.Itoa(port))
if err != nil {
return
}
}
func domainList(c *gin.Context) {
token := getToken(c)
domains := GetAppConfig().Domains
data, err := json.Marshal(domains)
if err != nil {
log.Fatal(err)
}
c.JSON(200, gin.H{
"code": 200,
"msg": "Success",
"data": encryptResult(string(data), token),
})
}
func getCert(c *gin.Context) {
name := c.Param("name")
token := getToken(c)
@ -155,7 +137,7 @@ func encryptResult(content string, token string) string {
ciphertext := make([]byte, len(plaintext))
mode.CryptBlocks(ciphertext, plaintext)
return base64.StdEncoding.EncodeToString(ciphertext)
return string(ciphertext)
}
func pad(src []byte, blockSize int) []byte {

View File

@ -32,11 +32,38 @@ func doRefreshCertOnce(domain Domain) {
name := domain.Name
dir := GetAppConfig().CertDir
certDir := path.Join(dir, name)
if !ValidExist(certDir, domain) {
// 判断文件夹和证书文件是否存在
_, 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)
}
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)
@ -44,41 +71,7 @@ func doRefreshCertOnce(domain Domain) {
}
}
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 {
func existFile(dir string, fileName string) bool {
f := path.Join(dir, fileName)
_, err := os.Stat(f)
return !os.IsNotExist(err)

View File

@ -4,7 +4,7 @@ import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
)
func GenRsa() (priKey string, pubKey string, err error) {
@ -13,28 +13,23 @@ func GenRsa() (priKey string, pubKey string, err error) {
return "", "", err
}
publicKey := &privateKey.PublicKey
publicKeyBytes := x509.MarshalPKCS1PublicKey(publicKey)
//publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey)
//if err != nil {
// return "", "", err
//}
pubKey = base64.StdEncoding.EncodeToString(publicKeyBytes)
//pemBlock := &pem.Block{
// Type: "",
// Bytes: publicKeyBytes,
//}
//pubKey = string(pem.EncodeToMemory(pemBlock))
//x509.MarshalPKCS8PrivateKey(privateKey)
privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(privateKey)
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return "", "", err
}
priKey = base64.StdEncoding.EncodeToString(privateKeyBytes)
//pemBlock = &pem.Block{
// Type: "",
// Bytes: privateKeyBytes,
//}
//priKey = string(pem.EncodeToMemory(pemBlock))
pemBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: publicKeyBytes,
}
pubKey = string(pem.EncodeToMemory(pemBlock))
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
pemBlock = &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: privateKeyBytes,
}
priKey = string(pem.EncodeToMemory(pemBlock))
err = nil
return
}

View File

@ -1,31 +0,0 @@
package test
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"fmt"
"log"
"testing"
)
func TestGenRsa(t *testing.T) {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
log.Fatal(err)
}
publicKeyBytes, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey)
if err != nil {
log.Fatal(err)
}
pubKey := base64.StdEncoding.EncodeToString(publicKeyBytes)
//publicKey := &privateKey.PublicKey
//publicKeyBytes := x509.MarshalPKCS1PublicKey(publicKey)
//pubKey := base64.StdEncoding.EncodeToString(publicKeyBytes)
key := x509.MarshalPKCS1PrivateKey(privateKey)
priKey := base64.StdEncoding.EncodeToString(key)
fmt.Println(pubKey)
fmt.Println()
fmt.Println(priKey)
}

View File

@ -1,73 +0,0 @@
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)
}