2021-09-09 23:12:29 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/xml"
|
2021-09-11 17:16:38 +02:00
|
|
|
"io"
|
2021-09-12 00:31:03 +02:00
|
|
|
"net"
|
2021-09-09 23:12:29 +02:00
|
|
|
|
|
|
|
"github.com/bdlm/log"
|
|
|
|
"mellium.im/xmlstream"
|
|
|
|
"mellium.im/xmpp"
|
2021-09-12 00:31:03 +02:00
|
|
|
"mellium.im/xmpp/component"
|
2021-09-15 15:47:29 +02:00
|
|
|
"mellium.im/xmpp/disco"
|
|
|
|
"mellium.im/xmpp/disco/info"
|
2021-09-09 23:12:29 +02:00
|
|
|
"mellium.im/xmpp/jid"
|
2021-09-11 17:16:38 +02:00
|
|
|
"mellium.im/xmpp/mux"
|
2021-09-15 15:47:29 +02:00
|
|
|
"mellium.im/xmpp/ping"
|
2021-09-09 23:12:29 +02:00
|
|
|
"mellium.im/xmpp/stanza"
|
2021-09-11 17:16:38 +02:00
|
|
|
|
|
|
|
"dev.sum7.eu/genofire/unified-push-xmpp/messages"
|
2021-09-09 23:12:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type XMPPService struct {
|
2021-09-15 15:47:29 +02:00
|
|
|
Addr string `toml:"address"`
|
|
|
|
JID string `toml:"jid"`
|
|
|
|
Secret string `toml:"secret"`
|
|
|
|
session *xmpp.Session
|
2021-09-09 23:12:29 +02:00
|
|
|
}
|
|
|
|
|
2021-09-15 15:47:29 +02:00
|
|
|
func (s *XMPPService) Run(jwt JWTSecret, endpoint string) error {
|
2021-09-11 17:16:38 +02:00
|
|
|
var err error
|
2021-09-12 00:31:03 +02:00
|
|
|
j := jid.MustParse(s.JID)
|
|
|
|
ctx := context.TODO()
|
|
|
|
conn, err := net.Dial("tcp", s.Addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if s.session, err = component.NewSession(
|
|
|
|
ctx, j.Domain(),
|
|
|
|
[]byte(s.Secret), conn,
|
2021-09-11 17:16:38 +02:00
|
|
|
); err != nil {
|
2021-09-09 23:12:29 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
2021-09-14 09:31:45 +02:00
|
|
|
log.Info("closing xmpp connection")
|
2021-09-11 17:16:38 +02:00
|
|
|
if err := s.session.Close(); err != nil {
|
2021-09-09 23:12:29 +02:00
|
|
|
log.Errorf("Error closing session: %q", err)
|
|
|
|
}
|
2021-09-14 09:31:45 +02:00
|
|
|
log.Info("closing xmpp connection")
|
2021-09-11 17:16:38 +02:00
|
|
|
if err := s.session.Conn().Close(); err != nil {
|
2021-09-09 23:12:29 +02:00
|
|
|
log.Errorf("Error closing connection: %q", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-09-11 17:16:38 +02:00
|
|
|
log.Infof("connected with %s", s.session.LocalAddr())
|
2021-09-12 00:31:03 +02:00
|
|
|
return s.session.Serve(mux.New(
|
2021-09-15 15:47:29 +02:00
|
|
|
disco.Handle(),
|
|
|
|
ping.Handle(),
|
|
|
|
XMPPUpHandle(jwt, endpoint),
|
2021-09-11 17:16:38 +02:00
|
|
|
))
|
2021-09-09 23:12:29 +02:00
|
|
|
}
|
2021-09-11 17:16:38 +02:00
|
|
|
|
2021-09-15 15:47:29 +02:00
|
|
|
// SendMessage of an UP Notification
|
|
|
|
func (s *XMPPService) SendMessage(to jid.JID, publicToken, content string) error {
|
|
|
|
log.WithFields(map[string]interface{}{
|
|
|
|
"to": to.String(),
|
|
|
|
"publicToken": publicToken,
|
|
|
|
}).Debug("forward message to xmpp")
|
|
|
|
return s.session.Encode(context.TODO(), messages.Message{
|
|
|
|
Message: stanza.Message{
|
|
|
|
To: to,
|
|
|
|
From: jid.MustParse(s.JID),
|
|
|
|
// Type: stanza.ChatMessage,
|
|
|
|
Type: stanza.NormalMessage,
|
|
|
|
},
|
|
|
|
PublicToken: publicToken,
|
|
|
|
Body: content,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// XMPPUpHandler struct
|
|
|
|
// for handling UnifiedPush specifical requests
|
|
|
|
type XMPPUpHandler struct {
|
|
|
|
jwtSecret JWTSecret
|
|
|
|
endpoint string
|
|
|
|
}
|
|
|
|
|
|
|
|
// XMPPUpHandle - setup UnfiedPush handler to mux
|
|
|
|
func XMPPUpHandle(jwt JWTSecret, endpoint string) mux.Option {
|
|
|
|
return func(m *mux.ServeMux) {
|
|
|
|
s := &XMPPUpHandler{jwtSecret: jwt, endpoint: endpoint}
|
|
|
|
// register - get + set - need direct handler (not IQFunc) to bind ForIdentities and ForFeatures to disco
|
|
|
|
mux.IQ(stanza.SetIQ, xml.Name{Local: messages.LocalRegister, Space: messages.Space}, s)(m)
|
|
|
|
mux.IQ(stanza.GetIQ, xml.Name{Local: messages.LocalRegister, Space: messages.Space}, s)(m)
|
|
|
|
// unregister - get + set
|
|
|
|
mux.IQFunc(stanza.SetIQ, xml.Name{Local: messages.LocalUnregister, Space: messages.Space}, s.handleUnregister)(m)
|
|
|
|
mux.IQFunc(stanza.GetIQ, xml.Name{Local: messages.LocalUnregister, Space: messages.Space}, s.handleUnregister)(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
upIdentity = info.Identity{
|
|
|
|
Category: "pubsub",
|
|
|
|
Type: "push",
|
|
|
|
Name: "Unified Push over XMPP",
|
|
|
|
}
|
|
|
|
upFeature = info.Feature{Var: messages.Space}
|
|
|
|
)
|
|
|
|
|
|
|
|
// ForIdentities disco handler
|
|
|
|
func (h *XMPPUpHandler) ForIdentities(node string, f func(info.Identity) error) error {
|
|
|
|
if node != "" {
|
|
|
|
log.Debugf("response disco feature for %s", node)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
err = f(upIdentity)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug("response disco identity")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForFeatures disco handler
|
|
|
|
func (h *XMPPUpHandler) ForFeatures(node string, f func(info.Feature) error) error {
|
|
|
|
if node != "" {
|
|
|
|
log.Debugf("response disco feature for %s", node)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
err = f(upFeature)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug("response disco feature")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleIQ - handleRegister for UnifiedPush request
|
|
|
|
func (h *XMPPUpHandler) HandleIQ(iq stanza.IQ, t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
|
|
|
|
if start.Name.Local != "register" || start.Name.Space != messages.Space {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-11 17:16:38 +02:00
|
|
|
reply := messages.RegisterIQ{
|
|
|
|
IQ: stanza.IQ{
|
|
|
|
ID: iq.ID,
|
|
|
|
Type: stanza.ErrorIQ,
|
2021-09-12 00:31:03 +02:00
|
|
|
From: iq.To,
|
2021-09-11 17:16:38 +02:00
|
|
|
To: iq.From,
|
|
|
|
},
|
|
|
|
}
|
2021-09-12 00:31:03 +02:00
|
|
|
defer func() {
|
2021-09-11 17:16:38 +02:00
|
|
|
if err := t.Encode(reply); err != nil {
|
2021-09-12 00:31:03 +02:00
|
|
|
log.Errorf("sending register response: %v", err)
|
2021-09-11 17:16:38 +02:00
|
|
|
}
|
|
|
|
}()
|
2021-09-16 01:04:43 +02:00
|
|
|
log.Infof("received iq: %v", iq)
|
2021-09-11 17:16:38 +02:00
|
|
|
|
|
|
|
tokenData := messages.TokenData{}
|
|
|
|
err := xml.NewTokenDecoder(t).Decode(&tokenData)
|
|
|
|
if err != nil && err != io.EOF {
|
2021-09-14 09:31:45 +02:00
|
|
|
log.Warnf("decoding message: %q", err)
|
2021-09-12 00:31:03 +02:00
|
|
|
reply.Register.Error = &messages.ErrorData{Body: "unable decode"}
|
2021-09-11 17:16:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-14 21:55:21 +02:00
|
|
|
publicToken := tokenData.Body
|
|
|
|
if publicToken == "" {
|
|
|
|
log.Warnf("no token found: %v", publicToken)
|
2021-09-12 00:31:03 +02:00
|
|
|
reply.Register.Error = &messages.ErrorData{Body: "no token"}
|
2021-09-11 17:16:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-15 15:47:29 +02:00
|
|
|
endpointToken, err := h.jwtSecret.Generate(iq.From, publicToken)
|
2021-09-14 01:32:37 +02:00
|
|
|
if err != nil {
|
2021-09-14 21:55:21 +02:00
|
|
|
log.Errorf("unable entpointToken generation: %v", err)
|
|
|
|
reply.Register.Error = &messages.ErrorData{Body: "endpointToken error on gateway"}
|
2021-09-14 01:32:37 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-15 15:47:29 +02:00
|
|
|
endpoint := h.endpoint + "/UP?token=" + endpointToken
|
2021-09-11 17:16:38 +02:00
|
|
|
reply.IQ.Type = stanza.ResultIQ
|
2021-09-12 00:31:03 +02:00
|
|
|
reply.Register.Endpoint = &messages.EndpointData{Body: endpoint}
|
2021-09-16 01:04:43 +02:00
|
|
|
log.Debugf("generate response: %v", endpoint)
|
2021-09-11 17:16:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-15 15:47:29 +02:00
|
|
|
|
|
|
|
// handleUnregister for UnifiedPush request
|
|
|
|
func (h *XMPPUpHandler) handleUnregister(iq stanza.IQ, t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
|
2021-09-11 17:16:38 +02:00
|
|
|
reply := messages.UnregisterIQ{
|
|
|
|
IQ: stanza.IQ{
|
|
|
|
ID: iq.ID,
|
|
|
|
Type: stanza.ErrorIQ,
|
2021-09-12 00:31:03 +02:00
|
|
|
From: iq.To,
|
2021-09-11 17:16:38 +02:00
|
|
|
To: iq.From,
|
|
|
|
},
|
|
|
|
}
|
2021-09-12 00:31:03 +02:00
|
|
|
defer func() {
|
2021-09-11 17:16:38 +02:00
|
|
|
if err := t.Encode(reply); err != nil {
|
2021-09-12 00:31:03 +02:00
|
|
|
log.Errorf("sending unregister response: %v", err)
|
2021-09-11 17:16:38 +02:00
|
|
|
}
|
|
|
|
}()
|
2021-09-14 09:31:45 +02:00
|
|
|
log.Debugf("unregistered unhandled: %v", start)
|
2021-09-11 17:16:38 +02:00
|
|
|
|
|
|
|
reply.Unregister.Error = "not implemented"
|
2021-09-09 23:12:29 +02:00
|
|
|
return nil
|
|
|
|
}
|