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 (
git.devvul.com/asara/gologger v0.9.0
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/lib/pq v1.10.9
github.com/nbd-wtf/go-nostr v0.37.2
github.com/nbd-wtf/go-nostr v0.37.3
)
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/fiatjaf/eventstore v0.9.1 h1:dLum4W6NOkDXMgckjj5lLfKrRVW8oN3zS7tfbXzGxJg=
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.1/go.mod h1:jRmqbbIbEH+y0unt3wMUBwqY/btVussqx5SmBoGhXtg=
github.com/fiatjaf/khatru v0.8.2 h1:kYfdi4piAS2+/Lqnxzk0IAdjM4PsSK47N9+k5mE18rU=
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/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
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-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/nbd-wtf/go-nostr v0.37.2 h1:42rriFqqz07EdydERwYeQnewl+Rah1Gq46I+Wh0KYYg=
github.com/nbd-wtf/go-nostr v0.37.2/go.mod h1:TGKGj00BmJRXvRe0LlpDN3KKbELhhPXgBwUEhzu3Oq0=
github.com/nbd-wtf/go-nostr v0.37.3 h1:p/rrOWhaAk78UCVwzWtTN1C8WbP2k5eQV4HlpEYAeeA=
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
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
// as long as a registered user is tagged in the `p` tag
// 4: nip-04 encrypted dms
// 6: nip-18 reposts (kind 1)
// 7: nip-25 reactions
// 14: nip-17 private dms
// 16: nip-18 reposts (generic)
// 1059: nip-59 gift wraps
// 9802: nip-84 highlights
// 24133: nip-46 nostr connect
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"}) {
npubs = append(npubs, npub.Value())
}

View file

@ -58,14 +58,12 @@ func NewRelay(version string) *khatru.Relay {
relay.DeleteEvent = append(relay.DeleteEvent, RelayDb.DeleteEvent)
// apply policies
relay.RejectEvent = append(
relay.RejectEvent,
relay.RejectEvent = append(relay.RejectEvent,
RejectUnregisteredNpubs,
policies.ValidateKind,
)
relay.RejectFilter = append(
relay.RejectFilter,
relay.RejectFilter = append(relay.RejectFilter,
policies.RejectKind04Snoopers,
policies.NoEmptyFilters,
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.
func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) (skipBroadcast bool, writeError error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if evt == nil {
return false, errors.New("error: event is nil")
}

View file

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

View file

@ -18,13 +18,12 @@ func startRateLimitSystem[K comparable](
go func() {
for {
time.Sleep(interval)
negativeBuckets.Range(func(key K, bucket *atomic.Int32) bool {
for key, bucket := range negativeBuckets.Range {
newv := bucket.Add(int32(-tokensPerInterval))
if newv <= 0 {
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)
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,
NoEmptyFilters,
NoComplexFilters,
FilterIPRateLimiter(20, time.Minute, 100),
)

View file

@ -41,7 +41,12 @@ func GetAuthed(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 {

View file

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

6
vendor/modules.txt vendored
View file

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