2017-08-02 20:02:34 +00:00
|
|
|
package api
|
|
|
|
|
2017-10-01 18:42:17 +00:00
|
|
|
import (
|
|
|
|
"net/http"
|
2017-08-02 20:02:34 +00:00
|
|
|
|
2017-10-01 18:42:17 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2018-02-20 19:33:32 +00:00
|
|
|
"github.com/pterodactyl/wings/control"
|
2017-10-01 18:42:17 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
2017-08-02 20:02:34 +00:00
|
|
|
|
2017-10-01 18:42:17 +00:00
|
|
|
// GET /servers
|
|
|
|
// TODO: make jsonapi compliant
|
|
|
|
func handleGetServers(c *gin.Context) {
|
|
|
|
servers := control.GetServers()
|
|
|
|
c.JSON(http.StatusOK, servers)
|
2017-08-02 20:02:34 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 18:42:17 +00:00
|
|
|
// POST /servers
|
|
|
|
// TODO: make jsonapi compliant
|
2017-08-02 20:02:34 +00:00
|
|
|
func handlePostServers(c *gin.Context) {
|
2017-10-01 18:42:17 +00:00
|
|
|
server := control.ServerStruct{}
|
|
|
|
if err := c.BindJSON(&server); err != nil {
|
|
|
|
log.WithField("server", server).WithError(err).Error("Failed to parse server request.")
|
|
|
|
c.Status(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var srv control.Server
|
|
|
|
var err error
|
|
|
|
if srv, err = control.CreateServer(&server); err != nil {
|
|
|
|
if _, ok := err.(control.ErrServerExists); ok {
|
|
|
|
log.WithError(err).Error("Cannot create server, it already exists.")
|
|
|
|
c.Status(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.WithField("server", server).WithError(err).Error("Failed to create server.")
|
|
|
|
c.Status(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
env, err := srv.Environment()
|
|
|
|
if err != nil {
|
|
|
|
log.WithField("server", srv).WithError(err).Error("Failed to get server environment.")
|
|
|
|
}
|
|
|
|
env.Create()
|
|
|
|
}()
|
|
|
|
c.JSON(http.StatusOK, srv)
|
2017-08-02 20:02:34 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 18:42:17 +00:00
|
|
|
// GET /servers/:server
|
|
|
|
// TODO: make jsonapi compliant
|
2017-08-02 20:02:34 +00:00
|
|
|
func handleGetServer(c *gin.Context) {
|
2017-10-01 18:42:17 +00:00
|
|
|
id := c.Param("server")
|
|
|
|
server := control.GetServer(id)
|
|
|
|
if server == nil {
|
|
|
|
c.Status(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, server)
|
2017-08-02 20:02:34 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 18:42:17 +00:00
|
|
|
// PATCH /servers/:server
|
2017-08-02 20:02:34 +00:00
|
|
|
func handlePatchServer(c *gin.Context) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-01 18:42:17 +00:00
|
|
|
// DELETE /servers/:server
|
|
|
|
// TODO: make jsonapi compliant
|
|
|
|
func handleDeleteServer(c *gin.Context) {
|
|
|
|
id := c.Param("server")
|
|
|
|
server := control.GetServer(id)
|
|
|
|
if server == nil {
|
|
|
|
c.Status(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
env, err := server.Environment()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).WithField("server", server).Error("Failed to delete server.")
|
|
|
|
}
|
|
|
|
if err := env.Destroy(); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to delete server, the environment couldn't be destroyed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := control.DeleteServer(id); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to delete server.")
|
|
|
|
c.Status(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2017-08-02 20:02:34 +00:00
|
|
|
func handlePostServerReinstall(c *gin.Context) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlePostServerPassword(c *gin.Context) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlePostServerRebuild(c *gin.Context) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-01 18:42:17 +00:00
|
|
|
// POST /servers/:server/power
|
|
|
|
// TODO: make jsonapi compliant
|
|
|
|
func handlePostServerPower(c *gin.Context) {
|
|
|
|
server := getServerFromContext(c)
|
|
|
|
if server == nil {
|
|
|
|
c.Status(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := GetContextAuthManager(c)
|
|
|
|
if auth == nil {
|
|
|
|
c.Status(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.Query("action") {
|
|
|
|
case "start":
|
|
|
|
{
|
|
|
|
if !auth.HasPermission("s:power:start") {
|
|
|
|
c.Status(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
server.Start()
|
|
|
|
}
|
|
|
|
case "stop":
|
|
|
|
{
|
|
|
|
if !auth.HasPermission("s:power:stop") {
|
|
|
|
c.Status(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
server.Stop()
|
|
|
|
}
|
|
|
|
case "restart":
|
|
|
|
{
|
|
|
|
if !auth.HasPermission("s:power:restart") {
|
|
|
|
c.Status(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
server.Restart()
|
|
|
|
}
|
|
|
|
case "kill":
|
|
|
|
{
|
|
|
|
if !auth.HasPermission("s:power:kill") {
|
|
|
|
c.Status(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
server.Kill()
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
c.Status(http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
}
|
2017-08-02 20:02:34 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 18:42:17 +00:00
|
|
|
// POST /servers/:server/command
|
|
|
|
// TODO: make jsonapi compliant
|
2017-08-02 20:02:34 +00:00
|
|
|
func handlePostServerCommand(c *gin.Context) {
|
2017-10-01 18:42:17 +00:00
|
|
|
server := getServerFromContext(c)
|
|
|
|
cmd := c.Query("command")
|
|
|
|
server.Exec(cmd)
|
2017-08-02 20:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleGetServerLog(c *gin.Context) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlePostServerSuspend(c *gin.Context) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlePostServerUnsuspend(c *gin.Context) {
|
|
|
|
|
|
|
|
}
|