diff --git a/go.mod b/go.mod index 200021b4..69c005c7 100644 --- a/go.mod +++ b/go.mod @@ -64,7 +64,7 @@ require ( github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/go-errors/errors v1.0.1 // indirect github.com/go-redis/redis/v8 v8.8.2 // indirect - github.com/gorilla/websocket v1.4.2 // indirect + github.com/gorilla/websocket v1.4.2 github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.2 // indirect github.com/kkdai/bstream v1.0.0 // indirect diff --git a/internal/telegram/nostr.go b/internal/telegram/nostr.go index 31649a62..af019f20 100644 --- a/internal/telegram/nostr.go +++ b/internal/telegram/nostr.go @@ -7,12 +7,35 @@ import ( "time" "github.com/LightningTipBot/LightningTipBot/internal" + "github.com/LightningTipBot/LightningTipBot/internal/netguard" "github.com/LightningTipBot/LightningTipBot/internal/telegram/intercept" + "github.com/gorilla/websocket" "github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr/nip19" log "github.com/sirupsen/logrus" ) +// maxRequestedRelays caps the relays taken from a zap request, so they can't +// push the default relays out of the list. +const maxRequestedRelays = 10 + +// go-nostr connects with the default websocket dialer. Relay URLs come from +// zap requests, so it must only reach public addresses. +func init() { + websocket.DefaultDialer.NetDialContext = netguard.DialContext +} + +// requestedRelays keeps the wss:// relays of a zap request, up to maxRequestedRelays. +func requestedRelays(relays []string) []string { + list := []string{} + for _, relay := range relays { + if strings.HasPrefix(relay, "wss://") && len(list) < maxRequestedRelays { + list = append(list, relay) + } + } + return list +} + var ( nosterRegisterMessage = "📖 Add your nostr pubkey for zap receipts" nostrInfoMessage = "💜 *Your nostr information*\n\nYour pubkey: `%s`" @@ -64,8 +87,8 @@ func (bot *TipBot) publishNostrEvent(ev nostr.Event, relays []string) { ev.Sign(pk) log.Debugf("[NOSTR] 🟣 publishing nostr event %s", ev.ID) - // more relays - relays = append(relays, "wss://relay.nostr.ch", "wss://eden.nostr.land", "wss://nostr.btcmp.com", "wss://nostr.relayer.se", "wss://relay.current.fyi", "wss://nos.lol", "wss://nostr.mom", "wss://relay.nostr.info", "wss://nostr.zebedee.cloud", "wss://nostr-pub.wellorder.net", "wss://relay.snort.social/", "wss://relay.damus.io/", "wss://nostr.oxtr.dev/", "wss://nostr.fmt.wiz.biz/", "wss://brb.io") + // more relays (only public wss:// relays are taken from the zap request) + relays = append(requestedRelays(relays), "wss://relay.nostr.ch", "wss://eden.nostr.land", "wss://nostr.btcmp.com", "wss://nostr.relayer.se", "wss://relay.current.fyi", "wss://nos.lol", "wss://nostr.mom", "wss://relay.nostr.info", "wss://nostr.zebedee.cloud", "wss://nostr-pub.wellorder.net", "wss://relay.snort.social/", "wss://relay.damus.io/", "wss://nostr.oxtr.dev/", "wss://nostr.fmt.wiz.biz/", "wss://brb.io") // remove trailing / relays = cleanUrls(relays)