diff --git a/http.go b/http.go index 6decab0..1f08104 100644 --- a/http.go +++ b/http.go @@ -22,7 +22,6 @@ import ( "path/filepath" "strconv" "strings" - "time" ) // Retrieves a server out of the collection by UUID. @@ -96,62 +95,6 @@ func (rt *Router) AuthenticateToken(h httprouter.Handle) httprouter.Handle { } } -func (rt *Router) routeServerBackup(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - s := rt.GetServer(ps.ByName("server")) - defer r.Body.Close() - - data := rt.ReaderToBytes(r.Body) - b, err := s.NewBackup(data) - if err != nil { - zap.S().Errorw("failed to create backup struct for server", zap.String("server", s.Uuid), zap.Error(err)) - - http.Error(w, "failed to update data structure", http.StatusInternalServerError) - return - } - - zap.S().Infow("starting backup process for server", zap.String("server", s.Uuid), zap.String("backup", b.Uuid)) - go func(bk *server.Backup) { - if err := bk.BackupAndNotify(); err != nil { - zap.S().Errorw("failed to generate backup for server", zap.Error(err)) - } else { - zap.S().Infow("completed backup process for server", zap.String("backup", b.Uuid)) - } - }(b) - - w.WriteHeader(http.StatusAccepted) -} - -func (rt *Router) routeRequestServerArchive(w http.ResponseWriter, _ *http.Request, ps httprouter.Params) { - s := rt.GetServer(ps.ByName("server")) - - go func() { - start := time.Now() - - if err := s.Archiver.Archive(); err != nil { - zap.S().Errorw("failed to get archive for server", zap.String("server", s.Uuid), zap.Error(err)) - return - } - - zap.S().Debugw("successfully created archive for server", zap.String("server", s.Uuid), zap.Duration("time", time.Now().Sub(start).Round(time.Microsecond))) - - r := api.NewRequester() - rerr, err := r.SendArchiveStatus(s.Uuid, true) - if rerr != nil || err != nil { - if err != nil { - zap.S().Errorw("failed to notify panel with archive status", zap.String("server", s.Uuid), zap.Error(err)) - return - } - - zap.S().Errorw("panel returned an error when sending the archive status", zap.String("server", s.Uuid), zap.Error(errors.New(rerr.String()))) - return - } - - zap.S().Debugw("successfully notified panel about archive status", zap.String("server", s.Uuid)) - }() - - w.WriteHeader(http.StatusAccepted) -} - func (rt *Router) routeGetServerArchive(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { auth := strings.SplitN(r.Header.Get("Authorization"), " ", 2) @@ -396,9 +339,6 @@ func (rt *Router) ReaderToBytes(r io.Reader) []byte { func (rt *Router) ConfigureRouter() *httprouter.Router { router := httprouter.New() - router.POST("/api/servers/:server/backup", rt.AuthenticateRequest(rt.routeServerBackup)) - - router.POST("/api/servers/:server/archive", rt.AuthenticateRequest(rt.routeRequestServerArchive)) router.GET("/api/servers/:server/archive", rt.AuthenticateServer(rt.routeGetServerArchive)) router.POST("/api/transfer", rt.AuthenticateToken(rt.routeIncomingTransfer)) diff --git a/router/router.go b/router/router.go index a1d0325..0336f2a 100644 --- a/router/router.go +++ b/router/router.go @@ -36,6 +36,10 @@ func Configure() *gin.Engine { server.POST("/commands", postServerCommands) server.POST("/install", postServerInstall) server.POST("/reinstall", postServerReinstall) + server.POST("/backup", postServerBackup) + + server.GET("/archive", getServerArchive) + server.POST("/archive", postServerArchive) files := server.Group("/files") { diff --git a/router/router_server_backup.go b/router/router_server_backup.go new file mode 100644 index 0000000..9e7b6a3 --- /dev/null +++ b/router/router_server_backup.go @@ -0,0 +1,64 @@ +package router + +import ( + "github.com/gin-gonic/gin" + "github.com/pkg/errors" + "github.com/pterodactyl/wings/api" + "github.com/pterodactyl/wings/server" + "go.uber.org/zap" + "net/http" + "time" +) + +// Backs up a server. +func postServerBackup(c *gin.Context) { + s := GetServer(c.Param("server")) + + var data struct{ + Uuid string `json:"uuid"` + IgnoredFiles []string `json:"ignored_files"` + } + c.BindJSON(&data) + + go func(backup *server.Backup) { + if err := backup.BackupAndNotify(); err != nil { + zap.S().Errorw("failed to generate backup for server", zap.Error(err)) + } + }(s.NewBackup(data.Uuid, data.IgnoredFiles)) + + c.Status(http.StatusAccepted) +} + +func getServerArchive(c *gin.Context) { +} + +func postServerArchive(c *gin.Context) { + s := GetServer(c.Param("server")) + + go func() { + start := time.Now() + + if err := s.Archiver.Archive(); err != nil { + zap.S().Errorw("failed to get archive for server", zap.String("server", s.Uuid), zap.Error(err)) + return + } + + zap.S().Debugw("successfully created archive for server", zap.String("server", s.Uuid), zap.Duration("time", time.Now().Sub(start).Round(time.Microsecond))) + + r := api.NewRequester() + rerr, err := r.SendArchiveStatus(s.Uuid, true) + if rerr != nil || err != nil { + if err != nil { + zap.S().Errorw("failed to notify panel with archive status", zap.String("server", s.Uuid), zap.Error(err)) + return + } + + zap.S().Errorw("panel returned an error when sending the archive status", zap.String("server", s.Uuid), zap.Error(errors.New(rerr.String()))) + return + } + + zap.S().Debugw("successfully notified panel about archive status", zap.String("server", s.Uuid)) + }() + + c.Status(http.StatusAccepted) +} \ No newline at end of file diff --git a/router/router_system.go b/router/router_system.go index 24ee31c..882708a 100644 --- a/router/router_system.go +++ b/router/router_system.go @@ -61,4 +61,4 @@ func postCreateServer(c *gin.Context) { }(install) c.Status(http.StatusAccepted) -} +} \ No newline at end of file diff --git a/server/backup.go b/server/backup.go index fe6f7a5..f8bbc83 100644 --- a/server/backup.go +++ b/server/backup.go @@ -3,7 +3,6 @@ package server import ( "crypto/sha256" "encoding/hex" - "encoding/json" "github.com/mholt/archiver/v3" "github.com/pkg/errors" "github.com/pterodactyl/wings/api" @@ -24,23 +23,19 @@ type Backup struct { } // Create a new Backup struct from data passed through in a request. -func (s *Server) NewBackup(data []byte) (*Backup, error) { - backup := &Backup{} - - if err := json.Unmarshal(data, backup); err != nil { - return nil, errors.WithStack(err) +func (s *Server) NewBackup(uuid string, ignore []string) *Backup { + return &Backup{ + Uuid: uuid, + IgnoredFiles: ignore, + server: s, + localDirectory: path.Join(config.Get().System.BackupDirectory, s.Uuid), } - - backup.server = s - backup.localDirectory = path.Join(config.Get().System.BackupDirectory, s.Uuid) - - return backup, nil } // Locates the backup for a server and returns the local path. This will obviously only // work if the backup was created as a local backup. func (s *Server) LocateBackup(uuid string) (string, os.FileInfo, error) { - p := path.Join(config.Get().System.BackupDirectory, s.Uuid, uuid + ".tar.gz") + p := path.Join(config.Get().System.BackupDirectory, s.Uuid, uuid+".tar.gz") st, err := os.Stat(p) if err != nil {