This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathmain.go
439 lines (389 loc) · 10.6 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// This code is in Public Domain. Take all the code you want, I'll just write more.
package main
import (
"bytes"
"context"
"crypto/tls"
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"math/rand"
"net/http"
"path/filepath"
"regexp"
"strings"
"time"
"golang.org/x/crypto/acme/autocert"
"github.com/garyburd/go-oauth/oauth"
"github.com/gorilla/securecookie"
"github.com/kjk/u"
)
var (
dataDir string
configPath = flag.String("config", "config.json", "Path to configuration file")
httpAddr = flag.String("http-addr", ":5010", "HTTP server address")
inProduction = flag.Bool("production", false, "are we running in production")
noS3Backup = flag.Bool("no-backup", false, "did we disable s3 backup")
cookieName = "ckie"
)
var (
oauthClient = oauth.Client{
TemporaryCredentialRequestURI: "https://api.twitter.com/oauth/request_token",
ResourceOwnerAuthorizationURI: "https://api.twitter.com/oauth/authenticate",
TokenRequestURI: "https://api.twitter.com/oauth/access_token",
}
config = struct {
TwitterOAuthCredentials *oauth.Credentials
CookieAuthKeyHexStr *string
CookieEncrKeyHexStr *string
AnalyticsCode *string
AwsAccess *string
AwsSecret *string
S3BackupBucket *string
S3BackupDir *string
}{
&oauthClient.Credentials,
nil, nil,
nil,
nil, nil,
nil, nil,
}
forums = make([]*ForumConfig, 0)
logger *ServerLogger
cookieAuthKey []byte
cookieEncrKey []byte
secureCookie *securecookie.SecureCookie
appState = AppState{
Users: make([]*User, 0),
Forums: make([]*Forum, 0),
}
alwaysLogTime = true
)
// ForumConfig is a static configuration of a single forum
type ForumConfig struct {
Title string
ForumUrl string
WebsiteUrl string
Tagline string
DataDir string
// we authenticate only with Twitter, this is the twitter user name
// of the admin user
AdminTwitterUser string
Disabled bool
BannedIps *[]string
BannedWords *[]string
SidebarTmpl *template.Template
}
// User describes a user
type User struct {
Login string
}
// Forum describes forum
type Forum struct {
ForumConfig
Store *Store
}
// AppState describes state of the app
type AppState struct {
Users []*User
Forums []*Forum
}
// StringEmpty returns true if string is empty
func StringEmpty(s *string) bool {
return s == nil || 0 == len(*s)
}
// S3BackupEnabled returns true if backup to s3 is enabled
func S3BackupEnabled() bool {
if *noS3Backup {
return false
}
if !*inProduction {
logger.Notice("s3 backups disabled because not in production")
return false
}
if StringEmpty(config.AwsAccess) {
logger.Notice("s3 backups disabled because AwsAccess not defined in config.json\n")
return false
}
if StringEmpty(config.AwsSecret) {
logger.Notice("s3 backups disabled because AwsSecret not defined in config.json\n")
return false
}
if StringEmpty(config.S3BackupBucket) {
logger.Notice("s3 backups disabled because S3BackupBucket not defined in config.json\n")
return false
}
if StringEmpty(config.S3BackupDir) {
logger.Notice("s3 backups disabled because S3BackupDir not defined in config.json\n")
return false
}
return true
}
func getDataDir() string {
if dataDir != "" {
return dataDir
}
dirsToCheck := []string{
"/data",
filepath.Join("..", "..", "data"), // old on the server
u.ExpandTildeInPath("~/data/fofou"), // locally
}
for _, dir := range dirsToCheck {
if u.PathExists(dir) {
dataDir = dir
return dataDir
}
}
log.Fatalf("data directory (%q) doesn't exist\n", dirsToCheck)
return ""
}
// NewForum creates new forum
func NewForum(config *ForumConfig) *Forum {
forum := &Forum{ForumConfig: *config}
sidebarTmplPath := filepath.Join("forums", fmt.Sprintf("%s_sidebar.html", forum.ForumUrl))
if !u.PathExists(sidebarTmplPath) {
panic(fmt.Sprintf("sidebar template %s for forum %s doesn't exist", sidebarTmplPath, forum.ForumUrl))
}
forum.SidebarTmpl = template.Must(template.ParseFiles(sidebarTmplPath))
store, err := NewStore(getDataDir(), config.DataDir)
if err != nil {
logger.Errorf("NewStore('%s', '%s') failed with '%s'\n", getDataDir(), config.DataDir, err)
panic("failed to create store for a forum")
}
logger.Noticef("%d topics, %d posts in forum %q", store.TopicsCount(), store.PostsCount(), config.ForumUrl)
forum.Store = store
return forum
}
func findForum(forumURL string) *Forum {
for _, f := range appState.Forums {
if f.ForumUrl == forumURL {
return f
}
}
return nil
}
func forumAlreadyExists(siteURL string) bool {
return nil != findForum(siteURL)
}
func forumInvalidField(forum *Forum) string {
forum.Title = strings.TrimSpace(forum.Title)
if forum.Title == "" {
return "Title"
}
if forum.ForumUrl == "" {
return "ForumUrl"
}
if forum.WebsiteUrl == "" {
return "WebsiteUrl"
}
if forum.DataDir == "" {
return "DataDir"
}
if forum.AdminTwitterUser == "" {
return "AdminTwitterUser"
}
return ""
}
func addForum(forum *Forum) error {
if invalidField := forumInvalidField(forum); invalidField != "" {
return fmt.Errorf("Forum has invalid field %q", invalidField)
}
if forumAlreadyExists(forum.ForumUrl) {
return errors.New("Forum already exists")
}
// verify BannedIps are valid regexpes
banned := forum.BannedIps
if banned != nil {
for _, s := range *banned {
_, err := regexp.Compile(s)
if err != nil {
log.Fatalf("%q is not a valid regexp, err: %s", s, err)
}
}
}
appState.Forums = append(appState.Forums, forum)
return nil
}
// DoSidebarTemplate renders sidebar template
func DoSidebarTemplate(forum *Forum, isAdmin bool) string {
n := forum.Store.GetBlockedIpsCount()
model := struct {
IsAdmin bool
BlockedIpsCount int
}{
IsAdmin: isAdmin,
BlockedIpsCount: n,
}
var buf bytes.Buffer
tmpl := forum.SidebarTmpl
s := ""
if err := tmpl.Execute(&buf, model); err != nil {
logger.Errorf("Failed to execute sidebar template for forum %q error: %s", forum.ForumUrl, err)
} else {
s = string(buf.Bytes())
}
return s
}
func isTopLevelURL(url string) bool {
return 0 == len(url) || "/" == url
}
func userIsAdmin(f *Forum, cookie *SecureCookieValue) bool {
return cookie.TwitterUser == f.AdminTwitterUser
}
// reads forums/*_config.json files
func readForumConfigs(configDir string) error {
pat := filepath.Join(configDir, "*_config.json")
files, err := filepath.Glob(pat)
if err != nil {
return err
}
if files == nil {
return errors.New("No forums configured!")
}
for _, configFile := range files {
var forum ForumConfig
b, err := ioutil.ReadFile(configFile)
if err != nil {
return err
}
err = json.Unmarshal(b, &forum)
if err != nil {
return err
}
if !forum.Disabled {
forums = append(forums, &forum)
}
}
if len(forums) == 0 {
return errors.New("All forums are disabled!")
}
return nil
}
// reads the configuration file from the path specified by
// the config command line flag.
func readConfig(configFile string) error {
b, err := ioutil.ReadFile(configFile)
if err != nil {
return fmt.Errorf("%s config file doesn't exist. Read readme.md for config instructions", configFile)
}
err = json.Unmarshal(b, &config)
if err != nil {
return err
}
cookieAuthKey, err = hex.DecodeString(*config.CookieAuthKeyHexStr)
if err != nil {
return err
}
cookieEncrKey, err = hex.DecodeString(*config.CookieEncrKeyHexStr)
if err != nil {
return err
}
secureCookie = securecookie.New(cookieAuthKey, cookieEncrKey)
// verify auth/encr keys are correct
val := map[string]string{
"foo": "bar",
}
_, err = secureCookie.Encode(cookieName, val)
if err != nil {
// for convenience, if the auth/encr keys are not set,
// generate valid, random value for them
fmt.Printf("CookieAuthKeyHexStr and CookieEncrKeyHexStr are invalid or missing in %q\nYou can use the following random values:\n", configFile)
auth := securecookie.GenerateRandomKey(32)
encr := securecookie.GenerateRandomKey(32)
fmt.Printf("CookieAuthKeyHexStr: %s\nCookieEncrKeyHexStr: %s\n", hex.EncodeToString(auth), hex.EncodeToString(encr))
}
// TODO: somehow verify twitter creds
return err
}
func getReferer(r *http.Request) string {
return r.Header.Get("Referer")
}
func makeTimingHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
fn(w, r)
duration := time.Now().Sub(startTime)
// log urls that take long time to generate i.e. over 1 sec in production
// or over 0.1 sec in dev
shouldLog := duration.Seconds() > 1.0
if alwaysLogTime && duration.Seconds() > 0.1 {
shouldLog = true
}
if shouldLog {
url := r.URL.Path
if len(r.URL.RawQuery) > 0 {
url = fmt.Sprintf("%s?%s", url, r.URL.RawQuery)
}
logger.Noticef("%q took %f seconds to serve", url, duration.Seconds())
}
}
}
func fofouHostPolicy(ctx context.Context, host string) error {
if strings.HasSuffix(host, "fofou.org") {
return nil
}
return errors.New("acme/autocert: only *.fofou.org hosts are allowed")
}
func main() {
flag.StringVar(&dataDir, "data-dir", "", "data directory")
flag.Parse()
if *inProduction {
reloadTemplates = false
alwaysLogTime = false
}
useStdout := !*inProduction
logger = NewServerLogger(256, 256, useStdout)
rand.Seed(time.Now().UnixNano())
if err := readConfig(*configPath); err != nil {
log.Fatalf("Failed reading config file %s. %s\n", *configPath, err)
}
if err := readForumConfigs("forums"); err != nil {
log.Fatalf("Failed to read forum configs, err: %s", err)
}
for _, forumData := range forums {
f := NewForum(forumData)
if err := addForum(f); err != nil {
log.Fatalf("Failed to add the forum: %s, err: %s\n", f.Title, err)
} else {
fmt.Printf("added forum %s\n", f.ForumUrl)
}
}
if len(appState.Forums) == 0 {
log.Fatalf("No forums defined in config.json")
}
backupConfig := &BackupConfig{
AwsAccess: *config.AwsAccess,
AwsSecret: *config.AwsSecret,
Bucket: *config.S3BackupBucket,
S3Dir: *config.S3BackupDir,
LocalDir: getDataDir(),
}
if S3BackupEnabled() {
go BackupLoop(backupConfig)
}
if *inProduction {
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: fofouHostPolicy,
}
srv := initHTTPServer()
srv.Addr = ":443"
srv.TLSConfig = &tls.Config{GetCertificate: m.GetCertificate}
logger.Noticef("Started runing HTTPS on %s\n", srv.Addr)
go func() {
srv.ListenAndServeTLS("", "")
}()
}
srv := initHTTPServer()
srv.Addr = *httpAddr
logger.Noticef(fmt.Sprintf("Started runing on %s\n", srv.Addr))
if err := srv.ListenAndServe(); err != nil {
fmt.Printf("http.ListendAndServer() failed with %s\n", err)
}
fmt.Printf("Exited\n")
}