move to dev.sum7.eu + use yaja xmpp client
This commit is contained in:
parent
a96efdf3cc
commit
5e5b54bab9
|
@ -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)
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/genofire/logmania/database"
|
||||
"dev.sum7.eu/genofire/logmania/database"
|
||||
)
|
||||
|
||||
type Bot struct {
|
||||
|
|
12
circle.yml
12
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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
||||
|
|
2
main.go
2
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()
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
package all
|
||||
|
||||
import (
|
||||
_ "github.com/genofire/logmania/notify/xmpp"
|
||||
_ "dev.sum7.eu/genofire/logmania/notify/xmpp"
|
||||
)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package receive
|
||||
|
||||
import (
|
||||
"github.com/genofire/logmania/lib"
|
||||
"dev.sum7.eu/genofire/logmania/lib"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue