From 940d328b091f9b017861aeea0d09eddc6349c496 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Sun, 11 Feb 2018 22:43:42 +0100 Subject: [PATCH] improve use bind of stream start + logging --- client/auth.go | 2 +- client/connect.go | 76 +++++++++++++++++++++++----------------- daemon/tester/bot.go | 4 +-- messages/utils.go | 6 ++-- yaja-tester_example.conf | 12 ++++--- 5 files changed, 58 insertions(+), 42 deletions(-) diff --git a/client/auth.go b/client/auth.go index 136a7fa..003264f 100644 --- a/client/auth.go +++ b/client/auth.go @@ -86,7 +86,7 @@ func (client *Client) auth(password string) error { if mechanism == "" { return fmt.Errorf("PLAIN authentication is not an option: %s", f.Mechanisms.Mechanism) } - client.Logging.Info("used auth with '%s'", mechanism) + client.Logging.Infof("used auth with '%s'", mechanism) element, err := client.Read() if err != nil { diff --git a/client/connect.go b/client/connect.go index f6a7c33..a97bdc9 100644 --- a/client/connect.go +++ b/client/connect.go @@ -42,8 +42,14 @@ func (client *Client) startStream() (*messages.StreamFeatures, error) { debug += " tls" } debug += " mechanism(" + mFirst := true for _, m := range f.Mechanisms.Mechanism { - debug += m + if mFirst { + mFirst = false + debug += m + } else { + debug += ", " + m + } } debug += ")" if f.Bind != nil { @@ -81,41 +87,47 @@ func (client *Client) connect(password string) error { return err } - if _, err := client.startStream(); err != nil { + f, err := client.startStream() + if err != nil { return err } - // bind to resource - bind := &messages.Bind{} - if client.JID.Resource != "" { - bind.Resource = client.JID.Resource - } - if err := client.Send(&messages.IQClient{ - Type: messages.IQTypeSet, - To: model.NewJID(client.JID.Domain), - Bind: bind, - }); err != nil { - return err - } - - var iq messages.IQClient - if err := client.ReadDecode(&iq); err != nil { - return err - } - if iq.Error != nil { - if iq.Error.ServiceUnavailable == nil { - return errors.New(fmt.Sprintf("recv error on iq>bind: %s[%s]: %s -> %s -> %s", iq.Error.Code, iq.Error.Type, iq.Error.Text, messages.XMLChildrenString(iq.Error.StanzaErrorGroup), messages.XMLChildrenString(iq.Error.Other))) - } else { - client.Logging.Warn("connected without setting resource with bind after auth: service-unavailable") + bind := f.Bind + if f.Bind == nil || (f.Bind.JID == nil && f.Bind.Resource == "") { + // bind to resource + if client.JID.Resource != "" { + bind.Resource = client.JID.Resource } - } else if iq.Bind == nil { - return errors.New(" result missing ") - } else if iq.Bind.JID != nil { - client.JID.Local = iq.Bind.JID.Local - client.JID.Domain = iq.Bind.JID.Domain - client.JID.Resource = iq.Bind.JID.Resource - client.Logging.Info("set resource by server bind") + if err := client.Send(&messages.IQClient{ + Type: messages.IQTypeSet, + To: model.NewJID(client.JID.Domain), + Bind: bind, + }); err != nil { + return err + } + + var iq messages.IQClient + if err := client.ReadDecode(&iq); err != nil { + return err + } + if iq.Error != nil { + return errors.New(fmt.Sprintf("recv error on iq>bind: %s[%s]: %s -> %s -> %s", iq.Error.Code, iq.Error.Type, iq.Error.Text, messages.XMLChildrenString(iq.Error.StanzaErrorGroup), messages.XMLChildrenString(iq.Error.Other))) + } else if iq.Bind == nil { + return errors.New("iq>bind is nil :" + messages.XMLChildrenString(iq.Other)) + } + bind = iq.Bind + } + if bind == nil { + return errors.New("bind is nil") + } else if bind.JID != nil { + client.JID.Local = bind.JID.Local + client.JID.Domain = bind.JID.Domain + client.JID.Resource = bind.JID.Resource + client.Logging.Infof("set jid by server bind '%s'", bind.JID.Full()) + } else if bind.Resource != "" { + client.JID.Resource = bind.Resource + client.Logging.Infof("set resource by server bind '%s'", bind.Resource) } else { - return errors.New(messages.XMLChildrenString(iq.Other)) + return errors.New("bind>jid is nil" + messages.XMLChildrenString(bind)) } // set status return client.Send(&messages.PresenceClient{Show: messages.PresenceShowXA, Status: "online"}) diff --git a/daemon/tester/bot.go b/daemon/tester/bot.go index 5cbdb59..9b0efef 100644 --- a/daemon/tester/bot.go +++ b/daemon/tester/bot.go @@ -112,10 +112,10 @@ func (t *Tester) StartBot(status *Status) { for _, jid := range t.Admins { if first { first = false + allAdmins += jid.Bare() } else { - allAdmins += ", " + allAdmins += ", " + jid.Bare() } - allAdmins += jid.Bare() if jid.Bare() == msg.From.Bare() { isAdmin = true status.client.Send(messages.MessageClient{Type: msg.Type, To: jid, Body: "last message, disconnect requested by " + msg.From.Bare()}) diff --git a/messages/utils.go b/messages/utils.go index 30d7728..ffa0e47 100644 --- a/messages/utils.go +++ b/messages/utils.go @@ -23,7 +23,7 @@ func XMLStartElementToString(element *xml.StartElement) string { } debug := fmt.Sprintf("<%s xmlns=\"%s\"", element.Name.Local, element.Name.Space) for _, attr := range element.Attr { - debug = fmt.Sprintf("%s %s=\"%s\"", debug, attr.Name, attr.Value) + debug = fmt.Sprintf("%s %s=\"%s\"", debug, attr.Name.Local, attr.Value) } debug += ">" return debug @@ -54,10 +54,10 @@ func XMLChildrenString(o interface{}) (result string) { if xmlElement, ok := valueField.Interface().(*xml.Name); ok && xmlElement != nil { if first { first = false + result += xmlElement.Local } else { - result += ", " + result += ", " + xmlElement.Local } - result += xmlElement.Local } } return diff --git a/yaja-tester_example.conf b/yaja-tester_example.conf index 62cec8b..4d0c43f 100644 --- a/yaja-tester_example.conf +++ b/yaja-tester_example.conf @@ -1,8 +1,12 @@ -tlsdir = "tmp/ssl" -state_path = "tmp/yaja-tester.json" -logging = 5 +accounts_path = "tmp/yaja-tester.json" +output_path = "tmp/yaja-tester-output.json" -webserver = ":https" +logging = 4 +logging_clients = 4 +logging_bots = 3 + +interval = "30s" +timeout = "10s" admins = ["a.admin@chat.sum7.eu"]