2020-04-06 01:00:33 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-07-11 22:33:53 +00:00
|
|
|
"context"
|
2020-04-06 01:00:33 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2020-07-11 20:13:49 +00:00
|
|
|
"github.com/pterodactyl/wings/server"
|
2020-07-11 22:33:53 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2020-04-06 01:00:33 +00:00
|
|
|
"net/http"
|
2020-05-17 22:07:11 +00:00
|
|
|
"net/url"
|
2020-04-06 01:00:33 +00:00
|
|
|
"os"
|
2020-07-11 22:33:53 +00:00
|
|
|
"path"
|
2020-04-06 01:00:33 +00:00
|
|
|
"strconv"
|
2020-05-17 22:07:11 +00:00
|
|
|
"strings"
|
2020-04-06 01:00:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Returns the contents of a file on the server.
|
|
|
|
func getServerFileContents(c *gin.Context) {
|
|
|
|
s := GetServer(c.Param("server"))
|
2020-05-17 22:07:11 +00:00
|
|
|
|
|
|
|
p, err := url.QueryUnescape(c.Query("file"))
|
|
|
|
if err != nil {
|
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p = "/" + strings.TrimLeft(p, "/")
|
|
|
|
|
|
|
|
cleaned, err := s.Filesystem.SafePath(p)
|
2020-04-06 01:00:33 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
|
|
|
|
"error": "The file requested could not be found.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := s.Filesystem.Stat(cleaned)
|
|
|
|
if err != nil {
|
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if st.Info.IsDir() {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
|
|
|
|
"error": "The requested resource was not found on the system.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(cleaned)
|
|
|
|
if err != nil {
|
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
c.Header("X-Mime-Type", st.Mimetype)
|
|
|
|
c.Header("Content-Length", strconv.Itoa(int(st.Info.Size())))
|
|
|
|
|
|
|
|
// If a download parameter is included in the URL go ahead and attach the necessary headers
|
|
|
|
// so that the file can be downloaded.
|
|
|
|
if c.Query("download") != "" {
|
|
|
|
c.Header("Content-Disposition", "attachment; filename="+st.Info.Name())
|
|
|
|
c.Header("Content-Type", "application/octet-stream")
|
|
|
|
}
|
|
|
|
|
|
|
|
bufio.NewReader(f).WriteTo(c.Writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the contents of a directory for a server.
|
|
|
|
func getServerListDirectory(c *gin.Context) {
|
|
|
|
s := GetServer(c.Param("server"))
|
|
|
|
|
2020-05-17 22:07:11 +00:00
|
|
|
d, err := url.QueryUnescape(c.Query("directory"))
|
|
|
|
if err != nil {
|
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stats, err := s.Filesystem.ListDirectory(d)
|
2020-04-06 01:00:33 +00:00
|
|
|
if err != nil {
|
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, stats)
|
|
|
|
}
|
|
|
|
|
2020-07-11 23:00:39 +00:00
|
|
|
type renameFile struct {
|
|
|
|
To string `json:"to"`
|
|
|
|
From string `json:"from"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renames (or moves) files for a server.
|
|
|
|
func putServerRenameFiles(c *gin.Context) {
|
2020-04-06 01:00:33 +00:00
|
|
|
s := GetServer(c.Param("server"))
|
|
|
|
|
2020-05-17 22:07:11 +00:00
|
|
|
var data struct {
|
2020-07-11 23:00:39 +00:00
|
|
|
Root string `json:"root"`
|
|
|
|
Files []renameFile `json:"files"`
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
2020-05-29 15:44:49 +00:00
|
|
|
// BindJSON sends 400 if the request fails, all we need to do is return
|
|
|
|
if err := c.BindJSON(&data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-06 01:00:33 +00:00
|
|
|
|
2020-07-11 23:00:39 +00:00
|
|
|
if len(data.Files) == 0 {
|
2020-04-06 01:00:33 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
|
2020-07-11 23:00:39 +00:00
|
|
|
"error": "No files to move or rename were provided.",
|
2020-04-06 01:00:33 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-11 23:00:39 +00:00
|
|
|
g, ctx := errgroup.WithContext(context.Background())
|
|
|
|
|
|
|
|
// Loop over the array of files passed in and perform the move or rename action against each.
|
|
|
|
for _, p := range data.Files {
|
|
|
|
pf := path.Join(data.Root, p.From)
|
|
|
|
pt := path.Join(data.Root, p.To)
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
return s.Filesystem.Rename(pf, pt)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.Wait(); err != nil {
|
2020-04-06 01:00:33 +00:00
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Status(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copies a server file.
|
|
|
|
func postServerCopyFile(c *gin.Context) {
|
|
|
|
s := GetServer(c.Param("server"))
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Location string `json:"location"`
|
|
|
|
}
|
2020-05-29 15:44:49 +00:00
|
|
|
// BindJSON sends 400 if the request fails, all we need to do is return
|
|
|
|
if err := c.BindJSON(&data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-06 01:00:33 +00:00
|
|
|
|
|
|
|
if err := s.Filesystem.Copy(data.Location); err != nil {
|
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Status(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
2020-07-11 22:33:53 +00:00
|
|
|
// Deletes files from a server.
|
|
|
|
func postServerDeleteFiles(c *gin.Context) {
|
2020-04-06 01:00:33 +00:00
|
|
|
s := GetServer(c.Param("server"))
|
|
|
|
|
|
|
|
var data struct {
|
2020-07-11 22:33:53 +00:00
|
|
|
Root string `json:"root"`
|
|
|
|
Files []string `json:"files"`
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
2020-07-11 22:33:53 +00:00
|
|
|
|
2020-05-29 15:44:49 +00:00
|
|
|
if err := c.BindJSON(&data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-06 01:00:33 +00:00
|
|
|
|
2020-07-11 22:33:53 +00:00
|
|
|
if len(data.Files) == 0 {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
|
|
|
|
"error": "No files were specififed for deletion.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
g, ctx := errgroup.WithContext(context.Background())
|
|
|
|
|
|
|
|
// Loop over the array of files passed in and delete them. If any of the file deletions
|
|
|
|
// fail just abort the process entirely.
|
|
|
|
for _, p := range data.Files {
|
|
|
|
pi := path.Join(data.Root, p)
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
return s.Filesystem.Delete(pi)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.Wait(); err != nil {
|
2020-04-06 01:00:33 +00:00
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Status(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writes the contents of the request to a file on a server.
|
|
|
|
func postServerWriteFile(c *gin.Context) {
|
|
|
|
s := GetServer(c.Param("server"))
|
|
|
|
|
2020-05-17 22:07:11 +00:00
|
|
|
f, err := url.QueryUnescape(c.Query("file"))
|
|
|
|
if err != nil {
|
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f = "/" + strings.TrimLeft(f, "/")
|
|
|
|
|
|
|
|
if err := s.Filesystem.Writefile(f, c.Request.Body); err != nil {
|
2020-04-06 01:00:33 +00:00
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Status(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a directory on a server.
|
|
|
|
func postServerCreateDirectory(c *gin.Context) {
|
|
|
|
s := GetServer(c.Param("server"))
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
}
|
2020-05-29 15:44:49 +00:00
|
|
|
// BindJSON sends 400 if the request fails, all we need to do is return
|
|
|
|
if err := c.BindJSON(&data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-06 01:00:33 +00:00
|
|
|
|
|
|
|
if err := s.Filesystem.CreateDirectory(data.Name, data.Path); err != nil {
|
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Status(http.StatusNoContent)
|
2020-05-17 22:07:11 +00:00
|
|
|
}
|
2020-07-11 20:13:49 +00:00
|
|
|
|
|
|
|
func postServerCompressFiles(c *gin.Context) {
|
|
|
|
s := GetServer(c.Param("server"))
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
RootPath string `json:"root"`
|
2020-07-11 20:28:17 +00:00
|
|
|
Files []string `json:"files"`
|
2020-07-11 20:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.BindJSON(&data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-11 20:38:25 +00:00
|
|
|
if len(data.Files) == 0 {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
|
|
|
|
"error": "No files were passed through to be compressed.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-11 20:16:12 +00:00
|
|
|
if !s.Filesystem.HasSpaceAvailable() {
|
|
|
|
c.AbortWithStatusJSON(http.StatusConflict, gin.H{
|
|
|
|
"error": "This server does not have enough available disk space to generate a compressed archive.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-11 20:28:17 +00:00
|
|
|
f, err := s.Filesystem.CompressFiles(data.RootPath, data.Files)
|
2020-07-11 20:13:49 +00:00
|
|
|
if err != nil {
|
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, &server.Stat{
|
|
|
|
Info: f,
|
|
|
|
Mimetype: "application/tar+gzip",
|
|
|
|
})
|
|
|
|
}
|