Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
)

type Config struct {
Port int `yaml:"port"`
DatabaseURL string `yaml:"database_url"`
AllowedKinds []int `yaml:"allowed_kinds"`
Nip11Pubkey string `yaml:"nip11_pubkey"`
Nip11Contact string `yaml:"nip11_contact"`
Nip11Description string `yaml:"nip11_description"`
Nip11Version string `yaml:"nip11_version"`
Port int `yaml:"port"`
DatabaseURL string `yaml:"database_url"`
AllowedKinds []int `yaml:"allowed_kinds"`
AllowedPubkeys []string `yaml:"allowed_pubkeys"`
Nip11Pubkey string `yaml:"nip11_pubkey"`
Nip11Contact string `yaml:"nip11_contact"`
Nip11Description string `yaml:"nip11_description"`
Nip11Version string `yaml:"nip11_version"`
}

// Load Config from a yaml file at path.
Expand Down
1 change: 1 addition & 0 deletions local/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ nip11_description: custom relay for https://stemstr.app
nip11_version: 0.1
database_url: "postgresql://admin:password@localhost:5432/relay?sslmode=disable"
allowed_kinds: []
allowed_pubkeys: []
22 changes: 22 additions & 0 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"log"
"strings"

"github.com/fiatjaf/relayer/v2"
"github.com/fiatjaf/relayer/v2/storage/postgresql"
Expand Down Expand Up @@ -70,6 +71,10 @@ func (r Relay) Init() error {
}

func (r Relay) AcceptEvent(ctx context.Context, evt *nostr.Event) bool {
if !pubkeyIsAllowed(r.cfg.AllowedPubkeys, evt.PubKey) {
return false
}

// block events that are too large
jsonb, _ := json.Marshal(evt)
if len(jsonb) > 10000 {
Expand Down Expand Up @@ -106,6 +111,23 @@ func (r Relay) Start() error {
return server.Start("0.0.0.0", r.cfg.Port)
}

func pubkeyIsAllowed(pubkeys []string, pubkey string) bool {
// If no whitelist of pubkeys are provided, it's allowed
if len(pubkeys) == 0 {
return true
}

allowed := false
for _, allowedPubkey := range pubkeys {
if strings.EqualFold(allowedPubkey, pubkey) {
allowed = true
break
}
}

return allowed
}

var defaultAllowedKinds = []int{
nostr.KindSetMetadata,
nostr.KindTextNote,
Expand Down
26 changes: 26 additions & 0 deletions relay_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestPubkeyIsAllowed(t *testing.T) {
var tests = []struct {
name string
pubkeys []string
pubkey string
expected bool
}{
{"default open", []string{}, "123", true},
{"whitelisted", []string{"123"}, "123", true},
{"whitelisted", []string{"123", "456"}, "123", true},
{"not whitelisted", []string{"123", "456"}, "789", false},
}

for _, tt := range tests {
result := pubkeyIsAllowed(tt.pubkeys, tt.pubkey)
assert.Equal(t, tt.expected, result)
}
}