with '#' will be ignored, and an empty message aborts the commit. Author: Ethan Alicea <64653625+Tech-Gamer@users.noreply.github.com> On branch develop Your branch is up to date with 'origin/develop'. Changes to be committed: modified: .github/workflows/push.yaml modified: .github/workflows/release.yaml modified: CHANGELOG.md modified: Dockerfile modified: Makefile modified: README.md modified: cmd/configure.go modified: cmd/diagnostics.go modified: cmd/root.go modified: config/config.go modified: environment/allocations.go modified: environment/docker.go modified: environment/docker/api.go modified: environment/docker/container.go modified: environment/docker/environment.go modified: environment/docker/power.go modified: environment/docker/stats.go modified: environment/environment.go modified: environment/settings.go modified: events/events.go modified: go.mod modified: internal/cron/activity_cron.go modified: internal/cron/cron.go modified: internal/cron/sftp_cron.go modified: internal/database/database.go modified: internal/progress/progress.go modified: internal/progress/progress_test.go modified: loggers/cli/cli.go new file: oryxBuildBinary modified: parser/parser.go modified: remote/http.go modified: remote/servers.go modified: remote/types.go modified: router/downloader/downloader.go modified: router/middleware.go modified: router/middleware/middleware.go modified: router/middleware/request_error.go modified: router/router.go modified: router/router_download.go modified: router/router_server.go modified: router/router_server_backup.go modified: router/router_server_files.go modified: router/router_server_transfer.go modified: router/router_server_ws.go modified: router/router_system.go modified: router/router_transfer.go modified: router/tokens/parser.go modified: router/websocket/listeners.go modified: router/websocket/websocket.go modified: server/activity.go modified: server/backup.go modified: server/backup/backup.go modified: server/backup/backup_local.go modified: server/backup/backup_s3.go modified: server/configuration.go modified: server/console.go modified: server/crash.go modified: server/events.go modified: server/filesystem/archive.go modified: server/filesystem/filesystem.go modified: server/filesystem/filesystem_test.go modified: server/install.go modified: server/installer/installer.go modified: server/listeners.go modified: server/manager.go modified: server/mounts.go modified: server/power.go modified: server/power_test.go modified: server/resources.go modified: server/server.go modified: server/transfer/archive.go modified: server/transfer/source.go modified: server/transfer/transfer.go modified: server/update.go modified: sftp/event.go modified: sftp/handler.go modified: sftp/server.go modified: wings.go
91 lines
2.7 KiB
Go
91 lines
2.7 KiB
Go
package router
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/goccy/go-json"
|
|
ws "github.com/gorilla/websocket"
|
|
|
|
"github.com/Tech-Gamer/nwy-wings/router/middleware"
|
|
"github.com/Tech-Gamer/nwy-wings/router/websocket"
|
|
)
|
|
|
|
var expectedCloseCodes = []int{
|
|
ws.CloseGoingAway,
|
|
ws.CloseAbnormalClosure,
|
|
ws.CloseNormalClosure,
|
|
ws.CloseNoStatusReceived,
|
|
ws.CloseServiceRestart,
|
|
}
|
|
|
|
// Upgrades a connection to a websocket and passes events along between.
|
|
func getServerWebsocket(c *gin.Context) {
|
|
manager := middleware.ExtractManager(c)
|
|
s, _ := manager.Get(c.Param("server"))
|
|
|
|
// Create a context that can be canceled when the user disconnects from this
|
|
// socket that will also cancel listeners running in separate threads. If the
|
|
// connection itself is terminated listeners using this context will also be
|
|
// closed.
|
|
ctx, cancel := context.WithCancel(c.Request.Context())
|
|
defer cancel()
|
|
|
|
handler, err := websocket.GetHandler(s, c.Writer, c.Request, c)
|
|
if err != nil {
|
|
middleware.CaptureAndAbort(c, err)
|
|
return
|
|
}
|
|
defer handler.Connection.Close()
|
|
|
|
// Track this open connection on the server so that we can close them all programmatically
|
|
// if the server is deleted.
|
|
s.Websockets().Push(handler.Uuid(), &cancel)
|
|
handler.Logger().Debug("opening connection to server websocket")
|
|
|
|
defer func() {
|
|
s.Websockets().Remove(handler.Uuid())
|
|
handler.Logger().Debug("closing connection to server websocket")
|
|
}()
|
|
|
|
// If the server is deleted we need to send a close message to the connected client
|
|
// so that they disconnect since there will be no more events sent along. Listen for
|
|
// the request context being closed to break this loop, otherwise this routine will
|
|
// be left hanging in the background.
|
|
go func() {
|
|
select {
|
|
case <-ctx.Done():
|
|
break
|
|
case <-s.Context().Done():
|
|
_ = handler.Connection.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(ws.CloseGoingAway, "server deleted"), time.Now().Add(time.Second*5))
|
|
break
|
|
}
|
|
}()
|
|
|
|
for {
|
|
j := websocket.Message{}
|
|
|
|
_, p, err := handler.Connection.ReadMessage()
|
|
if err != nil {
|
|
if ws.IsUnexpectedCloseError(err, expectedCloseCodes...) {
|
|
handler.Logger().WithField("error", err).Warn("error handling websocket message for server")
|
|
}
|
|
break
|
|
}
|
|
|
|
// 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 := json.Unmarshal(p, &j); err != nil {
|
|
continue
|
|
}
|
|
|
|
go func(msg websocket.Message) {
|
|
if err := handler.HandleInbound(ctx, msg); err != nil {
|
|
_ = handler.SendErrorJson(msg, err)
|
|
}
|
|
}(j)
|
|
}
|
|
}
|