snow/socket/jabber.go

94 lines
2.7 KiB
Go
Raw Normal View History

2024-01-28 22:04:40 +00:00
package socket
2024-01-29 21:08:05 +00:00
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,
}
2024-01-28 22:04:40 +00:00
func HandleNewJabberSocket(identifier string) {
2024-01-29 21:08:05 +00:00
socket, ok := JabberSockets.Get(identifier)
2024-01-28 22:04:40 +00:00
if !ok {
return
}
2024-01-29 21:08:05 +00:00
defer JabberSockets.Delete(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
}
2024-01-28 22:04:40 +00:00
2024-01-29 21:08:05 +00:00
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
2024-01-28 22:04:40 +00:00
}