Relay config by json (#47)

* relay-config.json

* fix README.md

* trustedPubKeys is not relay configuration

* update README.md
This commit is contained in:
mattn
2024-02-18 02:05:22 +09:00
committed by GitHub
parent d09ff98ab8
commit 1019fef1d3
7 changed files with 130 additions and 47 deletions

View File

@@ -52,6 +52,21 @@ DOMAIN="njump.me"
DISK_CACHE_PATH="/tmp/njump-internal" DISK_CACHE_PATH="/tmp/njump-internal"
EVENT_STORE_PATH="/tmp/njump-db" EVENT_STORE_PATH="/tmp/njump-db"
TAILWIND_DEBUG= TAILWIND_DEBUG=
RELAY_CONFIG_PATH=
TRUSTED_PUBKEYS=npub1...,npub1...
``` ```
`RELAY_CONFIG_FILE` is path to json file to update relay configuration. You can set relay list like below:
```json
{
"everything": [
"wss://relay.nostr.band",
"wss://nostr.lol"
]
}
```
See `relay-config.json.sample` for example.
For example, when running from a precompiled binary you can do something like `PORT=5000 ./njump`. For example, when running from a precompiled binary you can do something like `PORT=5000 ./njump`.

View File

@@ -99,7 +99,7 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e
rawAuthorRelays = relaysForPubkey(ctx, event.PubKey) rawAuthorRelays = relaysForPubkey(ctx, event.PubKey)
cancel() cancel()
for _, relay := range rawAuthorRelays { for _, relay := range rawAuthorRelays {
for _, excluded := range excludedRelays { for _, excluded := range relayConfig.ExcludedRelays {
if strings.Contains(relay, excluded) { if strings.Contains(relay, excluded) {
continue continue
} }

24
main.go
View File

@@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"embed" "embed"
"encoding/json"
"fmt" "fmt"
"html/template" "html/template"
"net/http" "net/http"
@@ -25,6 +26,8 @@ type Settings struct {
EventStorePath string `envconfig:"EVENT_STORE_PATH" default:"/tmp/njump-db"` EventStorePath string `envconfig:"EVENT_STORE_PATH" default:"/tmp/njump-db"`
TailwindDebug bool `envconfig:"TAILWIND_DEBUG"` TailwindDebug bool `envconfig:"TAILWIND_DEBUG"`
SkipLanguageModel bool `envconfig:"SKIP_LANGUAGE_MODEL"` SkipLanguageModel bool `envconfig:"SKIP_LANGUAGE_MODEL"`
RelayConfigPath string `envconfig:"RELAY_CONFIG_PATH"`
TrustedPubKeys []string `envconfig:"TRUSTED_PUBKEYS"`
} }
//go:embed static/* //go:embed static/*
@@ -47,6 +50,27 @@ func main() {
} }
} }
if len(s.TrustedPubKeys) == 0 {
s.TrustedPubKeys = defaultTrustedPubKeys
}
if s.RelayConfigPath != "" {
configr, err := os.ReadFile(s.RelayConfigPath)
if err != nil {
log.Fatal().Err(err).Msgf("failed to load %q", s.RelayConfigPath)
return
}
err = json.Unmarshal(configr, &relayConfig)
if err != nil {
log.Fatal().Err(err).Msgf("failed to load %q", s.RelayConfigPath)
return
}
if !relayConfig.Valid() {
log.Fatal().Err(err).Msgf("invalid relay config file %q", s.RelayConfigPath)
return
}
}
// if we're in tailwind debug mode, initialize the runtime tailwind stuff // if we're in tailwind debug mode, initialize the runtime tailwind stuff
if s.TailwindDebug { if s.TailwindDebug {
configb, err := os.ReadFile("tailwind.config.js") configb, err := os.ReadFile("tailwind.config.js")

View File

@@ -15,11 +15,26 @@ import (
sdk "github.com/nbd-wtf/nostr-sdk" sdk "github.com/nbd-wtf/nostr-sdk"
) )
type RelayConfig struct {
Everything []string `json:"everything"`
Profiles []string `json:"profiles"`
JustIds []string `json:"justIds"`
ExcludedRelays []string `json:"excludeRelays"`
}
func (r *RelayConfig) Valid() bool {
if len(r.Everything) == 0 || len(r.Profiles) == 0 || len(r.JustIds) == 0 || len(r.ExcludedRelays) == 0 {
return false
}
return true
}
var ( var (
pool = nostr.NewSimplePool(context.Background()) pool = nostr.NewSimplePool(context.Background())
serial int serial int
everything = []string{ relayConfig = RelayConfig{
Everything: []string{
"wss://nostr-pub.wellorder.net", "wss://nostr-pub.wellorder.net",
"wss://saltivka.org", "wss://saltivka.org",
"wss://relay.damus.io", "wss://relay.damus.io",
@@ -33,27 +48,27 @@ var (
"wss://relay.primal.net", "wss://relay.primal.net",
"wss://relay.nostr.band", "wss://relay.nostr.band",
"wss://public.relaying.io", "wss://public.relaying.io",
} },
profiles = []string{ Profiles: []string{
"wss://purplepag.es", "wss://purplepag.es",
"wss://relay.noswhere.com", "wss://relay.noswhere.com",
"wss://relay.nos.social", "wss://relay.nos.social",
} },
justIds = []string{ JustIds: []string{
"wss://cache2.primal.net/v1", "wss://cache2.primal.net/v1",
"wss://relay.noswhere.com", "wss://relay.noswhere.com",
},
ExcludedRelays: []string{
"wss://filter.nostr.wine", // paid
},
} }
trustedPubKeys = []string{ defaultTrustedPubKeys = []string{
"7bdef7be22dd8e59f4600e044aa53a1cf975a9dc7d27df5833bc77db784a5805", // dtonon "7bdef7be22dd8e59f4600e044aa53a1cf975a9dc7d27df5833bc77db784a5805", // dtonon
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d", // fiatjaf "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d", // fiatjaf
"97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322", // hodlbod "97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322", // hodlbod
"ee11a5dff40c19a555f41fe42b48f00e618c91225622ae37b6c2bb67b76c4e49", // Michael Dilger "ee11a5dff40c19a555f41fe42b48f00e618c91225622ae37b6c2bb67b76c4e49", // Michael Dilger
} }
excludedRelays = []string{
"wss://filter.nostr.wine", // paid
}
) )
type CachedEvent struct { type CachedEvent struct {
@@ -90,7 +105,7 @@ func getEvent(ctx context.Context, code string, relayHints []string) (*nostr.Eve
author = v.PublicKey author = v.PublicKey
filter.Authors = []string{v.PublicKey} filter.Authors = []string{v.PublicKey}
filter.Kinds = []int{0} filter.Kinds = []int{0}
relays = append(relays, profiles...) relays = append(relays, relayConfig.Profiles...)
relays = append(relays, v.Relays...) relays = append(relays, v.Relays...)
priorityRelays.Add(v.Relays...) priorityRelays.Add(v.Relays...)
withRelays = true withRelays = true
@@ -98,7 +113,7 @@ func getEvent(ctx context.Context, code string, relayHints []string) (*nostr.Eve
author = v.Author author = v.Author
filter.IDs = []string{v.ID} filter.IDs = []string{v.ID}
relays = append(relays, v.Relays...) relays = append(relays, v.Relays...)
relays = append(relays, justIds...) relays = append(relays, relayConfig.JustIds...)
priorityRelays.Add(v.Relays...) priorityRelays.Add(v.Relays...)
withRelays = true withRelays = true
case nostr.EntityPointer: case nostr.EntityPointer:
@@ -118,12 +133,12 @@ func getEvent(ctx context.Context, code string, relayHints []string) (*nostr.Eve
if prefix == "note" { if prefix == "note" {
filter.IDs = []string{v} filter.IDs = []string{v}
relays = append(relays, getRandomRelay()) relays = append(relays, getRandomRelay())
relays = append(relays, justIds...) relays = append(relays, relayConfig.JustIds...)
} else if prefix == "npub" { } else if prefix == "npub" {
author = v author = v
filter.Authors = []string{v} filter.Authors = []string{v}
filter.Kinds = []int{0} filter.Kinds = []int{0}
relays = append(relays, profiles...) relays = append(relays, relayConfig.Profiles...)
} }
} }
@@ -322,7 +337,7 @@ func contactsForPubkey(ctx context.Context, pubkey string, extraRelays ...string
pubkeyRelays := relaysForPubkey(ctx, pubkey, relays...) pubkeyRelays := relaysForPubkey(ctx, pubkey, relays...)
relays = append(relays, pubkeyRelays...) relays = append(relays, pubkeyRelays...)
relays = append(relays, profiles...) relays = append(relays, relayConfig.Profiles...)
ch := pool.SubManyEose(ctx, relays, nostr.Filters{ ch := pool.SubManyEose(ctx, relays, nostr.Filters{
{ {

29
relay-config.json.sample Normal file
View File

@@ -0,0 +1,29 @@
{
"everything": [
"wss://nostr-pub.wellorder.net",
"wss://saltivka.org",
"wss://relay.damus.io",
"wss://relay.nostr.bg",
"wss://nostr.wine",
"wss://nos.lol",
"wss://nostr.mom",
"wss://atlas.nostr.land",
"wss://relay.snort.social",
"wss://offchain.pub",
"wss://relay.primal.net",
"wss://relay.nostr.band",
"wss://public.relaying.io"
],
"profiles": [
"wss://purplepag.es",
"wss://relay.noswhere.com",
"wss://relay.nos.social"
],
"justIds": [
"wss://cache2.primal.net/v1",
"wss://relay.noswhere.com"
],
"excludeRelays": [
"wss://filter.nostr.wine"
]
}

View File

@@ -69,7 +69,7 @@ func loadNpubsArchive(ctx context.Context) {
log.Debug().Msg("refreshing the npubs archive") log.Debug().Msg("refreshing the npubs archive")
contactsArchive := make([]string, 0, 500) contactsArchive := make([]string, 0, 500)
for _, pubkey := range trustedPubKeys { for _, pubkey := range s.TrustedPubKeys {
ctx, cancel := context.WithTimeout(ctx, time.Second*4) ctx, cancel := context.WithTimeout(ctx, time.Second*4)
pubkeyContacts := contactsForPubkey(ctx, pubkey) pubkeyContacts := contactsForPubkey(ctx, pubkey)
contactsArchive = append(contactsArchive, pubkeyContacts...) contactsArchive = append(contactsArchive, pubkeyContacts...)
@@ -87,15 +87,15 @@ func loadRelaysArchive(ctx context.Context) {
relaysArchive := make([]string, 0, 500) relaysArchive := make([]string, 0, 500)
for _, pubkey := range trustedPubKeys { for _, pubkey := range s.TrustedPubKeys {
ctx, cancel := context.WithTimeout(ctx, time.Second*4) ctx, cancel := context.WithTimeout(ctx, time.Second*4)
pubkeyContacts := relaysForPubkey(ctx, pubkey, profiles...) pubkeyContacts := relaysForPubkey(ctx, pubkey, relayConfig.Profiles...)
relaysArchive = append(relaysArchive, pubkeyContacts...) relaysArchive = append(relaysArchive, pubkeyContacts...)
cancel() cancel()
} }
for _, relay := range unique(relaysArchive) { for _, relay := range unique(relaysArchive) {
for _, excluded := range excludedRelays { for _, excluded := range relayConfig.ExcludedRelays {
if strings.Contains(relay, excluded) { if strings.Contains(relay, excluded) {
log.Debug().Msgf("skipping relay %s", relay) log.Debug().Msgf("skipping relay %s", relay)
continue continue

View File

@@ -430,10 +430,10 @@ func humanDate(createdAt nostr.Timestamp) string {
func getRandomRelay() string { func getRandomRelay() string {
if serial == 0 { if serial == 0 {
serial = rand.Intn(len(everything)) serial = rand.Intn(len(relayConfig.Everything))
} }
serial = (serial + 1) % len(everything) serial = (serial + 1) % len(relayConfig.Everything)
return everything[serial] return relayConfig.Everything[serial]
} }
func isntRealRelay(url string) bool { func isntRealRelay(url string) bool {