2019-06-18 14:28:30 +00:00
|
|
|
package xmpp
|
2016-01-06 15:51:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2016-02-17 12:45:39 +00:00
|
|
|
"time"
|
2019-06-26 15:14:52 +00:00
|
|
|
|
|
|
|
"gosrc.io/xmpp/stanza"
|
2016-01-06 15:51:12 +00:00
|
|
|
)
|
|
|
|
|
2019-06-06 09:58:50 +00:00
|
|
|
//=============================================================================
|
2019-06-08 16:09:22 +00:00
|
|
|
// EventManager
|
2018-09-26 15:26:14 +00:00
|
|
|
|
2019-06-06 09:58:50 +00:00
|
|
|
// ConnState represents the current connection state.
|
|
|
|
type ConnState = uint8
|
2018-09-26 15:26:14 +00:00
|
|
|
|
2019-06-06 09:58:50 +00:00
|
|
|
// This is a the list of events happening on the connection that the
|
|
|
|
// client can be notified about.
|
|
|
|
const (
|
|
|
|
StateDisconnected ConnState = iota
|
|
|
|
StateConnected
|
|
|
|
StateSessionEstablished
|
2019-06-08 09:15:51 +00:00
|
|
|
StateStreamError
|
2019-06-06 09:58:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Event is a structure use to convey event changes related to client state. This
|
|
|
|
// is for example used to notify the client when the client get disconnected.
|
|
|
|
type Event struct {
|
|
|
|
State ConnState
|
|
|
|
Description string
|
2019-06-08 09:15:51 +00:00
|
|
|
StreamError string
|
2018-09-26 15:26:14 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 09:58:50 +00:00
|
|
|
// EventHandler is use to pass events about state of the connection to
|
|
|
|
// client implementation.
|
|
|
|
type EventHandler func(Event)
|
|
|
|
|
|
|
|
type EventManager struct {
|
|
|
|
// Store current state
|
|
|
|
CurrentState ConnState
|
|
|
|
|
|
|
|
// Callback used to propagate connection state changes
|
|
|
|
Handler EventHandler
|
2018-09-26 15:26:14 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 09:58:50 +00:00
|
|
|
func (em EventManager) updateState(state ConnState) {
|
|
|
|
em.CurrentState = state
|
|
|
|
if em.Handler != nil {
|
|
|
|
em.Handler(Event{State: em.CurrentState})
|
|
|
|
}
|
2018-09-26 15:26:14 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 09:15:51 +00:00
|
|
|
func (em EventManager) streamError(error, desc string) {
|
|
|
|
em.CurrentState = StateStreamError
|
|
|
|
if em.Handler != nil {
|
|
|
|
em.Handler(Event{State: em.CurrentState, StreamError: error, Description: desc})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 15:26:14 +00:00
|
|
|
// Client
|
|
|
|
// ============================================================================
|
|
|
|
|
2018-02-13 21:07:15 +00:00
|
|
|
// Client is the main structure used to connect as a client on an XMPP
|
2016-02-15 10:05:44 +00:00
|
|
|
// server.
|
2016-01-06 15:51:12 +00:00
|
|
|
type Client struct {
|
2019-06-06 09:58:50 +00:00
|
|
|
// Store user defined options and states
|
2018-09-26 14:25:04 +00:00
|
|
|
config Config
|
2016-01-06 15:51:12 +00:00
|
|
|
// Session gather data that can be accessed by users of this library
|
|
|
|
Session *Session
|
2017-10-21 11:58:58 +00:00
|
|
|
// TCP level connection / can be replaced by a TLS session after starttls
|
2016-01-06 15:51:12 +00:00
|
|
|
conn net.Conn
|
2019-06-18 10:34:25 +00:00
|
|
|
// Router is used to dispatch packets
|
|
|
|
router *Router
|
2019-06-06 09:58:50 +00:00
|
|
|
// Track and broadcast connection state
|
|
|
|
EventManager
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Setting up the client / Checking the parameters
|
|
|
|
*/
|
|
|
|
|
2018-09-26 14:25:04 +00:00
|
|
|
// NewClient generates a new XMPP client, based on Config passed as parameters.
|
2018-09-23 16:43:46 +00:00
|
|
|
// If host is not specified, the DNS SRV should be used to find the host from the domainpart of the JID.
|
2016-02-13 16:01:06 +00:00
|
|
|
// Default the port to 5222.
|
2019-06-18 10:34:25 +00:00
|
|
|
func NewClient(config Config, r *Router) (c *Client, err error) {
|
2019-06-26 07:04:03 +00:00
|
|
|
// Parse JID
|
|
|
|
if config.parsedJid, err = NewJid(config.Jid); err != nil {
|
|
|
|
err = errors.New("missing jid")
|
2019-06-07 13:23:23 +00:00
|
|
|
return nil, NewConnError(err, true)
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
2019-06-26 10:29:39 +00:00
|
|
|
|
2018-09-26 14:25:04 +00:00
|
|
|
if config.Password == "" {
|
2016-01-06 15:51:12 +00:00
|
|
|
err = errors.New("missing password")
|
2019-06-07 13:23:23 +00:00
|
|
|
return nil, NewConnError(err, true)
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
2019-06-26 10:29:39 +00:00
|
|
|
|
2019-06-26 07:04:03 +00:00
|
|
|
// fallback to jid domain
|
|
|
|
if config.Address == "" {
|
|
|
|
config.Address = config.parsedJid.Domain
|
2019-07-27 16:22:04 +00:00
|
|
|
|
2019-07-17 23:27:11 +00:00
|
|
|
// fetch srv DNS-Entries
|
|
|
|
_, srvEntries, err := net.LookupSRV("xmpp-client", "tcp", config.parsedJid.Domain)
|
2019-07-27 16:22:04 +00:00
|
|
|
|
2019-07-17 23:27:11 +00:00
|
|
|
if err == nil && len(srvEntries) > 0 {
|
|
|
|
// if find some use the entry with heightest weight
|
|
|
|
bestSrv := srvEntries[0]
|
|
|
|
for _, srv := range srvEntries {
|
|
|
|
if srv.Priority <= bestSrv.Priority && srv.Weight >= bestSrv.Weight {
|
|
|
|
bestSrv = srv
|
2019-07-27 16:22:04 +00:00
|
|
|
config.Address = ensurePort(srv.Target, int(srv.Port))
|
2019-07-17 23:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 07:04:03 +00:00
|
|
|
}
|
2019-06-26 10:29:39 +00:00
|
|
|
config.Address = ensurePort(config.Address, 5222)
|
2016-01-06 15:51:12 +00:00
|
|
|
|
2019-06-07 10:16:58 +00:00
|
|
|
c = new(Client)
|
|
|
|
c.config = config
|
2019-06-18 10:34:25 +00:00
|
|
|
c.router = r
|
2019-06-07 10:16:58 +00:00
|
|
|
|
2018-09-26 14:25:04 +00:00
|
|
|
if c.config.ConnectTimeout == 0 {
|
|
|
|
c.config.ConnectTimeout = 15 // 15 second as default
|
2016-02-17 12:45:39 +00:00
|
|
|
}
|
2019-06-06 09:58:50 +00:00
|
|
|
|
2016-01-06 15:51:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-13 16:01:06 +00:00
|
|
|
// Connect triggers actual TCP connection, based on previously defined parameters.
|
2019-06-08 16:09:22 +00:00
|
|
|
func (c *Client) Connect() error {
|
2016-01-06 15:51:12 +00:00
|
|
|
var err error
|
2016-02-17 12:45:39 +00:00
|
|
|
|
2019-06-06 09:58:50 +00:00
|
|
|
c.conn, err = net.DialTimeout("tcp", c.config.Address, time.Duration(c.config.ConnectTimeout)*time.Second)
|
2016-02-17 12:45:39 +00:00
|
|
|
if err != nil {
|
2019-06-08 16:09:22 +00:00
|
|
|
return err
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
2019-06-06 09:58:50 +00:00
|
|
|
c.updateState(StateConnected)
|
2016-01-06 15:51:12 +00:00
|
|
|
|
2019-06-08 16:52:19 +00:00
|
|
|
// Client is ok, we now open XMPP session
|
2018-09-26 14:25:04 +00:00
|
|
|
if c.conn, c.Session, err = NewSession(c.conn, c.config); err != nil {
|
2019-06-08 16:09:22 +00:00
|
|
|
return err
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
2019-06-06 09:58:50 +00:00
|
|
|
c.updateState(StateSessionEstablished)
|
2016-01-06 15:51:12 +00:00
|
|
|
|
|
|
|
// We're connected and can now receive and send messages.
|
|
|
|
//fmt.Fprintf(client.conn, "<presence xml:lang='en'><show>%s</show><status>%s</status></presence>", "chat", "Online")
|
2016-01-06 16:08:51 +00:00
|
|
|
// TODO: Do we always want to send initial presence automatically ?
|
|
|
|
// Do we need an option to avoid that or do we rely on client to send the presence itself ?
|
2019-06-29 08:45:25 +00:00
|
|
|
fmt.Fprintf(c.Session.streamLogger, "<presence/>")
|
2016-01-06 15:51:12 +00:00
|
|
|
|
2019-06-11 13:29:08 +00:00
|
|
|
// Start the keepalive go routine
|
|
|
|
keepaliveQuit := make(chan struct{})
|
|
|
|
go keepalive(c.conn, keepaliveQuit)
|
2019-06-06 09:58:50 +00:00
|
|
|
// Start the receiver go routine
|
2019-06-11 13:29:08 +00:00
|
|
|
go c.recv(keepaliveQuit)
|
2019-06-06 09:58:50 +00:00
|
|
|
|
2019-06-08 16:09:22 +00:00
|
|
|
return err
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 09:58:50 +00:00
|
|
|
func (c *Client) Disconnect() {
|
|
|
|
_ = c.SendRaw("</stream:stream>")
|
|
|
|
// TODO: Add a way to wait for stream close acknowledgement from the server for clean disconnect
|
|
|
|
_ = c.conn.Close()
|
|
|
|
}
|
|
|
|
|
2019-06-08 16:09:22 +00:00
|
|
|
func (c *Client) SetHandler(handler EventHandler) {
|
|
|
|
c.Handler = handler
|
|
|
|
}
|
|
|
|
|
2019-06-11 13:29:08 +00:00
|
|
|
// Send marshals XMPP stanza and sends it to the server.
|
2019-06-26 15:14:52 +00:00
|
|
|
func (c *Client) Send(packet stanza.Packet) error {
|
2019-06-26 13:58:42 +00:00
|
|
|
conn := c.conn
|
|
|
|
if conn == nil {
|
|
|
|
return errors.New("client is not connected")
|
|
|
|
}
|
|
|
|
|
2018-01-26 08:55:39 +00:00
|
|
|
data, err := xml.Marshal(packet)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("cannot marshal packet " + err.Error())
|
|
|
|
}
|
|
|
|
|
2019-06-29 08:45:25 +00:00
|
|
|
return c.sendWithLogger(string(data))
|
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 client. It is up to the user of this method to
|
|
|
|
// carefully craft the XML content to produce valid XMPP.
|
|
|
|
func (c *Client) SendRaw(packet string) error {
|
2019-06-26 13:58:42 +00:00
|
|
|
conn := c.conn
|
|
|
|
if conn == nil {
|
|
|
|
return errors.New("client is not connected")
|
|
|
|
}
|
|
|
|
|
2019-06-29 08:45:25 +00:00
|
|
|
return c.sendWithLogger(packet)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) sendWithLogger(packet string) error {
|
2019-06-06 09:58:50 +00:00
|
|
|
var err error
|
2019-06-29 08:45:25 +00:00
|
|
|
_, err = fmt.Fprintf(c.Session.streamLogger, packet)
|
2019-06-06 09:58:50 +00:00
|
|
|
return err
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 13:29:08 +00:00
|
|
|
// ============================================================================
|
|
|
|
// Go routines
|
|
|
|
|
|
|
|
// Loop: Receive data from server
|
|
|
|
func (c *Client) recv(keepaliveQuit chan<- struct{}) (err error) {
|
|
|
|
for {
|
2019-06-26 15:14:52 +00:00
|
|
|
val, err := stanza.NextPacket(c.Session.decoder)
|
2019-06-11 13:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
close(keepaliveQuit)
|
|
|
|
c.updateState(StateDisconnected)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle stream errors
|
|
|
|
switch packet := 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-18 10:34:25 +00:00
|
|
|
close(keepaliveQuit)
|
2019-06-11 13:29:08 +00:00
|
|
|
c.streamError(packet.Error.Local, packet.Text)
|
|
|
|
return errors.New("stream error: " + packet.Error.Local)
|
|
|
|
}
|
|
|
|
|
2019-06-24 09:13:25 +00:00
|
|
|
c.router.route(c, val)
|
2019-06-11 13:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop: send whitespace keepalive to server
|
|
|
|
// This is use to keep the connection open, but also to detect connection loss
|
|
|
|
// and trigger proper client connection shutdown.
|
|
|
|
func keepalive(conn net.Conn, quit <-chan struct{}) {
|
|
|
|
// TODO: Make keepalive interval configurable
|
|
|
|
ticker := time.NewTicker(30 * time.Second)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
if n, err := fmt.Fprintf(conn, "\n"); err != nil || n != 1 {
|
|
|
|
// When keep alive fails, we force close the connection. In all cases, the recv will also fail.
|
|
|
|
ticker.Stop()
|
|
|
|
_ = conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-quit:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|