2020-04-06 01:00:33 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2020-04-06 20:39:33 +00:00
|
|
|
"bytes"
|
2021-01-10 01:22:39 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2020-06-13 17:26:35 +00:00
|
|
|
"github.com/apex/log"
|
2020-04-06 01:00:33 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2020-04-11 23:17:46 +00:00
|
|
|
"github.com/pterodactyl/wings/config"
|
2020-04-06 01:00:33 +00:00
|
|
|
"github.com/pterodactyl/wings/installer"
|
|
|
|
"github.com/pterodactyl/wings/system"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Returns information about the system that wings is running on.
|
|
|
|
func getSystemInformation(c *gin.Context) {
|
|
|
|
i, err := system.GetSystemInformation()
|
|
|
|
if err != nil {
|
2020-12-16 05:08:00 +00:00
|
|
|
NewTrackedError(err).Abort(c)
|
2020-04-06 01:00:33 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns all of the servers that are registered and configured correctly on
|
|
|
|
// this wings instance.
|
|
|
|
func getAllServers(c *gin.Context) {
|
2021-01-10 02:18:02 +00:00
|
|
|
serverManager := ExtractServerManager(c)
|
2021-01-08 23:14:56 +00:00
|
|
|
c.JSON(http.StatusOK, serverManager.GetAll())
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new server on the wings daemon and begins the installation process
|
|
|
|
// for it.
|
|
|
|
func postCreateServer(c *gin.Context) {
|
2020-04-06 20:39:33 +00:00
|
|
|
buf := bytes.Buffer{}
|
|
|
|
buf.ReadFrom(c.Request.Body)
|
2020-04-06 01:00:33 +00:00
|
|
|
|
2020-04-06 20:39:33 +00:00
|
|
|
install, err := installer.New(buf.Bytes())
|
2020-04-06 01:00:33 +00:00
|
|
|
if err != nil {
|
2020-04-12 00:55:00 +00:00
|
|
|
if installer.IsValidationError(err) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
|
|
|
|
"error": "The data provided in the request could not be validated.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-16 05:08:00 +00:00
|
|
|
NewTrackedError(err).Abort(c)
|
2020-04-06 01:00:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Plop that server instance onto the request so that it can be referenced in
|
|
|
|
// requests from here-on out.
|
2021-01-10 02:18:02 +00:00
|
|
|
serverManager := ExtractServerManager(c)
|
2021-01-08 23:14:56 +00:00
|
|
|
serverManager.Add(install.Server())
|
2020-04-06 01:00:33 +00:00
|
|
|
|
|
|
|
// Begin the installation process in the background to not block the request
|
|
|
|
// cycle. If there are any errors they will be logged and communicated back
|
|
|
|
// to the Panel where a reinstall may take place.
|
|
|
|
go func(i *installer.Installer) {
|
2020-09-13 04:48:04 +00:00
|
|
|
err := i.Server().CreateEnvironment()
|
|
|
|
if err != nil {
|
|
|
|
i.Server().Log().WithField("error", err).Error("failed to create server environment during install process")
|
|
|
|
return
|
|
|
|
}
|
2020-04-06 01:00:33 +00:00
|
|
|
|
2020-07-01 03:56:55 +00:00
|
|
|
if err := i.Server().Install(false); err != nil {
|
2020-06-13 17:26:35 +00:00
|
|
|
log.WithFields(log.Fields{"server": i.Uuid(), "error": err}).Error("failed to run install process for server")
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
}(install)
|
|
|
|
|
|
|
|
c.Status(http.StatusAccepted)
|
2020-04-06 20:39:33 +00:00
|
|
|
}
|
2020-04-11 23:17:46 +00:00
|
|
|
|
|
|
|
// Updates the running configuration for this daemon instance.
|
|
|
|
func postUpdateConfiguration(c *gin.Context) {
|
|
|
|
// A backup of the configuration for error purposes.
|
|
|
|
ccopy := *config.Get()
|
2020-09-05 19:08:40 +00:00
|
|
|
// A copy of the configuration we're using to bind the data received into.
|
2020-04-11 23:17:46 +00:00
|
|
|
cfg := *config.Get()
|
|
|
|
|
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(&cfg); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-11 23:17:46 +00:00
|
|
|
|
2020-06-30 03:56:13 +00:00
|
|
|
// Keep the SSL certificates the same since the Panel will send through Lets Encrypt
|
|
|
|
// default locations. However, if we picked a different location manually we don't
|
|
|
|
// want to override that.
|
|
|
|
//
|
|
|
|
// If you pass through manual locations in the API call this logic will be skipped.
|
|
|
|
if strings.HasPrefix(cfg.Api.Ssl.KeyFile, "/etc/letsencrypt/live/") {
|
2020-11-29 19:47:52 +00:00
|
|
|
cfg.Api.Ssl.KeyFile = strings.ToLower(ccopy.Api.Ssl.KeyFile)
|
|
|
|
cfg.Api.Ssl.CertificateFile = strings.ToLower(ccopy.Api.Ssl.CertificateFile)
|
2020-06-30 03:56:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 23:17:46 +00:00
|
|
|
config.Set(&cfg)
|
|
|
|
if err := config.Get().WriteToDisk(); err != nil {
|
|
|
|
// If there was an error writing to the disk, revert back to the configuration we had
|
|
|
|
// before this code was run.
|
|
|
|
config.Set(&ccopy)
|
|
|
|
|
2020-12-16 05:08:00 +00:00
|
|
|
NewTrackedError(err).Abort(c)
|
2020-04-11 23:17:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Status(http.StatusNoContent)
|
2020-05-29 15:44:49 +00:00
|
|
|
}
|