2019-03-25 01:39:01 +00:00
|
|
|
package main
|
2017-06-18 22:01:44 +00:00
|
|
|
|
|
|
|
import (
|
2019-03-25 01:00:21 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2019-04-20 06:29:52 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2019-04-06 20:33:54 +00:00
|
|
|
"github.com/pterodactyl/wings/config"
|
2019-03-25 01:00:21 +00:00
|
|
|
"github.com/pterodactyl/wings/server"
|
|
|
|
"go.uber.org/zap"
|
2019-04-06 05:20:26 +00:00
|
|
|
"net/http"
|
2019-03-24 21:37:05 +00:00
|
|
|
)
|
2017-06-26 18:44:37 +00:00
|
|
|
|
2019-03-24 21:37:05 +00:00
|
|
|
// Entrypoint for the Wings application. Configures the logger and checks any
|
|
|
|
// flags that were passed through in the boot arguments.
|
2017-06-18 22:01:44 +00:00
|
|
|
func main() {
|
2019-03-25 01:00:21 +00:00
|
|
|
var configPath = *flag.String("config", "config.yml", "set the location for the configuration file")
|
|
|
|
var debug = *flag.Bool("debug", false, "pass in order to run wings in debug mode")
|
2019-03-24 21:37:05 +00:00
|
|
|
|
2019-03-25 01:00:21 +00:00
|
|
|
flag.Parse()
|
2019-03-24 21:37:05 +00:00
|
|
|
|
2019-03-25 01:00:21 +00:00
|
|
|
zap.S().Infof("using configuration file: %s", configPath)
|
2019-03-24 22:59:37 +00:00
|
|
|
|
2019-04-06 20:33:54 +00:00
|
|
|
c, err := config.ReadConfiguration(configPath)
|
2019-03-25 01:00:21 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
return
|
|
|
|
}
|
2019-03-24 21:37:05 +00:00
|
|
|
|
2019-03-25 01:00:21 +00:00
|
|
|
if debug {
|
|
|
|
c.Debug = true
|
|
|
|
}
|
2019-03-24 22:59:37 +00:00
|
|
|
|
2019-03-25 01:00:21 +00:00
|
|
|
printLogo()
|
|
|
|
if err := configureLogging(c.Debug); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-03-24 22:59:37 +00:00
|
|
|
|
2019-03-25 01:00:21 +00:00
|
|
|
if c.Debug {
|
|
|
|
zap.S().Debugw("running in debug mode")
|
|
|
|
}
|
2019-03-24 21:37:05 +00:00
|
|
|
|
2019-04-06 22:24:25 +00:00
|
|
|
zap.S().Infof("checking for pterodactyl system user \"%s\"", c.System.User)
|
|
|
|
if su, err := c.EnsurePterodactylUser(); err != nil {
|
|
|
|
zap.S().Panicw("failed to create pterodactyl system user", zap.Error(err))
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
zap.S().Infow("configured system user", zap.String("username", su.Username), zap.String("uid", su.Uid), zap.String("gid", su.Gid))
|
|
|
|
}
|
|
|
|
|
2019-04-06 22:39:57 +00:00
|
|
|
zap.S().Infow("beginnning file permission setting on server data directories")
|
|
|
|
if err := c.EnsureFilePermissions(); err != nil {
|
|
|
|
zap.S().Errorw("failed to properly chown data directories", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
zap.S().Infow("finished ensuring file permissions")
|
|
|
|
}
|
|
|
|
|
2019-04-06 20:33:54 +00:00
|
|
|
servers, err := server.LoadDirectory("data/servers", c.System)
|
2019-03-25 01:39:01 +00:00
|
|
|
if err != nil {
|
|
|
|
zap.S().Fatalw("failed to load server configurations", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
2019-03-25 01:00:21 +00:00
|
|
|
|
2019-03-25 01:39:01 +00:00
|
|
|
for _, s := range servers {
|
|
|
|
zap.S().Infow("loaded configuration for server", zap.String("server", s.Uuid))
|
2019-04-04 05:01:11 +00:00
|
|
|
zap.S().Infow("ensuring envrionment exists", zap.String("server", s.Uuid))
|
|
|
|
|
2019-04-20 23:26:55 +00:00
|
|
|
if err := s.Environment.Create(); err != nil {
|
2019-04-04 05:01:11 +00:00
|
|
|
zap.S().Errorw("failed to create an environment for server", zap.String("server", s.Uuid), zap.Error(err))
|
|
|
|
}
|
2019-04-20 23:20:08 +00:00
|
|
|
|
2019-04-20 23:26:55 +00:00
|
|
|
if r, err := s.Environment.IsRunning(); err != nil {
|
2019-04-20 23:20:08 +00:00
|
|
|
zap.S().Errorw("error checking server environment status", zap.String("server", s.Uuid), zap.Error(err))
|
|
|
|
} else if r {
|
|
|
|
zap.S().Infow("detected server is running, re-attaching to process", zap.String("server", s.Uuid))
|
2019-08-17 20:19:56 +00:00
|
|
|
s.SetState(server.ProcessRunningState)
|
2019-04-20 23:26:55 +00:00
|
|
|
if err := s.Environment.Attach(); err != nil {
|
2019-04-20 23:20:08 +00:00
|
|
|
zap.S().Errorw("error attaching to server environment", zap.String("server", s.Uuid), zap.Error(err))
|
2019-08-17 20:19:56 +00:00
|
|
|
s.SetState(server.ProcessOfflineState)
|
2019-04-20 23:20:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-25 01:39:01 +00:00
|
|
|
}
|
2019-04-06 05:20:26 +00:00
|
|
|
|
|
|
|
r := &Router{
|
|
|
|
Servers: servers,
|
2019-06-29 22:41:44 +00:00
|
|
|
token: c.AuthenticationToken,
|
2019-04-20 06:29:52 +00:00
|
|
|
upgrader: websocket.Upgrader{
|
|
|
|
// Ensure that the websocket request is originating from the Panel itself,
|
|
|
|
// and not some other location.
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return r.Header.Get("Origin") == c.PanelLocation
|
|
|
|
},
|
|
|
|
},
|
2019-04-06 05:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
router := r.ConfigureRouter()
|
2019-06-29 22:41:44 +00:00
|
|
|
zap.S().Infow("configuring webserver", zap.Bool("ssl", c.Api.Ssl.Enabled), zap.String("host", c.Api.Host), zap.Int("port", c.Api.Port))
|
|
|
|
|
|
|
|
addr := fmt.Sprintf("%s:%d", c.Api.Host, c.Api.Port)
|
|
|
|
if c.Api.Ssl.Enabled {
|
|
|
|
if err := http.ListenAndServeTLS(addr, c.Api.Ssl.CertificateFile, c.Api.Ssl.KeyFile, router); err != nil {
|
|
|
|
zap.S().Fatalw("failed to configure HTTPS server", zap.Error(err))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := http.ListenAndServe(addr, router); err != nil {
|
|
|
|
zap.S().Fatalw("failed to configure HTTP server", zap.Error(err))
|
|
|
|
}
|
2019-04-06 05:20:26 +00:00
|
|
|
}
|
2019-03-24 21:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Configures the global logger for Zap so that we can call it from any location
|
|
|
|
// in the code without having to pass around a logger instance.
|
2019-03-24 22:59:37 +00:00
|
|
|
func configureLogging(debug bool) error {
|
2019-03-25 01:00:21 +00:00
|
|
|
cfg := zap.NewProductionConfig()
|
|
|
|
if debug {
|
|
|
|
cfg = zap.NewDevelopmentConfig()
|
|
|
|
}
|
2019-03-24 21:37:05 +00:00
|
|
|
|
2019-03-25 01:00:21 +00:00
|
|
|
cfg.Encoding = "console"
|
|
|
|
cfg.OutputPaths = []string{
|
|
|
|
"stdout",
|
|
|
|
}
|
2019-03-24 21:37:05 +00:00
|
|
|
|
2019-03-25 01:00:21 +00:00
|
|
|
logger, err := cfg.Build()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-24 21:37:05 +00:00
|
|
|
|
2019-03-25 01:00:21 +00:00
|
|
|
zap.ReplaceGlobals(logger)
|
2019-03-24 21:37:05 +00:00
|
|
|
|
2019-03-25 01:00:21 +00:00
|
|
|
return nil
|
2019-03-24 21:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prints the wings logo, nothing special here!
|
|
|
|
func printLogo() {
|
2019-03-25 01:00:21 +00:00
|
|
|
fmt.Println()
|
|
|
|
fmt.Println(` ____`)
|
|
|
|
fmt.Println(`__ Pterodactyl _____/___/_______ _______ ______`)
|
|
|
|
fmt.Println(`\_____\ \/\/ / / / __ / ___/`)
|
|
|
|
fmt.Println(` \___\ / / / / /_/ /___ /`)
|
|
|
|
fmt.Println(` \___/\___/___/___/___/___ /______/`)
|
|
|
|
fmt.Println(` /_______/ v` + Version)
|
|
|
|
fmt.Println()
|
2017-06-18 22:01:44 +00:00
|
|
|
}
|