fiberpow

package module
v0.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2024 License: Apache-2.0 Imports: 11 Imported by: 1

README

fiberpow

Fiberpow is a Fiber middleware, it aims to block (or at least slow down) bots, by periodically asking clients for a proof of work challenge.

Config explaination

Difficulty
int

Maximum number of calculated hashes by the client, default: 30000.

PowInterval
time.Duration

Interval between challenges for the same IP.

Filter
func(c *fiber.Ctx) bool

Use this if you need to skip the PoW challenge in certain conditions, true equals skip.

Storage
fiber.Storage

Database used to keep track of challenges, for reference use https://github.com/gofiber/storage.

Installation

go get github.com/witer33/fiberpow

Usage

Basic config
import (
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/storage/redis/v3"
	"github.com/witer33/fiberpow"
)

func main() {
	app := fiber.New()

	app.Use(fiberpow.New(fiberpow.Config{
		Storage: redis.New(),
	}))

	app.Get("/", func(c *fiber.Ctx) error {
		return c.SendString("Hello World")
	})

	app.Listen(":3000")
}
Custom config
import (
	"time"

	"github.com/gofiber/fiber/v2"
	"github.com/witer33/fiberpow"
	"github.com/gofiber/storage/redis/v3"
)

func main() {
	app := fiber.New()

	app.Use(fiberpow.New(fiberpow.Config{
		PowInterval: 10 * time.Minute,
		Difficulty:  60000,
		Filter: func(c *fiber.Ctx) bool {
			return c.IP() == "127.0.0.1"
		},
		Storage: redis.New(),
	}))

	app.Get("/", func(c *fiber.Ctx) error {
		return c.SendString("Hello World")
	})

	app.Listen(":3000")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(config ...Config) fiber.Handler

New initialize the middleware with the default config or a custom one.

Types

type Config

type Config struct {
	Filter      func(*fiber.Ctx) bool
	PowInterval time.Duration
	Difficulty  int
	Storage     fiber.Storage
}

Config is the Middleware Config.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL