sync.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package cert
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/0xJacky/Nginx-UI/internal/helper"
  7. "github.com/0xJacky/Nginx-UI/internal/nginx"
  8. "github.com/0xJacky/Nginx-UI/internal/notification"
  9. "github.com/0xJacky/Nginx-UI/internal/transport"
  10. "github.com/0xJacky/Nginx-UI/model"
  11. "github.com/0xJacky/Nginx-UI/query"
  12. "github.com/go-acme/lego/v4/certcrypto"
  13. "github.com/uozi-tech/cosy/logger"
  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. t, err := transport.NewTransport()
  79. if err != nil {
  80. return
  81. }
  82. client := http.Client{
  83. Transport: t,
  84. }
  85. url, err := env.GetUrl("/api/cert_sync")
  86. if err != nil {
  87. return
  88. }
  89. req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(payloadBytes))
  90. if err != nil {
  91. return
  92. }
  93. req.Header.Set("X-Node-Secret", env.Token)
  94. resp, err := client.Do(req)
  95. if err != nil {
  96. return
  97. }
  98. defer resp.Body.Close()
  99. respBody, err := io.ReadAll(resp.Body)
  100. if err != nil {
  101. return
  102. }
  103. notificationPayload := &SyncNotificationPayload{
  104. StatusCode: resp.StatusCode,
  105. CertName: c.Name,
  106. EnvName: env.Name,
  107. RespBody: string(respBody),
  108. }
  109. notificationPayloadBytes, err := json.Marshal(notificationPayload)
  110. if err != nil {
  111. return
  112. }
  113. if resp.StatusCode != http.StatusOK {
  114. notification.Error("Sync Certificate Error", string(notificationPayloadBytes))
  115. return
  116. }
  117. notification.Success("Sync Certificate Success", string(notificationPayloadBytes))
  118. return
  119. }