2019-06-18 14:28:30 +00:00
|
|
|
package xmpp
|
2016-01-06 15:51:12 +00:00
|
|
|
|
|
|
|
import (
|
2020-03-06 15:44:01 +00:00
|
|
|
"encoding/xml"
|
2016-01-06 15:51:12 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-06-26 15:14:52 +00:00
|
|
|
"gosrc.io/xmpp/stanza"
|
2020-03-06 15:44:01 +00:00
|
|
|
"strconv"
|
2016-01-06 15:51:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Session struct {
|
|
|
|
// Session info
|
|
|
|
BindJid string // Jabber ID as provided by XMPP server
|
|
|
|
StreamId string
|
2019-07-31 16:47:30 +00:00
|
|
|
SMState SMState
|
2019-06-26 15:14:52 +00:00
|
|
|
Features stanza.StreamFeatures
|
2016-01-06 15:51:12 +00:00
|
|
|
TlsEnabled bool
|
|
|
|
lastPacketId int
|
|
|
|
|
|
|
|
// read / write
|
2019-10-18 18:29:54 +00:00
|
|
|
transport Transport
|
2016-01-06 15:51:12 +00:00
|
|
|
|
|
|
|
// error management
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2020-03-06 15:44:01 +00:00
|
|
|
func NewSession(c *Client, state SMState) (*Session, error) {
|
|
|
|
var s *Session
|
|
|
|
if c.Session == nil {
|
|
|
|
s = new(Session)
|
|
|
|
s.transport = c.transport
|
|
|
|
s.SMState = state
|
|
|
|
s.init()
|
|
|
|
} else {
|
|
|
|
s = c.Session
|
|
|
|
// We keep information about the previously set session, like the session ID, but we read server provided
|
|
|
|
// info again in case it changed between session break and resume, such as features.
|
|
|
|
s.init()
|
|
|
|
}
|
2016-01-06 15:51:12 +00:00
|
|
|
|
2019-07-31 09:43:54 +00:00
|
|
|
if s.err != nil {
|
2019-10-06 17:37:56 +00:00
|
|
|
return nil, NewConnError(s.err, true)
|
2019-07-31 09:43:54 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:44:01 +00:00
|
|
|
if !c.transport.IsSecure() {
|
|
|
|
s.startTlsIfSupported(c.config)
|
2019-10-28 13:46:13 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:44:01 +00:00
|
|
|
if !c.transport.IsSecure() && !c.config.Insecure {
|
2019-07-15 10:18:35 +00:00
|
|
|
err := fmt.Errorf("failed to negotiate TLS session : %s", s.err)
|
2019-10-06 17:37:56 +00:00
|
|
|
return nil, NewConnError(err, true)
|
2019-07-15 10:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.TlsEnabled {
|
2020-03-06 15:44:01 +00:00
|
|
|
s.reset()
|
2017-10-21 12:49:25 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 15:51:12 +00:00
|
|
|
// auth
|
2020-03-06 15:44:01 +00:00
|
|
|
s.auth(c.config)
|
|
|
|
if s.err != nil {
|
|
|
|
return s, s.err
|
|
|
|
}
|
|
|
|
s.reset()
|
|
|
|
if s.err != nil {
|
|
|
|
return s, s.err
|
|
|
|
}
|
2016-01-06 15:51:12 +00:00
|
|
|
|
2019-07-31 16:47:30 +00:00
|
|
|
// attempt resumption
|
2020-03-06 15:44:01 +00:00
|
|
|
if s.resume(c.config) {
|
2019-10-06 17:37:56 +00:00
|
|
|
return s, s.err
|
2019-07-31 16:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, bind resource and 'start' XMPP session
|
2020-03-06 15:44:01 +00:00
|
|
|
s.bind(c.config)
|
|
|
|
if s.err != nil {
|
|
|
|
return s, s.err
|
|
|
|
}
|
|
|
|
s.rfc3921Session()
|
|
|
|
if s.err != nil {
|
|
|
|
return s, s.err
|
|
|
|
}
|
2016-01-06 15:51:12 +00:00
|
|
|
|
2019-07-31 16:47:30 +00:00
|
|
|
// Enable stream management if supported
|
2020-03-06 15:44:01 +00:00
|
|
|
s.EnableStreamManagement(c.config)
|
|
|
|
if s.err != nil {
|
|
|
|
return s, s.err
|
|
|
|
}
|
2019-07-31 16:47:30 +00:00
|
|
|
|
2019-10-06 17:37:56 +00:00
|
|
|
return s, s.err
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) PacketId() string {
|
|
|
|
s.lastPacketId++
|
|
|
|
return fmt.Sprintf("%x", s.lastPacketId)
|
|
|
|
}
|
|
|
|
|
2020-03-06 15:44:01 +00:00
|
|
|
// init gathers information on the session such as stream features from the server.
|
|
|
|
func (s *Session) init() {
|
|
|
|
s.Features = s.extractStreamFeatures()
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:44:01 +00:00
|
|
|
func (s *Session) reset() {
|
2019-10-25 13:55:27 +00:00
|
|
|
if s.StreamId, s.err = s.transport.StartStream(); s.err != nil {
|
2016-01-06 15:51:12 +00:00
|
|
|
return
|
|
|
|
}
|
2019-10-25 13:55:27 +00:00
|
|
|
|
2020-03-06 15:44:01 +00:00
|
|
|
s.Features = s.extractStreamFeatures()
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:44:01 +00:00
|
|
|
func (s *Session) extractStreamFeatures() (f stanza.StreamFeatures) {
|
2016-01-06 15:51:12 +00:00
|
|
|
// extract stream features
|
2019-10-18 18:29:54 +00:00
|
|
|
if s.err = s.transport.GetDecoder().Decode(&f); s.err != nil {
|
2016-01-06 15:51:12 +00:00
|
|
|
s.err = errors.New("stream open decode features: " + s.err.Error())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-06 15:44:01 +00:00
|
|
|
func (s *Session) startTlsIfSupported(o *Config) {
|
2016-01-06 15:51:12 +00:00
|
|
|
if s.err != nil {
|
2019-10-06 17:37:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-18 18:29:54 +00:00
|
|
|
if !s.transport.DoesStartTLS() {
|
2019-10-06 17:37:56 +00:00
|
|
|
if !o.Insecure {
|
2020-01-31 10:48:03 +00:00
|
|
|
s.err = errors.New("transport does not support starttls")
|
2019-10-06 17:37:56 +00:00
|
|
|
}
|
|
|
|
return
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 14:27:52 +00:00
|
|
|
if _, ok := s.Features.DoesStartTLS(); ok {
|
2019-10-18 18:29:54 +00:00
|
|
|
fmt.Fprintf(s.transport, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>")
|
2016-01-06 15:51:12 +00:00
|
|
|
|
2019-06-26 15:14:52 +00:00
|
|
|
var k stanza.TLSProceed
|
2019-10-18 18:29:54 +00:00
|
|
|
if s.err = s.transport.GetDecoder().DecodeElement(&k, nil); s.err != nil {
|
2016-01-06 15:51:12 +00:00
|
|
|
s.err = errors.New("expecting starttls proceed: " + s.err.Error())
|
2019-10-06 17:37:56 +00:00
|
|
|
return
|
2019-07-15 22:26:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 13:55:27 +00:00
|
|
|
s.err = s.transport.StartTLS()
|
2019-07-15 10:18:35 +00:00
|
|
|
|
|
|
|
if s.err == nil {
|
|
|
|
s.TlsEnabled = true
|
|
|
|
}
|
2019-10-06 17:37:56 +00:00
|
|
|
return
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 16:15:16 +00:00
|
|
|
// If we do not allow cleartext serverConnections, make it explicit that server do not support starttls
|
2019-07-15 10:18:35 +00:00
|
|
|
if !o.Insecure {
|
|
|
|
s.err = errors.New("XMPP server does not advertise support for starttls")
|
|
|
|
}
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:44:01 +00:00
|
|
|
func (s *Session) auth(o *Config) {
|
2016-01-06 15:51:12 +00:00
|
|
|
if s.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-18 18:29:54 +00:00
|
|
|
s.err = authSASL(s.transport, s.transport.GetDecoder(), s.Features, o.parsedJid.Node, o.Credential)
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 16:47:30 +00:00
|
|
|
// Attempt to resume session using stream management
|
2020-03-06 15:44:01 +00:00
|
|
|
func (s *Session) resume(o *Config) bool {
|
2019-07-31 16:47:30 +00:00
|
|
|
if !s.Features.DoesStreamManagement() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if s.SMState.Id == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-03-06 15:44:01 +00:00
|
|
|
rsm := stanza.SMResume{
|
|
|
|
PrevId: s.SMState.Id,
|
|
|
|
H: &s.SMState.Inbound,
|
|
|
|
}
|
|
|
|
data, err := xml.Marshal(rsm)
|
2019-07-31 16:47:30 +00:00
|
|
|
|
2020-03-06 15:44:01 +00:00
|
|
|
_, err = s.transport.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2019-07-31 16:47:30 +00:00
|
|
|
var packet stanza.Packet
|
2019-10-18 18:29:54 +00:00
|
|
|
packet, s.err = stanza.NextPacket(s.transport.GetDecoder())
|
2019-07-31 16:47:30 +00:00
|
|
|
if s.err == nil {
|
|
|
|
switch p := packet.(type) {
|
|
|
|
case stanza.SMResumed:
|
|
|
|
if p.PrevId != s.SMState.Id {
|
|
|
|
s.err = errors.New("session resumption: mismatched id")
|
|
|
|
s.SMState = SMState{}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
case stanza.SMFailed:
|
|
|
|
default:
|
|
|
|
s.err = errors.New("unexpected reply to SM resume")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.SMState = SMState{}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-03-06 15:44:01 +00:00
|
|
|
func (s *Session) bind(o *Config) {
|
2016-01-06 15:51:12 +00:00
|
|
|
if s.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send IQ message asking to bind to the local user name.
|
2019-06-07 13:56:41 +00:00
|
|
|
var resource = o.parsedJid.Resource
|
2020-03-06 15:44:01 +00:00
|
|
|
iqB, err := stanza.NewIQ(stanza.Attrs{
|
|
|
|
Type: stanza.IQTypeSet,
|
|
|
|
Id: s.PacketId(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we already have a resource name, and include it in the request if so
|
2016-01-06 15:51:12 +00:00
|
|
|
if resource != "" {
|
2020-03-06 15:44:01 +00:00
|
|
|
iqB.Payload = &stanza.Bind{
|
|
|
|
Resource: resource,
|
|
|
|
}
|
2016-01-06 15:51:12 +00:00
|
|
|
} else {
|
2020-03-06 15:44:01 +00:00
|
|
|
iqB.Payload = &stanza.Bind{}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the bind request IQ
|
|
|
|
data, err := xml.Marshal(iqB)
|
|
|
|
if err != nil {
|
|
|
|
s.err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n, err := s.transport.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
s.err = err
|
|
|
|
return
|
|
|
|
} else if n == 0 {
|
|
|
|
s.err = errors.New("failed to write bind iq stanza to the server : wrote 0 bytes")
|
|
|
|
return
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:44:01 +00:00
|
|
|
// Check the server response
|
2019-06-26 15:14:52 +00:00
|
|
|
var iq stanza.IQ
|
2019-10-18 18:29:54 +00:00
|
|
|
if s.err = s.transport.GetDecoder().Decode(&iq); s.err != nil {
|
2016-02-15 14:22:51 +00:00
|
|
|
s.err = errors.New("error decoding iq bind result: " + s.err.Error())
|
2016-01-06 15:51:12 +00:00
|
|
|
return
|
|
|
|
}
|
2016-02-15 14:22:51 +00:00
|
|
|
|
2018-01-15 11:28:34 +00:00
|
|
|
// TODO Check all elements
|
2019-06-19 08:21:57 +00:00
|
|
|
switch payload := iq.Payload.(type) {
|
2019-06-29 14:49:54 +00:00
|
|
|
case *stanza.Bind:
|
2016-02-15 14:22:51 +00:00
|
|
|
s.BindJid = payload.Jid // our local id (with possibly randomly generated resource
|
|
|
|
default:
|
|
|
|
s.err = errors.New("iq bind result missing")
|
|
|
|
}
|
|
|
|
|
2016-01-06 15:51:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-29 14:49:54 +00:00
|
|
|
// After the bind, if the session is not optional (as per old RFC 3921), we send the session open iq.
|
2020-03-06 15:44:01 +00:00
|
|
|
func (s *Session) rfc3921Session() {
|
2016-01-06 15:51:12 +00:00
|
|
|
if s.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:14:52 +00:00
|
|
|
var iq stanza.IQ
|
2019-06-29 15:48:38 +00:00
|
|
|
// We only negotiate session binding if it is mandatory, we skip it when optional.
|
2019-06-29 15:39:19 +00:00
|
|
|
if !s.Features.Session.IsOptional() {
|
2020-03-06 15:44:01 +00:00
|
|
|
se, err := stanza.NewIQ(stanza.Attrs{
|
|
|
|
Type: stanza.IQTypeSet,
|
|
|
|
Id: s.PacketId(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
se.Payload = &stanza.StreamSession{}
|
|
|
|
data, err := xml.Marshal(se)
|
|
|
|
if err != nil {
|
|
|
|
s.err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n, err := s.transport.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
s.err = err
|
|
|
|
return
|
|
|
|
} else if n == 0 {
|
|
|
|
s.err = errors.New("there was a problem marshaling the session IQ : wrote 0 bytes to server")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-18 18:29:54 +00:00
|
|
|
if s.err = s.transport.GetDecoder().Decode(&iq); s.err != nil {
|
2017-10-04 23:27:35 +00:00
|
|
|
s.err = errors.New("expecting iq result after session open: " + s.err.Error())
|
|
|
|
return
|
|
|
|
}
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-31 16:47:30 +00:00
|
|
|
|
|
|
|
// Enable stream management, with session resumption, if supported.
|
2020-03-06 15:44:01 +00:00
|
|
|
func (s *Session) EnableStreamManagement(o *Config) {
|
2019-07-31 16:47:30 +00:00
|
|
|
if s.err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-03-06 15:44:01 +00:00
|
|
|
if !s.Features.DoesStreamManagement() || !o.StreamManagementEnable {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
q := stanza.NewUnAckQueue()
|
|
|
|
ebleNonza := stanza.SMEnable{Resume: &o.streamManagementResume}
|
|
|
|
pktStr, err := xml.Marshal(ebleNonza)
|
|
|
|
if err != nil {
|
|
|
|
s.err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = s.transport.Write(pktStr)
|
|
|
|
if err != nil {
|
|
|
|
s.err = err
|
2019-07-31 16:47:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var packet stanza.Packet
|
2019-10-18 18:29:54 +00:00
|
|
|
packet, s.err = stanza.NextPacket(s.transport.GetDecoder())
|
2019-07-31 16:47:30 +00:00
|
|
|
if s.err == nil {
|
|
|
|
switch p := packet.(type) {
|
|
|
|
case stanza.SMEnabled:
|
2020-03-06 15:44:01 +00:00
|
|
|
// Server allows resumption or not using SMEnabled attribute "resume". We must read the server response
|
|
|
|
// and update config accordingly
|
|
|
|
b, err := strconv.ParseBool(p.Resume)
|
|
|
|
if err != nil || !b {
|
|
|
|
o.StreamManagementEnable = false
|
|
|
|
}
|
|
|
|
s.SMState = SMState{Id: p.Id, preferredReconAddr: p.Location}
|
|
|
|
s.SMState.UnAckQueue = q
|
2019-07-31 16:47:30 +00:00
|
|
|
case stanza.SMFailed:
|
2019-07-31 16:51:16 +00:00
|
|
|
// TODO: Store error in SMState, for later inspection
|
2020-03-06 15:44:01 +00:00
|
|
|
s.SMState = SMState{StreamErrorGroup: p.StreamErrorGroup}
|
|
|
|
s.SMState.UnAckQueue = q
|
|
|
|
s.err = errors.New("failed to establish session : " + s.SMState.StreamErrorGroup.GroupErrorName())
|
2019-07-31 16:47:30 +00:00
|
|
|
default:
|
|
|
|
s.err = errors.New("unexpected reply to SM enable")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|