utils.go 794 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package v1
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/crowdsecurity/crowdsec/pkg/database/ent"
  6. "github.com/gin-gonic/gin"
  7. )
  8. var (
  9. bouncerContextKey = "bouncer_info"
  10. )
  11. func getBouncerFromContext(ctx *gin.Context) (*ent.Bouncer, error) {
  12. bouncerInterface, exist := ctx.Get(bouncerContextKey)
  13. if !exist {
  14. return nil, fmt.Errorf("bouncer not found")
  15. }
  16. bouncerInfo, ok := bouncerInterface.(*ent.Bouncer)
  17. if !ok {
  18. return nil, fmt.Errorf("bouncer not found")
  19. }
  20. return bouncerInfo, nil
  21. }
  22. func (c *Controller) AbortRemoteIf(option bool) gin.HandlerFunc {
  23. return func(gctx *gin.Context) {
  24. incomingIP := gctx.ClientIP()
  25. if option && incomingIP != "127.0.0.1" && incomingIP != "::1" {
  26. gctx.JSON(http.StatusForbidden, gin.H{"message": "access forbidden"})
  27. gctx.Abort()
  28. }
  29. }
  30. }