package router import ( "context" stderrors "errors" "mime" "net" "net/http" "net/netip" "net/url" "os" "strings" "time" "emperror.dev/errors" "github.com/apex/log" "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/pterodactyl/wings/config" wnet "github.com/pterodactyl/wings/internal/network" "github.com/pterodactyl/wings/router/middleware" "github.com/pterodactyl/wings/server" "github.com/pterodactyl/wings/server/backup" "github.com/pterodactyl/wings/server/filesystem" ) type backupDownloadError string func (e backupDownloadError) Error() string { return string(e) } // postServerBackup performs a backup against a given server instance using the // provided backup adapter. func postServerBackup(c *gin.Context) { s := middleware.ExtractServer(c) client := middleware.ExtractApiClient(c) logger := middleware.ExtractLogger(c) var data struct { Adapter backup.AdapterType `json:"adapter"` Uuid string `json:"uuid"` Ignore string `json:"ignore"` } if err := c.BindJSON(&data); err != nil { return } backupUuid, ok := parseBackupUuid(c, data.Uuid) if !ok { return } if err := filesystem.ValidateIgnore(data.Ignore); err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } var adapter backup.BackupInterface switch data.Adapter { case backup.LocalBackupAdapter: adapter = backup.NewLocal(client, backupUuid, data.Ignore) case backup.S3BackupAdapter: adapter = backup.NewS3(client, backupUuid, data.Ignore) default: middleware.CaptureAndAbort(c, errors.New("router/backups: provided adapter is not valid: "+string(data.Adapter))) return } // Attach the server ID and the request ID to the adapter log context for easier // parsing in the logs. adapter.WithLogContext(map[string]interface{}{ "server": s.ID(), "request_id": c.GetString("request_id"), }) go func(b backup.BackupInterface, s *server.Server, logger *log.Entry) { if err := s.Backup(b); err != nil { logger.WithField("error", errors.WithStackIf(err)).Error("router: failed to generate server backup") } }(adapter, s, logger) c.Status(http.StatusAccepted) } // postServerRestoreBackup handles restoring a backup for a server by downloading // or finding the given backup on the system and then unpacking the archive into // the server's data directory. If the TruncateDirectory field is provided and // is true all of the files will be deleted for the server. // // This endpoint will block until the backup is fully restored allowing for a // spinner to be displayed in the Panel UI effectively. func postServerRestoreBackup(c *gin.Context) { s := middleware.ExtractServer(c) client := middleware.ExtractApiClient(c) logger := middleware.ExtractLogger(c) var data struct { Adapter backup.AdapterType `binding:"required,oneof=wings s3" json:"adapter"` TruncateDirectory bool `json:"truncate_directory"` // A UUID is always required for this endpoint, however the download URL // is only present when the given adapter type is s3. DownloadUrl string `json:"download_url"` } if err := c.BindJSON(&data); err != nil { return } backupUuid, ok := parseBackupUuid(c, c.Param("backup")) if !ok { return } if data.Adapter == backup.S3BackupAdapter && data.DownloadUrl == "" { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "The download_url field is required when the backup adapter is set to S3."}) return } if data.Adapter == backup.S3BackupAdapter { if err := validateBackupDownloadUrl(data.DownloadUrl); err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } } s.SetRestoring(true) hasError := true defer func() { if !hasError { return } s.SetRestoring(false) }() logger.Info("processing server backup restore request") // Find the backup before changing anything, so that a restore that cannot // happen leaves the server's files as they are. If the truncate_directory flag // is set, the files are deleted once the server has stopped. if data.Adapter == backup.LocalBackupAdapter { b, _, err := backup.LocateLocal(client, backupUuid) if err != nil { middleware.CaptureAndAbort(c, err) return } go func(s *server.Server, b backup.BackupInterface, logger *log.Entry) { logger.Info("starting restoration process for server backup using local driver") if err := s.RestoreBackup(b, nil, data.TruncateDirectory); err != nil { logger.WithField("error", err).Error("failed to restore local backup to server") } s.Events().Publish(server.DaemonMessageEvent, "Completed server restoration from local backup.") s.Events().Publish(server.BackupRestoreCompletedEvent, "") logger.Info("completed server restoration from local backup") s.SetRestoring(false) }(s, b, logger) hasError = false c.Status(http.StatusAccepted) return } // Since this is not a local backup we need to stream the archive and then // parse over the contents as we go in order to restore it to the server. httpClient := backupRestoreHttpClient() logger.Info("downloading backup from remote location...") // TODO: this will hang if there is an issue. We can't use c.Request.Context() (or really any) // since it will be canceled when the request is closed which happens quickly since we push // this into the background. // // For now I'm just using the server context so at least the request is canceled if // the server gets deleted. req, err := http.NewRequestWithContext(s.Context(), http.MethodGet, data.DownloadUrl, nil) if err != nil { middleware.CaptureAndAbort(c, err) return } res, err := httpClient.Do(req) if err != nil { var downloadErr backupDownloadError if stderrors.As(err, &downloadErr) { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": downloadErr.Error()}) return } middleware.CaptureAndAbort(c, err) return } if res.StatusCode != http.StatusOK { _ = res.Body.Close() c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "The provided backup link returned an invalid response status: " + res.Status}) return } // Don't allow content types that we know are going to give us problems. if !isSupportedBackupRestoreContentType(res.Header.Get("Content-Type")) { _ = res.Body.Close() c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ "error": "The provided backup link is not a supported content type. \"" + res.Header.Get("Content-Type") + "\" is not application/x-gzip.", }) return } go func(s *server.Server, uuid string, logger *log.Entry) { logger.Info("starting restoration process for server backup using S3 driver") if err := s.RestoreBackup(backup.NewS3(client, uuid, ""), res.Body, data.TruncateDirectory); err != nil { logger.WithField("error", errors.WithStack(err)).Error("failed to restore remote S3 backup to server") } s.Events().Publish(server.DaemonMessageEvent, "Completed server restoration from S3 backup.") s.Events().Publish(server.BackupRestoreCompletedEvent, "") logger.Info("completed server restoration from S3 backup") s.SetRestoring(false) }(s, backupUuid, logger) hasError = false c.Status(http.StatusAccepted) } // deleteServerBackup deletes a local backup of a server. If the backup is not // found on the machine just return a 404 error. The service calling this // endpoint can make its own decisions as to how it wants to handle that // response. func deleteServerBackup(c *gin.Context) { backupUuid, ok := parseBackupUuid(c, c.Param("backup")) if !ok { return } b, _, err := backup.LocateLocal(middleware.ExtractApiClient(c), backupUuid) if err != nil { // Just return from the function at this point if the backup was not located. if errors.Is(err, os.ErrNotExist) { c.AbortWithStatusJSON(http.StatusNotFound, gin.H{ "error": "The requested backup was not found on this server.", }) return } middleware.CaptureAndAbort(c, err) return } // I'm not entirely sure how likely this is to happen, however if we did manage to // locate the backup previously and it is now missing when we go to delete, just // treat it as having been successful, rather than returning a 404. if err := b.Remove(); err != nil && !errors.Is(err, os.ErrNotExist) { middleware.CaptureAndAbort(c, err) return } c.Status(http.StatusNoContent) } func parseBackupUuid(c *gin.Context, value string) (string, bool) { parsed, err := uuid.Parse(value) if err == nil && len(value) == len(parsed.String()) && parsed.String() == strings.ToLower(value) { return parsed.String(), true } c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "The backup identifier must be a valid UUID."}) return "", false } func validateBackupDownloadUrl(raw string) error { parsed, err := url.Parse(raw) if err != nil || parsed.Host == "" { return backupDownloadError("The provided backup link is not a valid URL.") } if parsed.Scheme != "http" && parsed.Scheme != "https" { return backupDownloadError("The provided backup link must use HTTP or HTTPS.") } if ip := net.ParseIP(parsed.Hostname()); ip != nil && isBlockedBackupRestoreIP(parsed.Hostname(), ip) { return backupDownloadError("The provided backup link resolves to a blocked address.") } return nil } func backupRestoreHttpClient() http.Client { transport := http.DefaultTransport.(*http.Transport).Clone() transport.Proxy = nil transport.ResponseHeaderTimeout = 30 * time.Second dialer := &net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, } transport.DialContext = func(ctx context.Context, network string, address string) (net.Conn, error) { host, port, err := net.SplitHostPort(address) if err != nil { return nil, err } ips, err := net.DefaultResolver.LookupIPAddr(ctx, host) if err != nil { return nil, err } if len(ips) == 0 { return nil, errors.New("router/backups: backup download host did not resolve to any addresses") } for _, resolved := range ips { if isBlockedBackupRestoreIP(host, resolved.IP) { return nil, backupDownloadError("The provided backup link resolves to a blocked address.") } } var lastErr error for _, resolved := range ips { conn, err := dialer.DialContext(ctx, network, net.JoinHostPort(resolved.IP.String(), port)) if err == nil { return conn, nil } lastErr = err } return nil, lastErr } return http.Client{ Transport: transport, CheckRedirect: func(req *http.Request, via []*http.Request) error { if len(via) >= 10 { return backupDownloadError("The provided backup link redirects too many times.") } return validateBackupDownloadUrl(req.URL.String()) }, } } func isBlockedBackupRestoreIP(host string, ip net.IP) bool { addr, ok := netip.AddrFromSlice(ip) if !ok { return true } addr = addr.Unmap() if wnet.IsInternal(addr) { return !isAllowedBackupRestoreDestination(host, addr) } return false } func isAllowedBackupRestoreDestination(host string, addr netip.Addr) bool { return wnet.IsAllowed(config.Get().System.Backups.RestoreHostAllowlist, host, addr) } func isSupportedBackupRestoreContentType(value string) bool { mediaType, _, err := mime.ParseMediaType(value) if err != nil { mediaType = strings.TrimSpace(value) } switch strings.ToLower(mediaType) { case "application/x-gzip", "application/gzip": return true default: return false } }