move to dev.sum7.eu + use yaja xmpp client

This commit is contained in:
Martin/Geno 2018-04-13 16:44:23 +02:00
parent a96efdf3cc
commit 5e5b54bab9
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
19 changed files with 171 additions and 96 deletions

View File

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

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/genofire/logmania/database" "dev.sum7.eu/genofire/logmania/database"
) )
type Bot struct { type Bot struct {

View File

@ -3,18 +3,18 @@ jobs:
build: build:
docker: docker:
- image: circleci/golang:latest - image: circleci/golang:latest
working_directory: /go/src/github.com/genofire/logmania working_directory: /go/src/dev.sum7.eu/genofire/logmania
steps: steps:
- checkout - checkout
- run: go get -t -d -v ./... - run: go get -t -d -v ./...
- run: go install github.com/genofire/logmania - run: go install dev.sum7.eu/genofire/logmania
- store_artifacts: - store_artifacts:
path: /go/bin/ path: /go/bin/
destination: logmania destination: logmania
test: test:
docker: docker:
- image: circleci/golang:latest - image: circleci/golang:latest
working_directory: /go/src/github.com/genofire/logmania working_directory: /go/src/dev.sum7.eu/genofire/logmania
steps: steps:
- checkout - checkout
- run: go get -t -d -v ./... - run: go get -t -d -v ./...
@ -27,7 +27,7 @@ jobs:
test_race: test_race:
docker: docker:
- image: circleci/golang:latest - image: circleci/golang:latest
working_directory: /go/src/github.com/genofire/logmania working_directory: /go/src/dev.sum7.eu/genofire/logmania
steps: steps:
- checkout - checkout
- run: go get -t -d -v ./... - run: go get -t -d -v ./...
@ -35,11 +35,11 @@ jobs:
deploy: deploy:
docker: docker:
- image: circleci/golang:latest - image: circleci/golang:latest
working_directory: /go/src/github.com/genofire/logmania working_directory: /go/src/dev.sum7.eu/genofire/logmania
steps: steps:
- checkout - checkout
- run: go get -t -d -v ./... - 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 - run: ./deploy.sh $HOST_FOR_STAGING $PORT_FOR_STAGING
workflows: workflows:
version: 2 version: 2

View File

@ -10,15 +10,15 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/genofire/golang-lib/file" "dev.sum7.eu/genofire/golang-lib/file"
"github.com/genofire/golang-lib/worker" "dev.sum7.eu/genofire/golang-lib/worker"
"github.com/genofire/logmania/bot" "dev.sum7.eu/genofire/logmania/bot"
"github.com/genofire/logmania/database" "dev.sum7.eu/genofire/logmania/database"
"github.com/genofire/logmania/lib" "dev.sum7.eu/genofire/logmania/lib"
"github.com/genofire/logmania/notify" "dev.sum7.eu/genofire/logmania/notify"
allNotify "github.com/genofire/logmania/notify/all" allNotify "dev.sum7.eu/genofire/logmania/notify/all"
"github.com/genofire/logmania/receive" "dev.sum7.eu/genofire/logmania/receive"
allReceiver "github.com/genofire/logmania/receive/all" allReceiver "dev.sum7.eu/genofire/logmania/receive/all"
) )
var ( var (
@ -48,7 +48,7 @@ var serverCmd = &cobra.Command{
} }
db = database.ReadDBFile(config.DB) 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) logmaniaBot = bot.NewBot(db)

View File

@ -4,7 +4,7 @@ import (
"regexp" "regexp"
"time" "time"
"github.com/genofire/golang-lib/file" "dev.sum7.eu/genofire/golang-lib/file"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )

View File

@ -1,7 +1,7 @@
package lib package lib
// Struct of the configuration // 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 { type Config struct {
Notify NotifyConfig `toml:"notify"` Notify NotifyConfig `toml:"notify"`
Receive ReceiveConfig `toml:"receive"` Receive ReceiveConfig `toml:"receive"`
@ -13,15 +13,8 @@ type NotifyConfig struct {
AlertCheck Duration `toml:"alert_check"` AlertCheck Duration `toml:"alert_check"`
Console bool `toml:"debug"` Console bool `toml:"debug"`
XMPP struct { XMPP struct {
Host string `toml:"host"` JID string `toml:"jid"`
Username string `toml:"username"`
Password string `toml:"password"` 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"`
} `toml:"xmpp"` } `toml:"xmpp"`
} }

