more kind support, update vendor

This commit is contained in:
Amarpreet Minhas 2024-09-24 22:29:00 -04:00
parent ededb3be5c
commit c7971a8eeb
12 changed files with 36 additions and 25 deletions

4
go.mod
View file

@ -5,10 +5,10 @@ go 1.23.1
require ( require (
git.devvul.com/asara/gologger v0.9.0 git.devvul.com/asara/gologger v0.9.0
github.com/fiatjaf/eventstore v0.9.1 github.com/fiatjaf/eventstore v0.9.1
github.com/fiatjaf/khatru v0.8.1 github.com/fiatjaf/khatru v0.8.2
github.com/jmoiron/sqlx v1.4.0 github.com/jmoiron/sqlx v1.4.0
github.com/lib/pq v1.10.9 github.com/lib/pq v1.10.9
github.com/nbd-wtf/go-nostr v0.37.2 github.com/nbd-wtf/go-nostr v0.37.3
) )
require ( require (

8
go.sum
View file

@ -19,8 +19,8 @@ github.com/fasthttp/websocket v1.5.7 h1:0a6o2OfeATvtGgoMKleURhLT6JqWPg7fYfWnH4KH
github.com/fasthttp/websocket v1.5.7/go.mod h1:bC4fxSono9czeXHQUVKxsC0sNjbm7lPJR04GDFqClfU= github.com/fasthttp/websocket v1.5.7/go.mod h1:bC4fxSono9czeXHQUVKxsC0sNjbm7lPJR04GDFqClfU=
github.com/fiatjaf/eventstore v0.9.1 h1:dLum4W6NOkDXMgckjj5lLfKrRVW8oN3zS7tfbXzGxJg= github.com/fiatjaf/eventstore v0.9.1 h1:dLum4W6NOkDXMgckjj5lLfKrRVW8oN3zS7tfbXzGxJg=
github.com/fiatjaf/eventstore v0.9.1/go.mod h1:h5CdLSF7mEQ7/rWpEABTRIrNuFoSwdQDi/nZkW/vVFU= github.com/fiatjaf/eventstore v0.9.1/go.mod h1:h5CdLSF7mEQ7/rWpEABTRIrNuFoSwdQDi/nZkW/vVFU=
github.com/fiatjaf/khatru v0.8.1 h1:BWAZqwuT0272ZlyzPkuqAA0eGBOs5G3u0Dn1tlWrm6Q= github.com/fiatjaf/khatru v0.8.2 h1:kYfdi4piAS2+/Lqnxzk0IAdjM4PsSK47N9+k5mE18rU=
github.com/fiatjaf/khatru v0.8.1/go.mod h1:jRmqbbIbEH+y0unt3wMUBwqY/btVussqx5SmBoGhXtg= github.com/fiatjaf/khatru v0.8.2/go.mod h1:44X/Mcc+2ObOqz+/fDbhAW3BeUEPKxDgrX9St/cXEKc=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
@ -48,8 +48,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/nbd-wtf/go-nostr v0.37.2 h1:42rriFqqz07EdydERwYeQnewl+Rah1Gq46I+Wh0KYYg= github.com/nbd-wtf/go-nostr v0.37.3 h1:p/rrOWhaAk78UCVwzWtTN1C8WbP2k5eQV4HlpEYAeeA=
github.com/nbd-wtf/go-nostr v0.37.2/go.mod h1:TGKGj00BmJRXvRe0LlpDN3KKbELhhPXgBwUEhzu3Oq0= github.com/nbd-wtf/go-nostr v0.37.3/go.mod h1:TGKGj00BmJRXvRe0LlpDN3KKbELhhPXgBwUEhzu3Oq0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

View file

@ -36,12 +36,15 @@ func RejectUnregisteredNpubs(ctx context.Context, event *nostr.Event) (reject bo
// in addition to the registered users, others can use the relay for the following kinds // in addition to the registered users, others can use the relay for the following kinds
// as long as a registered user is tagged in the `p` tag // as long as a registered user is tagged in the `p` tag
// 4: nip-04 encrypted dms // 4: nip-04 encrypted dms
// 6: nip-18 reposts (kind 1)
// 7: nip-25 reactions // 7: nip-25 reactions
// 14: nip-17 private dms // 14: nip-17 private dms
// 16: nip-18 reposts (generic)
// 1059: nip-59 gift wraps // 1059: nip-59 gift wraps
// 9802: nip-84 highlights
// 24133: nip-46 nostr connect // 24133: nip-46 nostr connect
switch event.Kind { switch event.Kind {
case 4, 7, 14, 1059, 24133: case 4, 6, 7, 14, 16, 1059, 9802, 24133:
for _, npub := range event.Tags.GetAll([]string{"p"}) { for _, npub := range event.Tags.GetAll([]string{"p"}) {
npubs = append(npubs, npub.Value()) npubs = append(npubs, npub.Value())
} }

View file

@ -58,14 +58,12 @@ func NewRelay(version string) *khatru.Relay {
relay.DeleteEvent = append(relay.DeleteEvent, RelayDb.DeleteEvent) relay.DeleteEvent = append(relay.DeleteEvent, RelayDb.DeleteEvent)
// apply policies // apply policies
relay.RejectEvent = append( relay.RejectEvent = append(relay.RejectEvent,
relay.RejectEvent,
RejectUnregisteredNpubs, RejectUnregisteredNpubs,
policies.ValidateKind, policies.ValidateKind,
) )
relay.RejectFilter = append( relay.RejectFilter = append(relay.RejectFilter,
relay.RejectFilter,
policies.RejectKind04Snoopers, policies.RejectKind04Snoopers,
policies.NoEmptyFilters, policies.NoEmptyFilters,
policies.NoComplexFilters, policies.NoComplexFilters,

View file

@ -11,6 +11,9 @@ import (
// AddEvent sends an event through then normal add pipeline, as if it was received from a websocket. // AddEvent sends an event through then normal add pipeline, as if it was received from a websocket.
func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) (skipBroadcast bool, writeError error) { func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) (skipBroadcast bool, writeError error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if evt == nil { if evt == nil {
return false, errors.New("error: event is nil") return false, errors.New("error: event is nil")
} }

View file

@ -3,7 +3,6 @@ package khatru
import ( import (
"context" "context"
"crypto/rand" "crypto/rand"
"crypto/sha256"
"encoding/hex" "encoding/hex"
"errors" "errors"
"net/http" "net/http"
@ -128,9 +127,7 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
switch env := envelope.(type) { switch env := envelope.(type) {
case *nostr.EventEnvelope: case *nostr.EventEnvelope:
// check id // check id
hash := sha256.Sum256(env.Event.Serialize()) if env.Event.CheckID() {
id := hex.EncodeToString(hash[:])
if id != env.Event.ID {
ws.WriteJSON(nostr.OKEnvelope{EventID: env.Event.ID, OK: false, Reason: "invalid: id is computed incorrectly"}) ws.WriteJSON(nostr.OKEnvelope{EventID: env.Event.ID, OK: false, Reason: "invalid: id is computed incorrectly"})
return return
} }

View file

@ -18,13 +18,12 @@ func startRateLimitSystem[K comparable](
go func() { go func() {
for { for {
time.Sleep(interval) time.Sleep(interval)
negativeBuckets.Range(func(key K, bucket *atomic.Int32) bool { for key, bucket := range negativeBuckets.Range {
newv := bucket.Add(int32(-tokensPerInterval)) newv := bucket.Add(int32(-tokensPerInterval))
if newv <= 0 { if newv <= 0 {
negativeBuckets.Delete(key) negativeBuckets.Delete(key)
} }
return true }
})
} }
}() }()

View file

@ -13,7 +13,11 @@ func EventIPRateLimiter(tokensPerInterval int, interval time.Duration, maxTokens
rl := startRateLimitSystem[string](tokensPerInterval, interval, maxTokens) rl := startRateLimitSystem[string](tokensPerInterval, interval, maxTokens)
return func(ctx context.Context, _ *nostr.Event) (reject bool, msg string) { return func(ctx context.Context, _ *nostr.Event) (reject bool, msg string) {
return rl(khatru.GetIP(ctx)), "rate-limited: slow down, please" ip := khatru.GetIP(ctx)
if ip == "" {
return false, ""
}
return rl(ip), "rate-limited: slow down, please"
} }
} }

View file

@ -13,7 +13,6 @@ func ApplySaneDefaults(relay *khatru.Relay) {
) )
relay.RejectFilter = append(relay.RejectFilter, relay.RejectFilter = append(relay.RejectFilter,
NoEmptyFilters,
NoComplexFilters, NoComplexFilters,
FilterIPRateLimiter(20, time.Minute, 100), FilterIPRateLimiter(20, time.Minute, 100),
) )

View file

@ -41,7 +41,12 @@ func GetAuthed(ctx context.Context) string {
} }
func GetIP(ctx context.Context) string { func GetIP(ctx context.Context) string {
return GetIPFromRequest(GetConnection(ctx).Request) conn := GetConnection(ctx)
if conn == nil {
return ""
}
return GetIPFromRequest(conn.Request)
} }
func GetSubscriptionID(ctx context.Context) string { func GetSubscriptionID(ctx context.Context) string {

View file

@ -182,6 +182,10 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
// stop the ticker // stop the ticker
ticker.Stop() ticker.Stop()
// nil the connection
r.Connection = nil
// close all subscriptions // close all subscriptions
r.Subscriptions.Range(func(_ string, sub *Subscription) bool { r.Subscriptions.Range(func(_ string, sub *Subscription) bool {
go sub.Unsub() go sub.Unsub()
@ -520,7 +524,6 @@ func (r *Relay) Close() error {
} }
err := r.Connection.Close() err := r.Connection.Close()
r.Connection = nil
if err != nil { if err != nil {
return err return err
} }

6
vendor/modules.txt vendored
View file

@ -26,8 +26,8 @@ github.com/fasthttp/websocket
## explicit; go 1.23.0 ## explicit; go 1.23.0
github.com/fiatjaf/eventstore github.com/fiatjaf/eventstore
github.com/fiatjaf/eventstore/postgresql github.com/fiatjaf/eventstore/postgresql
# github.com/fiatjaf/khatru v0.8.1 # github.com/fiatjaf/khatru v0.8.2
## explicit; go 1.23 ## explicit; go 1.23.0
github.com/fiatjaf/khatru github.com/fiatjaf/khatru
github.com/fiatjaf/khatru/policies github.com/fiatjaf/khatru/policies
# github.com/gobwas/httphead v0.1.0 # github.com/gobwas/httphead v0.1.0
@ -73,7 +73,7 @@ github.com/mattn/go-colorable
# github.com/mattn/go-isatty v0.0.20 # github.com/mattn/go-isatty v0.0.20
## explicit; go 1.15 ## explicit; go 1.15
github.com/mattn/go-isatty github.com/mattn/go-isatty
# github.com/nbd-wtf/go-nostr v0.37.2 # github.com/nbd-wtf/go-nostr v0.37.3
## explicit; go 1.23.0 ## explicit; go 1.23.0
github.com/nbd-wtf/go-nostr github.com/nbd-wtf/go-nostr
github.com/nbd-wtf/go-nostr/nip11 github.com/nbd-wtf/go-nostr/nip11