-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler.go
197 lines (167 loc) · 4.19 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"context"
"fmt"
"log"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
"github.com/fiatjaf/relayer/v2"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
)
func adminHandler(cfg Config, db relayer.Storage) func(http.ResponseWriter, *http.Request) {
const template = "admin.html"
return func(w http.ResponseWriter, r *http.Request) {
if !auth(r, cfg.Admins) {
w.Header().Add("WWW-Authenticate", `Basic realm="username and password required"`)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
return
}
var (
id = r.URL.Query().Get("id")
kindStr = r.URL.Query().Get("kind")
pk = r.URL.Query().Get("pubkey")
limitStr = r.URL.Query().Get("limit")
)
var kind *int
if kindStr != "" {
if i, err := strconv.Atoi(kindStr); err == nil {
kind = &i
}
}
limit := 100
if limitStr != "" {
if i, err := strconv.Atoi(limitStr); err == nil {
limit = i
}
}
events, err := getEvents(db, id, pk, kind, limit)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
t, ok := templates[template]
if !ok {
log.Printf("template %s not found", template)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("missing template"))
return
}
tkn := make([]byte, 16)
rand.Read(tkn)
nonce := fmt.Sprintf("%x", tkn)
csp := fmt.Sprintf("script-src: 'self' 'unsafe-inline' 'nonce-%s'", nonce)
data := map[string]any{
"events": events,
"nonce": nonce,
}
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Content-Security-Policy", csp)
if err := t.Execute(w, data); err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
}
}
}
func adminDeleteHandler(cfg Config, db relayer.Storage) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if !strings.EqualFold(r.Method, http.MethodDelete) {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
if !auth(r, cfg.Admins) {
w.Header().Add("WWW-Authenticate", `Basic realm="username and password required"`)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
return
}
var (
id = r.URL.Query().Get("id")
)
filter := nostr.Filter{}
if id != "" {
filter.IDs = []string{id}
}
if len(filter.IDs) == 0 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("must provide id param"))
return
}
ctx := context.Background()
events, err := db.QueryEvents(ctx, &filter)
if err != nil {
fmt.Printf("[error] query events: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
for event := range events {
if err := db.DeleteEvent(ctx, event.ID, event.PubKey); err != nil {
log.Printf("[error] DeleteEvent %s: %s\n", event.ID, err)
} else {
log.Printf("deleted event: %s\n", event.String())
}
}
w.WriteHeader(http.StatusNoContent)
w.Write([]byte(""))
return
}
}
func auth(r *http.Request, admins map[string]string) bool {
user, pass, ok := r.BasicAuth()
if !ok {
fmt.Println("Error parsing basic auth")
return false
}
adminPass, ok := admins[user]
if !ok {
fmt.Printf("invalid user: %s\n", user)
return false
}
if !strings.EqualFold(pass, adminPass) {
fmt.Printf("invalid password for user: %s\n", user)
return false
}
return true
}
type uiEvent struct {
nostr.Event
PrettyTime string
Npub string
}
func getEvents(db relayer.Storage, id, pk string, kind *int, limit int) ([]uiEvent, error) {
filter := nostr.Filter{
Limit: limit,
}
if id != "" {
filter.IDs = []string{id}
}
if pk != "" {
filter.Authors = []string{pk}
}
if kind != nil {
filter.Kinds = []int{*kind}
}
ctx := context.Background()
events, err := db.QueryEvents(ctx, &filter)
if err != nil {
return nil, fmt.Errorf("query events: %w", err)
}
var out []uiEvent
for event := range events {
npub, _ := nip19.EncodePublicKey(event.PubKey)
out = append(out, uiEvent{
Event: *event,
PrettyTime: event.CreatedAt.Time().Format(time.RFC822),
Npub: npub,
})
}
return out, nil
}