sum7
/
yaja
Archived
1
0
Fork 0

change json struct for save extra properties

This commit is contained in:
Martin/Geno 2018-02-10 16:45:01 +01:00
parent 5076f53d81
commit 4d39701a74
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
6 changed files with 40 additions and 48 deletions

View File

@ -1,15 +1,12 @@
package daemon
import (
"crypto/tls"
"net/http"
"os"
"os/signal"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/acme/autocert"
"dev.sum7.eu/genofire/golang-lib/file"
"dev.sum7.eu/genofire/golang-lib/worker"
@ -43,29 +40,6 @@ var TesterCMD = &cobra.Command{
log.Warn("unable to load state file:", err)
}
// https server to handle acme (by letsencrypt)
hs := &http.Server{
Addr: configTester.Webserver,
}
if configTester.TLSDir != "" {
m := autocert.Manager{
Cache: autocert.DirCache(configTester.TLSDir),
Prompt: autocert.AcceptTOS,
}
hs.TLSConfig = &tls.Config{GetCertificate: m.GetCertificate}
go func(hs *http.Server) {
if err := hs.ListenAndServeTLS("", ""); err != http.ErrServerClosed {
log.Errorf("webserver with addr %s: %s", hs.Addr, err)
}
}(hs)
} else {
go func(hs *http.Server) {
if err := hs.ListenAndServe(); err != http.ErrServerClosed {
log.Errorf("webserver with addr %s: %s", hs.Addr, err)
}
}(hs)
}
mainClient, err := client.NewClient(configTester.Client.JID, configTester.Client.Password)
if err != nil {
log.Fatal("unable to connect with main jabber client: ", err)

8
daemon/tester/account.go Normal file
View File

@ -0,0 +1,8 @@
package tester
import "dev.sum7.eu/genofire/yaja/model"
type Account struct {
JID *model.JID `json:"jid"`
Password string `json:"password"`
}

View File

@ -10,7 +10,6 @@ type Config struct {
AccountsPath string `toml:"accounts_path"`
OutputPath string `toml:"output_path"`
Logging log.Level `toml:"logging"`
Webserver string `toml:"webserver"`
Admins []*model.JID `toml:"admins"`
Client struct {
JID *model.JID `toml:"jid"`

View File

@ -16,7 +16,9 @@ type Output struct {
type Link struct {
Source string `json:"source"`
SourceJID string `json:"source_jid"`
Target string `json:"target"`
TargetJID string `json:"target_jid"`
FromSource bool `json:"from_source"`
FromTarget bool `json:"from_target"`
}
@ -52,13 +54,17 @@ func (t *Tester) Output() *Output {
toJID := model.NewJID(to)
link := &Link{
Source: status.JID.Domain,
SourceJID: status.JID.Bare(),
Target: toJID.Domain,
TargetJID: toJID.Bare(),
FromSource: linkOK,
FromTarget: false,
}
if switchSourceTarget {
link.Source = toJID.Domain
link.SourceJID = toJID.Bare()
link.Target = status.JID.Domain
link.TargetJID = status.JID.Bare()
link.FromSource = false
link.FromTarget = linkOK
}

View File

@ -9,7 +9,7 @@ import (
type Status struct {
client *client.Client
password string
account *Account
JID *model.JID `json:"jid"`
Domain string `json:"domain"`
Login bool `json:"is_online"`
@ -18,10 +18,11 @@ type Status struct {
TLSVersion string `json:"tls_version"`
}
func NewStatus(jid *model.JID, password string) *Status {
func NewStatus(acc *Account) *Status {
return &Status{
JID: jid,
Domain: jid.Domain,
account: acc,
JID: acc.JID,
Domain: acc.JID.Domain,
MessageForConnection: make(map[string]string),
Connections: make(map[string]bool),
}

View File

@ -11,13 +11,13 @@ import (
type Tester struct {
mainClient *client.Client
Accounts map[string]string `json:"accounts"`
Accounts map[string]*Account `json:"accounts"`
Status map[string]*Status `json:"-"`
}
func NewTester() *Tester {
return &Tester{
Accounts: make(map[string]string),
Accounts: make(map[string]*Account),
Status: make(map[string]*Status),
}
}
@ -26,16 +26,19 @@ func (t *Tester) Start(mainClient *client.Client, password string) {
t.mainClient = mainClient
status := NewStatus(mainClient.JID, password)
status := NewStatus(&Account{
JID: mainClient.JID,
Password: password,
})
status.client = mainClient
status.Login = true
status.Update()
t.Status[mainClient.JID.Bare()] = status
t.Status[mainClient.JID.Domain] = status
go t.StartBot(status)
for jidString, passwd := range t.Accounts {
t.Connect(jidString, passwd)
for _, acc := range t.Accounts {
t.Connect(acc)
}
}
func (t *Tester) Close() {
@ -45,27 +48,28 @@ func (t *Tester) Close() {
}
}
func (t *Tester) Connect(jidString, password string) {
logCTX := log.WithField("jid", jidString)
jid := model.NewJID(jidString)
status, ok := t.Status[jidString]
func (t *Tester) Connect(acc *Account) {
logCTX := log.WithField("jid", acc.JID.Full())
status, ok := t.Status[acc.JID.Bare()]
if !ok {
status = NewStatus(jid, password)
t.Status[jidString] = status
status = NewStatus(acc)
t.Status[acc.JID.Bare()] = status
} else if status.JID == nil {
status.JID = jid
status.JID = acc.JID
}
if status.Login {
logCTX.Warn("is already loggedin")
return
}
c, err := client.NewClient(jid, password)
c, err := client.NewClient(acc.JID, acc.Password)
if err != nil {
logCTX.Warnf("could not connect client: %s", err)
} else {
logCTX.Info("client connected")
status.Login = true
status.client = c
status.account.JID = c.JID
status.JID = c.JID
status.Update()
go t.StartBot(status)
}
@ -102,9 +106,9 @@ func (t *Tester) CheckStatus() {
for ownJID, own := range t.Status {
logCTX := log.WithField("jid", ownJID)
if !own.Login {
pass, ok := t.Accounts[ownJID]
acc, ok := t.Accounts[ownJID]
if ok {
t.Connect(ownJID, pass)
t.Connect(acc)
}
if !own.Login {
continue