Reimplement jabber sockets

This commit is contained in:
Eccentric 2024-01-28 22:04:40 +00:00
parent efbd464bbb
commit 3bfe1443f0
10 changed files with 142 additions and 8 deletions

View File

@ -1,7 +1,6 @@
package aid package aid
import ( import (
"strings"
"time" "time"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
@ -14,7 +13,7 @@ func FiberLogger() fiber.Handler {
return logger.New(logger.Config{ return logger.New(logger.Config{
Format: "(${method}) (${status}) (${latency}) ${path}\n", Format: "(${method}) (${status}) (${latency}) ${path}\n",
Next: func(c *fiber.Ctx) bool { Next: func(c *fiber.Ctx) bool {
return strings.Contains(c.Path(), "//") return c.Response().StatusCode() == 302
}, },
}) })
} }

View File

@ -8,7 +8,7 @@ type GenericSyncMap[T any] struct {
m sync.Map 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) s.m.Store(key, value)
} }

5
go.mod
View File

@ -5,6 +5,7 @@ go 1.21.3
require ( require (
github.com/bwmarrin/discordgo v0.27.1 github.com/bwmarrin/discordgo v0.27.1
github.com/goccy/go-json v0.10.2 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/gofiber/fiber/v2 v2.51.0
github.com/golang-jwt/jwt/v5 v5.2.0 github.com/golang-jwt/jwt/v5 v5.2.0
github.com/google/uuid v1.4.0 github.com/google/uuid v1.4.0
@ -18,6 +19,7 @@ require (
require ( require (
github.com/andybalholm/brotli v1.0.6 // indirect 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/gorilla/websocket v1.4.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // 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/mattn/go-runewidth v0.0.15 // indirect
github.com/philhofer/fwd v1.1.2 // indirect github.com/philhofer/fwd v1.1.2 // indirect
github.com/rivo/uniseg v0.4.4 // 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/tinylib/msgp v1.1.8 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.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/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/crypto v0.15.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/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect golang.org/x/text v0.14.0 // indirect
) )

8
go.sum
View File

@ -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.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= 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 h1:JNACcZy5e2tGApWB2QrRpenTWn0fq0hkFm6k0C86gKQ=
github.com/gofiber/fiber/v2 v2.51.0/go.mod h1:xaQRZQJGqnKOQnbQw+ltvku3/h8QxvNi8o6JiJ7Ll0U= 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= 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.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= 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/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.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 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-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.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.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-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.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

View File

@ -5,6 +5,10 @@ import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func RedirectSocket(c *fiber.Ctx) error {
return c.Redirect("/socket")
}
func AnyNoContent(c *fiber.Ctx) error { func AnyNoContent(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusNoContent) return c.SendStatus(fiber.StatusNoContent)
} }

47
handlers/socket.go Normal file
View File

@ -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)
}
}

View File

@ -12,6 +12,7 @@ import (
"github.com/ectrc/snow/storage" "github.com/ectrc/snow/storage"
"github.com/goccy/go-json" "github.com/goccy/go-json"
"github.com/gofiber/contrib/websocket"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -67,8 +68,8 @@ func main() {
r.Get("/api/v1/search/:accountId", handlers.GetPersonSearch) r.Get("/api/v1/search/:accountId", handlers.GetPersonSearch)
r.Post("/api/v1/assets/Fortnite/:versionId/:assetName", handlers.PostAssets) r.Post("/api/v1/assets/Fortnite/:versionId/:assetName", handlers.PostAssets)
r.Get("//", func(c *fiber.Ctx) error { return c.Redirect("/socket") }) r.Get("/", handlers.RedirectSocket)
// r.Get("/socket", handlers.MiddlewareWebsocket, websocket.New(handlers.WebsocketConnection)) r.Get("/socket", handlers.MiddlewareWebsocket, websocket.New(handlers.WebsocketConnection))
account := r.Group("/account/api") account := r.Group("/account/api")
account.Get("/public/account", handlers.GetPublicAccounts) account.Get("/public/account", handlers.GetPublicAccounts)

12
socket/jabber.go Normal file
View File

@ -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)
}

60
socket/socket.go Normal file
View File

@ -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]]{}
)

View File

@ -9,12 +9,12 @@ func GetDefaultEngine() []byte {
[OnlineSubsystemMcp.Xmpp] [OnlineSubsystemMcp.Xmpp]
bUseSSL=false bUseSSL=false
Protocol=ws 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] [OnlineSubsystemMcp.Xmpp Prod]
bUseSSL=false bUseSSL=false
Protocol=ws Protocol=ws
ServerAddr="ws://`+ aid.Config.API.Host + aid.Config.API.Port +`" ServerAddr="ws://`+ aid.Config.API.Host + aid.Config.API.Port +`/?"
[OnlineSubsystemMcp] [OnlineSubsystemMcp]
bUsePartySystemV2=false bUsePartySystemV2=false