well-goknown/nostr/relay.go

80 lines
2 KiB
Go
Raw Normal View History

2024-08-16 23:26:49 +00:00
package nostr
import (
"context"
2024-09-26 01:59:44 +00:00
"time"
2024-08-16 23:26:49 +00:00
"git.devvul.com/asara/well-goknown/config"
"github.com/fiatjaf/eventstore/postgresql"
"github.com/fiatjaf/khatru"
"github.com/fiatjaf/khatru/policies"
"github.com/jmoiron/sqlx"
)
var (
DB *sqlx.DB
RelayDb postgresql.PostgresBackend
relay *khatru.Relay
)
type nostrUser struct {
Pubkey string `db:"pubkey"`
Relays *string `db:"relays,omitempty"`
}
type nostrWellKnown struct {
Names map[string]string `json:"names"`
Relays map[string][]string `json:"relays,omitempty"`
NIP46 map[string][]string `json:"nip46,omitempty"`
}
func NewRelay(version string) *khatru.Relay {
// relay configuration
relay = khatru.NewRelay()
relay.Info.Name = config.GetConfig().RelayName
relay.Info.PubKey = config.GetConfig().RelayPubkey
relay.Info.Description = config.GetConfig().RelayDescription
relay.Info.Icon = config.GetConfig().RelayIcon
relay.Info.Contact = config.GetConfig().RelayContact
relay.Info.Version = version
relay.Info.Software = "https://git.devvul.com/asara/well-goknown"
2024-08-16 23:26:49 +00:00
relay.Info.SupportedNIPs = []int{
1, // basic protocol
2, // contact lists
4, // encrypted DMs
11, // relay info
42, // auth
70, // protected events
}
relay.OnConnect = append(relay.OnConnect, func(ctx context.Context) {
khatru.RequestAuth(ctx)
})
// storage
relay.StoreEvent = append(relay.StoreEvent, RelayDb.SaveEvent)
relay.QueryEvents = append(relay.QueryEvents, RelayDb.QueryEvents)
relay.CountEvents = append(relay.CountEvents, RelayDb.CountEvents)
relay.DeleteEvent = append(relay.DeleteEvent, RelayDb.DeleteEvent)
// apply policies
2024-09-25 02:29:00 +00:00
relay.RejectEvent = append(relay.RejectEvent,
2024-08-16 23:26:49 +00:00
RejectUnregisteredNpubs,
policies.ValidateKind,
2024-09-26 01:59:44 +00:00
policies.EventIPRateLimiter(10, time.Minute*1, 30),
2024-08-16 23:26:49 +00:00
)
2024-09-25 02:29:00 +00:00
relay.RejectFilter = append(relay.RejectFilter,
2024-08-16 23:26:49 +00:00
policies.RejectKind04Snoopers,
2024-09-23 23:50:50 +00:00
policies.NoEmptyFilters,
policies.NoComplexFilters,
2024-08-16 23:26:49 +00:00
)
2024-09-26 01:59:44 +00:00
relay.RejectConnection = append(relay.RejectConnection,
policies.ConnectionRateLimiter(25, time.Minute*1, 100),
)
2024-08-16 23:26:49 +00:00
return relay
}