From bbc6d26b352d555b498a6195077f55bf285110fe Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Tue, 20 Feb 2018 15:20:57 -0600 Subject: [PATCH] Break more of everything --- Gopkg.lock | 6 ++-- Gopkg.toml | 2 +- api/api.go | 77 ++++++++++++++++++++++++++++++++++++++++--------- api/auth.go | 2 +- api/handlers.go | 33 +++++++++++++++++---- api/routes.go | 49 ------------------------------- main.go | 2 +- 7 files changed, 96 insertions(+), 75 deletions(-) delete mode 100644 api/routes.go diff --git a/Gopkg.lock b/Gopkg.lock index 7d671b8..f35a959 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -217,7 +217,6 @@ [[projects]] name = "github.com/shirou/gopsutil" packages = [ - ".", "cpu", "host", "internal/common", @@ -225,8 +224,7 @@ "net", "process" ] - revision = "c432be29ccce470088d07eea25b3ea7e68a8afbb" - version = "v2.18.01" + revision = "543a05cce094293c7747322720256bee15d88a12" [[projects]] branch = "master" @@ -343,6 +341,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "8a295fa1730b9ed537e5fceeeee2a87c233eee4dea1bf0697d9f94b0c0d23e7d" + inputs-digest = "3f1bdf5882e27f292b13d4b95c9f51eb1a7609af61ccda0fae50a806b0a2ba4f" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 3725ff4..b165b96 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -43,7 +43,7 @@ [[constraint]] name = "github.com/shirou/gopsutil" - version = "2.18.1" + revision = "543a05cce094293c7747322720256bee15d88a12" [[constraint]] name = "github.com/sirupsen/logrus" diff --git a/api/api.go b/api/api.go index ee0751c..f54d7f6 100644 --- a/api/api.go +++ b/api/api.go @@ -5,20 +5,15 @@ import ( "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 +23,75 @@ 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("Now listening on %s", l) + logrus.Fatal(http.ListenAndServe(l, nil)) +} + +// 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) + //} + } } diff --git a/api/auth.go b/api/auth.go index ef67b66..d958b1f 100644 --- a/api/auth.go +++ b/api/auth.go @@ -10,7 +10,7 @@ import ( ) const ( - accessTokenHeader = "X-Access-Token" + accessTokenHeader = "Authorization" contextVarServer = "server" contextVarAuth = "auth" diff --git a/api/handlers.go b/api/handlers.go index 813bd51..469f271 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -1,13 +1,15 @@ package api import ( + "fmt" "net/http" //"runtime" "github.com/gin-gonic/gin" - "github.com/pterodactyl/wings/constants" + //"github.com/pterodactyl/wings/constants" + //"github.com/shirou/gopsutil/host" - //"github.com/shirou/gopsutil/cpu" + "github.com/shirou/gopsutil/cpu" //"github.com/shirou/gopsutil/host" //"github.com/shirou/gopsutil/mem" log "github.com/sirupsen/logrus" @@ -15,6 +17,30 @@ import ( func GetIndex(c *gin.Context) { //auth := GetContextAuthManager(c) + //if auth == nil { + // c.Header("Content-Type", "text/html") + // c.String(http.StatusOK, constants.IndexPage) + //} + + s, err := cpu.Counts(true) + if err != nil { + log.WithError(err).Error("Failed to retrieve host information.") + } + + 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() @@ -55,9 +81,6 @@ func GetIndex(c *gin.Context) { // c.JSON(http.StatusOK, info) // return //} - - c.Header("Content-Type", "text/html") - c.String(http.StatusOK, constants.IndexPage) } type incomingConfiguration struct { diff --git a/api/routes.go b/api/routes.go deleted file mode 100644 index c80bcd6..0000000 --- a/api/routes.go +++ /dev/null @@ -1,49 +0,0 @@ -package api - -func (api *InternalAPI) RegisterRoutes() { - // Register routes for v1 of the API. This API should be fully backwards compatable with - // the existing Nodejs Daemon API. - v1 := api.router.Group("/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) - //} - } -} diff --git a/main.go b/main.go index 6aefe36..4e1e7e8 100644 --- a/main.go +++ b/main.go @@ -66,6 +66,6 @@ func run(cmd *cobra.Command, args []string) { } logrus.Info("Registering API server and booting.") - a := api.NewAPI() + a := api.InternalAPI{} a.Listen() }