2019-04-07 23:28:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-04-20 21:57:37 +00:00
|
|
|
"errors"
|
|
|
|
"github.com/gorilla/websocket"
|
2019-04-20 06:29:52 +00:00
|
|
|
"github.com/julienschmidt/httprouter"
|
2019-04-20 21:57:37 +00:00
|
|
|
"github.com/pterodactyl/wings/server"
|
2019-04-07 23:28:01 +00:00
|
|
|
"go.uber.org/zap"
|
2019-04-20 06:29:52 +00:00
|
|
|
"net/http"
|
2019-04-20 21:57:37 +00:00
|
|
|
"os"
|
2019-04-20 18:59:28 +00:00
|
|
|
"strings"
|
2019-04-21 00:38:12 +00:00
|
|
|
"sync"
|
2019-04-07 23:28:01 +00:00
|
|
|
)
|
|
|
|
|
2019-04-21 19:02:28 +00:00
|
|
|
const (
|
|
|
|
SetStateEvent = "set state"
|
|
|
|
SendServerLogsEvent = "send logs"
|
|
|
|
SendCommandEvent = "send command"
|
|
|
|
)
|
|
|
|
|
2019-04-20 06:29:52 +00:00
|
|
|
type WebsocketMessage struct {
|
2019-04-20 18:59:28 +00:00
|
|
|
// The event to perform. Should be one of the following that are supported:
|
2019-04-20 06:29:52 +00:00
|
|
|
//
|
|
|
|
// - status : Returns the server's power state.
|
|
|
|
// - logs : Returns the server log data at the time of the request.
|
|
|
|
// - power : Performs a power action aganist the server based the data.
|
|
|
|
// - command : Performs a command on a server using the data field.
|
2019-04-20 18:59:28 +00:00
|
|
|
Event string `json:"event"`
|
2019-04-07 23:28:01 +00:00
|
|
|
|
2019-04-20 06:29:52 +00:00
|
|
|
// The data to pass along, only used by power/command currently. Other requests
|
2019-04-20 18:59:28 +00:00
|
|
|
// should either omit the field or pass an empty value as it is ignored.
|
|
|
|
Args []string `json:"args,omitempty"`
|
2019-04-20 21:57:37 +00:00
|
|
|
|
|
|
|
inbound bool
|
2019-04-20 06:29:52 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 00:38:12 +00:00
|
|
|
type WebsocketHandler struct {
|
|
|
|
Server *server.Server
|
|
|
|
Mutex sync.Mutex
|
|
|
|
Connection *websocket.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle a request for a specific server websocket. This will handle inbound requests as well
|
|
|
|
// as ensure that any console output is also passed down the wire on the socket.
|
2019-04-20 06:29:52 +00:00
|
|
|
func (rt *Router) routeWebsocket(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
c, err := rt.upgrader.Upgrade(w, r, nil)
|
2019-04-07 23:28:01 +00:00
|
|
|
if err != nil {
|
2019-04-20 06:29:52 +00:00
|
|
|
zap.S().Error(err)
|
|
|
|
return
|
2019-04-07 23:28:01 +00:00
|
|
|
}
|
2019-04-20 06:29:52 +00:00
|
|
|
defer c.Close()
|
|
|
|
|
2019-04-20 18:59:28 +00:00
|
|
|
s := rt.Servers.Get(ps.ByName("server"))
|
2019-04-21 00:38:12 +00:00
|
|
|
handler := WebsocketHandler{
|
2019-04-21 19:02:28 +00:00
|
|
|
Server: s,
|
|
|
|
Mutex: sync.Mutex{},
|
2019-04-21 00:38:12 +00:00
|
|
|
Connection: c,
|
|
|
|
}
|
|
|
|
|
|
|
|
handleOutput := func(data string) {
|
|
|
|
handler.SendJson(&WebsocketMessage{
|
2019-05-27 23:58:05 +00:00
|
|
|
Event: server.ConsoleOutputEvent,
|
2019-04-21 19:02:28 +00:00
|
|
|
Args: []string{data},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
handleServerStatus := func(data string) {
|
|
|
|
handler.SendJson(&WebsocketMessage{
|
2019-05-27 23:58:05 +00:00
|
|
|
Event: server.StatusEvent,
|
2019-04-21 00:38:12 +00:00
|
|
|
Args: []string{data},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-21 19:02:28 +00:00
|
|
|
s.AddListener(server.StatusEvent, &handleServerStatus)
|
|
|
|
defer s.RemoveListener(server.StatusEvent, &handleServerStatus)
|
|
|
|
|
2019-04-21 00:38:12 +00:00
|
|
|
s.AddListener(server.ConsoleOutputEvent, &handleOutput)
|
|
|
|
defer s.RemoveListener(server.ConsoleOutputEvent, &handleOutput)
|
2019-04-20 18:59:28 +00:00
|
|
|
|
2019-05-27 23:58:05 +00:00
|
|
|
s.Emit(server.StatusEvent, s.State)
|
2019-05-28 01:03:37 +00:00
|
|
|
if s.State != server.ProcessOfflineState {
|
|
|
|
handler.HandleInbound(WebsocketMessage{inbound: true, Event: SendServerLogsEvent})
|
|
|
|
}
|
2019-05-27 23:58:05 +00:00
|
|
|
|
2019-04-20 06:29:52 +00:00
|
|
|
for {
|
2019-04-21 00:38:12 +00:00
|
|
|
j := WebsocketMessage{inbound: true}
|
2019-04-07 23:28:01 +00:00
|
|
|
|
2019-04-20 22:45:48 +00:00
|
|
|
if _, _, err := c.ReadMessage(); err != nil {
|
2019-04-21 00:38:12 +00:00
|
|
|
if !websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway, websocket.CloseNoStatusReceived, websocket.CloseServiceRestart) {
|
2019-04-20 22:45:48 +00:00
|
|
|
zap.S().Errorw("error handling websocket message", zap.Error(err))
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-04-20 06:29:52 +00:00
|
|
|
// Discard and JSON parse errors into the void and don't continue processing this
|
|
|
|
// specific socket request. If we did a break here the client would get disconnected
|
|
|
|
// from the socket, which is NOT what we want to do.
|
|
|
|
if err := c.ReadJSON(&j); err != nil {
|
2019-04-20 22:45:48 +00:00
|
|
|
continue
|
2019-04-20 06:29:52 +00:00
|
|
|
}
|
2019-04-07 23:28:01 +00:00
|
|
|
|
2019-04-21 00:38:12 +00:00
|
|
|
if err := handler.HandleInbound(j); err != nil {
|
2019-04-20 21:57:37 +00:00
|
|
|
zap.S().Warnw("error handling inbound websocket request", zap.Error(err))
|
2019-04-20 18:59:28 +00:00
|
|
|
break
|
2019-05-28 01:03:37 +00:00
|
|
|
} else {
|
|
|
|
zap.S().Debugw("handled event", zap.String("event", j.Event), zap.Strings("args", j.Args))
|
2019-04-20 06:29:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-21 00:38:12 +00:00
|
|
|
}
|
2019-04-20 21:57:37 +00:00
|
|
|
|
2019-04-21 00:38:12 +00:00
|
|
|
// Perform a blocking send operation on the websocket since we want to avoid any
|
|
|
|
// concurrent writes to the connection, which would cause a runtime panic and cause
|
|
|
|
// the program to crash out.
|
|
|
|
func (wsh *WebsocketHandler) SendJson(v interface{}) error {
|
|
|
|
wsh.Mutex.Lock()
|
|
|
|
defer wsh.Mutex.Unlock()
|
|
|
|
|
|
|
|
return wsh.Connection.WriteJSON(v)
|
2019-04-20 06:29:52 +00:00
|
|
|
}
|
2019-04-20 21:57:37 +00:00
|
|
|
|
2019-04-21 00:38:12 +00:00
|
|
|
// Handle the inbound socket request and route it to the proper server action.
|
|
|
|
func (wsh *WebsocketHandler) HandleInbound(m WebsocketMessage) error {
|
|
|
|
if !m.inbound {
|
2019-04-20 21:57:37 +00:00
|
|
|
return errors.New("cannot handle websocket message, not an inbound connection")
|
|
|
|
}
|
|
|
|
|
2019-04-21 00:38:12 +00:00
|
|
|
switch m.Event {
|
2019-04-21 19:02:28 +00:00
|
|
|
case SetStateEvent:
|
2019-04-20 21:57:37 +00:00
|
|
|
{
|
|
|
|
var err error
|
2019-04-21 00:38:12 +00:00
|
|
|
switch strings.Join(m.Args, "") {
|
2019-04-20 21:57:37 +00:00
|
|
|
case "start":
|
2019-04-21 00:38:12 +00:00
|
|
|
err = wsh.Server.Environment.Start()
|
2019-04-20 21:57:37 +00:00
|
|
|
break
|
|
|
|
case "stop":
|
2019-04-21 00:38:12 +00:00
|
|
|
err = wsh.Server.Environment.Stop()
|
2019-04-20 21:57:37 +00:00
|
|
|
break
|
|
|
|
case "restart":
|
2019-04-21 19:27:53 +00:00
|
|
|
break
|
|
|
|
case "kill":
|
2019-04-21 00:38:12 +00:00
|
|
|
err = wsh.Server.Environment.Terminate(os.Kill)
|
2019-04-20 21:57:37 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-04-21 19:02:28 +00:00
|
|
|
case SendServerLogsEvent:
|
2019-04-20 21:57:37 +00:00
|
|
|
{
|
2019-05-28 01:03:37 +00:00
|
|
|
logs, err := wsh.Server.Environment.Readlog(1024 * 16)
|
2019-04-20 21:57:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, line := range logs {
|
2019-04-21 00:38:12 +00:00
|
|
|
wsh.SendJson(&WebsocketMessage{
|
2019-05-27 23:58:05 +00:00
|
|
|
Event: server.ConsoleOutputEvent,
|
2019-04-21 00:38:12 +00:00
|
|
|
Args: []string{line},
|
2019-04-20 21:57:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-21 19:02:28 +00:00
|
|
|
case SendCommandEvent:
|
2019-04-20 21:57:37 +00:00
|
|
|
{
|
2019-04-21 00:38:12 +00:00
|
|
|
return wsh.Server.Environment.SendCommand(strings.Join(m.Args, ""))
|
2019-04-20 21:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-04-21 00:38:12 +00:00
|
|
|
}
|