cert.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. config.Certificate.KeyType = certcrypto.RSA2048
  58. logChan <- "Creating client facilitates communication with the CA server"
  59. // A client facilitates communication with the CA server.
  60. client, err := lego.NewClient(config)
  61. if err != nil {
  62. errChan <- errors.Wrap(err, "issue cert new client error")
  63. return
  64. }
  65. logChan <- "Using HTTP01 challenge provider"
  66. err = client.Challenge.SetHTTP01Provider(
  67. http01.NewProviderServer("",
  68. settings.ServerSettings.HTTPChallengePort,
  69. ),
  70. )
  71. if err != nil {
  72. errChan <- errors.Wrap(err, "issue cert challenge fail")
  73. return
  74. }
  75. // New users will need to register
  76. logChan <- "Registering user"
  77. reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
  78. if err != nil {
  79. errChan <- errors.Wrap(err, "issue cert register fail")
  80. return
  81. }
  82. myUser.Registration = reg
  83. request := certificate.ObtainRequest{
  84. Domains: domain,
  85. Bundle: true,
  86. }
  87. logChan <- "Obtaining certificate"
  88. certificates, err := client.Certificate.Obtain(request)
  89. if err != nil {
  90. errChan <- errors.Wrap(err, "issue cert fail to obtain")
  91. return
  92. }
  93. name := strings.Join(domain, " ")
  94. saveDir := nginx.GetNginxConfPath("ssl/" + name)
  95. if _, err = os.Stat(saveDir); os.IsNotExist(err) {
  96. err = os.MkdirAll(saveDir, 0755)
  97. if err != nil {
  98. errChan <- errors.Wrap(err, "issue cert fail to create")
  99. return
  100. }
  101. }
  102. // Each certificate comes back with the cert bytes, the bytes of the client's
  103. // private key, and a certificate URL. SAVE THESE TO DISK.
  104. logChan <- "Writing certificate to disk"
  105. err = os.WriteFile(filepath.Join(saveDir, "fullchain.cer"),
  106. certificates.Certificate, 0644)
  107. if err != nil {
  108. errChan <- errors.Wrap(err, "error issue cert write fullchain.cer")
  109. return
  110. }
  111. logChan <- "Writing certificate private key to disk"
  112. err = os.WriteFile(filepath.Join(saveDir, "private.key"),
  113. certificates.PrivateKey, 0644)
  114. if err != nil {
  115. errChan <- errors.Wrap(err, "error issue cert write key")
  116. return
  117. }
  118. close(errChan)
  119. logChan <- "Reloading nginx"
  120. nginx.ReloadNginx()
  121. logChan <- "Finished"
  122. }