| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 | package configimport (	"bytes"	"crypto/tls"	"encoding/json"	"io"	"net/http"	"os"	"path/filepath"	"strings"	"github.com/0xJacky/Nginx-UI/internal/helper"	"github.com/0xJacky/Nginx-UI/internal/nginx"	"github.com/0xJacky/Nginx-UI/internal/notification"	"github.com/0xJacky/Nginx-UI/internal/transport"	"github.com/0xJacky/Nginx-UI/model"	"github.com/0xJacky/Nginx-UI/query"	"github.com/0xJacky/Nginx-UI/settings"	"github.com/gin-gonic/gin"	"github.com/uozi-tech/cosy/logger")type SyncConfigPayload struct {	Name      string `json:"name" binding:"required"`	BaseDir   string `json:"base_dir"`	Content   string `json:"content"`	Overwrite bool   `json:"overwrite"`}func SyncToRemoteServer(c *model.Config) (err error) {	if c.Filepath == "" || len(c.SyncNodeIds) == 0 {		return	}	nginxConfPath := nginx.GetConfPath()	if !helper.IsUnderDirectory(c.Filepath, nginxConfPath) {		return e.NewWithParams(50006, ErrPathIsNotUnderTheNginxConfDir.Error(), c.Filepath, nginxConfPath)	}	configBytes, err := os.ReadFile(c.Filepath)	if err != nil {		return	}	payload := &SyncConfigPayload{		Name:      c.Name,		BaseDir:   strings.ReplaceAll(filepath.Dir(c.Filepath), nginx.GetConfPath(), ""),		Content:   string(configBytes),		Overwrite: c.SyncOverwrite,	}	payloadBytes, err := json.Marshal(payload)	if err != nil {		return	}	q := query.Environment	envs, _ := q.Where(q.ID.In(c.SyncNodeIds...), q.Enabled.Is(true)).Find()	for _, env := range envs {		go func() {			err := payload.deploy(env, c, payloadBytes)			if err != nil {				logger.Error(err)			}		}()	}	return}func SyncRenameOnRemoteServer(origPath, newPath string, syncNodeIds []uint64) (err error) {	if origPath == "" || newPath == "" || len(syncNodeIds) == 0 {		return	}	nginxConfPath := nginx.GetConfPath()	if !helper.IsUnderDirectory(origPath, nginxConfPath) {		return e.NewWithParams(50006, ErrPathIsNotUnderTheNginxConfDir.Error(), origPath, nginxConfPath)	}	if !helper.IsUnderDirectory(newPath, nginxConfPath) {		return e.NewWithParams(50006, ErrPathIsNotUnderTheNginxConfDir.Error(), newPath, nginxConfPath)	}	payload := &RenameConfigPayload{		Filepath:    origPath,		NewFilepath: newPath,	}	q := query.Environment	envs, _ := q.Where(q.ID.In(syncNodeIds...)).Find()	for _, env := range envs {		go func() {			err := payload.rename(env)			if err != nil {				logger.Error(err)			}		}()	}	return}type SyncNotificationPayload struct {	StatusCode int    `json:"status_code"`	ConfigName string `json:"config_name"`	EnvName    string `json:"env_name"`	Response   string `json:"response"`}func (p *SyncConfigPayload) deploy(env *model.Environment, c *model.Config, payloadBytes []byte) (err error) {	t, err := transport.NewTransport()	if err != nil {		return	}	client := http.Client{		Transport: t,	}	url, err := env.GetUrl("/api/configs")	if err != nil {		return	}	req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(payloadBytes))	if err != nil {		return	}	req.Header.Set("X-Node-Secret", env.Token)	resp, err := client.Do(req)	if err != nil {		return	}	defer resp.Body.Close()	respBody, err := io.ReadAll(resp.Body)	if err != nil {		return	}	notificationPayload := &SyncNotificationPayload{		StatusCode: resp.StatusCode,		ConfigName: c.Name,		EnvName:    env.Name,		Response:   string(respBody),	}	if resp.StatusCode != http.StatusOK {		notification.Error("Sync Config Error", "Sync config %{config_name} to %{env_name} failed", notificationPayload)		return	}	notification.Success("Sync Config Success", "Sync config %{config_name} to %{env_name} successfully", notificationPayload)	return}type RenameConfigPayload struct {	Filepath    string `json:"filepath"`	NewFilepath string `json:"new_filepath"`}type SyncRenameNotificationPayload struct {	StatusCode int    `json:"status_code"`	OrigPath   string `json:"orig_path"`	NewPath    string `json:"new_path"`	EnvName    string `json:"env_name"`	Response   string `json:"response"`}func (p *RenameConfigPayload) rename(env *model.Environment) (err error) {	// handle rename	if p.NewFilepath == "" || p.Filepath == p.NewFilepath {		return	}	client := http.Client{		Transport: &http.Transport{			TLSClientConfig: &tls.Config{InsecureSkipVerify: settings.HTTPSettings.InsecureSkipVerify},		},	}	payloadBytes, err := json.Marshal(gin.H{		"base_path": strings.ReplaceAll(filepath.Dir(p.Filepath), nginx.GetConfPath(), ""),		"orig_name": filepath.Base(p.Filepath),		"new_name":  filepath.Base(p.NewFilepath),	})	if err != nil {		return	}	url, err := env.GetUrl("/api/config_rename")	if err != nil {		return	}	req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(payloadBytes))	if err != nil {		return	}	req.Header.Set("X-Node-Secret", env.Token)	resp, err := client.Do(req)	if err != nil {		return	}	defer resp.Body.Close()	respBody, err := io.ReadAll(resp.Body)	if err != nil {		return	}	notificationPayload := &SyncRenameNotificationPayload{		StatusCode: resp.StatusCode,		OrigPath:   p.Filepath,		NewPath:    p.NewFilepath,		EnvName:    env.Name,		Response:   string(respBody),	}	if resp.StatusCode != http.StatusOK {		notification.Error("Rename Remote Config Error", "Rename %{orig_path} to %{new_path} on %{env_name} failed", notificationPayload)		return	}	notification.Success("Rename Remote Config Success", "Rename %{orig_path} to %{new_path} on %{env_name} successfully", notificationPayload)	return}
 |