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 {
case syscall.SIGTERM:
log.Panic("terminated")
quitTester()
os.Exit(0)
case syscall.SIGQUIT:
quitTester()

View File

@ -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

View File

@ -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:

View File

@ -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,

View File

@ -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"`
}