snow/handlers/xmpp.go

355 lines
12 KiB
Go
Raw Normal View History

2024-01-20 23:08:29 +00:00
package handlers
2024-01-21 00:18:40 +00:00
import (
"fmt"
"strings"
"time"
"github.com/beevik/etree"
"github.com/ectrc/snow/aid"
p "github.com/ectrc/snow/person"
2024-01-21 17:49:14 +00:00
"github.com/ectrc/snow/storage"
"github.com/goccy/go-json"
"github.com/google/uuid"
2024-01-21 00:18:40 +00:00
)
2024-01-21 17:49:14 +00:00
type PresenceStatus struct {
Status string `json:"Status"`
IsPlaying bool `json:"bIsPlaying"`
IsJoinable bool `json:"bIsJoinable"`
HasVoiceSupport bool `json:"bHasVoiceSupport"`
SessionId string `json:"SessionId"`
Properties map[string]struct {
SourceId string `json:"sourceId"`
SourceDisplayName string `json:"sourceDisplayName"`
SourcePlatform string `json:"sourcePlatform"`
PartyId string `json:"partyId"`
PartyTypeId int `json:"partyTypeId"`
Key string `json:"key"`
AppId string `json:"appId"`
BuildId string `json:"buildId"`
PartyFlags int `json:"partyFlags"`
NotAcceptingReason int `json:"notAcceptingReason"`
Pc int `json:"pc"`
} `json:"Properties"`
}
type PresenceState struct {
JID string
Open bool
RawStatus string
ParsedStatus PresenceStatus
}
2024-01-21 00:18:40 +00:00
var (
socketXmppMessageHandlers = map[string]func(*Socket, *etree.Document) error {
"open": presenceSocketOpenEvent,
"iq": presenceSocketIqRootEvent,
2024-01-21 17:49:14 +00:00
"message": presenceSocketMessageEvent,
"presence": presenceSocketPresenceEvent,
2024-01-21 00:18:40 +00:00
"close": presenceSocketCloseEvent,
}
)
2024-01-20 23:08:29 +00:00
2024-01-21 17:49:14 +00:00
func presenceSocketHandle(id string) {
2024-01-20 23:08:29 +00:00
socket, ok := sockets.Get(id)
if !ok {
return
}
2024-01-21 17:49:14 +00:00
socket.Type = SocketTypeXmpp
socket.PresenceState = &PresenceState{}
2024-01-20 23:08:29 +00:00
for {
2024-01-21 00:18:40 +00:00
_, message, err := socket.Connection.ReadMessage()
2024-01-20 23:08:29 +00:00
if err != nil {
break
}
2024-01-21 00:18:40 +00:00
parsed := etree.NewDocument()
if err := parsed.ReadFromBytes(message); err != nil {
return
}
if handler, ok := socketXmppMessageHandlers[parsed.Root().Tag]; ok {
if err := handler(socket, parsed); err != nil {
return
}
}
}
2024-01-21 17:49:14 +00:00
for _, partial := range storage.Repo.GetFriendsForPerson(socket.Person.ID) {
friend := socket.Person.GetFriend(partial.ID)
if friend == nil {
continue
}
2024-01-21 19:54:20 +00:00
friendSocket := FindSocketForPerson(friend.Person)
2024-01-21 17:49:14 +00:00
if friendSocket == nil {
continue
}
friendDocument := etree.NewDocument()
friendPresence := friendDocument.CreateElement("presence")
friendPresence.Attr = append(friendPresence.Attr, etree.Attr{Key: "from", Value: socket.PresenceState.JID})
friendPresence.Attr = append(friendPresence.Attr, etree.Attr{Key: "to", Value: friendSocket.PresenceState.JID})
friendPresence.Attr = append(friendPresence.Attr, etree.Attr{Key: "type", Value: "available"})
friendPresence.Attr = append(friendPresence.Attr, etree.Attr{Key: "xmlns", Value: "jabber:client"})
friendPresence.CreateElement("status").SetText(aid.JSONStringify(aid.JSON{}))
friendPresence.CreateElement("show").SetText("away")
2024-01-21 18:16:03 +00:00
friendSocket.PresenceWrite(friendDocument)
2024-01-21 17:49:14 +00:00
}
for _, party := range socket.PresenceState.ParsedStatus.Properties {
if party.PartyId == "" {
continue
}
sockets.Range(func(_ string, recieverSocket *Socket) bool {
if recieverSocket.Type != SocketTypeXmpp || recieverSocket.Person == nil {
return true
}
document := etree.NewDocument()
message := document.CreateElement("message")
message.Attr = append(message.Attr, etree.Attr{Key: "id", Value: uuid.New().String()})
message.Attr = append(message.Attr, etree.Attr{Key: "from", Value: socket.PresenceState.JID})
message.Attr = append(message.Attr, etree.Attr{Key: "to", Value: recieverSocket.PresenceState.JID})
message.Attr = append(message.Attr, etree.Attr{Key: "xmlns", Value: "jabber:client"})
message.CreateElement("body").SetText(aid.JSONStringify(aid.JSON{
"type": "com.epicgames.party.memberexited",
"timestamp": time.Now().Format(time.RFC3339),
"payload": aid.JSON{
"memberId": socket.Person.ID,
"partyId": party.PartyId,
"wasKicked": false,
},
}))
2024-01-21 18:16:03 +00:00
recieverSocket.PresenceWrite(document)
2024-01-21 17:49:14 +00:00
return true
})
}
2024-01-21 00:18:40 +00:00
}
func presenceSocketOpenEvent(socket *Socket, tree *etree.Document) error {
document := etree.NewDocument()
open := document.CreateElement("open")
open.Attr = append(open.Attr, etree.Attr{Key: "xmlns", Value: "urn:ietf:params:xml:ns:xmpp-framing"})
open.Attr = append(open.Attr, etree.Attr{Key: "from", Value: "prod.ol.epicgames.com"})
open.Attr = append(open.Attr, etree.Attr{Key: "version", Value: "1.0"})
open.Attr = append(open.Attr, etree.Attr{Key: "id", Value: socket.ID})
2024-01-21 18:16:03 +00:00
socket.PresenceWrite(document)
2024-01-21 00:18:40 +00:00
return nil
}
func presenceSocketCloseEvent(socket *Socket, tree *etree.Document) error {
return fmt.Errorf("safe exit")
}
func presenceSocketIqRootEvent(socket *Socket, tree *etree.Document) error {
redirect := map[string]func(*Socket, *etree.Document) error{
"set": presenceSocketIqSetEvent,
"get": presenceSocketIqGetEvent,
}
if handler, ok := redirect[tree.Root().SelectAttr("type").Value]; ok {
if err := handler(socket, tree); err != nil {
return err
}
2024-01-20 23:08:29 +00:00
}
2024-01-21 00:18:40 +00:00
return nil
}
func presenceSocketIqSetEvent(socket *Socket, tree *etree.Document) error {
token := tree.Root().SelectElement("query").SelectElement("password")
if token == nil || token.Text() == "" {
return fmt.Errorf("invalid token")
}
real := strings.ReplaceAll(token.Text(), "eg1~", "")
claims, err := aid.JWTVerify(real)
if err != nil {
return fmt.Errorf("invalid token")
}
if claims["snow_id"] == nil {
return fmt.Errorf("invalid token")
}
snowId, ok := claims["snow_id"].(string)
if !ok {
return fmt.Errorf("invalid token")
}
person := p.Find(snowId)
if person == nil {
return fmt.Errorf("invalid token")
}
socket.Person = person
2024-01-21 17:49:14 +00:00
socket.PresenceState.JID = person.ID + "@prod.ol.epicgames.com/" + tree.Root().SelectElement("query").SelectElement("resource").Text()
2024-01-21 00:18:40 +00:00
document := etree.NewDocument()
iq := document.CreateElement("iq")
iq.Attr = append(iq.Attr, etree.Attr{Key: "type", Value: "result"})
iq.Attr = append(iq.Attr, etree.Attr{Key: "id", Value: "_xmpp_auth1"})
iq.Attr = append(iq.Attr, etree.Attr{Key: "from", Value: "prod.ol.epicgames.com"})
2024-01-21 18:16:03 +00:00
socket.PresenceWrite(document)
socket.PresenceWriteStatus()
2024-01-21 00:18:40 +00:00
return nil
}
func presenceSocketIqGetEvent(socket *Socket, tree *etree.Document) error {
document := etree.NewDocument()
iq := document.CreateElement("iq")
iq.Attr = append(iq.Attr, etree.Attr{Key: "type", Value: "result"})
iq.Attr = append(iq.Attr, etree.Attr{Key: "id", Value: tree.Root().SelectAttr("id").Value})
iq.Attr = append(iq.Attr, etree.Attr{Key: "to", Value: tree.Root().SelectAttr("from").Value})
iq.Attr = append(iq.Attr, etree.Attr{Key: "from", Value: "prod.ol.epicgames.com"})
ping := iq.CreateElement("ping")
ping.Attr = append(ping.Attr, etree.Attr{Key: "xmlns", Value: "urn:xmpp:ping"})
2024-01-21 18:16:03 +00:00
socket.PresenceWrite(document)
2024-01-21 00:18:40 +00:00
return nil
}
2024-01-21 17:49:14 +00:00
func presenceSocketMessageEvent(socket *Socket, tree *etree.Document) error {
reciever := p.Find(strings.Split(tree.Root().SelectAttr("to").Value, "@")[0])
if reciever == nil {
return nil
}
2024-01-21 19:54:20 +00:00
recieverSocket := FindSocketForPerson(reciever)
2024-01-21 17:49:14 +00:00
if recieverSocket == nil {
return nil
}
document := etree.NewDocument()
message := document.CreateElement("message")
message.Attr = append(message.Attr, etree.Attr{Key: "id", Value: tree.Root().SelectAttr("id").Value})
message.Attr = append(message.Attr, etree.Attr{Key: "from", Value: socket.PresenceState.JID})
message.Attr = append(message.Attr, etree.Attr{Key: "to", Value: recieverSocket.PresenceState.JID})
message.Attr = append(message.Attr, etree.Attr{Key: "xmlns", Value: "jabber:client"})
message.CreateElement("body").SetText(tree.Root().SelectElement("body").Text())
2024-01-21 18:16:03 +00:00
recieverSocket.PresenceWrite(document)
2024-01-21 17:49:14 +00:00
return nil
}
func presenceSocketPresenceEvent(socket *Socket, tree *etree.Document) error {
status := tree.Root().SelectElement("status")
if status == nil {
return nil
}
socket.PresenceState.RawStatus = status.Text()
json.NewDecoder(strings.NewReader(status.Text())).Decode(&socket.PresenceState.ParsedStatus)
2024-01-21 18:16:03 +00:00
socket.PresenceWriteStatus()
2024-01-21 17:49:14 +00:00
return nil
}
2024-01-21 18:16:03 +00:00
func (s *Socket) PresenceWrite(message *etree.Document) {
bytes, err := message.WriteToBytes()
if err != nil {
return
}
s.Write(bytes)
}
func (s *Socket) PresenceWriteStatus() {
for _, partial := range storage.Repo.GetFriendsForPerson(s.Person.ID) {
friend := s.Person.GetFriend(partial.ID)
2024-01-21 17:49:14 +00:00
if friend == nil {
continue
}
2024-01-21 19:54:20 +00:00
friendSocket := FindSocketForPerson(friend.Person)
2024-01-21 17:49:14 +00:00
if friendSocket == nil {
continue
}
friendDocument := etree.NewDocument()
friendPresence := friendDocument.CreateElement("presence")
2024-01-21 18:16:03 +00:00
friendPresence.Attr = append(friendPresence.Attr, etree.Attr{Key: "from", Value: s.PresenceState.JID})
2024-01-21 17:49:14 +00:00
friendPresence.Attr = append(friendPresence.Attr, etree.Attr{Key: "to", Value: friendSocket.PresenceState.JID})
friendPresence.Attr = append(friendPresence.Attr, etree.Attr{Key: "type", Value: "available"})
friendPresence.Attr = append(friendPresence.Attr, etree.Attr{Key: "xmlns", Value: "jabber:client"})
2024-01-21 18:16:03 +00:00
friendPresence.CreateElement("status").SetText(s.PresenceState.RawStatus)
friendSocket.PresenceWrite(friendDocument)
2024-01-21 17:49:14 +00:00
document := etree.NewDocument()
presence := document.CreateElement("presence")
presence.Attr = append(presence.Attr, etree.Attr{Key: "from", Value: friendSocket.PresenceState.JID})
2024-01-21 18:16:03 +00:00
presence.Attr = append(presence.Attr, etree.Attr{Key: "to", Value: s.PresenceState.JID})
2024-01-21 17:49:14 +00:00
presence.Attr = append(presence.Attr, etree.Attr{Key: "type", Value: "available"})
presence.Attr = append(presence.Attr, etree.Attr{Key: "xmlns", Value: "jabber:client"})
presence.CreateElement("status").SetText(friendSocket.PresenceState.RawStatus)
2024-01-21 18:16:03 +00:00
s.PresenceWrite(document)
2024-01-21 17:49:14 +00:00
}
}
2024-01-21 18:16:03 +00:00
func (s *Socket) PresenceWriteJSON(body aid.JSON) {
2024-01-21 19:54:20 +00:00
aid.PrintJSON(body)
2024-01-21 18:16:03 +00:00
document := etree.NewDocument()
message := document.CreateElement("message")
message.Attr = append(message.Attr, etree.Attr{Key: "id", Value: uuid.New().String()})
2024-01-21 19:54:20 +00:00
message.Attr = append(message.Attr, etree.Attr{Key: "from", Value: "xmpp-admin@prod.ol.epicgames.com"})
2024-01-21 18:16:03 +00:00
message.Attr = append(message.Attr, etree.Attr{Key: "to", Value: s.PresenceState.JID})
message.Attr = append(message.Attr, etree.Attr{Key: "xmlns", Value: "jabber:client"})
message.CreateElement("body").SetText(aid.JSONStringify(body))
s.PresenceWrite(document)
}
func (s *Socket) PresenceWriteGiftReceived() {
document := etree.NewDocument()
message := document.CreateElement("message")
message.Attr = append(message.Attr, etree.Attr{Key: "id", Value: uuid.New().String()})
message.Attr = append(message.Attr, etree.Attr{Key: "from", Value: "prod.ol.epicgames.com"})
message.Attr = append(message.Attr, etree.Attr{Key: "to", Value: s.PresenceState.JID})
message.Attr = append(message.Attr, etree.Attr{Key: "xmlns", Value: "jabber:client"})
message.CreateElement("body").SetText(aid.JSONStringify(aid.JSON{
"type": "com.epicgames.gift.received",
"timestamp": time.Now().Format(time.RFC3339),
"payload": aid.JSON{},
}))
s.PresenceWrite(document)
}
2024-01-21 00:18:40 +00:00
func init() {
go func() {
for {
if aid.Config != nil {
break
}
}
timer := time.NewTicker(30 * time.Second)
for {
<-timer.C
sockets.Range(func(key string, socket *Socket) bool {
if socket.Type != SocketTypeXmpp || socket.Person == nil {
return true
}
document := etree.NewDocument()
iq := document.CreateElement("iq")
iq.Attr = append(iq.Attr, etree.Attr{Key: "id", Value: "_xmpp_auth1"})
iq.Attr = append(iq.Attr, etree.Attr{Key: "type", Value: "get"})
iq.Attr = append(iq.Attr, etree.Attr{Key: "from", Value: "prod.ol.epicgames.com"})
ping := iq.CreateElement("ping")
ping.Attr = append(iq.Attr, etree.Attr{Key: "xmlns", Value: "urn:xmpp:ping"})
2024-01-21 18:16:03 +00:00
socket.PresenceWrite(document)
2024-01-21 00:18:40 +00:00
return true
})
}
}()
2024-01-20 23:08:29 +00:00
}