Continue to rework jabber sockets
This commit is contained in:
parent
3bfe1443f0
commit
f803490c41
26
aid/token.go
26
aid/token.go
|
@ -2,10 +2,13 @@ package aid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var JWTCompile = regexp.MustCompile(`eg1~(.*)`)
|
||||||
|
|
||||||
func JWTSign(m JSON) (string, error) {
|
func JWTSign(m JSON) (string, error) {
|
||||||
claims := jwt.MapClaims{}
|
claims := jwt.MapClaims{}
|
||||||
|
|
||||||
|
@ -18,6 +21,11 @@ func JWTSign(m JSON) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func JWTVerify(tokenString string) (JSON, error) {
|
func JWTVerify(tokenString string) (JSON, error) {
|
||||||
|
compiled := JWTCompile.FindStringSubmatch(tokenString)
|
||||||
|
if len(compiled) > 0 {
|
||||||
|
tokenString = compiled[1]
|
||||||
|
}
|
||||||
|
|
||||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||||
return []byte(Config.JWT.Secret), nil
|
return []byte(Config.JWT.Secret), nil
|
||||||
})
|
})
|
||||||
|
@ -42,3 +50,21 @@ func JWTVerify(tokenString string) (JSON, error) {
|
||||||
|
|
||||||
return json, nil
|
return json, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetSnowFromToken(token string) (string, error) {
|
||||||
|
claims, err := JWTVerify(token)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if claims["snow_id"] == nil {
|
||||||
|
return "", fmt.Errorf("invalid access token")
|
||||||
|
}
|
||||||
|
|
||||||
|
snowId, ok := claims["snow_id"].(string)
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("invalid access token")
|
||||||
|
}
|
||||||
|
|
||||||
|
return snowId, nil
|
||||||
|
}
|
1
go.mod
1
go.mod
|
@ -3,6 +3,7 @@ module github.com/ectrc/snow
|
||||||
go 1.21.3
|
go 1.21.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/beevik/etree v1.3.0
|
||||||
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/contrib/websocket v1.3.0
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,5 +1,7 @@
|
||||||
github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI=
|
github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI=
|
||||||
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||||
|
github.com/beevik/etree v1.3.0 h1:hQTc+pylzIKDb23yYprodCWWTt+ojFfUZyzU09a/hmU=
|
||||||
|
github.com/beevik/etree v1.3.0/go.mod h1:aiPf89g/1k3AShMVAzriilpcE4R/Vuor90y83zVZWFc=
|
||||||
github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY=
|
github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY=
|
||||||
github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||||
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=
|
||||||
|
|
|
@ -177,26 +177,11 @@ func PostTokenPassword(c *fiber.Ctx, body *FortniteTokenBody) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTokenVerify(c *fiber.Ctx) error {
|
func GetTokenVerify(c *fiber.Ctx) error {
|
||||||
auth := c.Get("Authorization")
|
snowId, err := aid.GetSnowFromToken(c.Get("Authorization"))
|
||||||
if auth == "" {
|
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Authorization Header is empty"))
|
|
||||||
}
|
|
||||||
real := strings.ReplaceAll(auth, "bearer eg1~", "")
|
|
||||||
|
|
||||||
claims, err := aid.JWTVerify(real)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if claims["snow_id"] == nil {
|
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
|
||||||
}
|
|
||||||
|
|
||||||
snowId, ok := claims["snow_id"].(string)
|
|
||||||
if !ok {
|
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
|
||||||
}
|
|
||||||
|
|
||||||
person := p.Find(snowId)
|
person := p.Find(snowId)
|
||||||
if person == nil {
|
if person == nil {
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
||||||
|
@ -204,7 +189,7 @@ func GetTokenVerify(c *fiber.Ctx) error {
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(aid.JSON{
|
return c.Status(fiber.StatusOK).JSON(aid.JSON{
|
||||||
"app": "fortnite",
|
"app": "fortnite",
|
||||||
"token": real,
|
"token": strings.ReplaceAll(c.Get("Authorization"), "bearer eg1~", ""),
|
||||||
"token_type": "bearer",
|
"token_type": "bearer",
|
||||||
"expires_at": time.Now().Add(time.Hour * 24).Format("2006-01-02T15:04:05.999Z"),
|
"expires_at": time.Now().Add(time.Hour * 24).Format("2006-01-02T15:04:05.999Z"),
|
||||||
"expires_in": 86400,
|
"expires_in": 86400,
|
||||||
|
@ -226,26 +211,11 @@ func DeleteToken(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func MiddlewareFortnite(c *fiber.Ctx) error {
|
func MiddlewareFortnite(c *fiber.Ctx) error {
|
||||||
auth := c.Get("Authorization")
|
snowId, err := aid.GetSnowFromToken(c.Get("Authorization"))
|
||||||
if auth == "" {
|
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Authorization Header is empty"))
|
|
||||||
}
|
|
||||||
real := strings.ReplaceAll(auth, "bearer eg1~", "")
|
|
||||||
|
|
||||||
claims, err := aid.JWTVerify(real)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if claims["snow_id"] == nil {
|
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
|
||||||
}
|
|
||||||
|
|
||||||
snowId, ok := claims["snow_id"].(string)
|
|
||||||
if !ok {
|
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
|
||||||
}
|
|
||||||
|
|
||||||
person := p.Find(snowId)
|
person := p.Find(snowId)
|
||||||
if person == nil {
|
if person == nil {
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
||||||
|
@ -256,29 +226,11 @@ func MiddlewareFortnite(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func MiddlewareWeb(c *fiber.Ctx) error {
|
func MiddlewareWeb(c *fiber.Ctx) error {
|
||||||
auth := c.Get("Authorization")
|
snowId, err := aid.GetSnowFromToken(c.Get("Authorization"))
|
||||||
if auth == "" {
|
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Authorization Header is empty"))
|
|
||||||
}
|
|
||||||
|
|
||||||
claims, err := aid.JWTVerify(auth)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if claims["snow_id"] == nil {
|
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
|
||||||
}
|
|
||||||
|
|
||||||
if claims["frontend"] == nil {
|
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Claims"))
|
|
||||||
}
|
|
||||||
|
|
||||||
snowId, ok := claims["snow_id"].(string)
|
|
||||||
if !ok {
|
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
|
||||||
}
|
|
||||||
|
|
||||||
person := p.Find(snowId)
|
person := p.Find(snowId)
|
||||||
if person == nil {
|
if person == nil {
|
||||||
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
||||||
|
|
|
@ -1,12 +1,94 @@
|
||||||
package socket
|
package socket
|
||||||
|
|
||||||
import "github.com/ectrc/snow/aid"
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/beevik/etree"
|
||||||
|
"github.com/ectrc/snow/aid"
|
||||||
|
"github.com/ectrc/snow/person"
|
||||||
|
"github.com/gofiber/contrib/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
type JabberData struct {
|
||||||
|
JabberID string
|
||||||
|
}
|
||||||
|
|
||||||
|
var jabberHandlers = map[string]func(*Socket[JabberData], *etree.Document) error {
|
||||||
|
"open": jabberOpenHandler,
|
||||||
|
"iq": jabberIqRootHandler,
|
||||||
|
}
|
||||||
|
|
||||||
func HandleNewJabberSocket(identifier string) {
|
func HandleNewJabberSocket(identifier string) {
|
||||||
_, ok := JabberSockets.Get(identifier)
|
socket, ok := JabberSockets.Get(identifier)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer JabberSockets.Delete(identifier)
|
||||||
|
|
||||||
aid.Print("New jabber socket: " + identifier)
|
for {
|
||||||
|
_, message, failed := socket.Connection.ReadMessage()
|
||||||
|
if failed != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
aid.Print(string(message))
|
||||||
|
|
||||||
|
parsed := etree.NewDocument()
|
||||||
|
if err := parsed.ReadFromBytes(message); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if handler, ok := jabberHandlers[parsed.Root().Tag]; ok {
|
||||||
|
if err := handler(socket, parsed); err != nil {
|
||||||
|
socket.Connection.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func jabberOpenHandler(socket *Socket[JabberData], parsed *etree.Document) error {
|
||||||
|
socket.Connection.WriteMessage(websocket.TextMessage, []byte(`<open xmlns="urn:ietf:params:xml:ns:xmpp-framing" from="prod.ol.epicgames.com" version="1.0" id="`+ socket.ID +`" />`))
|
||||||
|
socket.Connection.WriteMessage(websocket.TextMessage, []byte(`<stream:features xmlns:stream="http://etherx.jabber.org/streams" />`))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func jabberIqRootHandler(socket *Socket[JabberData], parsed *etree.Document) error {
|
||||||
|
redirect := map[string]func(*Socket[JabberData], *etree.Document) error {
|
||||||
|
"set": jabberIqSetHandler,
|
||||||
|
"get": jabberIqGetHandler,
|
||||||
|
}
|
||||||
|
|
||||||
|
if handler, ok := redirect[parsed.Root().SelectAttr("type").Value]; ok {
|
||||||
|
if err := handler(socket, parsed); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func jabberIqSetHandler(socket *Socket[JabberData], parsed *etree.Document) error {
|
||||||
|
snowId, err := aid.GetSnowFromToken(parsed.FindElement("/iq/query/password").Text())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
person := person.Find(snowId)
|
||||||
|
if person == nil {
|
||||||
|
return fmt.Errorf("person not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.Data.JabberID = snowId + "@prod.ol.epicgames.com/" + parsed.FindElement("/iq/query/resource").Text()
|
||||||
|
socket.Person = person
|
||||||
|
|
||||||
|
socket.Connection.WriteMessage(websocket.TextMessage, []byte(`<iq xmlns="jabber:client" type="result" id="_xmpp_auth1" from="prod.ol.epicgames.com" to="`+ socket.Data.JabberID +`" />`))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func jabberIqGetHandler(socket *Socket[JabberData], parsed *etree.Document) error {
|
||||||
|
socket.Connection.WriteMessage(websocket.TextMessage, []byte(`<iq xmlns="jabber:client" type="result" id="`+ parsed.Root().SelectAttr("id").Value +`" from="prod.ol.epicgames.com" to="`+ socket.Data.JabberID +`" />`))
|
||||||
|
return nil
|
||||||
}
|
}
|
|
@ -7,34 +7,19 @@ import (
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SocketType int
|
|
||||||
|
|
||||||
var (
|
|
||||||
SocketTypeJabber SocketType = 1
|
|
||||||
SocketTypeMatchmaking SocketType = 2
|
|
||||||
)
|
|
||||||
|
|
||||||
type JabberData struct {
|
|
||||||
JabberID string
|
|
||||||
}
|
|
||||||
|
|
||||||
type MatchmakerData struct {
|
type MatchmakerData struct {
|
||||||
PlaylistID string
|
PlaylistID string
|
||||||
Region string
|
Region string
|
||||||
}
|
}
|
||||||
|
|
||||||
type data interface {
|
type Socket[T JabberData | MatchmakerData] struct {
|
||||||
JabberData | MatchmakerData
|
|
||||||
}
|
|
||||||
|
|
||||||
type Socket[T data] struct {
|
|
||||||
ID string
|
ID string
|
||||||
Connection *websocket.Conn
|
Connection *websocket.Conn
|
||||||
Data *T
|
Data *T
|
||||||
Person *person.Person
|
Person *person.Person
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSocket[T data](conn *websocket.Conn, data ...T) *Socket[T] {
|
func newSocket[T JabberData | MatchmakerData](conn *websocket.Conn, data ...T) *Socket[T] {
|
||||||
additional := data[0]
|
additional := data[0]
|
||||||
|
|
||||||
return &Socket[T]{
|
return &Socket[T]{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user