Skip to content

Commit

Permalink
Merge pull request #52 from planetary-social/filter-relays
Browse files Browse the repository at this point in the history
Filter relays
  • Loading branch information
dcadenas authored May 6, 2024
2 parents 68ab31b + ce2ecb3 commit 53aa66b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
25 changes: 24 additions & 1 deletion service/adapters/firestore/repository_relays.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package firestore
import (
"context"
"encoding/hex"
"strings"
"time"

"cloud.google.com/go/firestore"
Expand All @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions service/app/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
34 changes: 31 additions & 3 deletions service/domain/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package domain

import (
"encoding/json"
"net"
"net/url"
"strings"

"github.com/boreq/errors"
Expand Down Expand Up @@ -77,22 +79,48 @@ func (p Registration) Relays() []RelayAddress {
}

type RelayAddress struct {
s string
s string
hostWithoutPort string
}

func NewRelayAddress(s string) (RelayAddress, error) {
if !strings.HasPrefix(s, "ws://") && !strings.HasPrefix(s, "wss://") {
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"`
Expand Down

0 comments on commit 53aa66b

Please sign in to comment.