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 package daemon
import ( import (
"crypto/tls"
"net/http"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"time" "time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"golang.org/x/crypto/acme/autocert"
"dev.sum7.eu/genofire/golang-lib/file" "dev.sum7.eu/genofire/golang-lib/file"
"dev.sum7.eu/genofire/golang-lib/worker" "dev.sum7.eu/genofire/golang-lib/worker"
@ -43,29 +40,6 @@ var TesterCMD = &cobra.Command{
log.Warn("unable to load state file:", err) 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) mainClient, err := client.NewClient(configTester.Client.JID, configTester.Client.Password)
if err != nil { if err != nil {
log.Fatal("unable to connect with main jabber client: ", err) 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"` AccountsPath string `toml:"accounts_path"`
OutputPath string `toml:"output_path"` OutputPath string `toml:"output_path"`
Logging log.Level `toml:"logging"` Logging log.Level `toml:"logging"`
Webserver string `toml:"webserver"`
Admins []*model.JID `toml:"admins"` Admins []*model.JID `toml:"admins"`
Client struct { Client struct {
JID *model.JID `toml:"jid"` JID *model.JID `toml:"jid"`

View File

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

View File

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

View File

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