quantitylimiter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2021 License: MIT Imports: 8 Imported by: 0

README

Request Quantity Limiter for Caddy

The caddy-quantity-limiter module for Caddy allows to limit the number of requests for a specified token on resources and only allow requests after the token has been set.

This pattern makes it possible to enable requests from an external event and automatically block again after a configurable amount of requests.

Configuration

quantity_limiter [<matcher>] {
  parameterNamePrefix <prefix>
  quantity <quantity>
}

All options are optional:

  • matcher according to matcher
  • parameterNamePrefix prefix for token parameter (default: ql_)
  • quantity number of allowed requests for a token after set (default: 1)

The module is unordered by default and needs to be ordered using the global option

{
    order quantity_limiter before file_server
}

or be used inside a route block.

Example Caddyfile

{
  order quantity_limiter before file_server
}

:8080 {
  log
  file_server
  quantity_limiter
}

Usage

Without defining any of the modules parameters (default: ql_set and ql_get) there is no special behaviour of the module - it will pass through all requests.

Setting

To set a token's counter to quantity a GET request can be issued on any resource matched by the module (all by default). For example /does/not/need/to/exist?ql_set=token. This will set the counter token to the number of the quantity option and return status 202.

Getting

To limit the requests the GET parameter ql_get (default) is used. For example /existing/file.ext?ql_get=token. This will request the file /existing/file.ext but will only be returned, if the token has prior been set and requests are left on the counter (initialized with quantity). After the amount of remaining requests is 0, the token is deleted and further requests will return the status code 404.

Building

See xcaddy, short:

xcaddy build \
  --with github.com/cubic3d/caddy-quantity-limiter

or download from https://caddyserver.com/download by selecting this module.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type QuantityLimiter

type QuantityLimiter struct {

	// Prefix to be used for GET parameters for set and get tokens.
	ParameterNamePrefix string `json:"parameterNamePrefix,omitempty"`
	// Number of successful requests that can be made using a token.
	Quantity uint64 `json:"quantity,omitempty"`
	// contains filtered or unexported fields
}

QuantityLimiter limits the number of successful requests for a token and allows the counter to be reset.

func (QuantityLimiter) CaddyModule

func (QuantityLimiter) CaddyModule() caddy.ModuleInfo

func (*QuantityLimiter) Provision

func (ql *QuantityLimiter) Provision(ctx caddy.Context) error

func (QuantityLimiter) ServeHTTP

func (*QuantityLimiter) UnmarshalCaddyfile

func (ql *QuantityLimiter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error

Jump to

Keyboard shortcuts

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