2020-04-06 01:00:33 +00:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2020-08-01 22:20:39 +00:00
|
|
|
"context"
|
2020-04-06 01:00:33 +00:00
|
|
|
"fmt"
|
2021-08-02 21:07:00 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-10-05 23:24:11 +00:00
|
|
|
"github.com/pterodactyl/wings/internal/models"
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
"emperror.dev/errors"
|
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"
|
2022-10-05 00:11:07 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2022-02-01 00:09:08 +00:00
|
|
|
"github.com/goccy/go-json"
|
2020-04-06 01:00:33 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/gorilla/websocket"
|
2022-10-06 15:58:42 +00:00
|
|
|
|
2022-02-01 00:09:08 +00:00
|
|
|
"github.com/pterodactyl/wings/system"
|
2021-08-02 21:07:00 +00:00
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
"github.com/pterodactyl/wings/config"
|
2020-08-20 01:58:48 +00:00
|
|
|
"github.com/pterodactyl/wings/environment"
|
2020-09-27 00:35:11 +00:00
|
|
|
"github.com/pterodactyl/wings/environment/docker"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-12-25 21:32:41 +00:00
|
|
|
PermissionReceiveTransfer = "admin.websocket.transfer"
|
2020-04-07 04:03:39 +00:00
|
|
|
PermissionReceiveBackups = "backup.read"
|
2020-04-06 01:00:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
2020-12-06 23:10:08 +00:00
|
|
|
sync.RWMutex `json:"-"`
|
2020-12-16 05:56:53 +00:00
|
|
|
Connection *websocket.Conn `json:"-"`
|
|
|
|
jwt *tokens.WebsocketPayload
|
|
|
|
server *server.Server
|
2022-07-09 21:51:19 +00:00
|
|
|
ra server.RequestActivity
|
2020-12-16 05:56:53 +00:00
|
|
|
uuid uuid.UUID
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 03:30:51 +00:00
|
|
|
var (
|
2020-11-28 23:57:10 +00:00
|
|
|
ErrJwtNotPresent = errors.New("jwt: no jwt present")
|
|
|
|
ErrJwtNoConnectPerm = errors.New("jwt: missing connect permission")
|
|
|
|
ErrJwtUuidMismatch = errors.New("jwt: server uuid mismatch")
|
|
|
|
ErrJwtOnDenylist = errors.New("jwt: created too far in past (denylist)")
|
2020-09-18 03:30:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func IsJwtError(err error) bool {
|
|
|
|
return errors.Is(err, ErrJwtNotPresent) ||
|
|
|
|
errors.Is(err, ErrJwtNoConnectPerm) ||
|
|
|
|
errors.Is(err, ErrJwtUuidMismatch) ||
|
2020-11-04 04:33:33 +00:00
|
|
|
errors.Is(err, ErrJwtOnDenylist) ||
|
2020-09-18 03:30:51 +00:00
|
|
|
errors.Is(err, jwt.ErrExpValidation)
|
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// NewTokenPayload parses a JWT into a websocket token payload.
|
2020-04-06 01:44:16 +00:00
|
|
|
func NewTokenPayload(token []byte) (*tokens.WebsocketPayload, error) {
|
2021-08-02 21:07:00 +00:00
|
|
|
var payload tokens.WebsocketPayload
|
|
|
|
if err := tokens.ParseToken(token, &payload); err != nil {
|
2020-04-06 01:44:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-04 04:33:33 +00:00
|
|
|
if payload.Denylisted() {
|
|
|
|
return nil, ErrJwtOnDenylist
|
|
|
|
}
|
|
|
|
|
2020-04-06 01:44:16 +00:00
|
|
|
if !payload.HasPermission(PermissionConnect) {
|
2020-11-04 04:33:33 +00:00
|
|
|
return nil, ErrJwtNoConnectPerm
|
2020-04-06 01:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &payload, nil
|
|
|
|
}
|
|
|
|
|
2021-10-24 23:14:00 +00:00
|
|
|
// GetHandler returns a new websocket handler using the context provided.
|
2022-10-05 00:11:07 +00:00
|
|
|
func GetHandler(s *server.Server, w http.ResponseWriter, r *http.Request, c *gin.Context) (*Handler, error) {
|
2020-04-06 01:00:33 +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 {
|
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-12-25 22:40:28 +00:00
|
|
|
if origin == "*" || origin == o {
|
2020-09-11 03:08:00 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-07-31 22:19:09 +00:00
|
|
|
}
|
|
|
|
return false
|
2020-04-06 01:00:33 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-04 03:46:29 +00:00
|
|
|
u, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return nil, err
|
2020-10-04 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
return &Handler{
|
|
|
|
Connection: conn,
|
2020-04-11 00:06:59 +00:00
|
|
|
jwt: nil,
|
2020-04-06 01:00:33 +00:00
|
|
|
server: s,
|
2022-10-05 00:11:07 +00:00
|
|
|
ra: s.NewRequestActivity("", c.ClientIP()),
|
2020-10-04 03:46:29 +00:00
|
|
|
uuid: u,
|
2020-04-06 01:00:33 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-04 03:46:29 +00:00
|
|
|
func (h *Handler) Uuid() uuid.UUID {
|
|
|
|
return h.uuid
|
|
|
|
}
|
|
|
|
|
2021-10-24 23:14:00 +00:00
|
|
|
func (h *Handler) Logger() *log.Entry {
|
|
|
|
return log.WithField("subsystem", "websocket").
|
|
|
|
WithField("connection", h.Uuid().String()).
|
|
|
|
WithField("server", h.server.ID())
|
|
|
|
}
|
|
|
|
|
2022-01-18 03:23:29 +00:00
|
|
|
func (h *Handler) SendJson(v Message) error {
|
2020-09-18 03:30:51 +00:00
|
|
|
// Do not send JSON down the line if the JWT on the connection is not valid!
|
2020-04-06 01:00:33 +00:00
|
|
|
if err := h.TokenValid(); err != nil {
|
2022-01-18 03:23:29 +00:00
|
|
|
_ = h.unsafeSendJson(Message{
|
2020-09-27 16:09:34 +00:00
|
|
|
Event: JwtErrorEvent,
|
|
|
|
Args: []string{err.Error()},
|
2020-09-18 03:30:51 +00:00
|
|
|
})
|
2020-04-06 01:00:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-18 03:23:29 +00:00
|
|
|
if j := h.GetJwt(); j != nil {
|
2020-04-11 00:06:59 +00:00
|
|
|
// 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-12-25 21:32:41 +00:00
|
|
|
|
|
|
|
// If we are sending transfer output, only send it to the user if they have the required permissions.
|
|
|
|
if v.Event == server.TransferLogsEvent {
|
|
|
|
if !j.HasPermission(PermissionReceiveTransfer) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 04:03:39 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 23:10:32 +00:00
|
|
|
if err := h.unsafeSendJson(v); err != nil {
|
|
|
|
// Not entirely sure how this happens (likely just when there is a ton of console spam)
|
|
|
|
// but I don't care to fix it right now, so just mask the error and throw a warning into
|
|
|
|
// the logs for us to look into later.
|
|
|
|
if errors.Is(err, websocket.ErrCloseSent) {
|
|
|
|
if h.server != nil {
|
|
|
|
h.server.Log().WithField("subsystem", "websocket").
|
|
|
|
WithField("event", v.Event).
|
|
|
|
Warn("failed to send event to websocket: close already sent")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// TokenValid checks if the JWT is still valid.
|
2020-04-06 01:00:33 +00:00
|
|
|
func (h *Handler) TokenValid() error {
|
2020-04-11 00:06:59 +00:00
|
|
|
j := h.GetJwt()
|
|
|
|
if j == nil {
|
2020-09-18 03:30:51 +00:00
|
|
|
return ErrJwtNotPresent
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
|
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-11-04 04:33:33 +00:00
|
|
|
if j.Denylisted() {
|
|
|
|
return ErrJwtOnDenylist
|
|
|
|
}
|
|
|
|
|
2020-04-11 00:06:59 +00:00
|
|
|
if !j.HasPermission(PermissionConnect) {
|
2020-09-18 03:30:51 +00:00
|
|
|
return ErrJwtNoConnectPerm
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
if h.server.ID() != j.GetServerUuid() {
|
2020-09-18 03:30:51 +00:00
|
|
|
return ErrJwtUuidMismatch
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// SendErrorJson sends an error back to the connected websocket instance by checking the permissions
|
2020-04-06 01:00:33 +00:00
|
|
|
// 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-11-04 04:33:33 +00:00
|
|
|
isJWTError := IsJwtError(err)
|
2020-04-06 01:00:33 +00:00
|
|
|
|
2020-11-04 04:33:33 +00:00
|
|
|
wsm := Message{
|
|
|
|
Event: ErrorEvent,
|
|
|
|
Args: []string{"an unexpected error was encountered while handling this request"},
|
|
|
|
}
|
2020-12-06 23:10:08 +00:00
|
|
|
|
2020-11-04 04:33:33 +00:00
|
|
|
if isJWTError || (j != nil && j.HasPermission(PermissionReceiveErrors)) {
|
2020-12-06 23:10:08 +00:00
|
|
|
if isJWTError {
|
|
|
|
wsm.Event = JwtErrorEvent
|
|
|
|
}
|
2020-11-04 04:33:33 +00:00
|
|
|
wsm.Args = []string{err.Error()}
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 04:33:33 +00:00
|
|
|
m, u := h.GetErrorMessage(wsm.Args[0])
|
2020-04-06 01:00:33 +00:00
|
|
|
wsm.Args = []string{m}
|
|
|
|
|
2020-11-04 04:33:33 +00:00
|
|
|
if !isJWTError && (len(shouldLog) == 0 || (len(shouldLog) == 1 && shouldLog[0] == true)) {
|
|
|
|
h.server.Log().WithFields(log.Fields{"event": msg.Event, "error_identifier": u.String(), "error": err}).
|
2020-12-06 23:10:08 +00:00
|
|
|
Errorf("error processing websocket event \"%s\"", msg.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
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// GetErrorMessage converts an error message into a more readable representation and returns a UUID
|
2020-04-06 01:00:33 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// GetJwt returns the JWT for the websocket in a race-safe manner.
|
2020-04-11 00:06:59 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// setJwt sets the JWT for the websocket in a race-safe manner.
|
|
|
|
func (h *Handler) setJwt(token *tokens.WebsocketPayload) {
|
|
|
|
h.Lock()
|
2022-07-09 21:51:19 +00:00
|
|
|
h.ra = h.ra.SetUser(token.UserUUID)
|
2021-08-02 21:07:00 +00:00
|
|
|
h.jwt = token
|
|
|
|
h.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleInbound handles an inbound socket request and route it to the proper action.
|
2021-10-26 04:23:45 +00:00
|
|
|
func (h *Handler) HandleInbound(ctx context.Context, m Message) error {
|
2020-04-06 01:00:33 +00:00
|
|
|
if m.Event != AuthenticationEvent {
|
|
|
|
if err := h.TokenValid(); err != nil {
|
|
|
|
h.unsafeSendJson(Message{
|
2020-09-27 16:09:34 +00:00
|
|
|
Event: JwtErrorEvent,
|
|
|
|
Args: []string{err.Error()},
|
2020-04-06 01:00:33 +00:00
|
|
|
})
|
|
|
|
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 {
|
|
|
|
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.
|
2022-01-18 03:23:29 +00:00
|
|
|
_ = h.unsafeSendJson(Message{Event: AuthenticationSuccessEvent})
|
2020-04-06 01:00:33 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-26 04:23:45 +00:00
|
|
|
// Now that we've authenticated with the token and confirmed that we're not
|
|
|
|
// reconnecting to the socket, register the event listeners for the server and
|
|
|
|
// the token expiration.
|
|
|
|
h.registerListenerEvents(ctx)
|
|
|
|
|
2020-05-22 17:01:27 +00:00
|
|
|
// On every authentication event, send the current server status back
|
|
|
|
// to the client. :)
|
2020-11-20 21:35:29 +00:00
|
|
|
state := h.server.Environment.State()
|
2022-01-18 03:23:29 +00:00
|
|
|
_ = h.SendJson(Message{
|
2020-05-22 17:01:27 +00:00
|
|
|
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-12-25 21:32:41 +00:00
|
|
|
if !h.server.IsInstalling() && !h.server.IsTransferring() {
|
|
|
|
_ = h.server.Filesystem().HasSpaceAvailable(false)
|
|
|
|
|
|
|
|
b, _ := json.Marshal(h.server.Proc())
|
2022-01-18 03:23:29 +00:00
|
|
|
_ = h.SendJson(Message{
|
2020-12-25 21:32:41 +00:00
|
|
|
Event: server.StatsEvent,
|
|
|
|
Args: []string{string(b)},
|
|
|
|
})
|
|
|
|
}
|
2020-05-22 17:01:27 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2022-02-01 00:09:08 +00:00
|
|
|
if errors.Is(err, system.ErrLockerLocked) {
|
2020-08-01 22:20:39 +00:00
|
|
|
m, _ := h.GetErrorMessage("another power action is currently being processed for this server, please try again later")
|
|
|
|
|
2022-01-18 03:23:29 +00:00
|
|
|
_ = h.SendJson(Message{
|
2020-08-01 22:20:39 +00:00
|
|
|
Event: ErrorEvent,
|
|
|
|
Args: []string{m},
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-09 21:51:19 +00:00
|
|
|
if err == nil {
|
2022-07-24 20:27:25 +00:00
|
|
|
h.server.SaveActivity(h.ra, models.Event(server.ActivityPowerPrefix+action), nil)
|
2022-07-09 21:51:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-01 22:20:39 +00:00
|
|
|
return err
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
case SendServerLogsEvent:
|
|
|
|
{
|
2021-11-15 17:37:56 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
2021-09-11 21:13:19 +00:00
|
|
|
defer cancel()
|
|
|
|
if running, _ := h.server.Environment.IsRunning(ctx); !running {
|
2020-04-06 01:00:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-07 04:56:21 +00:00
|
|
|
logs, err := h.server.Environment.Readlog(config.Get().System.WebsocketLogCount)
|
2020-04-06 01:00:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, line := range logs {
|
2022-01-18 03:23:29 +00:00
|
|
|
_ = h.SendJson(Message{
|
2020-04-06 01:00:33 +00:00
|
|
|
Event: server.ConsoleOutputEvent,
|
|
|
|
Args: []string{line},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-27 16:46:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case SendStatsEvent:
|
|
|
|
{
|
|
|
|
b, _ := json.Marshal(h.server.Proc())
|
2022-01-18 03:23:29 +00:00
|
|
|
_ = h.SendJson(Message{
|
2020-09-27 16:46:00 +00:00
|
|
|
Event: server.StatsEvent,
|
|
|
|
Args: []string{string(b)},
|
|
|
|
})
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
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-11-20 21:35:29 +00:00
|
|
|
if h.server.Environment.State() == environment.ProcessOfflineState {
|
2020-04-06 01:00:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-27 00:35:11 +00:00
|
|
|
// TODO(dane): should probably add a new process state that is "booting environment" or something
|
|
|
|
// so that we can better handle this and only set the environment to booted once we're attached.
|
|
|
|
//
|
|
|
|
// Or maybe just an IsBooted function?
|
2020-11-20 21:35:29 +00:00
|
|
|
if h.server.Environment.State() == environment.ProcessStartingState {
|
2020-09-27 00:35:11 +00:00
|
|
|
if e, ok := h.server.Environment.(*docker.Environment); ok {
|
|
|
|
if !e.IsAttached() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 20:27:25 +00:00
|
|
|
if err := h.server.Environment.SendCommand(strings.Join(m.Args, "")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
h.server.SaveActivity(h.ra, server.ActivityConsoleCommand, models.ActivityMeta{
|
2022-07-09 21:51:19 +00:00
|
|
|
"command": strings.Join(m.Args, ""),
|
|
|
|
})
|
2022-07-24 20:27:25 +00:00
|
|
|
return nil
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-04-07 04:03:39 +00:00
|
|
|
}
|