blocker

package
v0.0.0-...-2c17daf Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2021 License: MIT, MIT Imports: 5 Imported by: 0

README

Blocker 请求拦截器

根据指定时间段内,服务器返回的http响应指定状态码出现的频率,对请求进行拦截

使用方法

b:=blocker.New(cache)
//每分钟请求不能超过20次
b.Block(blocker.StatusAny, 20, 1*time.Minute)
//每分钟错误请求(status code >400)不能超过10次
b.Block(blocker.StatusAnyError, 10, 1*time.Minute)
//每分钟404错误不能超过5次
b.Block(404, 5, 1*time.Minute)
//每分钟403错误不能超过5次
b.Block(403, 5, 1*time.Minute)

App.Use(blocker)

设置

任意状态于与任意错误状态

blocker.StatusAny (0) 代表任何状态码

blocker.StatusAnyError(-1) 代表任何大于等于400的状态码

自定义超过限制时的错误码

设置拦截器的 StatusCodeBlocked属性可以指定请求被拦截后的状态码

默认值为429

b:=blocker.New(cache)
b.StatusCodeBlocked=400
自定义请求的标识函数

设置拦截器的 Identifier方法可以设置用什么函数去标识请求. 默认值为通过http请求的remoteAddr中的ip地址来进行标识

b:=blocker.New(cache)
b.Identifier=func(r *http.Request) (string, error) {
	return r.Header.Get("name"), nil
}

Documentation

Index

Constants

View Source
const StatusAny = 0

StatusAny stand for any status when block.

View Source
const StatusAnyError = -1

StatusAnyError stand for any status greater than 400 when block.

Variables

This section is empty.

Functions

func IPIdentifier

func IPIdentifier(r *http.Request) (string, error)

IPIdentifier identify http request by ip address.

Types

type Blocker

type Blocker struct {

	//Cache cache which store blcok data
	Cache cache.Cacheable
	//StatusCodeBlocked error status which will returned when request blcoker.Default value is 429.
	StatusCodeBlocked int
	//Identifier http request identifier
	Identifier func(r *http.Request) (string, error)
	//OnBlock acitons execed when access blocked
	OnBlock func(w http.ResponseWriter, r *http.Request)
	// contains filtered or unexported fields
}

Blocker blocker struct.

func New

func New(cache cache.Cacheable) *Blocker

New create blocker with given cache and http request udentifier

func (*Blocker) Block

func (b *Blocker) Block(status int, max int64, ttl time.Duration)

Block block config method. Requester request for morethan param max request which response staus is param status in param ttl will be blocked.

func (*Blocker) DefaultBlockAction

func (b *Blocker) DefaultBlockAction(w http.ResponseWriter, r *http.Request)

DefaultBlockAction default block

func (*Blocker) ServeMiddleware

func (b *Blocker) ServeMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

ServeMiddleware serve blocker as a middleware.

type Rule

type Rule struct {
	StatusCode       int
	Limit            int64
	DurationInSecond int64
}

Rule blocker block rule

func (*Rule) ApplyTo

func (r *Rule) ApplyTo(b *Blocker) error

ApplyTo apply block rule to blocker

type Rules

type Rules []*Rule

Rules blocker block rule list

func NewRules

func NewRules() *Rules

NewRules create new rule list

func (*Rules) ApplyTo

func (r *Rules) ApplyTo(b *Blocker) error

ApplyTo apply rule list to blocker

Jump to

Keyboard shortcuts

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