-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.go
602 lines (506 loc) · 17.2 KB
/
server.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
package main
import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/big"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/glifio/go-logger"
"github.com/pkg/errors"
"gopkg.in/robfig/cron.v2"
)
func startFaucet(router *gin.Engine, c *cron.Cron) {
logger.Infof("Faucet address: %v", FaucetAddr.String())
logger.Infof("Faucet grant size: %v", env.FaucetGrantSize)
logger.Infof("Faucet min GH account age days: %v", env.FaucetMinAccountAgeDays)
// Add routes
router.POST("/faucet/:target_addr", serveFaucet, handleError("/faucet"))
// Add cron jobs
c.AddFunc("@hourly", reconcileFaucetMessages)
}
func startVerifier(router *gin.Engine, c *cron.Cron) {
logger.Infof("Verifier address: %v", VerifierAddr.String())
logger.Infof("Verifier min GH account age days: %v", env.VerifierMinAccountAgeDays)
logger.Infof("Verifier rate limit: %v", env.VerifierRateLimit)
logger.Infof("Verifier base allowance: %v", env.BaseAllowanceBytes)
logger.Infof("Max allocations: %v", env.MaxTotalAllocations)
// Init counter
err := initCounter(&gin.Context{})
if err != nil {
logger.Errorf("REDIS INIT COUNT FAILED: %v", err)
}
// Add routes
router.POST("/verify/:target_addr", serveVerifyAccount)
router.PUT("/verify/counter/:pwd", serveResetCounter)
router.GET("/verify/counter/:pwd", serveCurrentCount)
router.GET("/verifiers", serveListVerifiers)
router.GET("/verified-clients", serveListVerifiedClients)
router.GET("/allowance/:target_addr", serveAllowance)
router.GET("/account-remaining-bytes/:target_addr", serveCheckAccountRemainingBytes)
router.GET("/verifier-remaining-bytes/:target_addr", serveCheckVerifierRemainingBytes)
// Add cron jobs
c.AddFunc("@hourly", reconcileVerifierMessages)
}
func main() {
// Initialize logger
err := logger.Init(logger.LoggerOptions{
ModuleName: "verifier",
SentryEnabled: env.SentryDsn != "",
SentryDsn: env.SentryDsn,
SentryEnv: env.SentryEnv,
SentryLevel: logger.LogLevelWarning,
SentryTraces: 0,
})
if err != nil {
logger.Panic(err)
}
logger.Infof("Lotus node: %v", env.LotusAPIDialAddr)
logger.Infof("Dynamodb table name: %v", env.DynamodbTableName)
logger.Infof("Max transaction fee: %v", env.MaxFee)
logger.Infof("Mode: %v", env.Mode)
if err := initBlockListCache(); err != nil {
logger.Panic(err)
}
if _, err := instantiateWallet(&gin.Context{}); err != nil {
logger.Panic(err)
}
// Create Gin engine
router := gin.Default()
if logger.IsSentryEnabled() {
router.Use(logger.GetSentryGin())
}
// Set CORS headers
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"POST"},
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
// Add generic routes
router.GET("/", servePong)
router.GET("/healthz", servePong)
router.GET("/ping", servePong)
router.POST("/oauth/:provider", serveOauth, handleError("/oauth"))
// Add app-specific routes
c := cron.New()
if env.Mode == FaucetMode {
startFaucet(router, c)
} else if env.Mode == VerifierMode {
startVerifier(router, c)
} else {
startFaucet(router, c)
startVerifier(router, c)
}
// Start cron jobs
c.Start()
defer func() {
c.Stop()
}()
// Start Gin server
router.Run(":" + env.Port)
}
var (
ErrUnsupportedProvider = errors.New("unsupported oauth provider")
ErrUserTooNew = errors.New("User account is too new.")
ErrVerifiedClientExists = errors.New("This Filecoin address is already a verified client. Please try again with a new Filecoin address.")
ErrAllocatedTooRecently = errors.New("You must wait 30 days in between reallocations")
ErrStaleJWT = errors.New("The network has reset since your last visit. Please click the retry button above.")
ErrFaucetRepeatAttempt = errors.New("This GitHub account has already used the faucet.")
ErrUserLocked = errors.New("Our servers are processing your last transaction. Come back tomorrow.")
ErrAddressBlocked = errors.New("This address or Miner ID has reached its maximum usage of the faucet.")
ErrCounterReached = errors.New("This notary has run out of data cap for today! Come back tomorrow.")
ErrMaxAllowanceFailed = errors.New("Failed to calculate the maximum allowance for the user account and filecoin address")
)
type UserLock string
var (
UserLock_Verifier UserLock = "Verifier"
UserLock_Faucet UserLock = "Faucet"
)
func setError(c *gin.Context, code int, err error) {
c.Set("error", err)
c.Set("code", code)
}
func handleError(route string) gin.HandlerFunc {
return func(c *gin.Context) {
err, hasErr := c.Get("error")
code, hasCode := c.Get("code")
if hasErr {
if hasCode {
c.JSON(code.(int), gin.H{"error": err.(error).Error()})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.(error).Error()})
}
logger.Errorf("%v: %v", route, err)
}
}
}
func servePong(c *gin.Context) {
c.JSON(http.StatusOK, "pong")
}
func serveOauth(c *gin.Context) {
providerName := c.Param("provider")
provider, exists := oauthProviders[providerName]
if !exists {
setError(c, http.StatusBadRequest, errors.Wrapf(ErrUnsupportedProvider, "provider=%v", providerName))
return
}
type Request struct {
Code string `json:"code" binding:"required"`
State string `json:"state" binding:"required"`
}
var body Request
if err := c.ShouldBindJSON(&body); err != nil {
setError(c, http.StatusBadRequest, errors.Wrap(err, "binding request JSON"))
return
}
// Exchange the `code` for an `access_token`
token, err := OAuthExchangeCodeForToken(provider, body.Code, body.State)
if err != nil {
setError(c, http.StatusInternalServerError, errors.Wrap(err, "exchanging code for token"))
return
}
// Fetch the user's profile
accountData, err := provider.FetchAccountData(token)
if err != nil {
setError(c, http.StatusInternalServerError, errors.Wrap(err, "fetching account data"))
return
}
// Update user record in Dynamo
user, err := getUserWithProviderUniqueID(providerName, accountData.UniqueID)
if err != nil {
setError(c, http.StatusInternalServerError, errors.Wrap(err, "fetching DynamoDB user"))
return
}
user.Accounts[providerName] = accountData
err = saveUser(user)
if err != nil {
setError(c, http.StatusInternalServerError, errors.Wrap(err, "saving DynamoDB user"))
return
}
jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"userID": user.ID,
"nbf": time.Date(2015, 10, 10, 12, 0, 0, 0, time.UTC).Unix(),
})
// Sign and get the complete encoded token as a string using the secret
jwtTokenString, err := jwtToken.SignedString([]byte(env.JWTSecret))
if err != nil {
setError(c, http.StatusInternalServerError, errors.Wrap(err, "generating JWT"))
return
}
type Response struct {
JWT string `json:"jwt"`
}
c.JSON(http.StatusOK, Response{jwtTokenString})
}
func serveVerifyAccount(c *gin.Context) {
userID, err := getUserIDFromJWT(c)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
user, err := getUserByID(userID)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": ErrStaleJWT.Error()})
return
}
if len(user.Accounts) == 0 {
c.JSON(http.StatusForbidden, gin.H{"error": ErrStaleJWT.Error()})
return
}
if user.Locked_Verifier {
c.JSON(http.StatusForbidden, gin.H{"error": ErrUserLocked.Error()})
return
}
targetAddrStr := c.Param("target_addr")
// Ensure that the user hasn't used this address before
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
// Ensure that the user's account is old enough
minAccountAge := time.Duration(env.VerifierMinAccountAgeDays) * 24 * time.Hour
// No account less than MinAccountAge is allowed any FIL
if !user.HasAccountOlderThan(minAccountAge) {
accountName := user.Accounts["github"].Username
accountAge := user.Accounts["github"].CreatedAt.String()
logger.Errorf("ACCOUNT TOO NEW: User ID %q, FIL Address %q, Account name %q, Account age %q", user.ID, targetAddrStr, accountName, accountAge)
c.JSON(http.StatusForbidden, gin.H{"error": ErrUserTooNew.Error()})
return
}
// Ensure that the user hasn't asked for more allocation too recently
if user.MostRecentAllocation.Add(env.VerifierRateLimit).After(time.Now()) {
accountName := user.Accounts["github"].Username
lastAllocation := user.MostRecentAllocation.String()
logger.Errorf("REALLOCATION TOO SOON: User ID %q, FIL Address %q, Account name %q, Last allocation %q", user.ID, targetAddrStr, accountName, lastAllocation)
c.JSON(http.StatusForbidden, gin.H{"error": ErrAllocatedTooRecently.Error()})
return
}
// failsafe in case we're getting attacked so no one can drain the account of datacap
reachedCount, err := reachedCounter(c)
if reachedCount {
logger.Errorf("VERIFIER COUNTER REACHED: %v", env.MaxTotalAllocations)
c.JSON(http.StatusLocked, gin.H{"error": ErrCounterReached.Error()})
return
}
if err != nil {
logger.Errorf("VERIFIER COUNTER CALCULATION FAILED: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrCounterReached.Error()})
return
}
dataCap, err := lotusCheckVerifierRemainingBytes(c, VerifierAddr.String())
if err != nil {
logger.Errorf("LOTUS CHECK VERIFIER BYTES FAILED: %v", err)
c.JSON(http.StatusLocked, gin.H{"error": ErrCounterReached.Error()})
return
}
fiftyDataCaps := big.Mul(env.MaxAllowanceBytes, big.NewInt(50))
if dataCap.LessThanEqual(fiftyDataCaps) {
logger.Warningf("LOW DATA CAP: %v", dataCap.String())
}
targetAddr, err := address.NewFromString(targetAddrStr)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if isAddressBlocked(targetAddr) {
c.JSON(http.StatusForbidden, gin.H{"error": ErrAddressBlocked.Error()})
return
}
// Lock the user for the duration of this operation until cron job cleans it up
err = lockUser(userID, UserLock_Verifier)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": ErrUserLocked.Error()})
return
}
user, err = getUserByID(userID)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": ErrStaleJWT.Error()})
return
}
allowance := env.MaxAllowanceBytes
// Allocate the bytes
err = incrementCounter(c)
if err != nil {
logger.Errorf("REDIS INCREMENT COUNT FAILED: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx, cancel = context.WithTimeout(context.Background(), 60*time.Minute)
defer cancel()
cid, err := lotusVerifyAccount(ctx, targetAddrStr, allowance)
if err != nil {
logger.Errorf("LOTUS VERIFY ACCOUNT FAILED: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
user.MostRecentDataCapCid = cid.String()
user.MostRecentVerifiedAddress = targetAddrStr
err = saveUser(user)
if err != nil {
// TODO what to do here?
logger.Errorf("ERROR SAVING USER: %v", err)
}
// Respond to the HTTP request
type Response struct {
Cid string `json:"cid"`
Allowance string `json:"allowance"`
}
c.JSON(http.StatusOK, Response{
Cid: cid.String(),
Allowance: allowance.String(),
})
}
func serveListVerifiers(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
verifiers, err := lotusListVerifiers(ctx)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, verifiers)
}
func serveListVerifiedClients(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
verifiedClients, err := lotusListVerifiedClients(ctx)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, verifiedClients)
}
func serveAllowance(c *gin.Context) {
// Respond with allowance
type Response struct {
Allowance string `json:"allowance"`
}
c.JSON(http.StatusOK, Response{Allowance: env.MaxAllowanceBytes.String()})
}
func serveCheckAccountRemainingBytes(c *gin.Context) {
targetAddr := c.Param("target_addr")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
dcap, err := lotusCheckAccountRemainingBytes(ctx, targetAddr)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
type Response struct {
RemainingBytes string `json:"remainingBytes"`
}
c.JSON(http.StatusOK, Response{dcap.String()})
}
func serveCheckVerifierRemainingBytes(c *gin.Context) {
targetAddr := c.Param("target_addr")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
dcap, err := lotusCheckVerifierRemainingBytes(ctx, targetAddr)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, dcap)
}
func serveFaucet(c *gin.Context) {
userID, err := getUserIDFromJWT(c)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
user, err := getUserByID(userID)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": ErrStaleJWT.Error()})
return
}
if len(user.Accounts) == 0 {
c.JSON(http.StatusForbidden, gin.H{"error": ErrStaleJWT.Error()})
return
}
if user.Locked_Faucet {
c.JSON(http.StatusForbidden, gin.H{"error": ErrUserLocked.Error()})
return
}
// This can get deleted, along with the `ReceivedFaucetGrant` key in dynamo if the faucet policy changes away from 1 time use only
if user.ReceivedFaucetGrant {
c.JSON(http.StatusForbidden, gin.H{"error": ErrFaucetRepeatAttempt.Error()})
return
}
targetAddrStr := c.Param("target_addr")
minAccountAge := time.Duration(env.FaucetMinAccountAgeDays) * 24 * time.Hour
// No account less than MinAccountAge is allowed any FIL
if !user.HasAccountOlderThan(minAccountAge) {
accountName := user.Accounts["github"].Username
accountAge := user.Accounts["github"].CreatedAt.String()
logger.Errorf("ACCOUNT TOO NEW: User ID %q, FIL Address %q, Account name %q, Account age %q", user.ID, targetAddrStr, accountName, accountAge)
c.JSON(http.StatusForbidden, gin.H{"error": ErrUserTooNew.Error()})
return
}
// Lock the user for the duration of this operation
err = lockUser(userID, UserLock_Faucet)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": ErrUserLocked.Error()})
return
}
user, err = getUserByID(userID)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": ErrStaleJWT.Error()})
return
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
targetAddr, err := address.NewFromString(targetAddrStr)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if isAddressBlocked(targetAddr) {
c.JSON(http.StatusForbidden, gin.H{"error": ErrAddressBlocked.Error()})
return
}
api, closer, err := lotusGetFullNodeAPI(ctx)
if err != nil {
setError(c, http.StatusInternalServerError, errors.Wrap(err, "getting full node API"))
return
}
defer closer()
cid, err := lotusSendFIL(context.TODO(), api, FaucetAddr, targetAddr, env.FaucetGrantSize)
if err != nil {
setError(c, http.StatusInternalServerError, errors.Wrapf(err, "sending %v from %v to %v", env.FaucetGrantSize, FaucetAddr, targetAddr))
return
}
user.MostRecentFaucetGrantCid = cid.String()
user.MostRecentFaucetAddress = targetAddrStr
err = saveUser(user)
if err != nil {
logger.Errorf("ERR FOR NEW RELIC: %v", err)
}
// Respond to the HTTP request
type Response struct {
Cid string `json:"cid"`
Sent string `json:"sent"`
Address string `json:"toAddress"`
}
c.JSON(http.StatusOK, Response{
Cid: cid.String(),
Sent: env.FaucetGrantSize.String(),
Address: targetAddr.String(),
})
}
func getUserIDFromJWT(c *gin.Context) (string, error) {
authHeader := c.GetHeader("Authorization")
if !strings.HasPrefix(authHeader, "Bearer ") {
return "", errors.New("bad Authorization header")
}
jwtToken := strings.TrimSpace(authHeader[len("Bearer "):])
token, err := jwt.Parse(jwtToken, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return []byte(env.JWTSecret), nil
})
if err != nil {
return "", err
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok || !token.Valid {
return "", err
}
userID, ok := claims["userID"].(string)
if !ok {
return "", err
}
return userID, nil
}
func serveResetCounter(c *gin.Context) {
password := c.Param("pwd")
if password != env.AllocationsCounterResetPword {
c.JSON(http.StatusForbidden, gin.H{"error": "Not allowed"})
return
}
if _, err := resetCounter(c); err != nil {
logger.Errorf("REDIS RESET COUNT FAILED: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusAccepted, "")
}
func serveCurrentCount(c *gin.Context) {
password := c.Param("pwd")
if password != env.AllocationsCounterResetPword {
c.JSON(http.StatusForbidden, gin.H{"error": "Not allowed"})
return
}
count, err := getCount(c)
if err != nil {
logger.Errorf("REDIS GET COUNT FAILED: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusAccepted, count)
}