cert.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package cert
  2. import (
  3. "crypto"
  4. "crypto/ecdsa"
  5. "crypto/elliptic"
  6. "crypto/rand"
  7. "github.com/0xJacky/Nginx-UI/server/pkg/nginx"
  8. "github.com/0xJacky/Nginx-UI/server/settings"
  9. "github.com/go-acme/lego/v4/certcrypto"
  10. "github.com/go-acme/lego/v4/certificate"
  11. "github.com/go-acme/lego/v4/challenge/http01"
  12. "github.com/go-acme/lego/v4/lego"
  13. "github.com/go-acme/lego/v4/registration"
  14. "github.com/pkg/errors"
  15. "log"
  16. "os"
  17. "path/filepath"
  18. "strings"
  19. )
  20. // MyUser 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. func IssueCert(domain []string, logChan chan string, errChan chan error) {
  36. defer func() {
  37. if err := recover(); err != nil {
  38. log.Println("Issue Cert recover", err)
  39. }
  40. }()
  41. // Create a user. New accounts need an email and private key to start.
  42. logChan <- "Generating private key for registering account"
  43. privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  44. if err != nil {
  45. errChan <- errors.Wrap(err, "issue cert generate key error")
  46. return
  47. }
  48. logChan <- "Preparing lego configurations"
  49. myUser := MyUser{
  50. Email: settings.ServerSettings.Email,
  51. key: privateKey,
  52. }
  53. config := lego.NewConfig(&myUser)
  54. if settings.ServerSettings.Demo {
  55. config.CADirURL = "https://acme-staging-v02.api.letsencrypt.org/directory"
  56. }
  57. if settings.ServerSettings.CADir != "" {
  58. config.CADirURL = settings.ServerSettings.CADir
  59. }
  60. config.Certificate.KeyType = certcrypto.RSA2048
  61. logChan <- "Creating client facilitates communication with the CA server"
  62. // A client facilitates communication with the CA server.
  63. client, err := lego.NewClient(config)
  64. if err != nil {
  65. errChan <- errors.Wrap(err, "issue cert new client error")
  66. return
  67. }
  68. logChan <- "Using HTTP01 challenge provider"
  69. err = client.Challenge.SetHTTP01Provider(
  70. http01.NewProviderServer("",
  71. settings.ServerSettings.HTTPChallengePort,
  72. ),
  73. )
  74. if err != nil {
  75. errChan <- errors.Wrap(err, "issue cert challenge fail")
  76. return
  77. }
  78. // New users will need to register
  79. logChan <- "Registering user"
  80. reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
  81. if err != nil {
  82. errChan <- errors.Wrap(err, "issue cert register fail")
  83. return
  84. }
  85. myUser.Registration = reg
  86. request := certificate.ObtainRequest{
  87. Domains: domain,
  88. Bundle: true,
  89. }
  90. logChan <- "Obtaining certificate"
  91. certificates, err := client.Certificate.Obtain(request)
  92. if err != nil {
  93. errChan <- errors.Wrap(err, "issue cert fail to obtain")
  94. return
  95. }
  96. name := strings.Join(domain, " ")
  97. saveDir := nginx.GetNginxConfPath("ssl/" + name)
  98. if _, err = os.Stat(saveDir); os.IsNotExist(err) {
  99. err = os.MkdirAll(saveDir, 0755)
  100. if err != nil {
  101. errChan <- errors.Wrap(err, "issue cert fail to create")
  102. return
  103. }
  104. }
  105. // Each certificate comes back with the cert bytes, the bytes of the client's
  106. // private key, and a certificate URL. SAVE THESE TO DISK.
  107. logChan <- "Writing certificate to disk"
  108. err = os.WriteFile(filepath.Join(saveDir, "fullchain.cer"),
  109. certificates.Certificate, 0644)
  110. if err != nil {
  111. errChan <- errors.Wrap(err, "error issue cert write fullchain.cer")
  112. return
  113. }
  114. logChan <- "Writing certificate private key to disk"
  115. err = os.WriteFile(filepath.Join(saveDir, "private.key"),
  116. certificates.PrivateKey, 0644)
  117. if err != nil {
  118. errChan <- errors.Wrap(err, "error issue cert write key")
  119. return
  120. }
  121. close(errChan)
  122. logChan <- "Reloading nginx"
  123. nginx.ReloadNginx()
  124. logChan <- "Finished"
  125. }