sync.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package cert
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/0xJacky/Nginx-UI/internal/helper"
  8. "github.com/0xJacky/Nginx-UI/internal/logger"
  9. "github.com/0xJacky/Nginx-UI/internal/nginx"
  10. "github.com/0xJacky/Nginx-UI/internal/notification"
  11. "github.com/0xJacky/Nginx-UI/model"
  12. "github.com/0xJacky/Nginx-UI/query"
  13. "github.com/0xJacky/Nginx-UI/settings"
  14. "github.com/go-acme/lego/v4/certcrypto"
  15. "io"
  16. "net/http"
  17. "os"
  18. )
  19. type SyncCertificatePayload struct {
  20. Name string `json:"name"`
  21. SSLCertificatePath string `json:"ssl_certificate_path"`
  22. SSLCertificateKeyPath string `json:"ssl_certificate_key_path"`
  23. SSLCertificate string `json:"ssl_certificate"`
  24. SSLCertificateKey string `json:"ssl_certificate_key"`
  25. KeyType certcrypto.KeyType `json:"key_type"`
  26. }
  27. func SyncToRemoteServer(c *model.Cert) (err error) {
  28. if c.SSLCertificatePath == "" || c.SSLCertificateKeyPath == "" || len(c.SyncNodeIds) == 0 {
  29. return
  30. }
  31. nginxConfPath := nginx.GetConfPath()
  32. if !helper.IsUnderDirectory(c.SSLCertificatePath, nginxConfPath) {
  33. return fmt.Errorf("ssl_certificate_path: %s is not under the nginx conf path: %s",
  34. c.SSLCertificatePath, nginxConfPath)
  35. }
  36. if !helper.IsUnderDirectory(c.SSLCertificateKeyPath, nginxConfPath) {
  37. return fmt.Errorf("ssl_certificate_key_path: %s is not under the nginx conf path: %s",
  38. c.SSLCertificateKeyPath, nginxConfPath)
  39. }
  40. certBytes, err := os.ReadFile(c.SSLCertificatePath)
  41. if err != nil {
  42. return
  43. }
  44. keyBytes, err := os.ReadFile(c.SSLCertificateKeyPath)
  45. if err != nil {
  46. return
  47. }
  48. payload := &SyncCertificatePayload{
  49. Name: c.Name,
  50. SSLCertificatePath: c.SSLCertificatePath,
  51. SSLCertificateKeyPath: c.SSLCertificateKeyPath,
  52. SSLCertificate: string(certBytes),
  53. SSLCertificateKey: string(keyBytes),
  54. KeyType: c.KeyType,
  55. }
  56. payloadBytes, err := json.Marshal(payload)
  57. if err != nil {
  58. return
  59. }
  60. q := query.Environment
  61. envs, _ := q.Where(q.ID.In(c.SyncNodeIds...)).Find()
  62. for _, env := range envs {
  63. go func() {
  64. err := deploy(env, c, payloadBytes)
  65. if err != nil {
  66. logger.Error(err)
  67. }
  68. }()
  69. }
  70. return
  71. }
  72. type SyncNotificationPayload struct {
  73. StatusCode int `json:"status_code"`
  74. CertName string `json:"cert_name"`
  75. EnvName string `json:"env_name"`
  76. RespBody string `json:"resp_body"`
  77. }
  78. func deploy(env *model.Environment, c *model.Cert, payloadBytes []byte) (err error) {
  79. client := http.Client{
  80. Transport: &http.Transport{
  81. TLSClientConfig: &tls.Config{InsecureSkipVerify: settings.ServerSettings.InsecureSkipVerify},
  82. },
  83. }
  84. url, err := env.GetUrl("/api/cert_sync")
  85. if err != nil {
  86. return
  87. }
  88. req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(payloadBytes))
  89. if err != nil {
  90. return
  91. }
  92. req.Header.Set("X-Node-Secret", env.Token)
  93. resp, err := client.Do(req)
  94. if err != nil {
  95. return
  96. }
  97. defer resp.Body.Close()
  98. respBody, err := io.ReadAll(resp.Body)
  99. if err != nil {
  100. return
  101. }
  102. notificationPayload := &SyncNotificationPayload{
  103. StatusCode: resp.StatusCode,
  104. CertName: c.Name,
  105. EnvName: env.Name,
  106. RespBody: string(respBody),
  107. }
  108. notificationPayloadBytes, err := json.Marshal(notificationPayload)
  109. if err != nil {
  110. return
  111. }
  112. if resp.StatusCode != http.StatusOK {
  113. notification.Error("Sync Certificate Error", string(notificationPayloadBytes))
  114. return
  115. }
  116. notification.Success("Sync Certificate Success", string(notificationPayloadBytes))
  117. return
  118. }