Code clean-up

This commit is contained in:
Mickael Remond 2018-01-12 18:14:41 +01:00
parent 90865aeb8e
commit b21fee420f
No known key found for this signature in database
GPG key ID: E6F6045D79965AA3
3 changed files with 26 additions and 33 deletions

View file

@ -3,5 +3,6 @@ package main
import "fluux.io/xmpp"
func main() {
xmpp.Open("mqtt.localhost")
component := xmpp.Component{Host: "mqtt.localhost", Secret: "mypass"}
component.Connect("localhost:8888")
}

View file

@ -28,12 +28,8 @@ type Component struct {
decoder *xml.Decoder
}
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 {
// 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
@ -50,55 +46,51 @@ func (c *Component) Handshake(streamId string) string {
}
// TODO Helper to prepare connection string
func Open(connStr string) error {
c := Component{Host: connStr, Secret: "mypass"}
func (c *Component) Connect(connStr string) error {
var conn net.Conn
var err error
if conn, err = net.DialTimeout("tcp", "localhost:8888", time.Duration(5)*time.Second); err != nil {
if conn, err = net.DialTimeout("tcp", connStr, time.Duration(5)*time.Second); err != nil {
return err
}
c.conn = conn
// TODO send stream open and check for reply
// Send stream open tag
componentHost := connStr // TODO Fix me: Extract componentID + secret
if _, err := fmt.Fprintf(conn, componentStreamOpen, componentHost, NSComponent, NSStream); err != nil {
fmt.Println("cannot send stream open.")
return err
// 1. Send stream open tag
if _, err := fmt.Fprintf(conn, componentStreamOpen, c.Host, NSComponent, NSStream); err != nil {
return errors.New("cannot send stream open " + err.Error())
}
c.decoder = xml.NewDecoder(conn)
// Initialize xml decoder and extract streamID from reply
// 2. Initialize xml decoder and extract streamID from reply
streamId, err := initDecoder(c.decoder)
if err != nil {
fmt.Println("cannot init decoder")
return err
return errors.New("cannot init decoder " + err.Error())
}
fmt.Println("StreamID = ", streamId)
// Authentication
if _, err := fmt.Fprintf(conn, "<handshake>%s</handshake>", c.Handshake(streamId)); err != nil {
fmt.Println("cannot send stream open.")
return err
// 3. Authentication
if _, err := fmt.Fprintf(conn, "<handshake>%s</handshake>", c.handshake(streamId)); err != nil {
return errors.New("cannot send handshake " + err.Error())
}
// Next message should be either success or failure.
// 4. Check server response for authentication
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)
return errors.New("handshake failed " + v.Error.Local)
case *Handshake:
fmt.Println("Component connected")
return nil
default:
return errors.New("unexpected packet, got " + name.Local + " in " + name.Space)
}
return nil
panic("unreachable")
}
// ============================================================================
// XMPP packets struct
type Handshake struct {
XMLName xml.Name `xml:"jabber:component:accept handshake"`
}

View file

@ -11,7 +11,7 @@ func TestHandshake(t *testing.T) {
streamID := "1263952298440005243"
expected := "c77e2ef0109fbbc5161e83b51629cd1353495332"
result := c.Handshake(streamID)
result := c.handshake(streamID)
if result != expected {
t.Errorf("incorrect handshake calculation '%s' != '%s'", result, expected)
}