2020-04-06 19:49:49 +00:00
|
|
|
package router
|
2019-04-06 05:20:26 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-07 00:32:35 +00:00
|
|
|
"bufio"
|
2019-05-04 23:04:41 +00:00
|
|
|
"bytes"
|
2020-04-04 20:08:10 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2020-09-04 03:29:53 +00:00
|
|
|
"github.com/apex/log"
|
2019-05-04 23:04:41 +00:00
|
|
|
"github.com/buger/jsonparser"
|
2020-04-06 19:49:49 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2020-04-04 22:15:49 +00:00
|
|
|
"github.com/mholt/archiver/v3"
|
2020-11-28 23:57:10 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-04-04 05:17:26 +00:00
|
|
|
"github.com/pterodactyl/wings/api"
|
2019-12-17 05:43:07 +00:00
|
|
|
"github.com/pterodactyl/wings/config"
|
2019-11-16 23:10:53 +00:00
|
|
|
"github.com/pterodactyl/wings/installer"
|
2020-04-06 19:49:49 +00:00
|
|
|
"github.com/pterodactyl/wings/router/tokens"
|
2019-04-06 05:20:26 +00:00
|
|
|
"github.com/pterodactyl/wings/server"
|
2019-04-06 05:55:48 +00:00
|
|
|
"io"
|
2020-04-04 20:08:10 +00:00
|
|
|
"io/ioutil"
|
2019-04-06 05:20:26 +00:00
|
|
|
"net/http"
|
2019-04-06 05:55:48 +00:00
|
|
|
"os"
|
2020-04-04 20:08:10 +00:00
|
|
|
"path/filepath"
|
2019-04-06 19:27:44 +00:00
|
|
|
"strconv"
|
2019-04-06 17:49:31 +00:00
|
|
|
"strings"
|
2019-04-06 05:20:26 +00:00
|
|
|
)
|
|
|
|
|
2020-04-06 19:49:49 +00:00
|
|
|
func getServerArchive(c *gin.Context) {
|
|
|
|
auth := strings.SplitN(c.GetHeader("Authorization"), " ", 2)
|
2020-04-04 06:51:35 +00:00
|
|
|
|
|
|
|
if len(auth) != 2 || auth[0] != "Bearer" {
|
2020-04-06 19:49:49 +00:00
|
|
|
c.Header("WWW-Authenticate", "Bearer")
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
|
|
|
"error": "The required authorization heads were not present in the request.",
|
|
|
|
})
|
2020-04-04 06:51:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-06 19:49:49 +00:00
|
|
|
token := tokens.TransferPayload{}
|
2020-04-06 20:39:33 +00:00
|
|
|
if err := tokens.ParseToken([]byte(auth[1]), &token); err != nil {
|
2020-04-06 19:49:49 +00:00
|
|
|
TrackedError(err).AbortWithServerError(c)
|
2020-04-04 06:51:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-06 19:49:49 +00:00
|
|
|
if token.Subject != c.Param("server") {
|
|
|
|
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
|
2020-04-06 23:09:55 +00:00
|
|
|
"error": "( .. •˘___˘• .. )",
|
2020-04-06 19:49:49 +00:00
|
|
|
})
|
2020-04-04 06:51:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-06 19:49:49 +00:00
|
|
|
s := GetServer(c.Param("server"))
|
2020-04-04 05:17:26 +00:00
|
|
|
|
|
|
|
st, err := s.Archiver.Stat()
|
|
|
|
if err != nil {
|
2020-11-08 23:15:39 +00:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2020-04-06 19:49:49 +00:00
|
|
|
TrackedServerError(err, s).SetMessage("failed to stat archive").AbortWithServerError(c)
|
2020-04-04 05:17:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-06 19:49:49 +00:00
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
2020-04-04 05:17:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
checksum, err := s.Archiver.Checksum()
|
|
|
|
if err != nil {
|
2020-04-06 19:49:49 +00:00
|
|
|
TrackedServerError(err, s).SetMessage("failed to calculate checksum").AbortWithServerError(c)
|
2020-04-04 05:17:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-27 19:24:08 +00:00
|
|
|
file, err := os.Open(s.Archiver.Path())
|
2020-04-04 05:17:26 +00:00
|
|
|
if err != nil {
|
2020-04-06 19:49:49 +00:00
|
|
|
tserr := TrackedServerError(err, s)
|
2020-04-04 05:17:26 +00:00
|
|
|
if !os.IsNotExist(err) {
|
2020-04-06 19:49:49 +00:00
|
|
|
tserr.SetMessage("failed to open archive for reading")
|
|
|
|
} else {
|
|
|
|
tserr.SetMessage("failed to open archive")
|
2020-04-04 05:17:26 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 19:49:49 +00:00
|
|
|
tserr.AbortWithServerError(c)
|
2020-04-04 05:17:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2020-04-06 19:49:49 +00:00
|
|
|
c.Header("X-Checksum", checksum)
|
|
|
|
c.Header("X-Mime-Type", st.Mimetype)
|
|
|
|
c.Header("Content-Length", strconv.Itoa(int(st.Info.Size())))
|
2020-09-27 19:24:08 +00:00
|
|
|
c.Header("Content-Disposition", "attachment; filename="+s.Archiver.Name())
|
2020-04-06 19:49:49 +00:00
|
|
|
c.Header("Content-Type", "application/octet-stream")
|
2020-04-04 05:17:26 +00:00
|
|
|
|
2020-04-06 19:49:49 +00:00
|
|
|
bufio.NewReader(file).WriteTo(c.Writer)
|
2020-04-04 05:17:26 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 19:49:49 +00:00
|
|
|
func postServerArchive(c *gin.Context) {
|
|
|
|
s := GetServer(c.Param("server"))
|
|
|
|
|
2020-09-04 03:29:53 +00:00
|
|
|
go func(s *server.Server) {
|
2020-11-29 19:31:54 +00:00
|
|
|
r := api.New()
|
|
|
|
|
|
|
|
// Attempt to get an archive of the server. This **WILL NOT** modify the source files of a server,
|
|
|
|
// this process is 100% safe and will not corrupt a server's files if it fails.
|
2020-09-04 03:29:53 +00:00
|
|
|
if err := s.Archiver.Archive(); err != nil {
|
|
|
|
s.Log().WithField("error", err).Error("failed to get archive for server")
|
2020-11-29 19:31:54 +00:00
|
|
|
|
|
|
|
if err := r.SendArchiveStatus(s.Id(), false); err != nil {
|
|
|
|
if !api.IsRequestError(err) {
|
|
|
|
s.Log().WithField("error", err).Error("failed to notify panel of failed archive status")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Log().WithField("error", err.Error()).Error("panel returned an error when notifying it of a failed archive status")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Log().Info("successfully notified panel of failed archive status")
|
2020-04-06 19:49:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-04 03:29:53 +00:00
|
|
|
s.Log().Debug("successfully created server archive, notifying panel")
|
2020-04-06 19:49:49 +00:00
|
|
|
|
2020-11-29 19:31:54 +00:00
|
|
|
if err := r.SendArchiveStatus(s.Id(), true); err != nil {
|
2020-10-31 17:04:20 +00:00
|
|
|
if !api.IsRequestError(err) {
|
2020-11-29 19:31:54 +00:00
|
|
|
s.Log().WithField("error", err).Error("failed to notify panel of successful archive status")
|
2020-04-06 19:49:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-29 19:31:54 +00:00
|
|
|
s.Log().WithField("error", err.Error()).Error("panel returned an error when notifying it of a successful archive status")
|
2020-04-06 19:49:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-29 19:31:54 +00:00
|
|
|
s.Log().Info("successfully notified panel of successful archive status")
|
2020-04-06 19:49:49 +00:00
|
|
|
}(s)
|
|
|
|
|
|
|
|
c.Status(http.StatusAccepted)
|
|
|
|
}
|
|
|
|
|
|
|
|
func postTransfer(c *gin.Context) {
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
buf.ReadFrom(c.Request.Body)
|
2020-04-04 20:08:10 +00:00
|
|
|
|
|
|
|
go func(data []byte) {
|
|
|
|
serverID, _ := jsonparser.GetString(data, "server_id")
|
|
|
|
url, _ := jsonparser.GetString(data, "url")
|
|
|
|
token, _ := jsonparser.GetString(data, "token")
|
|
|
|
|
2020-09-04 03:29:53 +00:00
|
|
|
l := log.WithField("server", serverID)
|
2020-04-04 20:08:10 +00:00
|
|
|
// Create an http client with no timeout.
|
|
|
|
client := &http.Client{Timeout: 0}
|
|
|
|
|
2020-04-05 00:27:31 +00:00
|
|
|
hasError := true
|
|
|
|
defer func() {
|
|
|
|
if !hasError {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-04 03:29:53 +00:00
|
|
|
l.Info("server transfer failed, notifying panel")
|
2020-11-29 19:33:44 +00:00
|
|
|
if err := api.New().SendTransferFailure(serverID); err != nil {
|
2020-10-31 17:04:20 +00:00
|
|
|
if !api.IsRequestError(err) {
|
2020-09-04 03:29:53 +00:00
|
|
|
l.WithField("error", err).Error("failed to notify panel with transfer failure")
|
2020-04-05 00:27:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-31 17:04:20 +00:00
|
|
|
l.WithField("error", err.Error()).Error("received error response from panel while notifying of transfer failure")
|
2020-04-05 00:27:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-05 19:08:40 +00:00
|
|
|
l.Debug("notified panel of transfer failure")
|
2020-04-05 00:27:31 +00:00
|
|
|
}()
|
|
|
|
|
2020-04-04 20:08:10 +00:00
|
|
|
// Make a new GET request to the URL the panel gave us.
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
log.WithField("error", err).Error("failed to create http request for archive transfer")
|
2020-04-04 20:08:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the authorization header.
|
|
|
|
req.Header.Set("Authorization", token)
|
|
|
|
|
|
|
|
// Execute the http request.
|
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
l.WithField("error", err).Error("failed to send archive http request")
|
2020-04-04 20:08:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2020-04-04 22:15:49 +00:00
|
|
|
// Handle non-200 status codes.
|
2020-04-04 20:08:10 +00:00
|
|
|
if res.StatusCode != 200 {
|
2020-09-04 03:29:53 +00:00
|
|
|
_, err := ioutil.ReadAll(res.Body)
|
2020-04-04 20:08:10 +00:00
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
l.WithField("error", err).WithField("status", res.StatusCode).Error("failed read transfer response body")
|
2020-04-04 20:08:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-28 23:57:10 +00:00
|
|
|
l.WithField("error", err).WithField("status", res.StatusCode).Error("failed to request server archive")
|
2020-04-04 20:08:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-04 22:15:49 +00:00
|
|
|
// Get the path to the archive.
|
2020-09-04 03:29:53 +00:00
|
|
|
archivePath := filepath.Join(config.Get().System.ArchiveDirectory, serverID+".tar.gz")
|
2020-04-04 20:08:10 +00:00
|
|
|
|
2020-04-05 00:27:31 +00:00
|
|
|
// Check if the archive already exists and delete it if it does.
|
2020-11-29 19:33:44 +00:00
|
|
|
if _, err = os.Stat(archivePath); err != nil {
|
2020-04-05 00:27:31 +00:00
|
|
|
if !os.IsNotExist(err) {
|
2020-11-28 23:57:10 +00:00
|
|
|
l.WithField("error", err).Error("failed to stat archive file")
|
2020-04-05 00:27:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := os.Remove(archivePath); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
l.WithField("error", err).Warn("failed to remove old archive file")
|
2020-04-05 00:27:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 22:15:49 +00:00
|
|
|
// Create the file.
|
2020-04-04 20:08:10 +00:00
|
|
|
file, err := os.Create(archivePath)
|
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
l.WithField("error", err).Error("failed to open archive on disk")
|
2020-04-04 20:08:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-04 22:15:49 +00:00
|
|
|
// Copy the file.
|
2020-09-04 03:29:53 +00:00
|
|
|
buf := make([]byte, 1024*4)
|
2020-08-24 00:18:40 +00:00
|
|
|
_, err = io.CopyBuffer(file, res.Body, buf)
|
2020-04-04 20:08:10 +00:00
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
l.WithField("error", err).Error("failed to copy archive file to disk")
|
2020-04-04 20:08:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-04 22:15:49 +00:00
|
|
|
// Close the file so it can be opened to verify the checksum.
|
2020-04-04 20:08:10 +00:00
|
|
|
if err := file.Close(); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
l.WithField("error", err).Error("failed to close archive file")
|
2020-04-04 20:08:10 +00:00
|
|
|
return
|
|
|
|
}
|
2020-09-04 03:29:53 +00:00
|
|
|
|
2020-11-29 00:00:52 +00:00
|
|
|
// Whenever the transfer fails or succeeds, delete the temporary transfer archive.
|
|
|
|
defer func() {
|
|
|
|
log.WithField("server", serverID).Debug("deleting temporary transfer archive..")
|
|
|
|
if err := os.Remove(archivePath); err != nil && !os.IsNotExist(err) {
|
|
|
|
l.WithFields(log.Fields{
|
|
|
|
"server": serverID,
|
|
|
|
"error": err,
|
|
|
|
}).Warn("failed to delete transfer archive")
|
|
|
|
} else {
|
2020-11-29 19:35:02 +00:00
|
|
|
l.Debug("deleted temporary transfer archive successfully")
|
2020-11-29 00:00:52 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-11-29 19:35:02 +00:00
|
|
|
l.Debug("server archive downloaded, computing checksum...")
|
2020-04-04 20:08:10 +00:00
|
|
|
|
2020-04-04 22:15:49 +00:00
|
|
|
// Open the archive file for computing a checksum.
|
2020-04-04 20:08:10 +00:00
|
|
|
file, err = os.Open(archivePath)
|
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
l.WithField("error", err).Error("failed to open archive on disk")
|
2020-04-04 20:08:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-04 22:15:49 +00:00
|
|
|
// Compute the sha256 checksum of the file.
|
2020-04-04 20:08:10 +00:00
|
|
|
hash := sha256.New()
|
2020-09-04 03:29:53 +00:00
|
|
|
buf = make([]byte, 1024*4)
|
2020-08-24 00:18:40 +00:00
|
|
|
if _, err := io.CopyBuffer(hash, file, buf); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
l.WithField("error", err).Error("failed to copy archive file for checksum verification")
|
2020-04-04 20:08:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-04 22:15:49 +00:00
|
|
|
// Verify the two checksums.
|
2020-04-04 20:08:10 +00:00
|
|
|
if hex.EncodeToString(hash.Sum(nil)) != res.Header.Get("X-Checksum") {
|
2020-09-04 03:29:53 +00:00
|
|
|
l.Error("checksum verification failed for archive")
|
2020-04-04 20:08:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-04 22:15:49 +00:00
|
|
|
// Close the file.
|
|
|
|
if err := file.Close(); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
l.WithField("error", err).Error("failed to close archive file after calculating checksum")
|
2020-04-04 22:15:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-04 03:29:53 +00:00
|
|
|
l.Info("server archive transfer was successful")
|
2020-04-04 22:15:49 +00:00
|
|
|
|
|
|
|
// Get the server data from the request.
|
|
|
|
serverData, t, _, _ := jsonparser.Get(data, "server")
|
|
|
|
if t != jsonparser.Object {
|
2020-09-04 03:29:53 +00:00
|
|
|
l.Error("invalid server data passed in request")
|
2020-04-04 22:15:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new server installer (note this does not execute the install script)
|
|
|
|
i, err := installer.New(serverData)
|
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
l.WithField("error", err).Error("failed to validate received server data")
|
2020-04-04 22:15:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the server to the collection.
|
|
|
|
server.GetServers().Add(i.Server())
|
|
|
|
|
|
|
|
// Create the server's environment (note this does not execute the install script)
|
2020-09-13 04:48:04 +00:00
|
|
|
if err := i.Server().CreateEnvironment(); err != nil {
|
|
|
|
l.WithField("error", err).Error("failed to create server environment")
|
|
|
|
return
|
|
|
|
}
|
2020-04-04 22:15:49 +00:00
|
|
|
|
2020-11-29 00:00:52 +00:00
|
|
|
// Un-archive the archive, that sounds weird..
|
2020-09-27 19:24:08 +00:00
|
|
|
if err := archiver.NewTarGz().Unarchive(archivePath, i.Server().Filesystem().Path()); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
l.WithField("error", err).Error("failed to extract server archive")
|
2020-04-06 20:39:33 +00:00
|
|
|
return
|
|
|
|
}
|
2020-04-04 22:15:49 +00:00
|
|
|
|
2020-04-06 20:39:33 +00:00
|
|
|
// We mark the process as being successful here as if we fail to send a transfer success,
|
|
|
|
// then a transfer failure won't probably be successful either.
|
|
|
|
//
|
|
|
|
// It may be useful to retry sending the transfer success every so often just in case of a small
|
|
|
|
// hiccup or the fix of whatever error causing the success request to fail.
|
|
|
|
hasError = false
|
|
|
|
|
|
|
|
// Notify the panel that the transfer succeeded.
|
2020-10-31 17:04:20 +00:00
|
|
|
err = api.New().SendTransferSuccess(serverID)
|
|
|
|
if err != nil {
|
|
|
|
if !api.IsRequestError(err) {
|
2020-11-28 23:57:10 +00:00
|
|
|
l.WithField("error", err).Error("failed to notify panel of transfer success")
|
2020-04-04 22:15:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-31 17:04:20 +00:00
|
|
|
l.WithField("error", err.Error()).Error("panel responded with error after transfer success")
|
2020-04-04 22:15:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-29 19:35:02 +00:00
|
|
|
l.Info("successfully notified panel of transfer success")
|
2020-04-06 19:49:49 +00:00
|
|
|
}(buf.Bytes())
|
2020-04-04 05:17:26 +00:00
|
|
|
|
2020-04-06 19:49:49 +00:00
|
|
|
c.Status(http.StatusAccepted)
|
2019-04-06 05:55:48 +00:00
|
|
|
}
|