Adds 93 services, bringing the total to 109 plus webhook and email: chat and messengers (DingTalk, Feishu, LINE, Nextcloud Talk, Threema, WhatsApp gateways, Nostr, Home Assistant, …), push (Bark, Gorush, ServerChan, …), SMS and calls (Twilio, Plivo, 46elks, Alibaba Cloud, Teltonika routers, …), email APIs (SendGrid, Brevo, Resend, Microsoft Graph, …) and incident management (Grafana OnCall, Jira Service Management, Splunk On-Call, Squadcast, SIGNL4, …). Customers bring their own credentials; secrets are sealed like the others. Services pair incidents by their ID where they can resolve alerts. Signed requests (DingTalk, Nextcloud Talk, Alibaba Cloud) and replies that report failures with status 200 are handled. Nostr messages are sealed and gift-wrapped (NIP-17/59) with go-nostr, but sent over our own WebSocket connections so relays go through the network guard. Messages now carry the monitor's name, ID and target. The channel form groups services, the i18n test derives provider keys from the registry, and channel types may contain digits. Refs #92 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
131 lines
3.5 KiB
Go
131 lines
3.5 KiB
Go
package notify
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
"github.com/nbd-wtf/go-nostr"
|
|
"github.com/nbd-wtf/go-nostr/nip19"
|
|
"github.com/nbd-wtf/go-nostr/nip44"
|
|
"github.com/nbd-wtf/go-nostr/nip59"
|
|
)
|
|
|
|
// Nostr private messages (NIP-17): the message is sealed for each
|
|
// recipient and gift-wrapped (NIP-59). Relays are reached over our own
|
|
// WebSocket connections, so the network guard applies to them.
|
|
|
|
func sendNostr(ctx context.Context, c *client, cfg map[string]string, m Message) error {
|
|
sk, err := nostrKey(cfg["sender_nsec"], "nsec")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pub, err := nostr.GetPublicKey(sk)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var events []nostr.Event
|
|
for _, r := range list(cfg["recipients"]) {
|
|
to, err := nostrKey(r, "npub")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rumor := nostr.Event{Kind: nostr.KindDirectMessage, Content: m.text(), Tags: nostr.Tags{{"p", to}}, CreatedAt: nostr.Now(), PubKey: pub}
|
|
rumor.ID = rumor.GetID()
|
|
wrap, err := nip59.GiftWrap(rumor, to,
|
|
func(plain string) (string, error) {
|
|
key, err := nip44.GenerateConversationKey(to, sk)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return nip44.Encrypt(plain, key)
|
|
},
|
|
func(e *nostr.Event) error { return e.Sign(sk) }, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
events = append(events, wrap)
|
|
}
|
|
if len(events) == 0 {
|
|
return errors.New("no recipient set")
|
|
}
|
|
var errs []error
|
|
delivered := 0
|
|
for _, relay := range list(cfg["relays"]) {
|
|
if err := c.publishNostr(ctx, relay, events); err != nil {
|
|
errs = append(errs, fmt.Errorf("%s: %w", relayHost(relay), err))
|
|
continue
|
|
}
|
|
delivered++
|
|
}
|
|
if delivered == 0 {
|
|
return errors.Join(append([]error{errors.New("no relay accepted the message")}, errs...)...)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// nostrKey decodes a bech32 key with prefix want to hex.
|
|
func nostrKey(s, want string) (string, error) {
|
|
prefix, v, err := nip19.Decode(strings.TrimSpace(s))
|
|
if err != nil || prefix != want {
|
|
return "", fmt.Errorf("not an %s key", want)
|
|
}
|
|
return v.(string), nil
|
|
}
|
|
|
|
func relayHost(relay string) string {
|
|
h := strings.TrimPrefix(strings.TrimPrefix(relay, "wss://"), "ws://")
|
|
return strings.SplitN(h, "/", 2)[0]
|
|
}
|
|
|
|
// publishNostr sends events to a relay and waits for it to accept each.
|
|
func (c *client) publishNostr(ctx context.Context, relay string, events []nostr.Event) error {
|
|
if !strings.HasPrefix(relay, "wss://") && !strings.HasPrefix(relay, "ws://") {
|
|
return errors.New("relays are wss:// URLs")
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, 8*time.Second)
|
|
defer cancel()
|
|
conn, _, err := websocket.Dial(ctx, relay, &websocket.DialOptions{HTTPClient: c.hc})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = conn.CloseNow() }()
|
|
for _, e := range events {
|
|
msg, _ := json.Marshal([]any{"EVENT", e})
|
|
if err := conn.Write(ctx, websocket.MessageText, msg); err != nil {
|
|
return err
|
|
}
|
|
for {
|
|
_, data, err := conn.Read(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var reply []json.RawMessage
|
|
if json.Unmarshal(data, &reply) != nil || len(reply) < 3 {
|
|
continue
|
|
}
|
|
var kind, id string
|
|
var ok bool
|
|
_ = json.Unmarshal(reply[0], &kind)
|
|
_ = json.Unmarshal(reply[1], &id)
|
|
_ = json.Unmarshal(reply[2], &ok)
|
|
if kind != "OK" || id != e.ID {
|
|
continue
|
|
}
|
|
if !ok {
|
|
var reason string
|
|
if len(reply) > 3 {
|
|
_ = json.Unmarshal(reply[3], &reason)
|
|
}
|
|
return errors.New("rejected: " + truncate(reason, 200))
|
|
}
|
|
break
|
|
}
|
|
}
|
|
return conn.Close(websocket.StatusNormalClosure, "")
|
|
}
|