From 5e5b54bab9ef37735b44e513aad00147182e36e7 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Fri, 13 Apr 2018 16:44:23 +0200 Subject: [PATCH] move to dev.sum7.eu + use yaja xmpp client --- README.md | 6 +- bot/main.go | 2 +- circle.yml | 12 +-- cmd/server.go | 20 ++--- database/main.go | 2 +- lib/config.go | 13 +-- main.go | 2 +- notify/all/internal.go | 8 +- notify/all/main.go | 2 +- notify/main.go | 6 +- notify/xmpp/main.go | 162 +++++++++++++++++++++++++--------- receive/all/internal.go | 4 +- receive/all/main.go | 6 +- receive/journald_json/main.go | 4 +- receive/logrus/client/main.go | 4 +- receive/logrus/main.go | 6 +- receive/main.go | 2 +- receive/syslog/internal.go | 2 +- receive/syslog/main.go | 4 +- 19 files changed, 171 insertions(+), 96 deletions(-) diff --git a/README.md b/README.md index 7a9a8b8..aea8871 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# logmania [![CircleCI](https://circleci.com/gh/genofire/logmania/tree/master.svg?style=shield)](https://circleci.com/gh/genofire/logmania/tree/master) [![Coverage Status](https://coveralls.io/repos/github/genofire/logmania/badge.svg?branch=master)](https://coveralls.io/github/genofire/logmania?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/genofire/logmania)](https://goreportcard.com/report/github.com/genofire/logmania) [![GoDoc](https://godoc.org/github.com/genofire/logmania?status.svg)](https://godoc.org/github.com/genofire/logmania) +# logmania +[![CircleCI](https://circleci.com/gh/genofire/logmania/tree/master.svg?style=shield)](https://circleci.com/gh/genofire/logmania/tree/master) +[![Coverage Status](https://coveralls.io/repos/github/genofire/logmania/badge.svg?branch=master)](https://coveralls.io/github/genofire/logmania?branch=master) +[![Go Report Card](https://goreportcard.com/badge/dev.sum7.eu/genofire/logmania)](https://goreportcard.com/report/dev.sum7.eu/genofire/logmania) +[![GoDoc](https://godoc.org/dev.sum7.eu/genofire/logmania?status.svg)](https://godoc.org/dev.sum7.eu/genofire/logmania) diff --git a/bot/main.go b/bot/main.go index 0d5b77b..c3a3f9a 100644 --- a/bot/main.go +++ b/bot/main.go @@ -4,7 +4,7 @@ import ( "fmt" "strings" - "github.com/genofire/logmania/database" + "dev.sum7.eu/genofire/logmania/database" ) type Bot struct { diff --git a/circle.yml b/circle.yml index 64a38fd..a4d4925 100644 --- a/circle.yml +++ b/circle.yml @@ -3,18 +3,18 @@ jobs: build: docker: - image: circleci/golang:latest - working_directory: /go/src/github.com/genofire/logmania + working_directory: /go/src/dev.sum7.eu/genofire/logmania steps: - checkout - run: go get -t -d -v ./... - - run: go install github.com/genofire/logmania + - run: go install dev.sum7.eu/genofire/logmania - store_artifacts: path: /go/bin/ destination: logmania test: docker: - image: circleci/golang:latest - working_directory: /go/src/github.com/genofire/logmania + working_directory: /go/src/dev.sum7.eu/genofire/logmania steps: - checkout - run: go get -t -d -v ./... @@ -27,7 +27,7 @@ jobs: test_race: docker: - image: circleci/golang:latest - working_directory: /go/src/github.com/genofire/logmania + working_directory: /go/src/dev.sum7.eu/genofire/logmania steps: - checkout - run: go get -t -d -v ./... @@ -35,11 +35,11 @@ jobs: deploy: docker: - image: circleci/golang:latest - working_directory: /go/src/github.com/genofire/logmania + working_directory: /go/src/dev.sum7.eu/genofire/logmania steps: - checkout - run: go get -t -d -v ./... - - run: go install github.com/genofire/logmania + - run: go install dev.sum7.eu/genofire/logmania - run: ./deploy.sh $HOST_FOR_STAGING $PORT_FOR_STAGING workflows: version: 2 diff --git a/cmd/server.go b/cmd/server.go index 84501c3..5469d5b 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -10,15 +10,15 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/genofire/golang-lib/file" - "github.com/genofire/golang-lib/worker" - "github.com/genofire/logmania/bot" - "github.com/genofire/logmania/database" - "github.com/genofire/logmania/lib" - "github.com/genofire/logmania/notify" - allNotify "github.com/genofire/logmania/notify/all" - "github.com/genofire/logmania/receive" - allReceiver "github.com/genofire/logmania/receive/all" + "dev.sum7.eu/genofire/golang-lib/file" + "dev.sum7.eu/genofire/golang-lib/worker" + "dev.sum7.eu/genofire/logmania/bot" + "dev.sum7.eu/genofire/logmania/database" + "dev.sum7.eu/genofire/logmania/lib" + "dev.sum7.eu/genofire/logmania/notify" + allNotify "dev.sum7.eu/genofire/logmania/notify/all" + "dev.sum7.eu/genofire/logmania/receive" + allReceiver "dev.sum7.eu/genofire/logmania/receive/all" ) var ( @@ -48,7 +48,7 @@ var serverCmd = &cobra.Command{ } db = database.ReadDBFile(config.DB) - dbSaveWorker = file.NewSaveJSONWorker(time.Minute, config.DB, db) + go func(){ dbSaveWorker = file.NewSaveJSONWorker(time.Minute, config.DB, db) }() logmaniaBot = bot.NewBot(db) diff --git a/database/main.go b/database/main.go index 3402717..df15513 100644 --- a/database/main.go +++ b/database/main.go @@ -4,7 +4,7 @@ import ( "regexp" "time" - "github.com/genofire/golang-lib/file" + "dev.sum7.eu/genofire/golang-lib/file" log "github.com/sirupsen/logrus" ) diff --git a/lib/config.go b/lib/config.go index 86216c9..ef816b8 100644 --- a/lib/config.go +++ b/lib/config.go @@ -1,7 +1,7 @@ package lib // Struct of the configuration -// e.g. under github.com/genofire/logmania/logmania_example.conf +// e.g. under dev.sum7.eu/genofire/logmania/logmania_example.conf type Config struct { Notify NotifyConfig `toml:"notify"` Receive ReceiveConfig `toml:"receive"` @@ -13,15 +13,8 @@ type NotifyConfig struct { AlertCheck Duration `toml:"alert_check"` Console bool `toml:"debug"` XMPP struct { - Host string `toml:"host"` - Username string `toml:"username"` - Password string `toml:"password"` - Debug bool `toml:"debug"` - NoTLS bool `toml:"no_tls"` - Session bool `toml:"session"` - Status string `toml:"status"` - StatusMessage string `toml:"status_message"` - StartupNotify string `toml:"startup_notify"` + JID string `toml:"jid"` + Password string `toml:"password"` } `toml:"xmpp"` } diff --git a/main.go b/main.go index 7e929ac..e2bb6c9 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,6 @@ package main -import "github.com/genofire/logmania/cmd" +import "dev.sum7.eu/genofire/logmania/cmd" func main() { cmd.Execute() diff --git a/notify/all/internal.go b/notify/all/internal.go index 2687d68..3b59d56 100644 --- a/notify/all/internal.go +++ b/notify/all/internal.go @@ -3,10 +3,10 @@ package all import ( log "github.com/sirupsen/logrus" - "github.com/genofire/logmania/bot" - "github.com/genofire/logmania/database" - "github.com/genofire/logmania/lib" - "github.com/genofire/logmania/notify" + "dev.sum7.eu/genofire/logmania/bot" + "dev.sum7.eu/genofire/logmania/database" + "dev.sum7.eu/genofire/logmania/lib" + "dev.sum7.eu/genofire/logmania/notify" ) type Notifier struct { diff --git a/notify/all/main.go b/notify/all/main.go index 0c3dec8..e159d3b 100644 --- a/notify/all/main.go +++ b/notify/all/main.go @@ -1,5 +1,5 @@ package all import ( - _ "github.com/genofire/logmania/notify/xmpp" + _ "dev.sum7.eu/genofire/logmania/notify/xmpp" ) diff --git a/notify/main.go b/notify/main.go index c7082eb..0eb0231 100644 --- a/notify/main.go +++ b/notify/main.go @@ -3,9 +3,9 @@ package notify import ( log "github.com/sirupsen/logrus" - "github.com/genofire/logmania/bot" - "github.com/genofire/logmania/database" - "github.com/genofire/logmania/lib" + "dev.sum7.eu/genofire/logmania/bot" + "dev.sum7.eu/genofire/logmania/database" + "dev.sum7.eu/genofire/logmania/lib" ) var NotifyRegister []NotifyInit diff --git a/notify/xmpp/main.go b/notify/xmpp/main.go index 3e39c26..746323b 100644 --- a/notify/xmpp/main.go +++ b/notify/xmpp/main.go @@ -2,17 +2,17 @@ package xmpp import ( "errors" - "fmt" - "io" "strings" - xmpp "github.com/mattn/go-xmpp" + xmpp_client "dev.sum7.eu/genofire/yaja/client" + xmpp "dev.sum7.eu/genofire/yaja/xmpp" + "dev.sum7.eu/genofire/yaja/xmpp/base" log "github.com/sirupsen/logrus" - "github.com/genofire/logmania/bot" - "github.com/genofire/logmania/database" - "github.com/genofire/logmania/lib" - "github.com/genofire/logmania/notify" + "dev.sum7.eu/genofire/logmania/bot" + "dev.sum7.eu/genofire/logmania/database" + "dev.sum7.eu/genofire/logmania/lib" + "dev.sum7.eu/genofire/logmania/notify" ) const ( @@ -25,60 +25,113 @@ var logger = log.WithField("notify", proto) type Notifier struct { notify.Notifier - client *xmpp.Client + client *xmpp_client.Client channels map[string]bool db *database.DB formatter *log.TextFormatter } func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifier { - options := xmpp.Options{ - Host: config.XMPP.Host, - User: config.XMPP.Username, - Password: config.XMPP.Password, - NoTLS: config.XMPP.NoTLS, - Debug: config.XMPP.Debug, - Session: config.XMPP.Session, - Status: config.XMPP.Status, - StatusMessage: config.XMPP.StatusMessage, - } - client, err := options.NewClient() + channels := make(map[string]bool) + + client, err := xmpp_client.NewClient(xmppbase.NewJID(config.XMPP.JID), config.XMPP.Password) if err != nil { logger.Error(err) return nil } go func() { for { - chat, err := client.Recv() - if err != nil { - if err == io.EOF { - client, err = options.NewClient() - log.Warn("reconnect") - if err != nil { - log.Panic(err) - } - continue - } - logger.Warn(err) + if err := client.Start(); err != nil { + log.Warn("close connection, try reconnect") + client.Connect(config.XMPP.Password) + } else { + log.Warn("closed connection") + return } - switch v := chat.(type) { - case xmpp.Chat: + } + }() + go func() { + for { + element, more := client.Recv() + if !more { + log.Warn("could not recieve new message, try later") + continue + } + + switch element.(type) { + case *xmpp.PresenceClient: + pres := element.(*xmpp.PresenceClient) + sender := pres.From + logPres := logger.WithField("from", sender.Full()) + switch pres.Type { + case xmpp.PresenceTypeSubscribe: + logPres.Debugf("recv presence subscribe") + pres.Type = xmpp.PresenceTypeSubscribed + pres.To = sender + pres.From = nil + client.Send(pres) + logPres.Debugf("accept new subscribe") + + pres.Type = xmpp.PresenceTypeSubscribe + pres.ID = "" + client.Send(pres) + logPres.Info("request also subscribe") + case xmpp.PresenceTypeSubscribed: + logPres.Info("recv presence accepted subscribe") + case xmpp.PresenceTypeUnsubscribe: + logPres.Info("recv presence remove subscribe") + case xmpp.PresenceTypeUnsubscribed: + logPres.Info("recv presence removed subscribe") + case xmpp.PresenceTypeUnavailable: + logPres.Debug("recv presence unavailable") + case "": + logPres.Debug("recv empty presence, maybe from joining muc") + continue + default: + logPres.Warnf("recv presence unsupported: %s -> %s", pres.Type, xmpp.XMLChildrenString(pres)) + } + case *xmpp.MessageClient: + msg := element.(*xmpp.MessageClient) + from := msg.From.Bare().String() + if msg.Type == xmpp.MessageTypeGroupchat { + from = protoGroup + from + } else { + from = proto + from + } + bot.Handle(func(answer string) { - client.SendHtml(xmpp.Chat{Remote: v.Remote, Type: "chat", Text: answer}) - }, fmt.Sprintf("xmpp:%s", strings.Split(v.Remote, "/")[0]), v.Text) + err := client.Send(&xmpp.MessageClient{ + Type: msg.Type, + To: msg.From, + Body: answer, + }) + if err != nil { + logger.Error("xmpp to ", msg.From.String(), " error:", err) + } + }, from, msg.Body) } } }() for _, toAddresses := range db.HostTo { for to, _ := range toAddresses { toAddr := strings.TrimPrefix(to, protoGroup) - client.JoinMUCNoHistory(toAddr, nickname) + toJID := xmppbase.NewJID(toAddr) + toJID.Resource = nickname + err := client.Send(&xmpp.PresenceClient{ + To: toJID, + }) + if err != nil { + logger.Error("xmpp could not join ", toJID.String(), " error:", err) + } else { + channels[toAddr] = true + } } } logger.Info("startup") return &Notifier{ - client: client, - db: db, + channels: channels, + client: client, + db: db, formatter: &log.TextFormatter{ DisableTimestamp: true, }, @@ -98,15 +151,32 @@ func (n *Notifier) Send(e *log.Entry) error { if strings.HasPrefix(toAddr, protoGroup) { toAddr = strings.TrimPrefix(toAddr, protoGroup) if _, ok := n.channels[toAddr]; ok { - n.client.JoinMUCNoHistory(toAddr, nickname) + toJID := xmppbase.NewJID(toAddr) + toJID.Resource = nickname + err := n.client.Send(&xmpp.PresenceClient{ + To: toJID, + }) + if err != nil { + logger.Error("xmpp could not join ", toJID.String(), " error:", err) + } else { + n.channels[toAddr] = true + } } - _, err = n.client.SendHtml(xmpp.Chat{Remote: toAddr, Type: "groupchat", Text: string(text)}) + err := n.client.Send(&xmpp.MessageClient{ + Type: xmpp.MessageTypeGroupchat, + To: xmppbase.NewJID(toAddr), + Body: string(text), + }) if err != nil { - logger.Error("xmpp to ", to, " error:", err) + logger.Error("xmpp to ", toAddr, " error:", err) } } else { toAddr = strings.TrimPrefix(toAddr, proto) - _, err := n.client.SendHtml(xmpp.Chat{Remote: toAddr, Type: "chat", Text: string(text)}) + err := n.client.Send(&xmpp.MessageClient{ + Type: xmpp.MessageTypeChat, + To: xmppbase.NewJID(toAddr), + Body: string(text), + }) if err != nil { logger.Error("xmpp to ", to, " error:", err) } @@ -117,7 +187,15 @@ func (n *Notifier) Send(e *log.Entry) error { func (n *Notifier) Close() { for jid := range n.channels { - n.client.LeaveMUC(jid) + toJID := xmppbase.NewJID(jid) + toJID.Resource = nickname + err := n.client.Send(&xmpp.PresenceClient{ + To: toJID, + Type: xmpp.PresenceTypeUnavailable, + }) + if err != nil { + logger.Error("xmpp could not leave ", toJID.String(), " error:", err) + } } n.client.Close() } diff --git a/receive/all/internal.go b/receive/all/internal.go index 7314d21..7f0d27f 100644 --- a/receive/all/internal.go +++ b/receive/all/internal.go @@ -3,8 +3,8 @@ package all import ( log "github.com/sirupsen/logrus" - "github.com/genofire/logmania/lib" - "github.com/genofire/logmania/receive" + "dev.sum7.eu/genofire/logmania/lib" + "dev.sum7.eu/genofire/logmania/receive" ) type Receiver struct { diff --git a/receive/all/main.go b/receive/all/main.go index 313bce6..cc2c154 100644 --- a/receive/all/main.go +++ b/receive/all/main.go @@ -1,7 +1,7 @@ package all import ( - _ "github.com/genofire/logmania/receive/journald_json" - _ "github.com/genofire/logmania/receive/logrus" - _ "github.com/genofire/logmania/receive/syslog" + _ "dev.sum7.eu/genofire/logmania/receive/journald_json" + _ "dev.sum7.eu/genofire/logmania/receive/logrus" + _ "dev.sum7.eu/genofire/logmania/receive/syslog" ) diff --git a/receive/journald_json/main.go b/receive/journald_json/main.go index b168fd6..28a1bdd 100644 --- a/receive/journald_json/main.go +++ b/receive/journald_json/main.go @@ -5,8 +5,8 @@ import ( log "github.com/sirupsen/logrus" - "github.com/genofire/logmania/lib" - "github.com/genofire/logmania/receive" + "dev.sum7.eu/genofire/logmania/lib" + "dev.sum7.eu/genofire/logmania/receive" ) var logger = log.WithField("receive", "journald_json") diff --git a/receive/logrus/client/main.go b/receive/logrus/client/main.go index 74457b9..a7b8fa0 100644 --- a/receive/logrus/client/main.go +++ b/receive/logrus/client/main.go @@ -3,8 +3,8 @@ package client import ( "io" - websocketLib "github.com/genofire/golang-lib/websocket" - "github.com/genofire/logmania/receive/logrus" + websocketLib "dev.sum7.eu/genofire/golang-lib/websocket" + "dev.sum7.eu/genofire/logmania/receive/logrus" "github.com/google/uuid" "github.com/gorilla/websocket" log "github.com/sirupsen/logrus" diff --git a/receive/logrus/main.go b/receive/logrus/main.go index fd8e5d8..6a24602 100644 --- a/receive/logrus/main.go +++ b/receive/logrus/main.go @@ -3,11 +3,11 @@ package logrus import ( "net/http" - "github.com/genofire/golang-lib/websocket" + "dev.sum7.eu/genofire/golang-lib/websocket" log "github.com/sirupsen/logrus" - "github.com/genofire/logmania/lib" - "github.com/genofire/logmania/receive" + "dev.sum7.eu/genofire/logmania/lib" + "dev.sum7.eu/genofire/logmania/receive" ) const WS_LOG_ENTRY = "log" diff --git a/receive/main.go b/receive/main.go index 0be1b04..2206d87 100644 --- a/receive/main.go +++ b/receive/main.go @@ -1,7 +1,7 @@ package receive import ( - "github.com/genofire/logmania/lib" + "dev.sum7.eu/genofire/logmania/lib" log "github.com/sirupsen/logrus" ) diff --git a/receive/syslog/internal.go b/receive/syslog/internal.go index 6a3a8a0..5acde82 100644 --- a/receive/syslog/internal.go +++ b/receive/syslog/internal.go @@ -3,7 +3,7 @@ package syslog import ( log "github.com/sirupsen/logrus" - libSyslog "github.com/genofire/logmania/lib/syslog" + libSyslog "dev.sum7.eu/genofire/logmania/lib/syslog" ) var SyslogPriorityMap = map[int]log.Level{ diff --git a/receive/syslog/main.go b/receive/syslog/main.go index 0ef86be..47bfb15 100644 --- a/receive/syslog/main.go +++ b/receive/syslog/main.go @@ -5,8 +5,8 @@ import ( log "github.com/sirupsen/logrus" - "github.com/genofire/logmania/lib" - "github.com/genofire/logmania/receive" + "dev.sum7.eu/genofire/logmania/lib" + "dev.sum7.eu/genofire/logmania/receive" ) var logger = log.WithField("receive", "syslog")