2019-06-18 14:28:30 +00:00
|
|
|
package xmpp
|
2018-01-11 21:15:54 +00:00
|
|
|
|
|
|
|
import (
|
2019-10-31 19:08:39 +00:00
|
|
|
"context"
|
2018-01-12 17:01:27 +00:00
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
2018-01-11 22:00:59 +00:00
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
2018-01-11 21:15:54 +00:00
|
|
|
"fmt"
|
2019-06-26 15:14:52 +00:00
|
|
|
"gosrc.io/xmpp/stanza"
|
2019-11-22 14:07:40 +00:00
|
|
|
"io"
|
2018-01-11 21:15:54 +00:00
|
|
|
)
|
|
|
|
|
2019-06-08 17:42:02 +00:00
|
|
|
type ComponentOptions struct {
|
2019-10-11 04:24:47 +00:00
|
|
|
TransportConfiguration
|
|
|
|
|
2019-06-08 17:42:02 +00:00
|
|
|
// =================================
|
|
|
|
// Component Connection Info
|
|
|
|
|
|
|
|
// Domain is the XMPP server subdomain that the component will handle
|
|
|
|
Domain string
|
|
|
|
// Secret is the "password" used by the XMPP server to secure component access
|
|
|
|
Secret string
|
|
|
|
|
|
|
|
// =================================
|
|
|
|
// Component discovery
|
|
|
|
|
|
|
|
// Component human readable name, that will be shown in XMPP discovery
|
|
|
|
Name string
|
|
|
|
// Typical categories and types: https://xmpp.org/registrar/disco-categories.html
|
|
|
|
Category string
|
|
|
|
Type string
|
|
|
|
|
|
|
|
// =================================
|
|
|
|
// Communication with developer client / StreamManager
|
|
|
|
|
|
|
|
// Track and broadcast connection state
|
|
|
|
EventManager
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:15:54 +00:00
|
|
|
// Component implements an XMPP extension allowing to extend XMPP server
|
|
|
|
// using external components. Component specifications are defined
|
|
|
|
// in XEP-0114, XEP-0355 and XEP-0356.
|
|
|
|
type Component struct {
|
2019-06-08 17:42:02 +00:00
|
|
|
ComponentOptions
|
2019-06-13 15:22:39 +00:00
|
|
|
router *Router
|
2018-01-12 17:01:27 +00:00
|
|
|
|
2019-10-11 04:24:47 +00:00
|
|
|
transport Transport
|
2018-01-11 22:00:59 +00:00
|
|
|
|
|
|
|
// read / write
|
2019-12-05 17:12:00 +00:00
|
|
|
socketProxy io.ReadWriter // TODO
|
|
|
|
ErrorHandler func(error)
|
2018-01-11 21:15:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 17:12:00 +00:00
|
|
|
func NewComponent(opts ComponentOptions, r *Router, errorHandler func(error)) (*Component, error) {
|
|
|
|
c := Component{ComponentOptions: opts, router: r, ErrorHandler: errorHandler}
|
2019-06-09 10:48:22 +00:00
|
|
|
return &c, nil
|
2019-06-08 17:42:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:16:55 +00:00
|
|
|
// Connect triggers component connection to XMPP server component port.
|
2019-06-08 17:42:02 +00:00
|
|
|
// TODO: Failed handshake should be a permanent error
|
|
|
|
func (c *Component) Connect() error {
|
2019-09-05 20:01:40 +00:00
|
|
|
var state SMState
|
|
|
|
return c.Resume(state)
|
|
|
|
}
|
2019-11-08 11:07:55 +00:00
|
|
|
|
2019-09-05 20:01:40 +00:00
|
|
|
func (c *Component) Resume(sm SMState) error {
|
2018-01-11 21:15:54 +00:00
|
|
|
var err error
|
2019-10-18 18:29:54 +00:00
|
|
|
var streamId string
|
|
|
|
if c.ComponentOptions.TransportConfiguration.Domain == "" {
|
|
|
|
c.ComponentOptions.TransportConfiguration.Domain = c.ComponentOptions.Domain
|
2018-01-11 22:00:59 +00:00
|
|
|
}
|
2019-10-25 13:22:01 +00:00
|
|
|
c.transport, err = NewComponentTransport(c.ComponentOptions.TransportConfiguration)
|
|
|
|
if err != nil {
|
2019-11-08 11:07:55 +00:00
|
|
|
c.updateState(StatePermanentError)
|
2019-11-28 16:15:15 +00:00
|
|
|
|
2019-11-08 11:07:55 +00:00
|
|
|
return NewConnError(err, true)
|
2019-10-25 13:22:01 +00:00
|
|
|
}
|
2018-01-11 22:00:59 +00:00
|
|
|
|
2019-10-18 18:29:54 +00:00
|
|
|
if streamId, err = c.transport.Connect(); err != nil {
|
2019-11-08 11:07:55 +00:00
|
|
|
c.updateState(StatePermanentError)
|
2019-11-28 16:15:15 +00:00
|
|
|
|
2019-11-08 11:07:55 +00:00
|
|
|
return NewConnError(err, true)
|
2018-01-11 21:15:54 +00:00
|
|
|
}
|
2019-10-18 18:29:54 +00:00
|
|
|
c.updateState(StateConnected)
|
2018-01-11 21:15:54 +00:00
|
|
|
|
2019-10-18 18:29:54 +00:00
|
|
|
// Authentication
|
2019-12-09 12:31:01 +00:00
|
|
|
if err := c.sendWithWriter(c.transport, []byte(fmt.Sprintf("<handshake>%s</handshake>", c.handshake(streamId)))); err != nil {
|
2019-09-05 20:01:40 +00:00
|
|
|
c.updateState(StateStreamError)
|
2019-11-28 16:15:15 +00:00
|
|
|
|
2019-09-05 20:01:40 +00:00
|
|
|
return NewConnError(errors.New("cannot send handshake "+err.Error()), false)
|
2018-01-11 22:00:59 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 18:29:54 +00:00
|
|
|
// Check server response for authentication
|
2019-11-22 14:07:40 +00:00
|
|
|
val, err := stanza.NextPacket(c.transport.GetDecoder())
|
2018-01-11 22:00:59 +00:00
|
|
|
if err != nil {
|
2019-11-08 11:07:55 +00:00
|
|
|
c.updateState(StatePermanentError)
|
2019-09-05 20:01:40 +00:00
|
|
|
return NewConnError(err, true)
|
2018-01-11 22:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch v := val.(type) {
|
2019-06-26 15:14:52 +00:00
|
|
|
case stanza.StreamError:
|
2019-09-05 20:01:40 +00:00
|
|
|
c.streamError("conflict", "no auth loop")
|
|
|
|
return NewConnError(errors.New("handshake failed "+v.Error.Local), true)
|
2019-06-26 15:14:52 +00:00
|
|
|
case stanza.Handshake:
|
2019-06-08 17:42:02 +00:00
|
|
|
// Start the receiver go routine
|
2019-09-05 20:01:40 +00:00
|
|
|
c.updateState(StateSessionEstablished)
|
2019-12-05 17:12:00 +00:00
|
|
|
go c.recv()
|
|
|
|
return err // Should be empty at this point
|
2018-01-11 22:00:59 +00:00
|
|
|
default:
|
2019-11-08 11:07:55 +00:00
|
|
|
c.updateState(StatePermanentError)
|
2019-09-05 20:01:40 +00:00
|
|
|
return NewConnError(errors.New("expecting handshake result, got "+v.Name()), true)
|
2018-01-11 22:00:59 +00:00
|
|
|
}
|
2018-01-12 17:14:41 +00:00
|
|
|
}
|
2018-01-11 22:00:59 +00:00
|
|
|
|
2019-12-26 13:47:02 +00:00
|
|
|
func (c *Component) Disconnect() error {
|
2019-06-08 17:42:02 +00:00
|
|
|
// TODO: Add a way to wait for stream close acknowledgement from the server for clean disconnect
|
2019-10-11 04:24:47 +00:00
|
|
|
if c.transport != nil {
|
2019-12-26 13:47:02 +00:00
|
|
|
return c.transport.Close()
|
2019-09-06 08:28:49 +00:00
|
|
|
}
|
2019-12-26 13:47:02 +00:00
|
|
|
// No transport so no connection.
|
|
|
|
return nil
|
2019-06-08 17:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Component) SetHandler(handler EventHandler) {
|
|
|
|
c.Handler = handler
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:22:39 +00:00
|
|
|
// Receiver Go routine receiver
|
2019-12-05 17:12:00 +00:00
|
|
|
func (c *Component) recv() {
|
2019-06-08 17:42:02 +00:00
|
|
|
for {
|
2019-11-22 14:07:40 +00:00
|
|
|
val, err := stanza.NextPacket(c.transport.GetDecoder())
|
2019-06-08 17:42:02 +00:00
|
|
|
if err != nil {
|
|
|
|
c.updateState(StateDisconnected)
|
2019-12-05 17:12:00 +00:00
|
|
|
c.ErrorHandler(err)
|
2019-11-28 16:15:15 +00:00
|
|
|
return
|
2019-06-08 17:42:02 +00:00
|
|
|
}
|
|
|
|
// Handle stream errors
|
|
|
|
switch p := val.(type) {
|
2019-06-26 15:14:52 +00:00
|
|
|
case stanza.StreamError:
|
2019-06-24 09:13:25 +00:00
|
|
|
c.router.route(c, val)
|
2019-06-08 17:42:02 +00:00
|
|
|
c.streamError(p.Error.Local, p.Text)
|
2019-12-05 17:12:00 +00:00
|
|
|
c.ErrorHandler(errors.New("stream error: " + p.Error.Local))
|
2019-12-26 13:47:02 +00:00
|
|
|
// We don't return here, because we want to wait for the stream close tag from the server, or timeout.
|
|
|
|
c.Disconnect()
|
|
|
|
case stanza.StreamClosePacket:
|
|
|
|
// TCP messages should arrive in order, so we can expect to get nothing more after this occurs
|
|
|
|
c.transport.ReceivedStreamClose()
|
2019-11-28 16:15:15 +00:00
|
|
|
return
|
2019-06-08 17:42:02 +00:00
|
|
|
}
|
2019-06-24 09:13:25 +00:00
|
|
|
c.router.route(c, val)
|
2019-06-08 17:42:02 +00:00
|
|
|
}
|
2018-01-12 18:08:47 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 08:55:39 +00:00
|
|
|
// Send marshalls XMPP stanza and sends it to the server.
|
2019-06-26 15:14:52 +00:00
|
|
|
func (c *Component) Send(packet stanza.Packet) error {
|
2019-10-11 04:24:47 +00:00
|
|
|
transport := c.transport
|
|
|
|
if transport == nil {
|
2019-06-26 13:58:42 +00:00
|
|
|
return errors.New("component is not connected")
|
|
|
|
}
|
|
|
|
|
2018-01-17 17:47:34 +00:00
|
|
|
data, err := xml.Marshal(packet)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("cannot marshal packet " + err.Error())
|
|
|
|
}
|
|
|
|
|
2019-12-09 12:31:01 +00:00
|
|
|
if err := c.sendWithWriter(transport, data); err != nil {
|
2018-01-17 17:47:34 +00:00
|
|
|
return errors.New("cannot send packet " + err.Error())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-09 12:31:01 +00:00
|
|
|
func (c *Component) sendWithWriter(writer io.Writer, packet []byte) error {
|
|
|
|
var err error
|
|
|
|
_, err = writer.Write(packet)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-31 19:08:39 +00:00
|
|
|
// SendIQ sends an IQ set or get stanza to the server. If a result is received
|
|
|
|
// the provided handler function will automatically be called.
|
|
|
|
//
|
|
|
|
// The provided context should have a timeout to prevent the client from waiting
|
|
|
|
// forever for an IQ result. For example:
|
|
|
|
//
|
|
|
|
// ctx, _ := context.WithTimeout(context.Background(), 30 * time.Second)
|
|
|
|
// result := <- client.SendIQ(ctx, iq)
|
|
|
|
//
|
2020-01-31 10:48:03 +00:00
|
|
|
func (c *Component) SendIQ(ctx context.Context, iq *stanza.IQ) (chan stanza.IQ, error) {
|
2019-11-28 16:15:15 +00:00
|
|
|
if iq.Attrs.Type != stanza.IQTypeSet && iq.Attrs.Type != stanza.IQTypeGet {
|
2019-10-31 19:08:39 +00:00
|
|
|
return nil, ErrCanOnlySendGetOrSetIq
|
|
|
|
}
|
|
|
|
if err := c.Send(iq); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.router.NewIQResultRoute(ctx, iq.Attrs.Id), nil
|
|
|
|
}
|
|
|
|
|
2018-01-26 08:55:39 +00:00
|
|
|
// SendRaw sends an XMPP stanza as a string to the server.
|
|
|
|
// It can be invalid XML or XMPP content. In that case, the server will
|
|
|
|
// disconnect the component. It is up to the user of this method to
|
|
|
|
// carefully craft the XML content to produce valid XMPP.
|
|
|
|
func (c *Component) SendRaw(packet string) error {
|
2019-10-11 04:24:47 +00:00
|
|
|
transport := c.transport
|
|
|
|
if transport == nil {
|
2019-06-26 13:58:42 +00:00
|
|
|
return errors.New("component is not connected")
|
|
|
|
}
|
|
|
|
|
2019-06-09 10:21:20 +00:00
|
|
|
var err error
|
2019-12-09 12:31:01 +00:00
|
|
|
err = c.sendWithWriter(transport, []byte(packet))
|
2019-06-09 10:21:20 +00:00
|
|
|
return err
|
2018-01-26 08:55:39 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:16:55 +00:00
|
|
|
// handshake generates an authentication token based on StreamID and shared secret.
|
|
|
|
func (c *Component) handshake(streamId string) string {
|
|
|
|
// 1. Concatenate the Stream ID received from the server with the shared secret.
|
|
|
|
concatStr := streamId + c.Secret
|
|
|
|
|
|
|
|
// 2. Hash the concatenated string according to the SHA1 algorithm, i.e., SHA1( concat (sid, password)).
|
|
|
|
h := sha1.New()
|
|
|
|
h.Write([]byte(concatStr))
|
|
|
|
hash := h.Sum(nil)
|
|
|
|
|
|
|
|
// 3. Ensure that the hash output is in hexadecimal format, not binary or base64.
|
|
|
|
// 4. Convert the hash output to all lowercase characters.
|
|
|
|
encodedStr := hex.EncodeToString(hash)
|
|
|
|
|
|
|
|
return encodedStr
|
|
|
|
}
|
|
|
|
|
2019-06-09 11:02:58 +00:00
|
|
|
/*
|
|
|
|
TODO: Add support for discovery management directly in component
|
|
|
|
TODO: Support multiple identities on disco info
|
|
|
|
TODO: Support returning features on disco info
|
|
|
|
*/
|