Merge branch 'develop' into feature/docker-environment

# Conflicts:
#	.dev/vagrant/provision.sh
#	Gopkg.lock
#	Gopkg.toml
#	Vagrantfile
#	api/api.go
#	api/auth.go
#	api/handlers.go
#	api/handlers_server.go
#	api/routes.go
#	api/utils.go
#	command/root.go
#	control/docker_environment.go
#	control/server.go
#	glide.lock
#	glide.yaml
#	utils/logger.go
#	wings.go
This commit is contained in:
Jakob Schrettenbrunner
2018-05-16 10:00:07 +02:00
11 changed files with 261 additions and 170 deletions

View File

@@ -2,23 +2,17 @@ package api
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/pterodactyl/wings/config"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
type InternalAPI struct {
router *gin.Engine
}
func NewAPI() InternalAPI {
return InternalAPI{}
}
// Configure the API and begin listening on the configured IP and Port.
func (api *InternalAPI) Listen() {
if !viper.GetBool(config.Debug) {
@@ -28,21 +22,74 @@ func (api *InternalAPI) Listen() {
api.router = gin.Default()
api.router.RedirectTrailingSlash = false
// Setup Access-Control origin headers. Down the road once this is closer to
// release we should setup this header properly and lock it down to the domain
// used to run the Panel.
api.router.Use(func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
})
api.router.OPTIONS("/", func(c *gin.Context) {
c.Header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
c.Header("Access-Control-Allow-Headers", "X-Access-Token")
c.Header("Access-Control-Allow-Headers", "Authorization")
})
api.RegisterRoutes()
// Register all of the API route bindings.
api.register()
listenString := fmt.Sprintf("%s:%d", viper.GetString(config.APIHost), viper.GetInt(config.APIPort))
l := fmt.Sprintf("%s:%d", viper.GetString(config.APIHost), viper.GetInt(config.APIPort))
api.router.Run(l)
api.router.Run(listenString)
log.Info("Now listening on %s", listenString)
log.Fatal(http.ListenAndServe(listenString, nil))
logrus.Info("API Server is now listening on %s", l)
}
// Register routes for v1 of the API. This API should be fully backwards compatable with
// the existing Nodejs Daemon API.
//
// Routes that are not yet completed are commented out. Routes are grouped where possible
// to keep this function organized.
func (api *InternalAPI) register() {
v1 := api.router.Group("/api/v1")
{
v1.GET("/", AuthHandler(""), GetIndex)
//v1.PATCH("/config", AuthHandler("c:config"), PatchConfiguration)
v1.GET("/servers", AuthHandler("c:list"), handleGetServers)
v1.POST("/servers", AuthHandler("c:create"), handlePostServers)
v1ServerRoutes := v1.Group("/servers/:server")
{
v1ServerRoutes.GET("/", AuthHandler("s:get"), handleGetServer)
v1ServerRoutes.PATCH("/", AuthHandler("s:config"), handlePatchServer)
v1ServerRoutes.DELETE("/", AuthHandler("g:server:delete"), handleDeleteServer)
v1ServerRoutes.POST("/reinstall", AuthHandler("s:install-server"), handlePostServerReinstall)
v1ServerRoutes.POST("/rebuild", AuthHandler("g:server:rebuild"), handlePostServerRebuild)
v1ServerRoutes.POST("/password", AuthHandler(""), handlePostServerPassword)
v1ServerRoutes.POST("/power", AuthHandler("s:power"), handlePostServerPower)
v1ServerRoutes.POST("/command", AuthHandler("s:command"), handlePostServerCommand)
v1ServerRoutes.GET("/log", AuthHandler("s:console"), handleGetServerLog)
v1ServerRoutes.POST("/suspend", AuthHandler(""), handlePostServerSuspend)
v1ServerRoutes.POST("/unsuspend", AuthHandler(""), handlePostServerUnsuspend)
}
//v1ServerFileRoutes := v1.Group("/servers/:server/files")
//{
// v1ServerFileRoutes.GET("/file/:file", AuthHandler("s:files:read"), handleGetFile)
// v1ServerFileRoutes.GET("/stat/:file", AuthHandler("s:files:"), handleGetFileStat)
// v1ServerFileRoutes.GET("/dir/:directory", AuthHandler("s:files:get"), handleGetDirectory)
//
// v1ServerFileRoutes.POST("/dir/:directory", AuthHandler("s:files:create"), handlePostFilesFolder)
// v1ServerFileRoutes.POST("/file/:file", AuthHandler("s:files:post"), handlePostFile)
//
// v1ServerFileRoutes.POST("/copy/:file", AuthHandler("s:files:copy"), handlePostFileCopy)
// v1ServerFileRoutes.POST("/move/:file", AuthHandler("s:files:move"), handlePostFileMove)
// v1ServerFileRoutes.POST("/rename/:file", AuthHandler("s:files:move"), handlePostFileMove)
// v1ServerFileRoutes.POST("/compress/:file", AuthHandler("s:files:compress"), handlePostFileCompress)
// v1ServerFileRoutes.POST("/decompress/:file", AuthHandler("s:files:decompress"), handlePostFileDecompress)
//
// v1ServerFileRoutes.DELETE("/file/:file", AuthHandler("s:files:delete"), handleDeleteFile)
//
// v1ServerFileRoutes.GET("/download/:token", handleGetDownloadFile)
//}
}
}

View File

@@ -13,7 +13,7 @@ import (
)
const (
accessTokenHeader = "X-Access-Token"
accessTokenHeader = "Authorization"
contextVarServer = "server"
contextVarAuth = "auth"

View File

@@ -1,62 +1,83 @@
package api
import (
"fmt"
"net/http"
"runtime"
//"runtime"
"github.com/gin-gonic/gin"
"github.com/pterodactyl/wings/constants"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
//"github.com/shirou/gopsutil/host"
//"github.com/shirou/gopsutil/mem"
log "github.com/sirupsen/logrus"
)
func GetIndex(c *gin.Context) {
auth := GetContextAuthManager(c)
//auth := GetContextAuthManager(c)
//if auth == nil {
// c.Header("Content-Type", "text/html")
// c.String(http.StatusOK, constants.IndexPage)
//}
if auth != nil && auth.HasPermission("c:info") {
hostInfo, err := host.Info()
if err != nil {
log.WithError(err).Error("Failed to retrieve host information.")
}
cpuInfo, err := cpu.Info()
if err != nil {
log.WithError(err).Error("Failed to retrieve CPU information.")
}
memInfo, err := mem.VirtualMemory()
if err != nil {
log.WithError(err).Error("Failed to retrieve memory information.")
}
info := struct {
Name string `json:"name"`
Version string `json:"version"`
System struct {
SystemType string `json:"type"`
Platform string `json:"platform"`
Arch string `json:"arch"`
Release string `json:"release"`
Cpus int32 `json:"cpus"`
Freemem uint64 `json:"freemem"`
} `json:"system"`
}{
Name: "Pterodactyl wings",
Version: constants.Version,
}
info.System.SystemType = hostInfo.OS
info.System.Platform = hostInfo.Platform
info.System.Arch = runtime.GOARCH
info.System.Release = hostInfo.KernelVersion
info.System.Cpus = cpuInfo[0].Cores
info.System.Freemem = memInfo.Free
c.JSON(http.StatusOK, info)
return
s, err := cpu.Counts(true)
if err != nil {
log.WithError(err).Error("Failed to retrieve host information.")
}
c.Header("Content-Type", "text/html")
c.String(http.StatusOK, constants.IndexPage)
fmt.Println(s)
i := struct {
Name string
Cpu struct {
Cores int
}
}{
Name: "Wings",
}
i.Cpu.Cores = s
c.JSON(http.StatusOK, i)
return
//if auth != nil && auth.HasPermission("c:info") {
// hostInfo, err := host.Info()
// if err != nil {
// log.WithError(err).Error("Failed to retrieve host information.")
// }
// cpuInfo, err := cpu.Info()
// if err != nil {
// log.WithError(err).Error("Failed to retrieve CPU information.")
// }
// memInfo, err := mem.VirtualMemory()
// if err != nil {
// log.WithError(err).Error("Failed to retrieve memory information.")
// }
//
// info := struct {
// Name string `json:"name"`
// Version string `json:"version"`
// System struct {
// SystemType string `json:"type"`
// Platform string `json:"platform"`
// Arch string `json:"arch"`
// Release string `json:"release"`
// Cpus int32 `json:"cpus"`
// Freemem uint64 `json:"freemem"`
// } `json:"system"`
// }{
// Name: "Pterodactyl wings",
// Version: constants.Version,
// }
// info.System.SystemType = hostInfo.OS
// info.System.Platform = hostInfo.Platform
// info.System.Arch = runtime.GOARCH
// info.System.Release = hostInfo.KernelVersion
// info.System.Cpus = cpuInfo[0].Cores
// info.System.Freemem = memInfo.Free
//
// c.JSON(http.StatusOK, info)
// return
//}
}
type incomingConfiguration struct {