kopia lustrzana https://github.com/tmsmr/xmpp-webhook
Handle alerts with mellium-xmpp (WiP)
rodzic
87737c8f61
commit
e362a0ead8
44
main.go
44
main.go
|
@ -13,7 +13,9 @@ import (
|
||||||
"mellium.im/xmpp/dial"
|
"mellium.im/xmpp/dial"
|
||||||
"mellium.im/xmpp/jid"
|
"mellium.im/xmpp/jid"
|
||||||
"mellium.im/xmpp/stanza"
|
"mellium.im/xmpp/stanza"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func panicOnErr(err error) {
|
func panicOnErr(err error) {
|
||||||
|
@ -30,6 +32,7 @@ type MessageBody struct {
|
||||||
func initXMPP(address jid.JID, pass string, skipTLSVerify bool, legacyTLS bool, forceStartTLS bool) (*xmpp.Session, error) {
|
func initXMPP(address jid.JID, pass string, skipTLSVerify bool, legacyTLS bool, forceStartTLS bool) (*xmpp.Session, error) {
|
||||||
tlsConfig := tls.Config{InsecureSkipVerify: skipTLSVerify}
|
tlsConfig := tls.Config{InsecureSkipVerify: skipTLSVerify}
|
||||||
var dialer dial.Dialer
|
var dialer dial.Dialer
|
||||||
|
// only use the tls config for the dialer if necessary
|
||||||
if skipTLSVerify {
|
if skipTLSVerify {
|
||||||
dialer = dial.Dialer{NoTLS: !legacyTLS, TLSConfig: &tlsConfig}
|
dialer = dial.Dialer{NoTLS: !legacyTLS, TLSConfig: &tlsConfig}
|
||||||
} else {
|
} else {
|
||||||
|
@ -39,6 +42,7 @@ func initXMPP(address jid.JID, pass string, skipTLSVerify bool, legacyTLS bool,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// we need the domain in the tls config if we want to verify the cert
|
||||||
if !skipTLSVerify {
|
if !skipTLSVerify {
|
||||||
tlsConfig.ServerName = address.Domainpart()
|
tlsConfig.ServerName = address.Domainpart()
|
||||||
}
|
}
|
||||||
|
@ -81,29 +85,35 @@ func main() {
|
||||||
panicOnErr(err)
|
panicOnErr(err)
|
||||||
|
|
||||||
// connect to xmpp server
|
// connect to xmpp server
|
||||||
session, err := initXMPP(address, xp, skipTLSVerify, legacyTLS, forceStartTLS)
|
xmppSession, err := initXMPP(address, xp, skipTLSVerify, legacyTLS, forceStartTLS)
|
||||||
panicOnErr(err)
|
panicOnErr(err)
|
||||||
defer closeXMPP(session)
|
defer closeXMPP(xmppSession)
|
||||||
|
|
||||||
// send initial presence
|
// send initial presence
|
||||||
panicOnErr(session.Send(context.TODO(), stanza.WrapPresence(jid.JID{}, stanza.AvailablePresence, nil)))
|
panicOnErr(xmppSession.Send(context.TODO(), stanza.WrapPresence(jid.JID{}, stanza.AvailablePresence, nil)))
|
||||||
|
|
||||||
err = session.Serve(xmpp.HandlerFunc(func(t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
|
// listen for messages and echo them
|
||||||
|
go func() {
|
||||||
|
err = xmppSession.Serve(xmpp.HandlerFunc(func(t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
|
||||||
d := xml.NewTokenDecoder(t)
|
d := xml.NewTokenDecoder(t)
|
||||||
|
// ignore elements that aren't messages
|
||||||
if start.Name.Local != "message" {
|
if start.Name.Local != "message" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parse message into struct
|
||||||
msg := MessageBody{}
|
msg := MessageBody{}
|
||||||
err = d.DecodeElement(&msg, start)
|
err = d.DecodeElement(&msg, start)
|
||||||
if err != nil && err != io.EOF {
|
if err != nil && err != io.EOF {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ignore empty messages and stanzas that aren't messages
|
||||||
if msg.Body == "" || msg.Type != stanza.ChatMessage {
|
if msg.Body == "" || msg.Type != stanza.ChatMessage {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create reply with identical contents
|
||||||
reply := MessageBody{
|
reply := MessageBody{
|
||||||
Message: stanza.Message{
|
Message: stanza.Message{
|
||||||
To: msg.From.Bare(),
|
To: msg.From.Bare(),
|
||||||
|
@ -111,30 +121,26 @@ func main() {
|
||||||
Body: msg.Body,
|
Body: msg.Body,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = t.Encode(reply)
|
// try to send reply, ignore errors
|
||||||
if err != nil {
|
_ = t.Encode(reply)
|
||||||
fmt.Printf("Error responding to message %q: %q", msg.ID, err)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}))
|
}))
|
||||||
|
|
||||||
panicOnErr(err)
|
panicOnErr(err)
|
||||||
|
}()
|
||||||
|
|
||||||
/*// create chan for messages (webhooks -> xmpp)
|
// create chan for messages (webhooks -> xmpp)
|
||||||
messages := make(chan string)
|
messages := make(chan string)
|
||||||
|
|
||||||
// wait for messages from the webhooks and send them to all receivers
|
// wait for messages from the webhooks and send them to all receivers
|
||||||
go func() {
|
go func() {
|
||||||
for m := range messages {
|
for m := range messages {
|
||||||
for _, r := range strings.Split(xr, ",") {
|
for _, r := range strings.Split(xr, ",") {
|
||||||
xc.Out <- xmpp.Message{
|
recipient, err := jid.Parse(r)
|
||||||
To: r,
|
panicOnErr(err)
|
||||||
Body: []xmpp.MessageBody{
|
fmt.Println(m)
|
||||||
{
|
fmt.Println(recipient)
|
||||||
Value: m,
|
// try to send message, ignore errors
|
||||||
},
|
//_ = xmppSession.Send(context.TODO(), stanza.WrapMessage(recipient, stanza.NormalMessage))
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -143,5 +149,5 @@ func main() {
|
||||||
http.Handle("/grafana", newMessageHandler(messages, grafanaParserFunc))
|
http.Handle("/grafana", newMessageHandler(messages, grafanaParserFunc))
|
||||||
|
|
||||||
// listen for requests
|
// listen for requests
|
||||||
http.ListenAndServe(":4321", nil)*/
|
http.ListenAndServe(":4321", nil)
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue