2017-12-17 17:50:51 +01:00
|
|
|
package toserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
"golang.org/x/crypto/acme/autocert"
|
|
|
|
|
2018-02-07 15:34:18 +01:00
|
|
|
"dev.sum7.eu/genofire/yaja/database"
|
|
|
|
"dev.sum7.eu/genofire/yaja/server/extension"
|
|
|
|
"dev.sum7.eu/genofire/yaja/server/state"
|
|
|
|
"dev.sum7.eu/genofire/yaja/server/utils"
|
2018-02-14 18:49:26 +01:00
|
|
|
"dev.sum7.eu/genofire/yaja/xmpp"
|
2017-12-17 17:50:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ConnectionStartup return steps through TCP TLS state
|
|
|
|
func ConnectionStartup(db *database.State, tlsconfig *tls.Config, tlsmgmt *autocert.Manager, extensions extension.Extensions, c *utils.Client) state.State {
|
|
|
|
receiving := &state.ReceivingClient{Extensions: extensions, Client: c}
|
|
|
|
sending := &state.SendingClient{Next: receiving, Client: c}
|
|
|
|
tlsstream := &TLSStream{
|
|
|
|
Next: sending,
|
|
|
|
Client: c,
|
|
|
|
}
|
|
|
|
tlsupgrade := &state.TLSUpgrade{
|
|
|
|
Next: tlsstream,
|
|
|
|
Client: c,
|
|
|
|
TLSConfig: tlsconfig,
|
|
|
|
TLSManager: tlsmgmt,
|
|
|
|
}
|
|
|
|
dail := &Dailback{
|
|
|
|
Next: tlsupgrade,
|
|
|
|
Client: c,
|
|
|
|
}
|
|
|
|
return &state.Start{Next: dail, Client: c}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLSStream state
|
|
|
|
type Dailback struct {
|
|
|
|
Next state.State
|
|
|
|
Client *utils.Client
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
// Process xmpp
|
2017-12-17 17:50:51 +01:00
|
|
|
func (state *Dailback) Process() state.State {
|
|
|
|
state.Client.Log = state.Client.Log.WithField("state", "dialback")
|
|
|
|
state.Client.Log.Debug("running")
|
|
|
|
defer state.Client.Log.Debug("leave")
|
|
|
|
|
|
|
|
element, err := state.Client.Read()
|
|
|
|
if err != nil {
|
|
|
|
state.Client.Log.Warn("unable to read: ", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// dailback encode
|
|
|
|
type dailback struct {
|
|
|
|
XMLName xml.Name `xml:"urn:xmpp:ping ping"`
|
|
|
|
}
|
|
|
|
db := &dailback{}
|
|
|
|
if err = state.Client.In.DecodeElement(db, element); err != nil {
|
|
|
|
return state.Next
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Client.Log.Info(db)
|
|
|
|
return state.Next
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLSStream state
|
|
|
|
type TLSStream struct {
|
|
|
|
Next state.State
|
|
|
|
Client *utils.Client
|
|
|
|
domainRegisterAllowed utils.DomainRegisterAllowed
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
// Process xmpp
|
2017-12-17 17:50:51 +01:00
|
|
|
func (state *TLSStream) Process() state.State {
|
|
|
|
state.Client.Log = state.Client.Log.WithField("state", "tls stream")
|
|
|
|
state.Client.Log.Debug("running")
|
|
|
|
defer state.Client.Log.Debug("leave")
|
|
|
|
|
|
|
|
element, err := state.Client.Read()
|
|
|
|
if err != nil {
|
|
|
|
state.Client.Log.Warn("unable to read: ", err)
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-14 18:49:26 +01:00
|
|
|
if element.Name.Space != xmpp.NSStream || element.Name.Local != "stream" {
|
2017-12-17 17:50:51 +01:00
|
|
|
state.Client.Log.Warn("is no stream")
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(state.Client.Conn, `<?xml version='1.0'?>
|
2018-02-07 15:34:18 +01:00
|
|
|
<stream:stream id='%x' version='1.0' xmlns='%s' xmlns:stream='%s'>
|
|
|
|
<stream:features>
|
2017-12-17 17:50:51 +01:00
|
|
|
<mechanisms xmlns='%s'>
|
|
|
|
<mechanism>EXTERNAL</mechanism>
|
|
|
|
</mechanisms>
|
2018-02-07 15:34:18 +01:00
|
|
|
<bidi xmlns='urn:xmpp:features:bidi'/>
|
2017-12-17 17:50:51 +01:00
|
|
|
</stream:features>`,
|
2018-02-14 18:49:26 +01:00
|
|
|
xmpp.CreateCookie(), xmpp.NSClient, xmpp.NSStream,
|
|
|
|
xmpp.NSSASL)
|
2017-12-17 17:50:51 +01:00
|
|
|
|
|
|
|
return state.Next
|
|
|
|
}
|
|
|
|
|
|
|
|
// SASLAuth state
|
|
|
|
type SASLAuth struct {
|
|
|
|
Next state.State
|
|
|
|
Client *utils.Client
|
|
|
|
database *database.State
|
|
|
|
domainRegisterAllowed utils.DomainRegisterAllowed
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
// Process xmpp
|
2017-12-17 17:50:51 +01:00
|
|
|
func (state *SASLAuth) Process() state.State {
|
|
|
|
state.Client.Log = state.Client.Log.WithField("state", "sasl auth")
|
|
|
|
state.Client.Log.Debug("running")
|
|
|
|
defer state.Client.Log.Debug("leave")
|
|
|
|
|
|
|
|
// read the full auth stanza
|
|
|
|
element, err := state.Client.Read()
|
|
|
|
if err != nil {
|
|
|
|
state.Client.Log.Warn("unable to read: ", err)
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-14 18:49:26 +01:00
|
|
|
var auth xmpp.SASLAuth
|
2017-12-17 17:50:51 +01:00
|
|
|
if err = state.Client.In.DecodeElement(&auth, element); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
data, err := base64.StdEncoding.DecodeString(auth.Body)
|
|
|
|
if err != nil {
|
|
|
|
state.Client.Log.Warn("body decode: ", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Client.Log.Debug(auth.Mechanism, string(data))
|
|
|
|
|
|
|
|
state.Client.Log.Info("success auth")
|
2018-02-14 18:49:26 +01:00
|
|
|
fmt.Fprintf(state.Client.Conn, "<success xmlns='%s'/>", xmpp.NSSASL)
|
2017-12-17 17:50:51 +01:00
|
|
|
return state.Next
|
|
|
|
}
|