Compare commits
3 Commits
75a8fd1bcf
...
ee6ef8dde3
Author | SHA1 | Date | |
---|---|---|---|
ee6ef8dde3 | |||
c26ea0fe41 | |||
a90cc4eb1b |
54
main.go
54
main.go
|
@ -1,55 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "acme-mana/src"
|
||||||
"acme-mana/src"
|
|
||||||
"crypto/x509"
|
|
||||||
"encoding/json"
|
|
||||||
"encoding/pem"
|
|
||||||
"github.com/go-acme/lego/v4/certificate"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//src.Start()
|
src.Start()
|
||||||
testParseCert()
|
//test.TestParseCert()
|
||||||
}
|
//test.TestValidExist()
|
||||||
|
//test.TestParseCertInfo()
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,8 @@ func Start() {
|
||||||
}
|
}
|
||||||
command := args[1]
|
command := args[1]
|
||||||
switch command {
|
switch command {
|
||||||
|
case "block":
|
||||||
|
doTask()
|
||||||
case "start":
|
case "start":
|
||||||
daemonStart()
|
daemonStart()
|
||||||
case "stop":
|
case "stop":
|
||||||
|
@ -36,6 +38,8 @@ func Start() {
|
||||||
dumpConfig()
|
dumpConfig()
|
||||||
case "domains":
|
case "domains":
|
||||||
showDomains()
|
showDomains()
|
||||||
|
case "pubkey":
|
||||||
|
showPubkey()
|
||||||
case "apply":
|
case "apply":
|
||||||
applyOnce()
|
applyOnce()
|
||||||
case "-s":
|
case "-s":
|
||||||
|
@ -190,6 +194,11 @@ func showDomains() {
|
||||||
log.Println(string(config))
|
log.Println(string(config))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func showPubkey() {
|
||||||
|
key := GetAppConfig().Encrypt.PubKey
|
||||||
|
log.Println(key)
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
守护进程接收名称
|
守护进程接收名称
|
||||||
*/
|
*/
|
||||||
|
|
20
src/http.go
20
src/http.go
|
@ -7,6 +7,8 @@ import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"io"
|
"io"
|
||||||
|
@ -23,12 +25,28 @@ func InitHttpServer(host string, port int) {
|
||||||
h := gin.Default()
|
h := gin.Default()
|
||||||
h.GET("/api/v1/refresh", refreshCert)
|
h.GET("/api/v1/refresh", refreshCert)
|
||||||
h.GET("/api/v1/cert", getCert)
|
h.GET("/api/v1/cert", getCert)
|
||||||
|
h.GET("/api/v1/domain/list", domainList)
|
||||||
err := h.Run(host + ":" + strconv.Itoa(port))
|
err := h.Run(host + ":" + strconv.Itoa(port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
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) {
|
func getCert(c *gin.Context) {
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
token := getToken(c)
|
token := getToken(c)
|
||||||
|
@ -137,7 +155,7 @@ func encryptResult(content string, token string) string {
|
||||||
ciphertext := make([]byte, len(plaintext))
|
ciphertext := make([]byte, len(plaintext))
|
||||||
mode.CryptBlocks(ciphertext, plaintext)
|
mode.CryptBlocks(ciphertext, plaintext)
|
||||||
|
|
||||||
return string(ciphertext)
|
return base64.StdEncoding.EncodeToString(ciphertext)
|
||||||
}
|
}
|
||||||
|
|
||||||
func pad(src []byte, blockSize int) []byte {
|
func pad(src []byte, blockSize int) []byte {
|
||||||
|
|
69
src/task.go
69
src/task.go
|
@ -32,38 +32,11 @@ func doRefreshCertOnce(domain Domain) {
|
||||||
name := domain.Name
|
name := domain.Name
|
||||||
dir := GetAppConfig().CertDir
|
dir := GetAppConfig().CertDir
|
||||||
certDir := path.Join(dir, name)
|
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)
|
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)
|
log.Println("Checking if the certificate is expired, Domain: {}", name)
|
||||||
if certInfo.Info.NotAfter.Sub(time.Now()) < 7*24*time.Hour {
|
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)
|
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)
|
f := path.Join(dir, fileName)
|
||||||
_, err := os.Stat(f)
|
_, err := os.Stat(f)
|
||||||
return !os.IsNotExist(err)
|
return !os.IsNotExist(err)
|
||||||
|
|
35
src/util.go
35
src/util.go
|
@ -4,7 +4,7 @@ import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/base64"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenRsa() (priKey string, pubKey string, err error) {
|
func GenRsa() (priKey string, pubKey string, err error) {
|
||||||
|
@ -13,23 +13,28 @@ func GenRsa() (priKey string, pubKey string, err error) {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
publicKey := &privateKey.PublicKey
|
publicKey := &privateKey.PublicKey
|
||||||
|
publicKeyBytes := x509.MarshalPKCS1PublicKey(publicKey)
|
||||||
publicKeyBytes, err := x509.MarshalPKIXPublicKey(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)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
pemBlock := &pem.Block{
|
priKey = base64.StdEncoding.EncodeToString(privateKeyBytes)
|
||||||
Type: "PUBLIC KEY",
|
//pemBlock = &pem.Block{
|
||||||
Bytes: publicKeyBytes,
|
// Type: "",
|
||||||
}
|
// Bytes: privateKeyBytes,
|
||||||
pubKey = string(pem.EncodeToMemory(pemBlock))
|
//}
|
||||||
|
//priKey = string(pem.EncodeToMemory(pemBlock))
|
||||||
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
|
|
||||||
pemBlock = &pem.Block{
|
|
||||||
Type: "RSA PRIVATE KEY",
|
|
||||||
Bytes: privateKeyBytes,
|
|
||||||
}
|
|
||||||
priKey = string(pem.EncodeToMemory(pemBlock))
|
|
||||||
err = nil
|
err = nil
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
31
test/encrypt_test.go
Normal file
31
test/encrypt_test.go
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
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)
|
||||||
|
}
|
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