2023-09-06 20:49:18 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
2023-10-28 20:21:15 -03:00
|
|
|
"golang.org/x/exp/slices"
|
2023-09-06 20:49:18 +02:00
|
|
|
)
|
|
|
|
|
2023-10-11 00:19:33 +02:00
|
|
|
type WhitelistEntry struct {
|
2023-09-06 20:49:18 +02:00
|
|
|
InvitedBy string `json:"invited_by"`
|
2023-10-28 20:21:15 -03:00
|
|
|
PublicKey string `json:"pk"`
|
2023-09-06 20:49:18 +02:00
|
|
|
}
|
|
|
|
|
2023-10-11 00:19:33 +02:00
|
|
|
var whitelist []WhitelistEntry
|
2023-09-06 20:49:18 +02:00
|
|
|
|
|
|
|
func whitelistRejecter(ctx context.Context, evt *nostr.Event) (reject bool, msg string) {
|
|
|
|
// check if user in whitelist
|
2023-10-18 11:58:09 -03:00
|
|
|
if !isPublicKeyInWhitelist(evt.PubKey) {
|
2023-09-06 20:49:18 +02:00
|
|
|
return true, "You are not invited to this relay"
|
|
|
|
}
|
2023-10-11 00:19:33 +02:00
|
|
|
|
2023-10-11 22:26:31 +02:00
|
|
|
/*
|
|
|
|
kind 20201
|
|
|
|
invited/whitelisted user invites new user
|
|
|
|
*/
|
2023-10-11 00:19:33 +02:00
|
|
|
if evt.Kind == 20201 {
|
2023-09-06 20:49:18 +02:00
|
|
|
}
|
|
|
|
|
2023-10-11 22:26:31 +02:00
|
|
|
/*
|
|
|
|
kind 20202
|
|
|
|
p tag = user removes user they invited OR admin removes user
|
|
|
|
e tag = admin removes event
|
|
|
|
*/
|
2023-10-11 00:19:33 +02:00
|
|
|
if evt.Kind == 20202 {
|
2023-09-06 20:49:18 +02:00
|
|
|
pTags := evt.Tags.GetAll([]string{"p"})
|
|
|
|
for _, tag := range pTags {
|
|
|
|
for _, user := range whitelist {
|
2023-10-11 00:19:33 +02:00
|
|
|
/*
|
|
|
|
1: User in whitelist
|
|
|
|
2: Cant remove self
|
|
|
|
3: User should have invited user OR be relay admin
|
|
|
|
*/
|
2023-10-16 23:09:12 -03:00
|
|
|
if user.PublicKey == tag.Value() && evt.PubKey != tag.Value() && (user.InvitedBy == evt.PubKey || evt.PubKey == s.RelayPubkey) {
|
|
|
|
log.Info().Str("user", tag.Value()).Msg("deleting user")
|
2023-09-18 19:18:24 +00:00
|
|
|
deleteFromWhitelistRecursively(ctx, tag.Value())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-16 23:09:12 -03:00
|
|
|
if evt.PubKey == s.RelayPubkey {
|
2023-10-11 00:19:33 +02:00
|
|
|
eTags := evt.Tags.GetAll([]string{"e"})
|
|
|
|
for _, tag := range eTags {
|
|
|
|
filter := nostr.Filter{
|
|
|
|
IDs: []string{tag.Value()},
|
|
|
|
}
|
|
|
|
events, _ := db.QueryEvents(ctx, filter)
|
2023-09-18 19:18:24 +00:00
|
|
|
|
2023-10-16 23:09:12 -03:00
|
|
|
for evt := range events {
|
|
|
|
log.Info().Str("event", evt.ID).Msg("deleting event")
|
|
|
|
err := db.DeleteEvent(ctx, evt)
|
2023-10-11 00:19:33 +02:00
|
|
|
if err != nil {
|
2023-10-16 23:09:12 -03:00
|
|
|
log.Warn().Err(err).Msg("failed to delete event")
|
2023-10-11 00:19:33 +02:00
|
|
|
}
|
2023-09-06 20:49:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
|
2023-10-28 20:21:15 -03:00
|
|
|
func addToWhitelist(ctx context.Context, pubkey string, invitedBy string) error {
|
|
|
|
if nostr.IsValidPublicKeyHex(pubkey) && !isPublicKeyInWhitelist(pubkey) {
|
|
|
|
whitelist = append(whitelist, WhitelistEntry{PublicKey: pubkey, InvitedBy: invitedBy})
|
|
|
|
}
|
|
|
|
return saveWhitelist()
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeFromWhitelist(ctx context.Context, pubkey string) error {
|
|
|
|
idx := slices.IndexFunc(whitelist, func(we WhitelistEntry) bool { return we.PublicKey == pubkey })
|
|
|
|
if idx == -1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
whitelist = append(whitelist[0:idx], whitelist[idx+1:]...)
|
|
|
|
return saveWhitelist()
|
|
|
|
}
|
|
|
|
|
2023-10-11 00:19:33 +02:00
|
|
|
func loadWhitelist() error {
|
2023-09-06 20:49:18 +02:00
|
|
|
if _, err := os.Stat("whitelist.json"); os.IsNotExist(err) {
|
2023-10-11 00:19:33 +02:00
|
|
|
whitelist = []WhitelistEntry{}
|
2023-09-06 20:49:18 +02:00
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-11 00:19:33 +02:00
|
|
|
|
2023-09-06 20:49:18 +02:00
|
|
|
fileContent, err := os.ReadFile("whitelist.json")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-11 00:19:33 +02:00
|
|
|
|
2023-09-06 20:49:18 +02:00
|
|
|
if err := json.Unmarshal(fileContent, &whitelist); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-11 00:19:33 +02:00
|
|
|
|
2023-09-06 20:49:18 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-11 00:19:33 +02:00
|
|
|
func saveWhitelist() error {
|
2023-09-06 20:49:18 +02:00
|
|
|
jsonBytes, err := json.Marshal(whitelist)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-11 00:19:33 +02:00
|
|
|
|
2023-09-06 20:49:18 +02:00
|
|
|
if err := os.WriteFile("whitelist.json", jsonBytes, 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-11 00:19:33 +02:00
|
|
|
|
2023-09-06 20:49:18 +02:00
|
|
|
return nil
|
|
|
|
}
|