acme.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package acme
  2. import (
  3. "crypto"
  4. "crypto/ecdsa"
  5. "crypto/elliptic"
  6. "crypto/rand"
  7. "crypto/x509"
  8. "encoding/pem"
  9. "io/ioutil"
  10. "log"
  11. "os"
  12. "path/filepath"
  13. "time"
  14. "github.com/go-acme/lego/v4/certcrypto"
  15. "github.com/go-acme/lego/v4/certificate"
  16. "github.com/go-acme/lego/v4/challenge/http01"
  17. "github.com/go-acme/lego/v4/lego"
  18. "github.com/go-acme/lego/v4/registration"
  19. )
  20. // You'll need a user or account type that implements acme.User
  21. type MyUser struct {
  22. Email string
  23. Registration *registration.Resource
  24. key crypto.PrivateKey
  25. }
  26. func (u *MyUser) GetEmail() string {
  27. return u.Email
  28. }
  29. func (u MyUser) GetRegistration() *registration.Resource {
  30. return u.Registration
  31. }
  32. func (u *MyUser) GetPrivateKey() crypto.PrivateKey {
  33. return u.key
  34. }
  35. type ACMEHandler struct {
  36. email string
  37. domains []string
  38. }
  39. func NewACME(email string, domains []string) *ACMEHandler {
  40. return &ACMEHandler{
  41. email: email,
  42. domains: domains,
  43. }
  44. }
  45. func (a *ACMEHandler) ObtainCert() {
  46. log.Println("Obtaining certificate...")
  47. privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. adminUser := MyUser{
  52. Email: a.email,
  53. key: privateKey,
  54. }
  55. config := lego.NewConfig(&adminUser)
  56. config.CADirURL = "https://acme-staging-v02.api.letsencrypt.org/directory"
  57. config.Certificate.KeyType = certcrypto.RSA2048
  58. client, err := lego.NewClient(config)
  59. if err != nil {
  60. log.Println(err)
  61. return
  62. }
  63. err = client.Challenge.SetHTTP01Provider(http01.NewProviderServer("", "5002"))
  64. if err != nil {
  65. log.Println(err)
  66. return
  67. }
  68. // New users will need to register
  69. reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
  70. if err != nil {
  71. log.Println(err)
  72. return
  73. }
  74. adminUser.Registration = reg
  75. request := certificate.ObtainRequest{
  76. Domains: a.domains,
  77. Bundle: true,
  78. }
  79. certificates, err := client.Certificate.Obtain(request)
  80. if err != nil {
  81. log.Println(err)
  82. return
  83. }
  84. // Each certificate comes back with the cert bytes, the bytes of the client's
  85. // private key, and a certificate URL. SAVE THESE TO DISK.
  86. certificateName := ""
  87. if len(a.domains) == 1 {
  88. certificateName = certificates.Domain
  89. } else {
  90. certificateName = "default"
  91. }
  92. err = ioutil.WriteFile("./certs/"+certificateName+".crt", certificates.Certificate, 0777)
  93. if err != nil {
  94. log.Println(err)
  95. return
  96. }
  97. err = ioutil.WriteFile("./certs/"+certificateName+".key", certificates.PrivateKey, 0777)
  98. if err != nil {
  99. log.Println(err)
  100. return
  101. }
  102. // ... all done.
  103. }
  104. // Return a list of domains where the certificates covers
  105. func (a *ACMEHandler) CheckCertificate() {
  106. filenames, err := os.ReadDir("./certs/")
  107. if err != nil {
  108. log.Println(err)
  109. return
  110. }
  111. for _, filename := range filenames {
  112. certFilepath := filepath.Join("./certs/", filename.Name())
  113. certBtyes, err := os.ReadFile(certFilepath)
  114. if err != nil {
  115. //Unable to load this file
  116. continue
  117. } else {
  118. //Cert loaded. Check its expire time
  119. block, _ := pem.Decode(certBtyes)
  120. if block != nil {
  121. cert, err := x509.ParseCertificate(block.Bytes)
  122. if err == nil {
  123. elapsed := time.Since(cert.NotAfter)
  124. approxMonths := -int(elapsed.Hours() / (24 * 30.44))
  125. approxDays := -int(elapsed.Hours()/24) % 30
  126. if elapsed > 0 {
  127. log.Println("Certificate", certFilepath, " expired")
  128. } else {
  129. log.Println("Certificate", certFilepath, " will still vaild for the next ", approxMonths, "m", approxDays, "d")
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }