This repository has been archived by the owner on Apr 12, 2020. It is now read-only.
/
main.go
344 lines (323 loc) · 8.44 KB
/
main.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main // import "luit.eu/comments"
// TODO:
//
// Authenticated API for approving/unapproving comments
//
// Akismet ham/spam submit on manual approve/unapprove
// --
// Redis schema:
//
// key {luit.eu/comments}:auto_enable
// value: set of hostnames
// use: SISMEMBER to check if you can add a new :enabled -> "true" key
//
// key: {luit.eu/comments://%s%s}:enabled
// key variables: host, path
// value: github.com/garyburd/redigo/redis.Bool
// note: Key not present means false too.
//
// key {luit.eu/comments://%s%s}:all
// key variables: host, path
// value: zset with timestamps as score and member
// use: ZADD NX for adding, and Z(REV)RANGEBYSCORE for listing
//
// key {luit.eu/comments://%s%s}:approved
// key variables: host, path
// value: zset with timestamps as score and member
// use: ZADD NX for adding, and Z(REV)RANGEBYSCORE for listing, ZREM to mark as spam
//
// key: {luit.eu/comments://%s%s}:comment:%d
// key variables: host, path, timestamp
// value: hash with comment data
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"strconv"
"time"
"github.com/garyburd/redigo/redis"
)
const (
keyCORS = "{luit.eu/comments}:cors"
keyAutoEnable = "{luit.eu/comments}:auto_enable"
keyEnabled = "{luit.eu/comments://%s%s}:enabled"
keyAll = "{luit.eu/comments://%s%s}:all"
keyApproved = "{luit.eu/comments://%s%s}:approved"
keyComment = "{luit.eu/comments://%s%s}:comment:%d"
)
func newPool() *redis.Pool {
return &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", ":6379")
if err != nil {
return nil, err
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
}
var (
pool *redis.Pool
)
func init() {
http.HandleFunc("/comments/", commentHandler)
}
func getCORS(conn redis.Conn) (string, error) {
cors, err := redis.String(conn.Do("GET", keyCORS))
if err == redis.ErrNil {
return "*", nil
}
return cors, err
}
func commentHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, "unable to parse form", http.StatusBadRequest)
}
switch r.Method {
case "GET":
rawURL := r.FormValue("url")
u, err := url.Parse(rawURL)
if err != nil {
http.Error(w, "bad URL", http.StatusBadRequest)
return
}
conn := pool.Get()
defer conn.Close()
cors, err := getCORS(conn)
if err != nil {
http.Error(w, "backend error", http.StatusInternalServerError)
}
comments, err := getComments(conn, u.Host, u.Path)
if err != nil {
http.Error(w, "backend error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", cors)
e := json.NewEncoder(w)
e.Encode(comments)
case "POST":
req, err := cleanCommentSubmitRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
conn := pool.Get()
defer conn.Close()
en, err := autoEnabled(conn, req.host, req.path)
if err != nil {
log.Println(err)
http.Error(w, "backend error", http.StatusInternalServerError)
return
}
if !en {
http.Error(w, "comments not enabled", http.StatusBadRequest)
return
}
id, err := saveComment(conn, req)
if err != nil {
log.Println(err)
http.Error(w, "backend error", http.StatusInternalServerError)
return
}
approved, err := autoApproveComment(conn, req.host, req.path, id)
if err != nil {
log.Println(err)
// Just the approval that failed, no real harm done
}
if approved {
log.Printf("New approved comment at %s%s: %d\n", req.host, req.path, id)
} else {
log.Printf("New unapproved comment at %s%s: %d\n", req.host, req.path, id)
}
http.Redirect(w, r, req.Permalink, http.StatusFound)
}
}
func main() {
if len(os.Args) > 2 {
log.Fatal("too many arguments, expecting one or zero")
}
addr := "127.0.0.1:2668"
if len(os.Args) == 2 {
addr = os.Args[1]
}
pool = newPool()
log.Fatal(http.ListenAndServe(addr, nil))
}
type commentSubmitRequest struct {
Permalink string `redis:"permalink"`
host string
path string
UserIP string `redis:"user_ip"`
UserAgent string `redis:"user_agent"`
Referrer string `redis:"referrer"`
Author string `redis:"comment_author"`
AuthorEmail string `redis:"comment_author_email"`
AuthorURL string `redis:"comment_author_url"`
Content string `redis:"comment_content"`
}
func cleanCommentSubmitRequest(r *http.Request) (*commentSubmitRequest, error) {
rawURL := r.FormValue("url")
u, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
if u.Host == "" {
return nil, errors.New("bad url value")
}
if r.FormValue("comment_author") == "" {
return nil, errors.New("bad comment_author value")
}
if r.FormValue("comment_content") == "" {
return nil, errors.New("bad comment_content value")
}
userIP := r.Header.Get("X-Forwarded-For")
if userIP == "" {
addr, err := net.ResolveTCPAddr("tcp", r.RemoteAddr)
if err != nil {
return nil, err
}
userIP = addr.IP.String()
}
return &commentSubmitRequest{
Permalink: rawURL,
host: u.Host,
path: u.Path,
UserIP: userIP,
UserAgent: r.Header.Get("User-Agent"),
Referrer: r.Header.Get("Referer"),
Author: r.FormValue("comment_author"),
AuthorEmail: r.FormValue("comment_author_email"),
AuthorURL: r.FormValue("comment_author_url"),
Content: r.FormValue("comment_content"),
}, nil
}
// comment contains the part of the data that will be sent through the API
type comment struct {
ID string `json:"id" redis:"-"`
Author string `json:"author" redis:"comment_author"`
Content string `json:"content" redis:"comment_content"`
}
func getComments(conn redis.Conn, host, path string) ([]comment, error) {
ids, err := redis.Strings(conn.Do("ZRANGEBYSCORE",
fmt.Sprintf(keyApproved, host, path),
"-inf", "+inf", "LIMIT", "0", "10"))
if err != nil {
return nil, err
}
comments := make([]comment, 0) // empty list, instead of nil
for _, id := range ids {
intid, _ := strconv.ParseInt(id, 10, 64)
vals, err := redis.Values(conn.Do("HGETALL",
fmt.Sprintf(keyComment, host, path, intid)))
if err != nil {
return nil, err
}
var c comment
if err = redis.ScanStruct(vals, &c); err != nil {
return nil, err
}
c.ID = id
c.Author = c.Author
c.Content = c.Content
comments = append(comments, c)
}
return comments, nil
}
func autoEnabled(conn redis.Conn, host, path string) (en bool, err error) {
en, err = redis.Bool(conn.Do("GET", fmt.Sprintf(keyEnabled, host, path)))
if err == redis.ErrNil {
en, err = redis.Bool(conn.Do("SISMEMBER", keyAutoEnable, host))
if err != nil {
return
}
if en {
conn.Do("SET", fmt.Sprintf(keyEnabled, host, path), "true")
}
}
return
}
func saveComment(conn redis.Conn, req *commentSubmitRequest) (id int64, err error) {
for {
id = time.Now().Unix()
var added bool
added, err = redis.Bool(conn.Do("ZADD", fmt.Sprintf(keyAll, req.host, req.path), id, id))
if err != nil {
log.Println(err)
return
}
if added {
break
}
time.Sleep(time.Second)
}
var ok string
ok, err = redis.String(conn.Do("HMSET", redis.Args{}.
Add(fmt.Sprintf(keyComment, req.host, req.path, id)).
AddFlat(req)...))
if err != nil {
return
}
if ok != "OK" {
log.Println("Unexpected return value from HMSET: %q\n", ok)
}
return
}
const (
akismetCheckURL = "https://%s.rest.akismet.com/1.1/comment-check"
)
var (
akismetKey = os.Getenv("AKISMET_KEY")
)
func autoApproveComment(conn redis.Conn, host, path string, id int64) (bool, error) {
if akismetKey == "" {
return false, nil
}
values, err := redis.StringMap(conn.Do("HGETALL",
fmt.Sprintf(keyComment, host, path, id)))
if err != nil {
return false, err
}
data := url.Values{
"blog": []string{
"https://luit.eu/",
},
}
for key, value := range values {
data.Add(key, value)
}
resp, err := http.PostForm(fmt.Sprintf(akismetCheckURL, akismetKey), data)
if err != nil {
return false, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}
isSpam, err := strconv.ParseBool(string(body))
if err != nil {
return false, errors.New("unexpected return value from akismet: " + string(body))
}
if !isSpam {
added, err := redis.Bool(conn.Do("ZADD", fmt.Sprintf(keyApproved, host, path), id, id))
if err != nil {
return false, err
}
return added, nil
}
return false, nil
}