sum7
/
yaja
Archived
1
0
Fork 0

fix connection + ipv4/ipv6

This commit is contained in:
Martin/Geno 2018-02-11 11:15:11 +01:00
parent 3b656d7284
commit c7c7ff6f93
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
5 changed files with 62 additions and 16 deletions

View File

@ -73,6 +73,7 @@ var TesterCMD = &cobra.Command{
switch sig { switch sig {
case syscall.SIGTERM: case syscall.SIGTERM:
log.Panic("terminated") log.Panic("terminated")
quitTester()
os.Exit(0) os.Exit(0)
case syscall.SIGQUIT: case syscall.SIGQUIT:
quitTester() quitTester()

View File

@ -20,6 +20,15 @@ func (t *Tester) StartBot(status *Status) {
return 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{} iq := &messages.IQClient{}
err = status.client.In.DecodeElement(iq, element) err = status.client.In.DecodeElement(iq, element)
if err == nil { if err == nil {
@ -52,10 +61,10 @@ func (t *Tester) StartBot(status *Status) {
msg := &messages.MessageClient{} msg := &messages.MessageClient{}
err = status.client.In.DecodeElement(msg, element) err = status.client.In.DecodeElement(msg, element)
if err != nil { if err != nil {
logCTX.Warnf("unsupport xml recv: %s", err) logCTX.Warnf("unsupport xml recv: %s <-> %v", err, element)
continue 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 { if msg.Error != nil {
logCTX.Debugf("recv msg with error %s: %s", msg.Error.Code, msg.Error.Text) logCTX.Debugf("recv msg with error %s: %s", msg.Error.Code, msg.Error.Text)
continue continue

View File

@ -2,6 +2,7 @@ package tester
import ( import (
"crypto/tls" "crypto/tls"
"time"
"dev.sum7.eu/genofire/yaja/client" "dev.sum7.eu/genofire/yaja/client"
"dev.sum7.eu/genofire/yaja/model" "dev.sum7.eu/genofire/yaja/model"
@ -16,6 +17,8 @@ type Status struct {
MessageForConnection map[string]string `json:"-"` MessageForConnection map[string]string `json:"-"`
Connections map[string]bool `json:"-"` Connections map[string]bool `json:"-"`
TLSVersion string `json:"tls_version"` TLSVersion string `json:"tls_version"`
IPv4 bool `json:"ipv4"`
IPv6 bool `json:"ipv6"`
} }
func NewStatus(acc *Account) *Status { 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 { if s.client == nil || !s.Login {
s.Login = false s.Login = false
s.TLSVersion = "" s.TLSVersion = ""
return 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 { if tlsstate := s.client.TLSConnectionState(); tlsstate != nil {
switch tlsstate.Version { switch tlsstate.Version {
case tls.VersionSSL30: case tls.VersionSSL30:

View File

@ -1,6 +1,7 @@
package tester package tester
import ( import (
"sync"
"time" "time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -16,6 +17,7 @@ type Tester struct {
Timeout time.Duration `json:"-"` Timeout time.Duration `json:"-"`
Accounts map[string]*Account `json:"accounts"` Accounts map[string]*Account `json:"accounts"`
Status map[string]*Status `json:"-"` Status map[string]*Status `json:"-"`
mux sync.Mutex
} }
func NewTester() *Tester { func NewTester() *Tester {
@ -35,9 +37,12 @@ func (t *Tester) Start(mainClient *client.Client, password string) {
}) })
status.client = mainClient status.client = mainClient
status.Login = true 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) go t.StartBot(status)
for _, acc := range t.Accounts { for _, acc := range t.Accounts {
@ -73,7 +78,7 @@ func (t *Tester) Connect(acc *Account) {
status.client = c status.client = c
status.account.JID = c.JID status.account.JID = c.JID
status.JID = c.JID status.JID = c.JID
status.Update() status.Update(t.Timeout)
go t.StartBot(status) go t.StartBot(status)
} }
} }
@ -82,17 +87,20 @@ func (t *Tester) UpdateConnectionStatus(from, to *model.JID, recvmsg string) {
logCTX := log.WithFields(log.Fields{ logCTX := log.WithFields(log.Fields{
"jid": to.Full(), "jid": to.Full(),
"from": from.Full(), "from": from.Full(),
"recvmsg": recvmsg, "msg-recv": recvmsg,
}) })
t.mux.Lock()
defer t.mux.Unlock()
status, ok := t.Status[from.Bare()] status, ok := t.Status[from.Bare()]
if !ok { if !ok {
logCTX.Warn("recv wrong msg") logCTX.Warn("recv msg without receiver")
return return
} }
msg, ok := status.MessageForConnection[to.Bare()] msg, ok := status.MessageForConnection[to.Bare()]
logCTX = logCTX.WithField("msg", msg) logCTX = logCTX.WithField("msg-send", msg)
if !ok || msg != recvmsg { if !ok || msg != recvmsg || msg == "" || recvmsg == "" {
logCTX.Warn("recv wrong msg") logCTX.Warn("recv wrong msg")
return return
} }
@ -106,6 +114,10 @@ func (t *Tester) CheckStatus() {
send := 0 send := 0
online := 0 online := 0
connection := 0 connection := 0
t.mux.Lock()
defer t.mux.Unlock()
for ownJID, own := range t.Status { for ownJID, own := range t.Status {
logCTX := log.WithField("jid", ownJID) logCTX := log.WithField("jid", ownJID)
if !own.Login { if !own.Login {
@ -129,7 +141,7 @@ func (t *Tester) CheckStatus() {
} }
msg, ok := own.MessageForConnection[jid] msg, ok := own.MessageForConnection[jid]
if ok { if ok {
logCTXTo = logCTXTo.WithField("old-msg", msg) logCTXTo = logCTXTo.WithField("msg-old", msg)
own.Connections[jid] = false own.Connections[jid] = false
if ok, exists := own.Connections[jid]; !exists || ok { if ok, exists := own.Connections[jid]; !exists || ok {
logCTXTo.Warn("could not recv msg") logCTXTo.Warn("could not recv msg")
@ -138,18 +150,19 @@ func (t *Tester) CheckStatus() {
} }
} }
msg = utils.CreateCookieString() msg = utils.CreateCookieString()
logCTXTo = logCTXTo.WithField("msg", msg) logCTXTo = logCTXTo.WithField("msg-send", msg)
own.client.Send(&messages.MessageClient{ own.client.Send(&messages.MessageClient{
Body: "checkmsg " + msg, Body: "checkmsg " + msg,
Type: messages.ChatTypeChat, Type: messages.ChatTypeChat,
To: s.JID, To: s.JID,
}) })
own.MessageForConnection[jid] = msg own.MessageForConnection[s.JID.Bare()] = msg
logCTXTo.Info("test send") logCTXTo.Info("test send")
send++ send++
} }
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"online": online, "online": online,
"connection": connection, "connection": connection,

View File

@ -4,7 +4,7 @@ import "encoding/xml"
type StreamError struct { type StreamError struct {
XMLName xml.Name `xml:"http://etherx.jabber.org/streams error"` 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"` Any xml.Name `xml:",any"`
} }