2020-04-06 01:00:33 +00:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2020-08-01 22:20:39 +00:00
|
|
|
"context"
|
2020-05-22 17:01:27 +00:00
|
|
|
"encoding/json"
|
2020-04-06 01:00:33 +00:00
|
|
|
"fmt"
|
2020-06-13 17:26:35 +00:00
|
|
|
"github.com/apex/log"
|
2020-04-06 01:00:33 +00:00
|
|
|
"github.com/gbrlsnchs/jwt/v3"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/pterodactyl/wings/config"
|
2020-08-20 01:58:48 +00:00
|
|
|
"github.com/pterodactyl/wings/environment"
|
2020-04-06 01:44:16 +00:00
|
|
|
"github.com/pterodactyl/wings/router/tokens"
|
2020-04-06 01:00:33 +00:00
|
|
|
"github.com/pterodactyl/wings/server"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-04-17 17:21:33 +00:00
|
|
|
PermissionConnect = "websocket.connect"
|
2020-04-07 04:03:39 +00:00
|
|
|
PermissionSendCommand = "control.console"
|
|
|
|
PermissionSendPowerStart = "control.start"
|
|
|
|
PermissionSendPowerStop = "control.stop"
|
|
|
|
PermissionSendPowerRestart = "control.restart"
|
2020-04-17 17:21:33 +00:00
|
|
|
PermissionReceiveErrors = "admin.websocket.errors"
|
|
|
|
PermissionReceiveInstall = "admin.websocket.install"
|
2020-04-07 04:03:39 +00:00
|
|
|
PermissionReceiveBackups = "backup.read"
|
2020-04-06 01:00:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
2020-04-11 00:21:55 +00:00
|
|
|
sync.RWMutex
|
2020-04-06 01:00:33 +00:00
|
|
|
Connection *websocket.Conn
|
2020-04-11 00:06:59 +00:00
|
|
|
jwt *tokens.WebsocketPayload `json:"-"`
|
2020-04-06 01:00:33 +00:00
|
|
|
server *server.Server
|
|
|
|
}
|
|
|
|
|
2020-04-06 01:44:16 +00:00
|
|
|
// Parses a JWT into a websocket token payload.
|
|
|
|
func NewTokenPayload(token []byte) (*tokens.WebsocketPayload, error) {
|
|
|
|
payload := tokens.WebsocketPayload{}
|
|
|
|
err := tokens.ParseToken(token, &payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !payload.HasPermission(PermissionConnect) {
|
|
|
|
return nil, errors.New("not authorized to connect to this socket")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &payload, nil
|
|
|
|
}
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
// Returns a new websocket handler using the context provided.
|
|
|
|
func GetHandler(s *server.Server, w http.ResponseWriter, r *http.Request) (*Handler, error) {
|
|
|
|
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 {
|
2020-07-31 22:19:09 +00:00
|
|
|
o := r.Header.Get("Origin")
|
|
|
|
if o == config.Get().PanelLocation {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, origin := range config.Get().AllowedOrigins {
|
2020-09-11 03:08:00 +00:00
|
|
|
if origin == "*" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-07-31 22:19:09 +00:00
|
|
|
if o != origin {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2020-04-06 01:00:33 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Handler{
|
|
|
|
Connection: conn,
|
2020-04-11 00:06:59 +00:00
|
|
|
jwt: nil,
|
2020-04-06 01:00:33 +00:00
|
|
|
server: s,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) SendJson(v *Message) error {
|
|
|
|
// Do not send JSON down the line if the JWT on the connection is not
|
|
|
|
// valid!
|
|
|
|
if err := h.TokenValid(); err != nil {
|
2020-09-13 05:12:23 +00:00
|
|
|
h.SendErrorJson(*v, err)
|
2020-04-06 01:00:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-11 00:06:59 +00:00
|
|
|
j := h.GetJwt()
|
|
|
|
if j != nil {
|
|
|
|
// If we're sending installation output but the user does not have the required
|
|
|
|
// permissions to see the output, don't send it down the line.
|
|
|
|
if v.Event == server.InstallOutputEvent {
|
|
|
|
if !j.HasPermission(PermissionReceiveInstall) {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 00:06:59 +00:00
|
|
|
// If the user does not have permission to see backup events, do not emit
|
|
|
|
// them over the socket.
|
|
|
|
if strings.HasPrefix(v.Event, server.BackupCompletedEvent) {
|
|
|
|
if !j.HasPermission(PermissionReceiveBackups) {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-07 04:03:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
return h.unsafeSendJson(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sends JSON over the websocket connection, ignoring the authentication state of the
|
|
|
|
// socket user. Do not call this directly unless you are positive a response should be
|
|
|
|
// sent back to the client!
|
|
|
|
func (h *Handler) unsafeSendJson(v interface{}) error {
|
2020-04-11 00:06:59 +00:00
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
2020-04-06 01:00:33 +00:00
|
|
|
|
|
|
|
return h.Connection.WriteJSON(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if the JWT is still valid.
|
|
|
|
func (h *Handler) TokenValid() error {
|
2020-04-11 00:06:59 +00:00
|
|
|
j := h.GetJwt()
|
|
|
|
if j == nil {
|
2020-04-06 01:00:33 +00:00
|
|
|
return errors.New("no jwt present")
|
|
|
|
}
|
|
|
|
|
2020-04-11 00:06:59 +00:00
|
|
|
if err := jwt.ExpirationTimeValidator(time.Now())(&j.Payload); err != nil {
|
2020-04-06 01:00:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-11 00:06:59 +00:00
|
|
|
if !j.HasPermission(PermissionConnect) {
|
2020-04-06 01:00:33 +00:00
|
|
|
return errors.New("jwt does not have connect permission")
|
|
|
|
}
|
|
|
|
|
2020-07-18 23:03:25 +00:00
|
|
|
if h.server.Id() != j.GetServerUuid() {
|
2020-04-06 01:00:33 +00:00
|
|
|
return errors.New("jwt server uuid mismatch")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sends an error back to the connected websocket instance by checking the permissions
|
|
|
|
// of the token. If the user has the "receive-errors" grant we will send back the actual
|
|
|
|
// error message, otherwise we just send back a standard error message.
|
2020-08-01 22:20:39 +00:00
|
|
|
func (h *Handler) SendErrorJson(msg Message, err error, shouldLog ...bool) error {
|
2020-04-11 00:06:59 +00:00
|
|
|
j := h.GetJwt()
|
2020-09-12 06:11:57 +00:00
|
|
|
expected := errors.Is(err, server.ErrSuspended) || errors.Is(err, server.ErrIsRunning)
|
2020-04-06 01:00:33 +00:00
|
|
|
|
|
|
|
message := "an unexpected error was encountered while handling this request"
|
2020-09-12 06:11:57 +00:00
|
|
|
if expected || (j != nil && j.HasPermission(PermissionReceiveErrors)) {
|
2020-04-11 00:06:59 +00:00
|
|
|
message = err.Error()
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m, u := h.GetErrorMessage(message)
|
|
|
|
|
|
|
|
wsm := Message{Event: ErrorEvent}
|
|
|
|
wsm.Args = []string{m}
|
|
|
|
|
2020-08-01 22:20:39 +00:00
|
|
|
if len(shouldLog) == 0 || (len(shouldLog) == 1 && shouldLog[0] == true) {
|
2020-09-12 06:11:57 +00:00
|
|
|
if !expected {
|
2020-08-01 22:20:39 +00:00
|
|
|
h.server.Log().WithFields(log.Fields{"event": msg.Event, "error_identifier": u.String(), "error": err}).
|
|
|
|
Error("failed to handle websocket process; an error was encountered processing an event")
|
|
|
|
}
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 02:52:47 +00:00
|
|
|
return h.unsafeSendJson(wsm)
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Converts an error message into a more readable representation and returns a UUID
|
|
|
|
// that can be cross-referenced to find the specific error that triggered.
|
|
|
|
func (h *Handler) GetErrorMessage(msg string) (string, uuid.UUID) {
|
|
|
|
u := uuid.Must(uuid.NewRandom())
|
|
|
|
|
|
|
|
m := fmt.Sprintf("Error Event [%s]: %s", u.String(), msg)
|
|
|
|
|
|
|
|
return m, u
|
|
|
|
}
|
|
|
|
|
2020-04-11 00:06:59 +00:00
|
|
|
// Sets the JWT for the websocket in a race-safe manner.
|
|
|
|
func (h *Handler) setJwt(token *tokens.WebsocketPayload) {
|
|
|
|
h.Lock()
|
|
|
|
h.jwt = token
|
|
|
|
h.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) GetJwt() *tokens.WebsocketPayload {
|
2020-04-11 00:21:55 +00:00
|
|
|
h.RLock()
|
|
|
|
defer h.RUnlock()
|
2020-04-11 00:06:59 +00:00
|
|
|
|
|
|
|
return h.jwt
|
|
|
|
}
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
// Handle the inbound socket request and route it to the proper server action.
|
|
|
|
func (h *Handler) HandleInbound(m Message) error {
|
|
|
|
if m.Event != AuthenticationEvent {
|
|
|
|
if err := h.TokenValid(); err != nil {
|
2020-06-13 17:26:35 +00:00
|
|
|
log.WithField("message", err.Error()).Debug("jwt for server websocket is no longer valid")
|
2020-04-06 01:00:33 +00:00
|
|
|
|
|
|
|
h.unsafeSendJson(Message{
|
|
|
|
Event: ErrorEvent,
|
|
|
|
Args: []string{"could not authenticate client: " + err.Error()},
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch m.Event {
|
|
|
|
case AuthenticationEvent:
|
|
|
|
{
|
2020-04-06 01:44:16 +00:00
|
|
|
token, err := NewTokenPayload([]byte(strings.Join(m.Args, "")))
|
2020-04-06 01:00:33 +00:00
|
|
|
if err != nil {
|
2020-04-10 21:39:07 +00:00
|
|
|
// If the error says the JWT expired, send a token expired
|
|
|
|
// event and hopefully the client renews the token.
|
|
|
|
if err == jwt.ErrExpValidation {
|
|
|
|
h.SendJson(&Message{Event: TokenExpiredEvent})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-10 21:00:59 +00:00
|
|
|
// Check if the user has previously authenticated successfully.
|
|
|
|
newConnection := h.GetJwt() == nil
|
|
|
|
|
|
|
|
// Previously there was a HasPermission(PermissionConnect) check around this,
|
|
|
|
// however NewTokenPayload will return an error if it doesn't have the connect
|
|
|
|
// permission meaning that it was a redundant function call.
|
|
|
|
h.setJwt(token)
|
2020-04-06 01:00:33 +00:00
|
|
|
|
2020-06-10 21:00:59 +00:00
|
|
|
// Tell the client they authenticated successfully.
|
2020-04-06 01:00:33 +00:00
|
|
|
h.unsafeSendJson(Message{
|
|
|
|
Event: AuthenticationSuccessEvent,
|
|
|
|
Args: []string{},
|
|
|
|
})
|
|
|
|
|
2020-06-10 21:00:59 +00:00
|
|
|
// Check if the client was refreshing their authentication token
|
|
|
|
// instead of authenticating for the first time.
|
|
|
|
if !newConnection {
|
|
|
|
// This prevents duplicate status messages as outlined in
|
|
|
|
// https://github.com/pterodactyl/panel/issues/2077
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-22 17:01:27 +00:00
|
|
|
// On every authentication event, send the current server status back
|
|
|
|
// to the client. :)
|
|
|
|
state := h.server.GetState()
|
|
|
|
h.SendJson(&Message{
|
|
|
|
Event: server.StatusEvent,
|
|
|
|
Args: []string{state},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Only send the current disk usage if the server is offline, if docker container is running,
|
|
|
|
// Environment#EnableResourcePolling() will send this data to all clients.
|
2020-08-20 01:58:48 +00:00
|
|
|
if state == environment.ProcessOfflineState {
|
2020-09-01 03:27:41 +00:00
|
|
|
_ = h.server.Filesystem.HasSpaceAvailable(false)
|
2020-05-22 17:01:27 +00:00
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
b, _ := json.Marshal(h.server.Proc())
|
2020-05-22 17:01:27 +00:00
|
|
|
h.SendJson(&Message{
|
|
|
|
Event: server.StatsEvent,
|
|
|
|
Args: []string{string(b)},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case SetStateEvent:
|
|
|
|
{
|
2020-08-01 22:20:39 +00:00
|
|
|
action := server.PowerAction(strings.Join(m.Args, ""))
|
|
|
|
|
|
|
|
actions := make(map[server.PowerAction]string)
|
|
|
|
actions[server.PowerActionStart] = PermissionSendPowerStart
|
|
|
|
actions[server.PowerActionStop] = PermissionSendPowerStop
|
|
|
|
actions[server.PowerActionRestart] = PermissionSendPowerRestart
|
|
|
|
actions[server.PowerActionTerminate] = PermissionSendPowerStop
|
|
|
|
|
|
|
|
// Check that they have permission to perform this action if it is needed.
|
|
|
|
if permission, exists := actions[action]; exists {
|
|
|
|
if !h.GetJwt().HasPermission(permission) {
|
|
|
|
return nil
|
2020-04-07 04:03:39 +00:00
|
|
|
}
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
|
2020-08-01 22:20:39 +00:00
|
|
|
err := h.server.HandlePowerAction(action)
|
|
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
m, _ := h.GetErrorMessage("another power action is currently being processed for this server, please try again later")
|
|
|
|
|
|
|
|
h.SendJson(&Message{
|
|
|
|
Event: ErrorEvent,
|
|
|
|
Args: []string{m},
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
case SendServerLogsEvent:
|
|
|
|
{
|
|
|
|
if running, _ := h.server.Environment.IsRunning(); !running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-12 04:52:07 +00:00
|
|
|
logs, err := h.server.Environment.Readlog(100)
|
2020-04-06 01:00:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, line := range logs {
|
|
|
|
h.SendJson(&Message{
|
|
|
|
Event: server.ConsoleOutputEvent,
|
|
|
|
Args: []string{line},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case SendCommandEvent:
|
|
|
|
{
|
2020-04-11 00:06:59 +00:00
|
|
|
if !h.GetJwt().HasPermission(PermissionSendCommand) {
|
2020-04-06 01:00:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-20 01:58:48 +00:00
|
|
|
if h.server.GetState() == environment.ProcessOfflineState {
|
2020-04-06 01:00:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.server.Environment.SendCommand(strings.Join(m.Args, ""))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-04-07 04:03:39 +00:00
|
|
|
}
|