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" import "fluux.io/xmpp"
func main() { 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 decoder *xml.Decoder
} }
type Handshake struct { // handshake generates an authentication token based on StreamID and shared secret.
XMLName xml.Name `xml:"jabber:component:accept handshake"` 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. // 1. Concatenate the Stream ID received from the server with the shared secret.
concatStr := streamId + c.Secret concatStr := streamId + c.Secret
@ -50,55 +46,51 @@ func (c *Component) Handshake(streamId string) string {
} }
// TODO Helper to prepare connection string // TODO Helper to prepare connection string
func Open(connStr string) error { func (c *Component) Connect(connStr string) error {
c := Component{Host: connStr, Secret: "mypass"}
var conn net.Conn var conn net.Conn
var err error 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 return err
} }
c.conn = conn c.conn = conn
// TODO send stream open and check for reply // 1. Send stream open tag
// Send stream open tag if _, err := fmt.Fprintf(conn, componentStreamOpen, c.Host, NSComponent, NSStream); err != nil {
componentHost := connStr // TODO Fix me: Extract componentID + secret return errors.New("cannot send stream open " + err.Error())
if _, err := fmt.Fprintf(conn, componentStreamOpen, componentHost, NSComponent, NSStream); err != nil {
fmt.Println("cannot send stream open.")
return err
} }
c.decoder = xml.NewDecoder(conn) 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) streamId, err := initDecoder(c.decoder)
if err != nil { if err != nil {
fmt.Println("cannot init decoder") return errors.New("cannot init decoder " + err.Error())
return err
} }
fmt.Println("StreamID = ", streamId) // 3. Authentication
if _, err := fmt.Fprintf(conn, "<handshake>%s</handshake>", c.handshake(streamId)); err != nil {
// Authentication return errors.New("cannot send handshake " + err.Error())
if _, err := fmt.Fprintf(conn, "<handshake>%s</handshake>", c.Handshake(streamId)); err != nil {
fmt.Println("cannot send stream open.")
return err
} }
// Next message should be either success or failure. // 4. Check server response for authentication
name, val, err := next(c.decoder) name, val, err := next(c.decoder)
if err != nil { if err != nil {
fmt.Println(err)
return err return err
} }
switch v := val.(type) { switch v := val.(type) {
case *StreamError: case *StreamError:
fmt.Printf("error: %s", v.Error.Local) return errors.New("handshake failed " + v.Error.Local)
case *Handshake: case *Handshake:
fmt.Println("Component connected") return nil
default: default:
return errors.New("unexpected packet, got " + name.Local + " in " + name.Space) return errors.New("unexpected packet, got " + name.Local + " in " + name.Space)
} }
panic("unreachable")
return nil }
// ============================================================================
// 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" streamID := "1263952298440005243"
expected := "c77e2ef0109fbbc5161e83b51629cd1353495332" expected := "c77e2ef0109fbbc5161e83b51629cd1353495332"
result := c.Handshake(streamID) result := c.handshake(streamID)
if result != expected { if result != expected {
t.Errorf("incorrect handshake calculation '%s' != '%s'", result, expected) t.Errorf("incorrect handshake calculation '%s' != '%s'", result, expected)
} }