cert.go 3.6 KB

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