2018-02-10 13:34:42 +01:00
|
|
|
package tester
|
|
|
|
|
|
|
|
import (
|
2018-02-11 11:15:11 +01:00
|
|
|
"sync"
|
2018-02-11 09:07:07 +01:00
|
|
|
"time"
|
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"dev.sum7.eu/genofire/yaja/client"
|
2018-02-14 18:49:26 +01:00
|
|
|
"dev.sum7.eu/genofire/yaja/xmpp"
|
|
|
|
"dev.sum7.eu/genofire/yaja/xmpp/base"
|
2018-02-10 13:34:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Tester struct {
|
2018-02-11 22:03:58 +01:00
|
|
|
mainClient *client.Client
|
|
|
|
Timeout time.Duration `json:"-"`
|
|
|
|
Accounts map[string]*Account `json:"accounts"`
|
|
|
|
Status map[string]*Status `json:"-"`
|
|
|
|
mux sync.Mutex
|
2018-02-22 18:13:17 +01:00
|
|
|
LoggingClients *log.Entry `json:"-"`
|
2018-02-13 20:05:18 +01:00
|
|
|
LoggingBots log.Level `json:"-"`
|
2018-02-14 18:49:26 +01:00
|
|
|
Admins []*xmppbase.JID `json:"-"`
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTester() *Tester {
|
|
|
|
return &Tester{
|
2018-02-10 16:45:01 +01:00
|
|
|
Accounts: make(map[string]*Account),
|
2018-02-10 13:34:42 +01:00
|
|
|
Status: make(map[string]*Status),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tester) Start(mainClient *client.Client, password string) {
|
2018-02-22 18:13:17 +01:00
|
|
|
mainClient.SkipError = true
|
2018-02-10 13:34:42 +01:00
|
|
|
t.mainClient = mainClient
|
|
|
|
|
2018-02-11 23:28:21 +01:00
|
|
|
status := NewStatus(mainClient, &Account{
|
2018-02-10 16:45:01 +01:00
|
|
|
JID: mainClient.JID,
|
|
|
|
Password: password,
|
|
|
|
})
|
2018-02-10 13:34:42 +01:00
|
|
|
status.client = mainClient
|
|
|
|
status.Login = true
|
|
|
|
|
2018-02-11 11:15:11 +01:00
|
|
|
t.mux.Lock()
|
|
|
|
defer t.mux.Unlock()
|
|
|
|
|
2018-02-15 22:03:49 +01:00
|
|
|
t.Status[mainClient.JID.Bare().String()] = status
|
2018-02-22 18:13:17 +01:00
|
|
|
go t.startBot(status)
|
|
|
|
status.update(t.Timeout)
|
2018-02-10 13:34:42 +01:00
|
|
|
|
2018-02-10 16:45:01 +01:00
|
|
|
for _, acc := range t.Accounts {
|
|
|
|
t.Connect(acc)
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
func (t *Tester) Close() {
|
|
|
|
for _, s := range t.Status {
|
2018-02-11 23:28:21 +01:00
|
|
|
s.Disconnect("yaja tester stopped")
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 16:45:01 +01:00
|
|
|
func (t *Tester) Connect(acc *Account) {
|
2018-02-15 22:03:49 +01:00
|
|
|
logCTX := log.WithField("jid", acc.JID.Full().String())
|
|
|
|
bare := acc.JID.Bare().String()
|
|
|
|
status, ok := t.Status[bare]
|
2018-02-10 13:34:42 +01:00
|
|
|
if !ok {
|
2018-02-11 23:28:21 +01:00
|
|
|
status = NewStatus(t.mainClient, acc)
|
2018-02-15 22:03:49 +01:00
|
|
|
t.Status[bare] = status
|
2018-02-10 13:34:42 +01:00
|
|
|
} else if status.JID == nil {
|
2018-02-10 16:45:01 +01:00
|
|
|
status.JID = acc.JID
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
if status.Login {
|
|
|
|
logCTX.Warn("is already loggedin")
|
|
|
|
return
|
|
|
|
}
|
2018-02-11 22:03:58 +01:00
|
|
|
c := &client.Client{
|
2018-02-22 18:13:17 +01:00
|
|
|
Timeout: t.Timeout,
|
|
|
|
JID: acc.JID,
|
|
|
|
Logging: t.LoggingClients.WithField("jid", acc.JID.String()),
|
|
|
|
SkipError: true,
|
2018-02-11 22:03:58 +01:00
|
|
|
}
|
|
|
|
err := c.Connect(acc.Password)
|
2018-02-10 13:34:42 +01:00
|
|
|
if err != nil {
|
|
|
|
logCTX.Warnf("could not connect client: %s", err)
|
|
|
|
} else {
|
|
|
|
logCTX.Info("client connected")
|
|
|
|
status.Login = true
|
|
|
|
status.client = c
|
2018-02-10 16:45:01 +01:00
|
|
|
status.account.JID = c.JID
|
|
|
|
status.JID = c.JID
|
2018-02-22 18:13:17 +01:00
|
|
|
go t.startBot(status)
|
|
|
|
status.update(t.Timeout)
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 18:13:17 +01:00
|
|
|
func (t *Tester) updateConnectionStatus(from, to *xmppbase.JID, recvmsg string) {
|
2018-02-10 13:34:42 +01:00
|
|
|
logCTX := log.WithFields(log.Fields{
|
2018-02-11 11:15:11 +01:00
|
|
|
"jid": to.Full(),
|
|
|
|
"from": from.Full(),
|
|
|
|
"msg-recv": recvmsg,
|
2018-02-10 13:34:42 +01:00
|
|
|
})
|
|
|
|
|
2018-02-11 11:15:11 +01:00
|
|
|
t.mux.Lock()
|
|
|
|
defer t.mux.Unlock()
|
|
|
|
|
2018-02-15 22:03:49 +01:00
|
|
|
status, ok := t.Status[from.Bare().String()]
|
2018-02-10 13:34:42 +01:00
|
|
|
if !ok {
|
2018-02-11 11:15:11 +01:00
|
|
|
logCTX.Warn("recv msg without receiver")
|
2018-02-10 13:34:42 +01:00
|
|
|
return
|
|
|
|
}
|
2018-02-15 22:03:49 +01:00
|
|
|
toBare := to.Bare().String()
|
|
|
|
msg, ok := status.MessageForConnection[toBare]
|
2018-02-11 11:15:11 +01:00
|
|
|
logCTX = logCTX.WithField("msg-send", msg)
|
|
|
|
if !ok || msg != recvmsg || msg == "" || recvmsg == "" {
|
2018-02-10 13:34:42 +01:00
|
|
|
logCTX.Warn("recv wrong msg")
|
|
|
|
return
|
|
|
|
}
|
2018-02-15 22:03:49 +01:00
|
|
|
delete(status.MessageForConnection, toBare)
|
|
|
|
status.Connections[toBare] = true
|
2018-02-10 13:34:42 +01:00
|
|
|
logCTX.Info("recv msg")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tester) CheckStatus() {
|
|
|
|
send := 0
|
|
|
|
online := 0
|
|
|
|
connection := 0
|
2018-02-11 11:15:11 +01:00
|
|
|
|
|
|
|
t.mux.Lock()
|
|
|
|
defer t.mux.Unlock()
|
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
for ownJID, own := range t.Status {
|
|
|
|
logCTX := log.WithField("jid", ownJID)
|
|
|
|
if !own.Login {
|
2018-02-10 16:45:01 +01:00
|
|
|
acc, ok := t.Accounts[ownJID]
|
2018-02-10 13:34:42 +01:00
|
|
|
if ok {
|
2018-02-10 16:45:01 +01:00
|
|
|
t.Connect(acc)
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
if !own.Login {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
online++
|
|
|
|
for jid, s := range t.Status {
|
|
|
|
logCTXTo := logCTX.WithField("to", jid)
|
|
|
|
if jid == ownJID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
connection++
|
|
|
|
if own.MessageForConnection == nil {
|
|
|
|
own.MessageForConnection = make(map[string]string)
|
|
|
|
}
|
|
|
|
msg, ok := own.MessageForConnection[jid]
|
|
|
|
if ok {
|
2018-02-11 11:15:11 +01:00
|
|
|
logCTXTo = logCTXTo.WithField("msg-old", msg)
|
2018-02-10 13:34:42 +01:00
|
|
|
own.Connections[jid] = false
|
|
|
|
if ok, exists := own.Connections[jid]; !exists || ok {
|
2018-02-11 22:03:58 +01:00
|
|
|
logCTXTo.Info("could not recv msg")
|
2018-02-10 13:34:42 +01:00
|
|
|
} else {
|
|
|
|
logCTXTo.Debug("could not recv msg")
|
|
|
|
}
|
|
|
|
}
|
2018-02-14 18:49:26 +01:00
|
|
|
msg = xmpp.CreateCookieString()
|
2018-02-11 11:15:11 +01:00
|
|
|
logCTXTo = logCTXTo.WithField("msg-send", msg)
|
2018-02-10 13:34:42 +01:00
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
own.client.Send(&xmpp.MessageClient{
|
2018-02-10 13:34:42 +01:00
|
|
|
Body: "checkmsg " + msg,
|
2018-02-14 18:49:26 +01:00
|
|
|
Type: xmpp.MessageTypeChat,
|
2018-02-10 13:34:42 +01:00
|
|
|
To: s.JID,
|
|
|
|
})
|
2018-02-15 22:03:49 +01:00
|
|
|
own.MessageForConnection[s.JID.Bare().String()] = msg
|
2018-02-11 22:03:58 +01:00
|
|
|
logCTXTo.Debug("test send")
|
2018-02-10 13:34:42 +01:00
|
|
|
send++
|
|
|
|
}
|
|
|
|
}
|
2018-02-11 11:15:11 +01:00
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"online": online,
|
|
|
|
"connection": connection,
|
|
|
|
"send": send,
|
|
|
|
"all": len(t.Status),
|
|
|
|
}).Info("checked complete")
|
|
|
|
}
|