sync.go 3.4 KB

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