2023-11-02 16:08:53 -03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
|
|
)
|
|
|
|
|
|
|
|
func rejectEventsFromUsersNotInWhitelist(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
|
|
|
|
if isPublicKeyInWhitelist(event.PubKey) {
|
|
|
|
return false, ""
|
|
|
|
}
|
2025-02-02 08:30:13 -03:00
|
|
|
if event.Kind == 1984 {
|
2023-11-02 20:59:50 -03:00
|
|
|
// we accept reports from anyone (will filter them for relevance in the next function)
|
|
|
|
return false, ""
|
|
|
|
}
|
2023-11-02 16:08:53 -03:00
|
|
|
return true, "not authorized"
|
|
|
|
}
|
2023-11-02 20:59:50 -03:00
|
|
|
|
2023-11-07 17:31:33 -03:00
|
|
|
var supportedKinds = []uint16{
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
3,
|
|
|
|
5,
|
|
|
|
6,
|
2025-01-14 12:35:05 -03:00
|
|
|
7,
|
2023-11-07 17:31:33 -03:00
|
|
|
8,
|
2024-11-11 22:28:37 -03:00
|
|
|
9,
|
|
|
|
11,
|
2023-11-07 17:31:33 -03:00
|
|
|
16,
|
2025-02-02 08:28:15 -03:00
|
|
|
20,
|
|
|
|
21,
|
|
|
|
22,
|
2024-04-19 14:16:52 -03:00
|
|
|
818,
|
2024-09-11 08:52:11 -03:00
|
|
|
1040,
|
2023-11-07 17:31:33 -03:00
|
|
|
1063,
|
2024-11-11 22:28:37 -03:00
|
|
|
1111,
|
2024-04-01 20:11:28 -03:00
|
|
|
1984,
|
2023-11-07 17:31:33 -03:00
|
|
|
1985,
|
2025-02-04 13:40:42 -03:00
|
|
|
17375,
|
|
|
|
7375,
|
|
|
|
7376,
|
|
|
|
9321,
|
2023-11-07 17:31:33 -03:00
|
|
|
9735,
|
|
|
|
10000,
|
|
|
|
10001,
|
|
|
|
10002,
|
2023-11-22 17:12:25 -03:00
|
|
|
10003,
|
|
|
|
10004,
|
|
|
|
10005,
|
|
|
|
10006,
|
|
|
|
10007,
|
2024-11-11 22:28:37 -03:00
|
|
|
10009,
|
2023-11-22 17:12:25 -03:00
|
|
|
10015,
|
2025-02-04 13:40:42 -03:00
|
|
|
10019,
|
|
|
|
10030,
|
2024-11-11 22:28:37 -03:00
|
|
|
10050,
|
2024-04-19 14:05:40 -03:00
|
|
|
10101,
|
|
|
|
10102,
|
2025-02-04 13:40:42 -03:00
|
|
|
17375,
|
2024-07-08 09:09:30 -03:00
|
|
|
24133,
|
2023-11-22 17:12:25 -03:00
|
|
|
30000,
|
|
|
|
30002,
|
|
|
|
30003,
|
|
|
|
30004,
|
2023-11-07 17:31:33 -03:00
|
|
|
30008,
|
|
|
|
30009,
|
2023-11-22 17:12:25 -03:00
|
|
|
30015,
|
2024-04-19 14:16:52 -03:00
|
|
|
30818,
|
|
|
|
30819,
|
2023-11-22 17:12:25 -03:00
|
|
|
30030,
|
|
|
|
30078,
|
2023-11-07 17:31:33 -03:00
|
|
|
30311,
|
|
|
|
31922,
|
|
|
|
31923,
|
|
|
|
31924,
|
|
|
|
31925,
|
|
|
|
}
|
2023-11-02 20:59:50 -03:00
|
|
|
|
|
|
|
func validateAndFilterReports(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
|
2025-02-02 08:30:13 -03:00
|
|
|
if event.Kind == 1984 {
|
2023-11-02 20:59:50 -03:00
|
|
|
if e := event.Tags.GetFirst([]string{"e", ""}); e != nil {
|
|
|
|
// event report: check if the target event is here
|
2024-11-08 22:43:35 -03:00
|
|
|
res, _ := sys.StoreRelay.QuerySync(ctx, nostr.Filter{IDs: []string{(*e)[1]}})
|
2023-11-02 20:59:50 -03:00
|
|
|
if len(res) == 0 {
|
|
|
|
return true, "we don't know anything about the target event"
|
|
|
|
}
|
|
|
|
} else if p := event.Tags.GetFirst([]string{"p", ""}); p != nil {
|
|
|
|
// pubkey report
|
|
|
|
if !isPublicKeyInWhitelist((*p)[1]) {
|
|
|
|
return true, "target pubkey is not a user of this relay"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true, "invalid report"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, ""
|
|
|
|
}
|
2023-11-07 17:31:33 -03:00
|
|
|
|
|
|
|
func removeAuthorsNotWhitelisted(ctx context.Context, filter *nostr.Filter) {
|
2023-11-12 09:33:44 -03:00
|
|
|
if n := len(filter.Authors); n > len(whitelist)*11/10 {
|
|
|
|
// this query was clearly badly constructed, so we will not bother even looking
|
2024-04-05 12:29:43 -03:00
|
|
|
filter.LimitZero = true // this causes the query to be short cut
|
2023-11-12 09:33:44 -03:00
|
|
|
} else if n > 0 {
|
|
|
|
// otherwise we go through the authors list and remove the irrelevant ones
|
2023-11-07 17:31:33 -03:00
|
|
|
newAuthors := make([]string, 0, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
k := filter.Authors[i]
|
|
|
|
if _, ok := whitelist[k]; ok {
|
|
|
|
newAuthors = append(newAuthors, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filter.Authors = newAuthors
|
2023-11-12 09:30:25 -03:00
|
|
|
|
|
|
|
if len(newAuthors) == 0 {
|
2024-04-05 12:29:43 -03:00
|
|
|
filter.LimitZero = true // this causes the query to be short cut
|
2023-11-12 09:30:25 -03:00
|
|
|
}
|
2023-11-07 17:31:33 -03:00
|
|
|
}
|
|
|
|
}
|