cert.go 3.8 KB

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