-
Notifications
You must be signed in to change notification settings - Fork 0
/
www.go
613 lines (544 loc) · 15.5 KB
/
www.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// Copyright (c) 2017-2019 The Bitum developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"bytes"
"crypto/elliptic"
"crypto/tls"
_ "encoding/gob"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"path/filepath"
"runtime/debug"
"strings"
"syscall"
"text/template"
"time"
"github.com/bitum-project/politeia/politeiad/cache"
cachedb "github.com/bitum-project/politeia/politeiad/cache/cockroachdb"
www "github.com/bitum-project/politeia/politeiawww/api/www/v1"
cmsdb "github.com/bitum-project/politeia/politeiawww/cmsdatabase/cockroachdb"
userdb "github.com/bitum-project/politeia/politeiawww/user/cockroachdb"
"github.com/bitum-project/politeia/politeiawww/user/localdb"
"github.com/bitum-project/politeia/util"
"github.com/bitum-project/politeia/util/version"
"github.com/google/uuid"
"github.com/gorilla/csrf"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/robfig/cron"
)
type permission uint
const (
permissionPublic permission = iota
permissionLogin
permissionAdmin
csrfKeyLength = 32
sessionMaxAge = 86400 //One day
)
var (
// ErrSessionUUIDNotFound is emitted when a UUID value is not found
// in a session and indicates that the user is not logged in.
ErrSessionUUIDNotFound = errors.New("session UUID not found")
)
// Fetch remote identity
func (p *politeiawww) getIdentity() error {
id, err := util.RemoteIdentity(false, p.cfg.RPCHost, p.cfg.RPCCert)
if err != nil {
return err
}
// Pretty print identity.
log.Infof("Identity fetched from politeiad")
log.Infof("Key : %x", id.Key)
log.Infof("Fingerprint: %v", id.Fingerprint())
if p.cfg.Interactive != allowInteractive {
// Ask user if we like this identity
log.Infof("Press enter to save to %v or ctrl-c to abort",
p.cfg.RPCIdentityFile)
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
if err = scanner.Err(); err != nil {
return err
}
} else {
log.Infof("Saving identity to %v", p.cfg.RPCIdentityFile)
}
// Save identity
err = os.MkdirAll(filepath.Dir(p.cfg.RPCIdentityFile), 0700)
if err != nil {
return err
}
err = id.SavePublicIdentity(p.cfg.RPCIdentityFile)
if err != nil {
return err
}
log.Infof("Identity saved to: %v", p.cfg.RPCIdentityFile)
return nil
}
// RespondWithError returns an HTTP error status to the client. If it's a user
// error, it returns a 4xx HTTP status and the specific user error code. If it's
// an internal server error, it returns 500 and an error code which is also
// outputted to the logs so that it can be correlated later if the user
// files a complaint.
func RespondWithError(w http.ResponseWriter, r *http.Request, userHttpCode int, format string, args ...interface{}) {
// XXX this function needs to get an error in and a format + args
// instead of what it is doing now.
// So inError error, format string, args ...interface{}
// if err == nil -> internal error using format + args
// if err != nil -> if defined error -> return defined error + log.Errorf format+args
// if err != nil -> if !defined error -> return + log.Errorf format+args
if userErr, ok := args[0].(www.UserError); ok {
if userHttpCode == 0 {
userHttpCode = http.StatusBadRequest
}
if len(userErr.ErrorContext) == 0 {
log.Errorf("RespondWithError: %v %v %v",
remoteAddr(r),
int64(userErr.ErrorCode),
www.ErrorStatus[userErr.ErrorCode])
} else {
log.Errorf("RespondWithError: %v %v %v: %v",
remoteAddr(r),
int64(userErr.ErrorCode),
www.ErrorStatus[userErr.ErrorCode],
strings.Join(userErr.ErrorContext, ", "))
}
util.RespondWithJSON(w, userHttpCode,
www.ErrorReply{
ErrorCode: int64(userErr.ErrorCode),
ErrorContext: userErr.ErrorContext,
})
return
}
if pdError, ok := args[0].(www.PDError); ok {
pdErrorCode := convertErrorStatusFromPD(pdError.ErrorReply.ErrorCode)
if pdErrorCode == www.ErrorStatusInvalid {
errorCode := time.Now().Unix()
log.Errorf("%v %v %v %v Internal error %v: error "+
"code from politeiad: %v", remoteAddr(r),
r.Method, r.URL, r.Proto, errorCode,
pdError.ErrorReply.ErrorCode)
util.RespondWithJSON(w, http.StatusInternalServerError,
www.ErrorReply{
ErrorCode: errorCode,
})
return
}
util.RespondWithJSON(w, pdError.HTTPCode,
www.ErrorReply{
ErrorCode: int64(pdErrorCode),
ErrorContext: pdError.ErrorReply.ErrorContext,
})
return
}
errorCode := time.Now().Unix()
ec := fmt.Sprintf("%v %v %v %v Internal error %v: ", remoteAddr(r),
r.Method, r.URL, r.Proto, errorCode)
log.Errorf(ec+format, args...)
log.Errorf("Stacktrace (NOT A REAL CRASH): %s", debug.Stack())
util.RespondWithJSON(w, http.StatusInternalServerError,
www.ErrorReply{
ErrorCode: errorCode,
})
}
// addRoute sets up a handler for a specific method+route. If methos is not
// specified it adds a websocket.
func (p *politeiawww) addRoute(method string, route string, handler http.HandlerFunc, perm permission) {
fullRoute := www.PoliteiaWWWAPIRoute + route
switch perm {
case permissionAdmin:
handler = logging(p.isLoggedInAsAdmin(handler))
case permissionLogin:
handler = logging(p.isLoggedIn(handler))
default:
handler = logging(handler)
}
// All handlers need to close the body
handler = closeBody(handler)
if method == "" {
// Websocket
log.Tracef("Adding websocket: %v", fullRoute)
p.router.StrictSlash(true).HandleFunc(fullRoute, handler)
} else {
p.router.StrictSlash(true).HandleFunc(fullRoute, handler).Methods(method)
}
}
// makeRequest makes an http request to the method and route provided,
// serializing the provided object as the request body.
//
// XXX doesn't belong in this file but stuff it here for now.
func (p *politeiawww) makeRequest(method string, route string, v interface{}) ([]byte, error) {
var (
requestBody []byte
err error
)
if v != nil {
requestBody, err = json.Marshal(v)
if err != nil {
return nil, err
}
}
fullRoute := p.cfg.RPCHost + route
if p.client == nil {
p.client, err = util.NewClient(false, p.cfg.RPCCert)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, fullRoute,
bytes.NewReader(requestBody))
if err != nil {
return nil, err
}
req.SetBasicAuth(p.cfg.RPCUser, p.cfg.RPCPass)
r, err := p.client.Do(req)
if err != nil {
return nil, err
}
defer r.Body.Close()
if r.StatusCode != http.StatusOK {
var pdErrorReply www.PDErrorReply
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&pdErrorReply); err != nil {
return nil, err
}
return nil, www.PDError{
HTTPCode: r.StatusCode,
ErrorReply: pdErrorReply,
}
}
responseBody := util.ConvertBodyToByteArray(r.Body, false)
return responseBody, nil
}
func _main() error {
// Load configuration and parse command line. This function also
// initializes logging and configures it accordingly.
loadedCfg, _, err := loadConfig()
if err != nil {
return fmt.Errorf("Could not load configuration file: %v", err)
}
defer func() {
if logRotator != nil {
logRotator.Close()
}
}()
log.Infof("Version : %v", version.String())
log.Infof("Network : %v", activeNetParams.Params.Name)
log.Infof("Home dir: %v", loadedCfg.HomeDir)
if loadedCfg.PaywallAmount != 0 && loadedCfg.PaywallXpub != "" {
paywallAmountInBitum := float64(loadedCfg.PaywallAmount) / 1e8
log.Infof("Paywall : %v BITUM", paywallAmountInBitum)
} else if loadedCfg.PaywallAmount == 0 && loadedCfg.PaywallXpub == "" {
log.Infof("Paywall : DISABLED")
} else {
return fmt.Errorf("Paywall settings invalid, both an amount " +
"and public key MUST be set")
}
if loadedCfg.MailHost == "" {
log.Infof("Email : DISABLED")
}
// Create the data directory in case it does not exist.
err = os.MkdirAll(loadedCfg.DataDir, 0700)
if err != nil {
return err
}
// Generate the TLS cert and key file if both don't already
// exist.
if !fileExists(loadedCfg.HTTPSKey) &&
!fileExists(loadedCfg.HTTPSCert) {
log.Infof("Generating HTTPS keypair...")
err := util.GenCertPair(elliptic.P256(), "politeiadwww",
loadedCfg.HTTPSCert, loadedCfg.HTTPSKey)
if err != nil {
return fmt.Errorf("unable to create https keypair: %v",
err)
}
log.Infof("HTTPS keypair created...")
}
// Setup application context.
p := &politeiawww{
cfg: loadedCfg,
ws: make(map[string]map[string]*wsContext),
templates: make(map[string]*template.Template),
// XXX reevaluate where this goes
userPubkeys: make(map[string]string),
userEmails: make(map[string]uuid.UUID),
userPaywallPool: make(map[uuid.UUID]paywallPoolMember),
commentScores: make(map[string]int64),
voteStatuses: make(map[string]www.VoteStatusReply),
params: activeNetParams.Params,
}
// Check if this command is being run to fetch the identity.
if p.cfg.FetchIdentity {
return p.getIdentity()
}
// Setup email
smtp, err := newSMTP(p.cfg.MailHost, p.cfg.MailUser,
p.cfg.MailPass, p.cfg.MailAddress, p.cfg.SystemCerts,
p.cfg.SMTPSkipVerify)
if err != nil {
return fmt.Errorf("unable to initialize SMTP client: %v",
err)
}
p.smtp = smtp
// Setup user database
switch p.cfg.UserDB {
case userDBLevel:
// localdb.UseLogger(localdbLog)
db, err := localdb.New(p.cfg.DataDir)
if err != nil {
return err
}
p.db = db
case userDBCockroach:
// If old encryption key is set it means that we need
// to open a db connection using the old key and then
// rotate keys.
var encryptionKey string
if p.cfg.OldEncryptionKey != "" {
encryptionKey = p.cfg.OldEncryptionKey
} else {
encryptionKey = p.cfg.EncryptionKey
}
// Setup logging
userdb.UseLogger(cockroachdbLog)
// Open db connection
network := filepath.Base(p.cfg.DataDir)
db, err := userdb.New(p.cfg.DBHost, network, p.cfg.DBRootCert,
p.cfg.DBCert, p.cfg.DBKey, encryptionKey)
if err != nil {
return fmt.Errorf("new cockroachdb: %v", err)
}
p.db = db
// Rotate keys
if p.cfg.OldEncryptionKey != "" {
err = db.RotateKeys(p.cfg.EncryptionKey)
if err != nil {
return fmt.Errorf("rotate userdb keys: %v", err)
}
}
default:
return fmt.Errorf("no user db option found")
}
// Get plugins from politeiad
p.plugins, err = p.getPluginInventory()
if err != nil {
return fmt.Errorf("getPluginInventory: %v", err)
}
// Setup cache connection
cachedb.UseLogger(cockroachdbLog)
net := filepath.Base(p.cfg.DataDir)
p.cache, err = cachedb.New(cachedb.UserPoliteiawww, p.cfg.DBHost,
net, p.cfg.DBRootCert, p.cfg.DBCert, p.cfg.DBKey)
if err != nil {
switch err {
case cache.ErrNoVersionRecord:
err = fmt.Errorf("cache version record not found; " +
"start politeiad to setup the cache")
case cache.ErrWrongVersion:
err = fmt.Errorf("wrong cache version found; " +
"restart politeiad to rebuild the cache")
}
return fmt.Errorf("cachedb new: %v", err)
}
// Register plugins with cache
for _, v := range p.plugins {
cp := convertPluginToCache(v)
err = p.cache.RegisterPlugin(cp)
if err != nil {
switch err {
case cache.ErrNoVersionRecord:
err = fmt.Errorf("version record not found;" +
"start politeiad to setup the cache")
case cache.ErrWrongVersion:
err = fmt.Errorf("wrong version found; " +
"restart politeiad to rebuild the cache")
}
return fmt.Errorf("cache register plugin '%v': %v",
v.ID, err)
}
log.Infof("Registered cache plugin: %v", v.ID)
}
// Setup pubkey-userid map
err = p.initUserPubkeys()
if err != nil {
return err
}
// Setup email-userID map
err = p.initUserEmails()
if err != nil {
return err
}
// Setup comment scores map
err = p.initCommentScores()
if err != nil {
return fmt.Errorf("initCommentScore: %v", err)
}
// Setup events
p.initEventManager()
// Set up the code that checks for paywall payments.
if p.cfg.Mode == "piwww" {
err = p.initPaywallChecker()
if err != nil {
return err
}
}
// Load or create new CSRF key
log.Infof("Load CSRF key")
csrfKeyFilename := filepath.Join(p.cfg.DataDir, "csrf.key")
fCSRF, err := os.Open(csrfKeyFilename)
if err != nil {
if os.IsNotExist(err) {
key, err := util.Random(csrfKeyLength)
if err != nil {
return err
}
// Persist key
fCSRF, err = os.OpenFile(csrfKeyFilename,
os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
_, err = fCSRF.Write(key)
if err != nil {
return err
}
_, err = fCSRF.Seek(0, 0)
if err != nil {
return err
}
} else {
return err
}
}
csrfKey := make([]byte, csrfKeyLength)
r, err := fCSRF.Read(csrfKey)
if err != nil {
return err
}
if r != csrfKeyLength {
return fmt.Errorf("CSRF key corrupt")
}
fCSRF.Close()
csrfHandle := csrf.Protect(csrfKey, csrf.Path("/"))
p.router = mux.NewRouter()
p.router.Use(recoverMiddleware)
switch p.cfg.Mode {
case politeiaWWWMode:
p.setPoliteiaWWWRoutes()
// XXX setup user routes
p.setUserWWWRoutes()
case cmsWWWMode:
p.setCMSWWWRoutes()
// XXX setup user routes
p.setCMSUserWWWRoutes()
cmsdb.UseLogger(cockroachdbLog)
net := filepath.Base(p.cfg.DataDir)
p.cmsDB, err = cmsdb.New(p.cfg.DBHost, net, p.cfg.DBRootCert,
p.cfg.DBCert, p.cfg.DBKey)
if err != nil {
return err
}
err = p.cmsDB.Setup()
if err != nil {
return fmt.Errorf("cmsdb setup: %v", err)
}
p.cron = cron.New()
p.checkInvoiceNotifications()
default:
return fmt.Errorf("unknown mode %v:", p.cfg.Mode)
}
// Persist session cookies.
var cookieKey []byte
if cookieKey, err = ioutil.ReadFile(p.cfg.CookieKeyFile); err != nil {
log.Infof("Cookie key not found, generating one...")
cookieKey, err = util.Random(32)
if err != nil {
return err
}
err = ioutil.WriteFile(p.cfg.CookieKeyFile, cookieKey, 0400)
if err != nil {
return err
}
log.Infof("Cookie key generated.")
}
sessionsDir := filepath.Join(p.cfg.DataDir, "sessions")
err = os.MkdirAll(sessionsDir, 0700)
if err != nil {
return err
}
p.store = sessions.NewFilesystemStore(sessionsDir, cookieKey)
p.store.Options = &sessions.Options{
Path: "/",
MaxAge: sessionMaxAge,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
}
// Bind to a port and pass our router in
listenC := make(chan error)
for _, listener := range loadedCfg.Listeners {
listen := listener
go func() {
cfg := &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{
tls.CurveP256, // BLAME CHROME, NOT ME!
tls.CurveP521,
tls.X25519},
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
},
}
srv := &http.Server{
Handler: csrfHandle(p.router),
Addr: listen,
TLSConfig: cfg,
TLSNextProto: make(map[string]func(*http.Server,
*tls.Conn, http.Handler)),
}
log.Infof("Listen: %v", listen)
listenC <- srv.ListenAndServeTLS(loadedCfg.HTTPSCert,
loadedCfg.HTTPSKey)
}()
}
// Tell user we are ready to go.
log.Infof("Start of day")
// Setup OS signals
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGINT)
for {
select {
case sig := <-sigs:
log.Infof("Terminating with %v", sig)
goto done
case err := <-listenC:
log.Errorf("%v", err)
goto done
}
}
done:
log.Infof("Exiting")
// Close user db connection
p.db.Close()
return nil
}
func main() {
err := _main()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}