From 3bfe1443f0bf6d5096caa2e300007c54cd516342 Mon Sep 17 00:00:00 2001 From: Eccentric Date: Sun, 28 Jan 2024 22:04:40 +0000 Subject: [PATCH] Reimplement jabber sockets --- aid/fiber.go | 3 +-- aid/syncer.go | 2 +- go.mod | 5 +++- go.sum | 8 +++++++ handlers/common.go | 4 ++++ handlers/socket.go | 47 ++++++++++++++++++++++++++++++++++++ main.go | 5 ++-- socket/jabber.go | 12 ++++++++++ socket/socket.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++ storage/hotfix.go | 4 ++-- 10 files changed, 142 insertions(+), 8 deletions(-) create mode 100644 handlers/socket.go create mode 100644 socket/jabber.go create mode 100644 socket/socket.go diff --git a/aid/fiber.go b/aid/fiber.go index 610bd8c..7d87194 100644 --- a/aid/fiber.go +++ b/aid/fiber.go @@ -1,7 +1,6 @@ package aid import ( - "strings" "time" "github.com/gofiber/fiber/v2" @@ -14,7 +13,7 @@ func FiberLogger() fiber.Handler { return logger.New(logger.Config{ Format: "(${method}) (${status}) (${latency}) ${path}\n", Next: func(c *fiber.Ctx) bool { - return strings.Contains(c.Path(), "//") + return c.Response().StatusCode() == 302 }, }) } diff --git a/aid/syncer.go b/aid/syncer.go index ffbe3f3..94ec55a 100644 --- a/aid/syncer.go +++ b/aid/syncer.go @@ -8,7 +8,7 @@ type GenericSyncMap[T any] struct { m sync.Map } -func (s *GenericSyncMap[T]) Add(key string, value *T) { +func (s *GenericSyncMap[T]) Set(key string, value *T) { s.m.Store(key, value) } diff --git a/go.mod b/go.mod index eddbab1..bed7af3 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21.3 require ( github.com/bwmarrin/discordgo v0.27.1 github.com/goccy/go-json v0.10.2 + github.com/gofiber/contrib/websocket v1.3.0 github.com/gofiber/fiber/v2 v2.51.0 github.com/golang-jwt/jwt/v5 v5.2.0 github.com/google/uuid v1.4.0 @@ -18,6 +19,7 @@ require ( require ( github.com/andybalholm/brotli v1.0.6 // indirect + github.com/fasthttp/websocket v1.5.7 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect @@ -30,7 +32,7 @@ require ( github.com/mattn/go-runewidth v0.0.15 // indirect github.com/philhofer/fwd v1.1.2 // indirect github.com/rivo/uniseg v0.4.4 // indirect - github.com/stretchr/testify v1.8.4 // indirect + github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect github.com/tinylib/msgp v1.1.8 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.51.0 // indirect @@ -38,6 +40,7 @@ require ( github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect golang.org/x/crypto v0.15.0 // indirect + golang.org/x/net v0.18.0 // indirect golang.org/x/sys v0.14.0 // indirect golang.org/x/text v0.14.0 // indirect ) diff --git a/go.sum b/go.sum index 8a5ab62..9c410d2 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,12 @@ github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fasthttp/websocket v1.5.7 h1:0a6o2OfeATvtGgoMKleURhLT6JqWPg7fYfWnH4KHau4= +github.com/fasthttp/websocket v1.5.7/go.mod h1:bC4fxSono9czeXHQUVKxsC0sNjbm7lPJR04GDFqClfU= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/gofiber/contrib/websocket v1.3.0 h1:XADFAGorer1VJ1bqC4UkCjqS37kwRTV0415+050NrMk= +github.com/gofiber/contrib/websocket v1.3.0/go.mod h1:xguaOzn2ZZ759LavtosEP+rcxIgBEE/rdumPINhR+Xo= github.com/gofiber/fiber/v2 v2.51.0 h1:JNACcZy5e2tGApWB2QrRpenTWn0fq0hkFm6k0C86gKQ= github.com/gofiber/fiber/v2 v2.51.0/go.mod h1:xaQRZQJGqnKOQnbQw+ltvku3/h8QxvNi8o6JiJ7Ll0U= github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw= @@ -47,6 +51,8 @@ github.com/r3labs/diff/v3 v3.0.1/go.mod h1:f1S9bourRbiM66NskseyUdo0fTmEE0qKrikYJ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1AvpV+7XmhI4r39LGNzwUL4YpMuL5vk= +github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -77,6 +83,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= +golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= +golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/handlers/common.go b/handlers/common.go index 7c263b8..cc420ad 100644 --- a/handlers/common.go +++ b/handlers/common.go @@ -5,6 +5,10 @@ import ( "github.com/gofiber/fiber/v2" ) +func RedirectSocket(c *fiber.Ctx) error { + return c.Redirect("/socket") +} + func AnyNoContent(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusNoContent) } diff --git a/handlers/socket.go b/handlers/socket.go new file mode 100644 index 0000000..c81ec47 --- /dev/null +++ b/handlers/socket.go @@ -0,0 +1,47 @@ +package handlers + +import ( + "github.com/ectrc/snow/aid" + "github.com/ectrc/snow/socket" + "github.com/gofiber/contrib/websocket" + "github.com/gofiber/fiber/v2" + "github.com/google/uuid" +) + +func MiddlewareWebsocket(c *fiber.Ctx) error { + if !websocket.IsWebSocketUpgrade(c) { + return fiber.ErrUpgradeRequired + } + + var identifier string + var protocol string + + switch c.Get("Sec-WebSocket-Protocol") { + case "xmpp": + identifier = c.Get("Sec-WebSocket-Key") + protocol = "jabber" + default: + protocol = "matchmaking" + identifier = uuid.New().String() + } + + c.Locals("identifier", identifier) + c.Locals("protocol", protocol) + + return c.Next() +} + +func WebsocketConnection(c *websocket.Conn) { + protocol := c.Locals("protocol").(string) + identifier := c.Locals("identifier").(string) + + switch protocol { + case "jabber": + socket.JabberSockets.Set(identifier, socket.NewJabberSocket(c, identifier, socket.JabberData{})) + socket.HandleNewJabberSocket(identifier) + case "matchmaking": + // socket.MatchmakerSockets.Set(identifier, socket.NewMatchmakerSocket(c, socket.MatchmakerData{})) + default: + aid.Print("Invalid protocol: " + protocol) + } +} \ No newline at end of file diff --git a/main.go b/main.go index 83a3ebc..23c3a05 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "github.com/ectrc/snow/storage" "github.com/goccy/go-json" + "github.com/gofiber/contrib/websocket" "github.com/gofiber/fiber/v2" ) @@ -67,8 +68,8 @@ func main() { r.Get("/api/v1/search/:accountId", handlers.GetPersonSearch) r.Post("/api/v1/assets/Fortnite/:versionId/:assetName", handlers.PostAssets) - r.Get("//", func(c *fiber.Ctx) error { return c.Redirect("/socket") }) - // r.Get("/socket", handlers.MiddlewareWebsocket, websocket.New(handlers.WebsocketConnection)) + r.Get("/", handlers.RedirectSocket) + r.Get("/socket", handlers.MiddlewareWebsocket, websocket.New(handlers.WebsocketConnection)) account := r.Group("/account/api") account.Get("/public/account", handlers.GetPublicAccounts) diff --git a/socket/jabber.go b/socket/jabber.go new file mode 100644 index 0000000..64a7eb5 --- /dev/null +++ b/socket/jabber.go @@ -0,0 +1,12 @@ +package socket + +import "github.com/ectrc/snow/aid" + +func HandleNewJabberSocket(identifier string) { + _, ok := JabberSockets.Get(identifier) + if !ok { + return + } + + aid.Print("New jabber socket: " + identifier) +} \ No newline at end of file diff --git a/socket/socket.go b/socket/socket.go new file mode 100644 index 0000000..6696f5d --- /dev/null +++ b/socket/socket.go @@ -0,0 +1,60 @@ +package socket + +import ( + "github.com/ectrc/snow/aid" + "github.com/ectrc/snow/person" + "github.com/gofiber/contrib/websocket" + "github.com/google/uuid" +) + +type SocketType int + +var ( + SocketTypeJabber SocketType = 1 + SocketTypeMatchmaking SocketType = 2 +) + +type JabberData struct { + JabberID string +} + +type MatchmakerData struct { + PlaylistID string + Region string +} + +type data interface { + JabberData | MatchmakerData +} + +type Socket[T data] struct { + ID string + Connection *websocket.Conn + Data *T + Person *person.Person +} + +func newSocket[T data](conn *websocket.Conn, data ...T) *Socket[T] { + additional := data[0] + + return &Socket[T]{ + ID: uuid.New().String(), + Connection: conn, + Data: &additional, + } +} + +func NewJabberSocket(conn *websocket.Conn, id string, data JabberData) *Socket[JabberData] { + socket := newSocket[JabberData](conn, data) + socket.ID = id + return socket +} + +func NewMatchmakerSocket(conn *websocket.Conn, data MatchmakerData) *Socket[MatchmakerData] { + return newSocket[MatchmakerData](conn, data) +} + +var ( + JabberSockets = aid.GenericSyncMap[Socket[JabberData]]{} + MatchmakerSockets = aid.GenericSyncMap[Socket[MatchmakerData]]{} +) \ No newline at end of file diff --git a/storage/hotfix.go b/storage/hotfix.go index 60b9186..e50ead2 100644 --- a/storage/hotfix.go +++ b/storage/hotfix.go @@ -9,12 +9,12 @@ func GetDefaultEngine() []byte { [OnlineSubsystemMcp.Xmpp] bUseSSL=false Protocol=ws -ServerAddr="ws://`+ aid.Config.API.Host + aid.Config.API.Port +`" +ServerAddr="ws://`+ aid.Config.API.Host + aid.Config.API.Port +`/?" [OnlineSubsystemMcp.Xmpp Prod] bUseSSL=false Protocol=ws -ServerAddr="ws://`+ aid.Config.API.Host + aid.Config.API.Port +`" +ServerAddr="ws://`+ aid.Config.API.Host + aid.Config.API.Port +`/?" [OnlineSubsystemMcp] bUsePartySystemV2=false