View File

@ -1,6 +1,6 @@
package main package main
import "github.com/genofire/logmania/cmd" import "dev.sum7.eu/genofire/logmania/cmd"
func main() { func main() {
cmd.Execute() cmd.Execute()

View File

@ -3,10 +3,10 @@ package all
import ( import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/genofire/logmania/bot" "dev.sum7.eu/genofire/logmania/bot"
"github.com/genofire/logmania/database" "dev.sum7.eu/genofire/logmania/database"
"github.com/genofire/logmania/lib" "dev.sum7.eu/genofire/logmania/lib"
"github.com/genofire/logmania/notify" "dev.sum7.eu/genofire/logmania/notify"
) )
type Notifier struct { type Notifier struct {

View File

@ -1,5 +1,5 @@
package all package all
import ( import (
_ "github.com/genofire/logmania/notify/xmpp" _ "dev.sum7.eu/genofire/logmania/notify/xmpp"
) )

View File

@ -3,9 +3,9 @@ package notify
import ( import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/genofire/logmania/bot" "dev.sum7.eu/genofire/logmania/bot"
"github.com/genofire/logmania/database" "dev.sum7.eu/genofire/logmania/database"
"github.com/genofire/logmania/lib" "dev.sum7.eu/genofire/logmania/lib"
) )
var NotifyRegister []NotifyInit var NotifyRegister []NotifyInit

View File

@ -2,17 +2,17 @@ package xmpp
import ( import (
"errors" "errors"
"fmt"
"io"
"strings" "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" log "github.com/sirupsen/logrus"
"github.com/genofire/logmania/bot" "dev.sum7.eu/genofire/logmania/bot"
"github.com/genofire/logmania/database" "dev.sum7.eu/genofire/logmania/database"
"github.com/genofire/logmania/lib" "dev.sum7.eu/genofire/logmania/lib"
"github.com/genofire/logmania/notify" "dev.sum7.eu/genofire/logmania/notify"
) )
const ( const (
@ -25,58 +25,111 @@ var logger = log.WithField("notify", proto)
type Notifier struct { type Notifier struct {
notify.Notifier notify.Notifier
client *xmpp.Client client *xmpp_client.Client
channels map[string]bool channels map[string]bool
db *database.DB db *database.DB
formatter *log.TextFormatter formatter *log.TextFormatter
} }
func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifier { func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifier {
options := xmpp.Options{ channels := make(map[string]bool)
Host: config.XMPP.Host,
User: config.XMPP.Username, client, err := xmpp_client.NewClient(xmppbase.NewJID(config.XMPP.JID), config.XMPP.Password)
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()
if err != nil { if err != nil {
logger.Error(err) logger.Error(err)
return nil return nil
} }
go func() { go func() {
for { for {
chat, err := client.Recv() if err := client.Start(); err != nil {
if err != nil { log.Warn("close connection, try reconnect")
if err == io.EOF { client.Connect(config.XMPP.Password)
client, err = options.NewClient() } else {
log.Warn("reconnect") log.Warn("closed connection")
if err != nil { return
log.Panic(err)
} }
}
}()
go func() {
for {
element, more := client.Recv()
if !more {
log.Warn("could not recieve new message, try later")
continue continue
} }
logger.Warn(err)
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))
} }
switch v := chat.(type) { case *xmpp.MessageClient:
case xmpp.Chat: 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) { bot.Handle(func(answer string) {
client.SendHtml(xmpp.Chat{Remote: v.Remote, Type: "chat", Text: answer}) err := client.Send(&xmpp.MessageClient{
}, fmt.Sprintf("xmpp:%s", strings.Split(v.Remote, "/")[0]), v.Text) 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 _, toAddresses := range db.HostTo {
for to, _ := range toAddresses { for to, _ := range toAddresses {
toAddr := strings.TrimPrefix(to, protoGroup) 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") logger.Info("startup")
return &Notifier{ return &Notifier{
channels: channels,
client: client, client: client,
db: db, db: db,
formatter: &log.TextFormatter{ formatter: &log.TextFormatter{
@ -98,15 +151,32 @@ func (n *Notifier) Send(e *log.Entry) error {
if strings.HasPrefix(toAddr, protoGroup) { if strings.HasPrefix(toAddr, protoGroup) {
toAddr = strings.TrimPrefix(toAddr, protoGroup) toAddr = strings.TrimPrefix(toAddr, protoGroup)
if _, ok := n.channels[toAddr]; ok { if _, ok := n.channels[toAddr]; ok {
n.client.JoinMUCNoHistory(toAddr, nickname) toJID := xmppbase.NewJID(toAddr)
} toJID.Resource = nickname
_, err = n.client.SendHtml(xmpp.Chat{Remote: toAddr, Type: "groupchat", Text: string(text)}) err := n.client.Send(&xmpp.PresenceClient{
To: toJID,
})
if err != nil { if err != nil {
logger.Error("xmpp to ", to, " error:", err) logger.Error("xmpp could not join ", toJID.String(), " error:", err)
} else {
n.channels[toAddr] = true
}
}
err := n.client.Send(&xmpp.MessageClient{
Type: xmpp.MessageTypeGroupchat,
To: xmppbase.NewJID(toAddr),
Body: string(text),
})
if err != nil {
logger.Error("xmpp to ", toAddr, " error:", err)
} }
} else { } else {
toAddr = strings.TrimPrefix(toAddr, proto) 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 { if err != nil {
logger.Error("xmpp to ", to, " error:", err) logger.Error("xmpp to ", to, " error:", err)
} }
@ -117,7 +187,15 @@ func (n *Notifier) Send(e *log.Entry) error {
func (n *Notifier) Close() { func (n *Notifier) Close() {
for jid := range n.channels { 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() n.client.Close()
} }

View File

@ -3,8 +3,8 @@ package all
import ( import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/genofire/logmania/lib" "dev.sum7.eu/genofire/logmania/lib"
"github.com/genofire/logmania/receive" "dev.sum7.eu/genofire/logmania/receive"
) )
type Receiver struct { type Receiver struct {

View File

@ -1,7 +1,7 @@
package all package all
import ( import (
_ "github.com/genofire/logmania/receive/journald_json" _ "dev.sum7.eu/genofire/logmania/receive/journald_json"
_ "github.com/genofire/logmania/receive/logrus" _ "dev.sum7.eu/genofire/logmania/receive/logrus"
_ "github.com/genofire/logmania/receive/syslog" _ "dev.sum7.eu/genofire/logmania/receive/syslog"
) )

View File

@ -5,8 +5,8 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/genofire/logmania/lib" "dev.sum7.eu/genofire/logmania/lib"
"github.com/genofire/logmania/receive" "dev.sum7.eu/genofire/logmania/receive"
) )
var logger = log.WithField("receive", "journald_json") var logger = log.WithField("receive", "journald_json")

View File

@ -3,8 +3,8 @@ package client
import ( import (
"io" "io"
websocketLib "github.com/genofire/golang-lib/websocket" websocketLib "dev.sum7.eu/genofire/golang-lib/websocket"
"github.com/genofire/logmania/receive/logrus" "dev.sum7.eu/genofire/logmania/receive/logrus"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"

View File

@ -3,11 +3,11 @@ package logrus
import ( import (
"net/http" "net/http"
"github.com/genofire/golang-lib/websocket" "dev.sum7.eu/genofire/golang-lib/websocket"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/genofire/logmania/lib" "dev.sum7.eu/genofire/logmania/lib"
"github.com/genofire/logmania/receive" "dev.sum7.eu/genofire/logmania/receive"
) )
const WS_LOG_ENTRY = "log" const WS_LOG_ENTRY = "log"

View File

@ -1,7 +1,7 @@
package receive package receive
import ( import (
"github.com/genofire/logmania/lib" "dev.sum7.eu/genofire/logmania/lib"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )

View File

@ -3,7 +3,7 @@ package syslog
import ( import (
log "github.com/sirupsen/logrus" 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{ var SyslogPriorityMap = map[int]log.Level{

View File

@ -5,8 +5,8 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/genofire/logmania/lib" "dev.sum7.eu/genofire/logmania/lib"
"github.com/genofire/logmania/receive" "dev.sum7.eu/genofire/logmania/receive"
) )
var logger = log.WithField("receive", "syslog") var logger = log.WithField("receive", "syslog")