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"
EVENT_STORE_PATH="/tmp/njump-db"
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`.

View File

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

36
main.go
View File

@@ -3,6 +3,7 @@ package main
import (
"context"
"embed"
"encoding/json"
"fmt"
"html/template"
"net/http"
@@ -19,12 +20,14 @@ import (
)
type Settings struct {
Port string `envconfig:"PORT" default:"2999"`
Domain string `envconfig:"DOMAIN" default:"njump.me"`
DiskCachePath string `envconfig:"DISK_CACHE_PATH" default:"/tmp/njump-internal"`
EventStorePath string `envconfig:"EVENT_STORE_PATH" default:"/tmp/njump-db"`
TailwindDebug bool `envconfig:"TAILWIND_DEBUG"`
SkipLanguageModel bool `envconfig:"SKIP_LANGUAGE_MODEL"`
Port string `envconfig:"PORT" default:"2999"`
Domain string `envconfig:"DOMAIN" default:"njump.me"`
DiskCachePath string `envconfig:"DISK_CACHE_PATH" default:"/tmp/njump-internal"`
EventStorePath string `envconfig:"EVENT_STORE_PATH" default:"/tmp/njump-db"`
TailwindDebug bool `envconfig:"TAILWIND_DEBUG"`
SkipLanguageModel bool `envconfig:"SKIP_LANGUAGE_MODEL"`
RelayConfigPath string `envconfig:"RELAY_CONFIG_PATH"`
TrustedPubKeys []string `envconfig:"TRUSTED_PUBKEYS"`
}
//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 s.TailwindDebug {
configb, err := os.ReadFile("tailwind.config.js")

View File

@@ -15,45 +15,60 @@ import (
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 (
pool = nostr.NewSimplePool(context.Background())
serial int
everything = []string{
"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 = []string{
"wss://purplepag.es",
"wss://relay.noswhere.com",
"wss://relay.nos.social",
}
justIds = []string{
"wss://cache2.primal.net/v1",
"wss://relay.noswhere.com",
relayConfig = RelayConfig{
Everything: []string{
"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: []string{
"wss://purplepag.es",
"wss://relay.noswhere.com",
"wss://relay.nos.social",
},
JustIds: []string{
"wss://cache2.primal.net/v1",
"wss://relay.noswhere.com",
},
ExcludedRelays: []string{
"wss://filter.nostr.wine", // paid
},
}
trustedPubKeys = []string{
defaultTrustedPubKeys = []string{
"7bdef7be22dd8e59f4600e044aa53a1cf975a9dc7d27df5833bc77db784a5805", // dtonon
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d", // fiatjaf
"97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322", // hodlbod
"ee11a5dff40c19a555f41fe42b48f00e618c91225622ae37b6c2bb67b76c4e49", // Michael Dilger
}
excludedRelays = []string{
"wss://filter.nostr.wine", // paid
}
)
type CachedEvent struct {
@@ -90,7 +105,7 @@ func getEvent(ctx context.Context, code string, relayHints []string) (*nostr.Eve
author = v.PublicKey
filter.Authors = []string{v.PublicKey}
filter.Kinds = []int{0}
relays = append(relays, profiles...)
relays = append(relays, relayConfig.Profiles...)
relays = append(relays, v.Relays...)
priorityRelays.Add(v.Relays...)
withRelays = true
@@ -98,7 +113,7 @@ func getEvent(ctx context.Context, code string, relayHints []string) (*nostr.Eve
author = v.Author
filter.IDs = []string{v.ID}
relays = append(relays, v.Relays...)
relays = append(relays, justIds...)
relays = append(relays, relayConfig.JustIds...)
priorityRelays.Add(v.Relays...)
withRelays = true
case nostr.EntityPointer:
@@ -118,12 +133,12 @@ func getEvent(ctx context.Context, code string, relayHints []string) (*nostr.Eve
if prefix == "note" {
filter.IDs = []string{v}
relays = append(relays, getRandomRelay())
relays = append(relays, justIds...)
relays = append(relays, relayConfig.JustIds...)
} else if prefix == "npub" {
author = v
filter.Authors = []string{v}
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...)
relays = append(relays, pubkeyRelays...)
relays = append(relays, profiles...)
relays = append(relays, relayConfig.Profiles...)
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")
contactsArchive := make([]string, 0, 500)
for _, pubkey := range trustedPubKeys {
for _, pubkey := range s.TrustedPubKeys {
ctx, cancel := context.WithTimeout(ctx, time.Second*4)
pubkeyContacts := contactsForPubkey(ctx, pubkey)
contactsArchive = append(contactsArchive, pubkeyContacts...)
@@ -87,15 +87,15 @@ func loadRelaysArchive(ctx context.Context) {
relaysArchive := make([]string, 0, 500)
for _, pubkey := range trustedPubKeys {
for _, pubkey := range s.TrustedPubKeys {
ctx, cancel := context.WithTimeout(ctx, time.Second*4)
pubkeyContacts := relaysForPubkey(ctx, pubkey, profiles...)
pubkeyContacts := relaysForPubkey(ctx, pubkey, relayConfig.Profiles...)
relaysArchive = append(relaysArchive, pubkeyContacts...)
cancel()
}
for _, relay := range unique(relaysArchive) {
for _, excluded := range excludedRelays {
for _, excluded := range relayConfig.ExcludedRelays {
if strings.Contains(relay, excluded) {
log.Debug().Msgf("skipping relay %s", relay)
continue

View File

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