Skip to content

Commit

Permalink
Merge pull request #487 from getAlby/limit-logs
Browse files Browse the repository at this point in the history
Add logs to outgoing/incoming limit checks
  • Loading branch information
bumi authored Jun 7, 2024
2 parents 8780420 + 806e847 commit ba5057c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
49 changes: 39 additions & 10 deletions lib/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/security"
"github.com/getAlby/lndhub.go/lnd"
"github.com/getsentry/sentry-go"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/uptrace/bun"
Expand Down Expand Up @@ -138,7 +137,15 @@ func (svc *LndhubService) CheckOutgoingPaymentAllowed(c echo.Context, lnpayReq *
limits := svc.GetLimits(c)
if limits.MaxSendAmount >= 0 {
if lnpayReq.PayReq.NumSatoshis > limits.MaxSendAmount {
svc.Logger.Errorf("Max send amount exceeded for user_id %v (amount:%v)", userId, lnpayReq.PayReq.NumSatoshis)
svc.Logger.Warnj(
log.JSON{
"message": "max send amount exceeded",
"user_id": userId,
"lndhub_user_id": userId,
"amount": lnpayReq.PayReq.NumSatoshis,
"limit": limits.MaxSendAmount,
},
)
return &responses.SendExceededError, nil
}
}
Expand All @@ -156,13 +163,14 @@ func (svc *LndhubService) CheckOutgoingPaymentAllowed(c echo.Context, lnpayReq *
return nil, err
}
if volume > limits.MaxSendVolume {
svc.Logger.Errorj(
svc.Logger.Warnj(
log.JSON{
"message": "transaction volume exceeded",
"lndhub_user_id": userId,
"message": "max send volume exceeded",
"lndhub_user_id": userId,
"volume": volume,
"limit": limits.MaxSendVolume,
},
)
sentry.CaptureMessage(fmt.Sprintf("transaction volume exceeded for user %d", userId))
return &responses.TooMuchVolumeError, nil
}
}
Expand Down Expand Up @@ -197,7 +205,15 @@ func (svc *LndhubService) CheckIncomingPaymentAllowed(c echo.Context, amount, us
limits := svc.GetLimits(c)
if limits.MaxReceiveAmount >= 0 {
if amount > limits.MaxReceiveAmount {
svc.Logger.Errorf("Max receive amount exceeded for user_id %d", userId)
svc.Logger.Warnj(
log.JSON{
"message": "max receive amount exceeded",
"user_id": userId,
"lndhub_user_id": userId,
"amount": amount,
"limit": limits.MaxReceiveAmount,
},
)
return &responses.ReceiveExceededError, nil
}
}
Expand All @@ -215,8 +231,14 @@ func (svc *LndhubService) CheckIncomingPaymentAllowed(c echo.Context, amount, us
return nil, err
}
if volume > limits.MaxReceiveVolume {
svc.Logger.Errorf("Transaction volume exceeded for user_id %d", userId)
sentry.CaptureMessage(fmt.Sprintf("transaction volume exceeded for user %d", userId))
svc.Logger.Warnj(
log.JSON{
"message": "max receive volume exceeded",
"lndhub_user_id": userId,
"volume": volume,
"limit": limits.MaxReceiveVolume,
},
)
return &responses.TooMuchVolumeError, nil
}
}
Expand All @@ -234,7 +256,14 @@ func (svc *LndhubService) CheckIncomingPaymentAllowed(c echo.Context, amount, us
return nil, err
}
if currentBalance+amount > limits.MaxAccountBalance {
svc.Logger.Errorf("Max account balance exceeded for user_id %d", userId)
svc.Logger.Warnj(
log.JSON{
"message": "max balance exceeded",
"lndhub_user_id": userId,
"new_balance": currentBalance + amount,
"limit": limits.MaxAccountBalance,
},
)
return &responses.BalanceExceededError, nil
}
}
Expand Down
24 changes: 19 additions & 5 deletions lib/tokens/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/golang-jwt/jwt"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
)

type jwtCustomClaims struct {
Expand Down Expand Up @@ -42,12 +43,25 @@ func Middleware(secret []byte) echo.MiddlewareFunc {
config.SuccessHandler = func(c echo.Context) {
token := c.Get("UserJwt").(*jwt.Token)
claims := token.Claims.(*jwtCustomClaims)
c.Logger().Warnj(log.JSON{
"msg": "JWT details",
"UserID": claims.ID,
"MaxSendVolume": claims.MaxSendVolume,
"MaxSendAmount": claims.MaxSendAmount,
"MaxReceiveVolume": claims.MaxReceiveVolume,
"MaxReceiveAmount": claims.MaxReceiveAmount,
"MaxAccountBalance": claims.MaxAccountBalance,
})
c.Set("UserID", claims.ID)
c.Set("MaxSendVolume", claims.MaxSendVolume)
c.Set("MaxSendAmount", claims.MaxSendAmount)
c.Set("MaxReceiveVolume", claims.MaxReceiveVolume)
c.Set("MaxReceiveAmount", claims.MaxReceiveAmount)
c.Set("MaxAccountBalance", claims.MaxAccountBalance)
// enable it only for getalbycom calls
// there might still be old tokens out there that have these set to 0 (which meant disabled)
if claims.Issuer == "getalbycom" {
c.Set("MaxSendVolume", claims.MaxSendVolume)
c.Set("MaxSendAmount", claims.MaxSendAmount)
c.Set("MaxReceiveVolume", claims.MaxReceiveVolume)
c.Set("MaxReceiveAmount", claims.MaxReceiveAmount)
c.Set("MaxAccountBalance", claims.MaxAccountBalance)
}
// pass UserID to sentry for exception notifications
if hub := sentryecho.GetHubFromContext(c); hub != nil {
hub.Scope().SetUser(sentry.User{ID: strconv.FormatInt(claims.ID, 10)})
Expand Down

0 comments on commit ba5057c

Please sign in to comment.