2018-01-11 21:15:54 +00:00
|
|
|
package xmpp
|
|
|
|
|
|
|
|
import (
|
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"
|
2018-01-11 22:00:59 +00:00
|
|
|
"io"
|
2018-01-11 21:15:54 +00:00
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const componentStreamOpen = "<?xml version='1.0'?><stream:stream to='%s' xmlns='%s' xmlns:stream='%s'>"
|
|
|
|
|
|
|
|
// 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 {
|
2018-01-12 17:01:27 +00:00
|
|
|
Host string
|
|
|
|
Secret string
|
|
|
|
|
2018-01-11 21:15:54 +00:00
|
|
|
// TCP level connection
|
|
|
|
conn net.Conn
|
2018-01-11 22:00:59 +00:00
|
|
|
|
|
|
|
// read / write
|
2018-01-12 17:01:27 +00:00
|
|
|
socketProxy io.ReadWriter // TODO
|
2018-01-11 22:00:59 +00:00
|
|
|
decoder *xml.Decoder
|
2018-01-11 21:15:54 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 17:01:27 +00:00
|
|
|
type Handshake struct {
|
|
|
|
XMLName xml.Name `xml:"jabber:component:accept handshake"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:15:54 +00:00
|
|
|
// TODO Helper to prepare connection string
|
|
|
|
func Open(connStr string) error {
|
2018-01-12 17:01:27 +00:00
|
|
|
c := Component{Host: connStr, Secret: "mypass"}
|
2018-01-11 22:00:59 +00:00
|
|
|
|
2018-01-11 21:15:54 +00:00
|
|
|
var conn net.Conn
|
|
|
|
var err error
|
|
|
|
if conn, err = net.DialTimeout("tcp", "localhost:8888", time.Duration(5)*time.Second); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-11 22:00:59 +00:00
|
|
|
c.conn = conn
|
2018-01-11 21:15:54 +00:00
|
|
|
|
|
|
|
// TODO send stream open and check for reply
|
|
|
|
// Send stream open tag
|
2018-01-12 17:01:27 +00:00
|
|
|
componentHost := connStr // TODO Fix me: Extract componentID + secret
|
2018-01-11 21:15:54 +00:00
|
|
|
if _, err := fmt.Fprintf(conn, componentStreamOpen, componentHost, NSComponent, NSStream); err != nil {
|
2018-01-11 22:00:59 +00:00
|
|
|
fmt.Println("cannot send stream open.")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.decoder = xml.NewDecoder(conn)
|
|
|
|
|
|
|
|
// Initialize xml decoder and extract streamID from reply
|
|
|
|
streamId, err := initDecoder(c.decoder)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("cannot init decoder")
|
2018-01-11 21:15:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-11 22:00:59 +00:00
|
|
|
fmt.Println("StreamID = ", streamId)
|
|
|
|
|
|
|
|
// Authentication
|
2018-01-12 17:01:27 +00:00
|
|
|
if _, err := fmt.Fprintf(conn, "<handshake>%s</handshake>", c.Handshake(streamId)); err != nil {
|
2018-01-11 22:00:59 +00:00
|
|
|
fmt.Println("cannot send stream open.")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next message should be either success or failure.
|
|
|
|
name, val, err := next(c.decoder)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := val.(type) {
|
|
|
|
case *StreamError:
|
|
|
|
fmt.Printf("error: %s", v.Error.Local)
|
2018-01-12 17:01:27 +00:00
|
|
|
case *Handshake:
|
|
|
|
fmt.Println("Component connected")
|
2018-01-11 22:00:59 +00:00
|
|
|
default:
|
|
|
|
return errors.New("unexpected packet, got " + name.Local + " in " + name.Space)
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:15:54 +00:00
|
|
|
return nil
|
|
|
|
}
|