From c7c7ff6f93b80de644358a74ced9804dd77401d1 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Sun, 11 Feb 2018 11:15:11 +0100 Subject: [PATCH] fix connection + ipv4/ipv6 --- daemon/tester.go | 1 + daemon/tester/bot.go | 13 +++++++++++-- daemon/tester/status.go | 25 ++++++++++++++++++++++++- daemon/tester/tester.go | 37 +++++++++++++++++++++++++------------ messages/error.go | 2 +- 5 files changed, 62 insertions(+), 16 deletions(-) diff --git a/daemon/tester.go b/daemon/tester.go index 5895986..cc54e24 100644 --- a/daemon/tester.go +++ b/daemon/tester.go @@ -73,6 +73,7 @@ var TesterCMD = &cobra.Command{ switch sig { case syscall.SIGTERM: log.Panic("terminated") + quitTester() os.Exit(0) case syscall.SIGQUIT: quitTester() diff --git a/daemon/tester/bot.go b/daemon/tester/bot.go index 7229368..d10f715 100644 --- a/daemon/tester/bot.go +++ b/daemon/tester/bot.go @@ -20,6 +20,15 @@ func (t *Tester) StartBot(status *Status) { return } + errMSG := &messages.StreamError{} + err = status.client.In.DecodeElement(errMSG, element) + if err == nil { + logCTX.Errorf("recv stream error: %s: %v", errMSG.Text, errMSG.Any) + status.client.Close() + status.Login = false + return + } + iq := &messages.IQClient{} err = status.client.In.DecodeElement(iq, element) if err == nil { @@ -52,10 +61,10 @@ func (t *Tester) StartBot(status *Status) { msg := &messages.MessageClient{} err = status.client.In.DecodeElement(msg, element) if err != nil { - logCTX.Warnf("unsupport xml recv: %s", err) + logCTX.Warnf("unsupport xml recv: %s <-> %v", err, element) continue } - logCTX = logCTX.WithField("from", msg.From.Full()).WithField("msg", msg.Body) + logCTX = logCTX.WithField("from", msg.From.Full()).WithField("msg-recv", msg.Body) if msg.Error != nil { logCTX.Debugf("recv msg with error %s: %s", msg.Error.Code, msg.Error.Text) continue diff --git a/daemon/tester/status.go b/daemon/tester/status.go index 769a5e6..66c36cd 100644 --- a/daemon/tester/status.go +++ b/daemon/tester/status.go @@ -2,6 +2,7 @@ package tester import ( "crypto/tls" + "time" "dev.sum7.eu/genofire/yaja/client" "dev.sum7.eu/genofire/yaja/model" @@ -16,6 +17,8 @@ type Status struct { MessageForConnection map[string]string `json:"-"` Connections map[string]bool `json:"-"` TLSVersion string `json:"tls_version"` + IPv4 bool `json:"ipv4"` + IPv6 bool `json:"ipv6"` } func NewStatus(acc *Account) *Status { @@ -28,12 +31,32 @@ func NewStatus(acc *Account) *Status { } } -func (s *Status) Update() { +func (s *Status) Update(timeout time.Duration) { if s.client == nil || !s.Login { s.Login = false s.TLSVersion = "" return } + + bareJID := model.NewJID(s.account.JID.Bare()) + if client, err := client.NewClientProtocolDuration(bareJID, s.account.Password, "tcp4", timeout/2); err == nil { + s.IPv4 = true + client.Close() + } else { + s.IPv4 = false + } + if client, err := client.NewClientProtocolDuration(bareJID, s.account.Password, "tcp6", timeout/2); err == nil { + s.IPv6 = true + client.Close() + } else { + s.IPv6 = false + } + if !s.IPv4 && !s.IPv6 { + s.client.Close() + s.Login = false + s.TLSVersion = "" + } + if tlsstate := s.client.TLSConnectionState(); tlsstate != nil { switch tlsstate.Version { case tls.VersionSSL30: diff --git a/daemon/tester/tester.go b/daemon/tester/tester.go index f6f9c72..ca59f1d 100644 --- a/daemon/tester/tester.go +++ b/daemon/tester/tester.go @@ -1,6 +1,7 @@ package tester import ( + "sync" "time" log "github.com/sirupsen/logrus" @@ -16,6 +17,7 @@ type Tester struct { Timeout time.Duration `json:"-"` Accounts map[string]*Account `json:"accounts"` Status map[string]*Status `json:"-"` + mux sync.Mutex } func NewTester() *Tester { @@ -35,9 +37,12 @@ func (t *Tester) Start(mainClient *client.Client, password string) { }) status.client = mainClient status.Login = true - status.Update() + status.Update(t.Timeout) - t.Status[mainClient.JID.Domain] = status + t.mux.Lock() + defer t.mux.Unlock() + + t.Status[mainClient.JID.Bare()] = status go t.StartBot(status) for _, acc := range t.Accounts { @@ -73,26 +78,29 @@ func (t *Tester) Connect(acc *Account) { status.client = c status.account.JID = c.JID status.JID = c.JID - status.Update() + status.Update(t.Timeout) go t.StartBot(status) } } func (t *Tester) UpdateConnectionStatus(from, to *model.JID, recvmsg string) { logCTX := log.WithFields(log.Fields{ - "jid": to.Full(), - "from": from.Full(), - "recvmsg": recvmsg, + "jid": to.Full(), + "from": from.Full(), + "msg-recv": recvmsg, }) + t.mux.Lock() + defer t.mux.Unlock() + status, ok := t.Status[from.Bare()] if !ok { - logCTX.Warn("recv wrong msg") + logCTX.Warn("recv msg without receiver") return } msg, ok := status.MessageForConnection[to.Bare()] - logCTX = logCTX.WithField("msg", msg) - if !ok || msg != recvmsg { + logCTX = logCTX.WithField("msg-send", msg) + if !ok || msg != recvmsg || msg == "" || recvmsg == "" { logCTX.Warn("recv wrong msg") return } @@ -106,6 +114,10 @@ func (t *Tester) CheckStatus() { send := 0 online := 0 connection := 0 + + t.mux.Lock() + defer t.mux.Unlock() + for ownJID, own := range t.Status { logCTX := log.WithField("jid", ownJID) if !own.Login { @@ -129,7 +141,7 @@ func (t *Tester) CheckStatus() { } msg, ok := own.MessageForConnection[jid] if ok { - logCTXTo = logCTXTo.WithField("old-msg", msg) + logCTXTo = logCTXTo.WithField("msg-old", msg) own.Connections[jid] = false if ok, exists := own.Connections[jid]; !exists || ok { logCTXTo.Warn("could not recv msg") @@ -138,18 +150,19 @@ func (t *Tester) CheckStatus() { } } msg = utils.CreateCookieString() - logCTXTo = logCTXTo.WithField("msg", msg) + logCTXTo = logCTXTo.WithField("msg-send", msg) own.client.Send(&messages.MessageClient{ Body: "checkmsg " + msg, Type: messages.ChatTypeChat, To: s.JID, }) - own.MessageForConnection[jid] = msg + own.MessageForConnection[s.JID.Bare()] = msg logCTXTo.Info("test send") send++ } } + log.WithFields(log.Fields{ "online": online, "connection": connection, diff --git a/messages/error.go b/messages/error.go index 5e37d84..0e03ef2 100644 --- a/messages/error.go +++ b/messages/error.go @@ -4,7 +4,7 @@ import "encoding/xml" type StreamError struct { XMLName xml.Name `xml:"http://etherx.jabber.org/streams error"` - Text string + Text string `xml:"urn:ietf:params:xml:ns:xmpp-streams text"` Any xml.Name `xml:",any"` }