From ce2ecb38cca08632f5d8effe34da40fb42f6b05d Mon Sep 17 00:00:00 2001 From: Daniel Cadenas Date: Fri, 3 May 2024 13:29:05 -0300 Subject: [PATCH] Filter relays --- .../adapters/firestore/repository_relays.go | 25 +++++++++++++- service/app/downloader.go | 2 ++ service/domain/registration.go | 34 +++++++++++++++++-- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/service/adapters/firestore/repository_relays.go b/service/adapters/firestore/repository_relays.go index 07e12bf..f815ab9 100644 --- a/service/adapters/firestore/repository_relays.go +++ b/service/adapters/firestore/repository_relays.go @@ -3,6 +3,7 @@ package firestore import ( "context" "encoding/hex" + "strings" "time" "cloud.google.com/go/firestore" @@ -11,6 +12,14 @@ import ( "google.golang.org/api/iterator" ) +var relaySuffixesToSkip = []string{ + "127.0.0.1", + "localhost", + "nostr.band", + "nostrja-kari-nip50.heguro.com", + "nostr.sebastix.social", +} + const ( collectionRelays = "relays" collectionRelaysFieldAddress = "address" @@ -75,12 +84,26 @@ func (r *RelayRepository) GetRelays(ctx context.Context, updatedAfter time.Time) if err != nil { return nil, errors.Wrapf(err, "error creating a relay address from key '%s'", docRef.Ref.ID) } - result = append(result, relayAddress) + + if !endsWithAny(relayAddress.HostWithoutPort(), relaySuffixesToSkip) { + result = append(result, relayAddress) + } } return result, nil } +// endsWithAny checks if the given string ends with any of the strings in the +// list. +func endsWithAny(s string, list []string) bool { + for _, suffix := range list { + if strings.HasSuffix(s, suffix) { + return true + } + } + return false +} + func (r *RelayRepository) GetPublicKeys(ctx context.Context, address domain.RelayAddress, updatedAfter time.Time) ([]domain.PublicKey, error) { iter := r.tx.Documents( r.client. diff --git a/service/app/downloader.go b/service/app/downloader.go index c0b69eb..4c8d208 100644 --- a/service/app/downloader.go +++ b/service/app/downloader.go @@ -338,6 +338,8 @@ func (d *RelayDownloader) manageSubs( } } +// For each pubkey for which we don't have an active nostr REQ, create a new REQ +// For each pubkey for which we do have and active nostr REQ but it's not in the list, close it func (d *RelayDownloader) updateSubs( conn *websocket.Conn, activeSubscriptions *internal.Set[domain.PublicKey], diff --git a/service/domain/registration.go b/service/domain/registration.go index c102f67..25c8bee 100644 --- a/service/domain/registration.go +++ b/service/domain/registration.go @@ -2,6 +2,8 @@ package domain import ( "encoding/json" + "net" + "net/url" "strings" "github.com/boreq/errors" @@ -77,7 +79,8 @@ func (p Registration) Relays() []RelayAddress { } type RelayAddress struct { - s string + s string + hostWithoutPort string } func NewRelayAddress(s string) (RelayAddress, error) { @@ -85,14 +88,39 @@ func NewRelayAddress(s string) (RelayAddress, error) { return RelayAddress{}, errors.New("invalid protocol") } - // todo validate - return RelayAddress{s: s}, nil + s = strings.TrimSpace(s) + s = strings.TrimRight(s, "/") + + u, err := url.Parse(s) + if err != nil { + return RelayAddress{}, errors.Wrap(err, "url parse error") + } + + if u.Scheme != "ws" && u.Scheme != "wss" { + return RelayAddress{}, errors.New("invalid protocol") + } + + u.Host = strings.ToLower(u.Host) + hostWithoutPort, _, err := net.SplitHostPort(u.Host) + if err != nil { + hostWithoutPort = u.Host + } + normalizedURI := u.String() + + return RelayAddress{ + s: normalizedURI, + hostWithoutPort: hostWithoutPort, + }, nil } func (r RelayAddress) String() string { return r.s } +func (r RelayAddress) HostWithoutPort() string { + return r.hostWithoutPort +} + type registrationTransport struct { APNSToken string `json:"apnsToken"` PublicKey string `json:"publicKey"`