sync.go 3.4 KB

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