shared

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2020 License: MIT, MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	RegexpDigitNumber    = regexp.MustCompile(`^\d+$`)                    // 正整数
	RegexpFloatNumber    = regexp.MustCompile(`^\d+(\.\d+)?$`)            // 正浮点数,不支持e
	RegexpAllDigitNumber = regexp.MustCompile(`^[+-]?\d+$`)               // 整数,支持正负数
	RegexpAllFloatNumber = regexp.MustCompile(`^[+-]?\d+(\.\d+)?$`)       // 浮点数,支持正负数,不支持e
	RegexpExternalURL    = regexp.MustCompile("(?i)^(http|https|ftp)://") // URL
	RegexpNamedVariable  = regexp.MustCompile("\\${[\\w.-]+}")            // 命名变量
)

常用的正则表达式

View Source
var DefaultSkippedResponseCacheControlValues = []string{"private", "no-cache", "no-store"}
View Source
var Locker = new(FileLocker)

Functions

func AllRequestOperators

func AllRequestOperators() []maps.Map

所有的运算符

Types

type AccessConfig

type AccessConfig struct {
	On      bool            `yaml:"on" json:"on"`           // 是否开启
	AllowOn bool            `yaml:"allowOn" json:"allowOn"` // 白名单是否开启
	DenyOn  bool            `yaml:"denyOn" json:"denyOn"`   // 黑名单是否开启
	Allow   []*ClientConfig `yaml:"allow" json:"allow"`     // 允许的IP
	Deny    []*ClientConfig `yaml:"deny" json:"deny"`       // 禁止的IP
}

访问控制

func (*AccessConfig) AddAllow

func (this *AccessConfig) AddAllow(client *ClientConfig)

添加允许的客户端

func (*AccessConfig) AddDeny

func (this *AccessConfig) AddDeny(client *ClientConfig)

添加禁用的客户端

func (*AccessConfig) Validate

func (this *AccessConfig) Validate() error

校验

type AccessPolicy

type AccessPolicy struct {

	// 流量控制
	Traffic struct {
		On    bool `yaml:"on" json:"on"` // 是否开启
		Total struct {
			On    bool  `yaml:"on" json:"on"`       // 是否开启
			Total int64 `yaml:"total" json:"total"` // 总量
			Used  int64 `yaml:"used" json:"used"`   // 已使用量
		} `yaml:"total" json:"total"` // 总量控制
		Second struct {
			On       bool  `yaml:"on" json:"on"`             // 是否开启
			Total    int64 `yaml:"total" json:"total"`       // 总量
			Duration int64 `yaml:"duration" json:"duration"` // 时间长度
			FromTime int64 `yaml:"fromTime" json:"fromTime"` // 开始时间,也是结束时间
			Used     int64 `yaml:"used" json:"used"`         // 已使用量
		} `yaml:"second" json:"second"`
		Minute struct {
			On       bool  `yaml:"on" json:"on"`             // 是否开启
			Total    int64 `yaml:"total" json:"total"`       // 总量
			Duration int64 `yaml:"duration" json:"duration"` // 时间长度
			FromTime int64 `yaml:"fromTime" json:"fromTime"` // 开始时间
			ToTime   int64 `yaml:"toTime" json:"toTime"`     // 结束时间
			Used     int64 `yaml:"used" json:"used"`         // 已使用量
		} `yaml:"minute" json:"minute"`
		Hour struct {
			On       bool  `yaml:"on" json:"on"`             // 是否开启
			Total    int64 `yaml:"total" json:"total"`       // 总量
			Duration int64 `yaml:"duration" json:"duration"` // 时间长度
			FromTime int64 `yaml:"fromTime" json:"fromTime"` // 开始时间
			ToTime   int64 `yaml:"toTime" json:"toTime"`     // 结束时间
			Used     int64 `yaml:"used" json:"used"`         // 已使用量
		} `yaml:"hour" json:"hour"`
		Day struct {
			On       bool  `yaml:"on" json:"on"`             // 是否开启
			Total    int64 `yaml:"total" json:"total"`       // 总量
			Duration int64 `yaml:"duration" json:"duration"` // 时间长度
			FromTime int64 `yaml:"fromTime" json:"fromTime"` // 开始时间
			ToTime   int64 `yaml:"toTime" json:"toTime"`     // 结束时间
			Used     int64 `yaml:"used" json:"used"`         // 已使用量
		} `yaml:"day" json:"day"`
		Month struct {
			On       bool  `yaml:"on" json:"on"`             // 是否开启
			Total    int64 `yaml:"total" json:"total"`       // 总量
			Duration int64 `yaml:"duration" json:"duration"` // 时间长度
			FromTime int64 `yaml:"fromTime" json:"fromTime"` // 开始时间
			ToTime   int64 `yaml:"toTime" json:"toTime"`     // 结束时间
			Used     int64 `yaml:"used" json:"used"`         // 已使用量
		} `yaml:"month" json:"month"`
	} `yaml:"traffic" json:"traffic"` // 流量控制

	// 访问控制
	Access AccessConfig `yaml:"access" json:"access"` // 访问控制
	// contains filtered or unexported fields
}

API控制策略

func NewAccessPolicy

func NewAccessPolicy() *AccessPolicy

获取新对象

func (*AccessPolicy) AllowAccess

func (this *AccessPolicy) AllowAccess(ip string) bool

检查权限

func (*AccessPolicy) AllowTraffic

func (this *AccessPolicy) AllowTraffic() (reason string, allowed bool)

检查流量

func (*AccessPolicy) FinishChange

func (this *AccessPolicy) FinishChange()

设置已完成改变

func (*AccessPolicy) IncreaseTraffic

func (this *AccessPolicy) IncreaseTraffic()

增加流量

func (*AccessPolicy) IsChanged

func (this *AccessPolicy) IsChanged() bool

判断是否改变

func (*AccessPolicy) Validate

func (this *AccessPolicy) Validate() error

校验

type CachePolicy

type CachePolicy struct {
	Filename string `yaml:"filename" json:"filename"` // 文件名
	Id       string `yaml:"id" json:"id"`
	On       bool   `yaml:"on" json:"on"`     // 是否开启 TODO
	Name     string `yaml:"name" json:"name"` // 名称

	Key      string `yaml:"key" json:"key"`           // 每个缓存的Key规则,里面可以有变量
	Capacity string `yaml:"capacity" json:"capacity"` // 最大内容容量
	Life     string `yaml:"life" json:"life"`         // 时间
	Status   []int  `yaml:"status" json:"status"`     // 缓存的状态码列表
	MaxSize  string `yaml:"maxSize" json:"maxSize"`   // 能够请求的最大尺寸

	SkipResponseCacheControlValues []string `yaml:"skipCacheControlValues" json:"skipCacheControlValues"`     // 可以跳过的响应的Cache-Control值
	SkipResponseSetCookie          bool     `yaml:"skipSetCookie" json:"skipSetCookie"`                       // 是否跳过响应的Set-Cookie Header
	EnableRequestCachePragma       bool     `yaml:"enableRequestCachePragma" json:"enableRequestCachePragma"` // 是否支持客户端的Pragma: no-cache

	Cond []*RequestCond `yaml:"cond" json:"cond"`

	Type    string                 `yaml:"type" json:"type"`       // 类型
	Options map[string]interface{} `yaml:"options" json:"options"` // 选项
	// contains filtered or unexported fields
}

缓存策略配置

func NewCachePolicy

func NewCachePolicy() *CachePolicy

获取新对象

func NewCachePolicyFromFile

func NewCachePolicyFromFile(file string) *CachePolicy

从文件中读取缓存策略

func (*CachePolicy) CapacitySize

func (this *CachePolicy) CapacitySize() float64

容量

func (*CachePolicy) ContainsCacheControl

func (this *CachePolicy) ContainsCacheControl(value string) bool

是否包含某个Cache-Control值

func (*CachePolicy) Delete

func (this *CachePolicy) Delete() error

删除

func (*CachePolicy) LifeDuration

func (this *CachePolicy) LifeDuration() time.Duration

生命周期

func (*CachePolicy) MatchKeyword

func (this *CachePolicy) MatchKeyword(keyword string) (matched bool, name string, tags []string)

检查是否匹配关键词

func (*CachePolicy) MaxDataSize

func (this *CachePolicy) MaxDataSize() float64

最大数据尺寸

func (*CachePolicy) Save

func (this *CachePolicy) Save() error

保存

func (*CachePolicy) Validate

func (this *CachePolicy) Validate() error

校验

type ClientConfig

type ClientConfig struct {
	Id          string `yaml:"id" json:"id"`                   // ID
	On          bool   `yaml:"on" json:"on"`                   // 是否开启
	IP          string `yaml:"ip" json:"ip"`                   // IP
	Description string `yaml:"description" json:"description"` // 描述
	// contains filtered or unexported fields
}

客户端配置

func NewClientConfig

func NewClientConfig() *ClientConfig

取得新配置对象

func (*ClientConfig) Match

func (this *ClientConfig) Match(ip string) bool

判断是否匹配某个IP

func (*ClientConfig) Validate

func (this *ClientConfig) Validate() error

校验

type FileLocker

type FileLocker struct {
	// contains filtered or unexported fields
}

global file modify locker

func (*FileLocker) Lock

func (this *FileLocker) Lock()

lock

func (*FileLocker) ReadUnlock

func (this *FileLocker) ReadUnlock()

unlock for read

func (*FileLocker) WriteUnlock

func (this *FileLocker) WriteUnlock()

unlock for write

func (*FileLocker) WriteUnlockNotify

func (this *FileLocker) WriteUnlockNotify()

unlock for write and notify

type HeaderConfig

type HeaderConfig struct {
	On     bool   `yaml:"on" json:"on"`         // 是否开启
	Id     string `yaml:"id" json:"id"`         // ID
	Name   string `yaml:"name" json:"name"`     // Name
	Value  string `yaml:"value" json:"value"`   // Value
	Always bool   `yaml:"always" json:"always"` // 是否忽略状态码
	Status []int  `yaml:"status" json:"status"` // 支持的状态码
	// contains filtered or unexported fields
}

头部信息定义

func NewHeaderConfig

func NewHeaderConfig() *HeaderConfig

获取新Header对象

func (*HeaderConfig) HasVariables

func (this *HeaderConfig) HasVariables() bool

是否有变量

func (*HeaderConfig) Match

func (this *HeaderConfig) Match(statusCode int) bool

判断是否匹配状态码

func (*HeaderConfig) Validate

func (this *HeaderConfig) Validate() error

校验

type HeaderList

type HeaderList struct {
	// 添加的响应Headers
	Headers []*HeaderConfig `yaml:"headers" json:"headers"`

	// 忽略的响应Headers
	IgnoreHeaders []string `yaml:"ignoreHeaders" json:"ignoreHeaders"`

	// 自定义请求Headers
	RequestHeaders []*HeaderConfig `yaml:"requestHeaders" json:"requestHeaders"`
	// contains filtered or unexported fields
}

HeaderList定义

func (*HeaderList) AddIgnoreResponseHeader

func (this *HeaderList) AddIgnoreResponseHeader(name string)

添加IgnoreHeader

func (*HeaderList) AddRequestHeader

func (this *HeaderList) AddRequestHeader(header *HeaderConfig)

添加请求Header

func (*HeaderList) AddResponseHeader

func (this *HeaderList) AddResponseHeader(header *HeaderConfig)

添加Header

func (*HeaderList) AllIgnoreResponseHeaders

func (this *HeaderList) AllIgnoreResponseHeaders() []string

取得所有的IgnoreHeader

func (*HeaderList) AllRequestHeaders

func (this *HeaderList) AllRequestHeaders() []*HeaderConfig

取得所有的请求Header

func (*HeaderList) AllResponseHeaders

func (this *HeaderList) AllResponseHeaders() []*HeaderConfig

取得所有的Header

func (*HeaderList) ContainsIgnoreResponseHeader

func (this *HeaderList) ContainsIgnoreResponseHeader(name string) bool

判断是否包含IgnoreHeader

func (*HeaderList) ContainsResponseHeader

func (this *HeaderList) ContainsResponseHeader(name string) bool

判断是否包含Header

func (*HeaderList) FindRequestHeader

func (this *HeaderList) FindRequestHeader(headerId string) *HeaderConfig

查找请求Header

func (*HeaderList) FindResponseHeader

func (this *HeaderList) FindResponseHeader(headerId string) *HeaderConfig

查找Header

func (*HeaderList) HasIgnoreHeaders

func (this *HeaderList) HasIgnoreHeaders() bool

判断是否有Ignore Headers

func (*HeaderList) HasRequestHeaders

func (this *HeaderList) HasRequestHeaders() bool

判断是否有请求Header

func (*HeaderList) HasResponseHeaders

func (this *HeaderList) HasResponseHeaders() bool

是否有Headers

func (*HeaderList) RemoveIgnoreResponseHeader

func (this *HeaderList) RemoveIgnoreResponseHeader(name string)

移除IgnoreHeader

func (*HeaderList) RemoveRequestHeader

func (this *HeaderList) RemoveRequestHeader(headerId string)

移除请求Header

func (*HeaderList) RemoveResponseHeader

func (this *HeaderList) RemoveResponseHeader(headerId string)

移除Header

func (*HeaderList) UpdateIgnoreResponseHeader

func (this *HeaderList) UpdateIgnoreResponseHeader(oldName string, newName string)

修改IgnoreHeader

func (*HeaderList) UppercaseIgnoreHeaders

func (this *HeaderList) UppercaseIgnoreHeaders() []string

查找大写的Ignore Headers

func (*HeaderList) ValidateHeaders

func (this *HeaderList) ValidateHeaders() error

校验

type HeaderListInterface

type HeaderListInterface interface {
	// 校验
	ValidateHeaders() error

	// 取得所有的IgnoreHeader
	AllIgnoreResponseHeaders() []string

	// 添加IgnoreHeader
	AddIgnoreResponseHeader(name string)

	// 判断是否包含IgnoreHeader
	ContainsIgnoreResponseHeader(name string) bool

	// 移除IgnoreHeader
	RemoveIgnoreResponseHeader(name string)

	// 修改IgnoreHeader
	UpdateIgnoreResponseHeader(oldName string, newName string)

	// 取得所有的Header
	AllResponseHeaders() []*HeaderConfig

	// 添加Header
	AddResponseHeader(header *HeaderConfig)

	// 判断是否包含Header
	ContainsResponseHeader(name string) bool

	// 查找Header
	FindResponseHeader(headerId string) *HeaderConfig

	// 移除Header
	RemoveResponseHeader(headerId string)

	// 取得所有的请求Header
	AllRequestHeaders() []*HeaderConfig

	// 添加请求Header
	AddRequestHeader(header *HeaderConfig)

	// 查找请求Header
	FindRequestHeader(headerId string) *HeaderConfig

	// 移除请求Header
	RemoveRequestHeader(headerId string)
}

HeaderList相关操作接口

type IPRangeConfig

type IPRangeConfig struct {
	Id string `yaml:"id" json:"id"`

	Type IPRangeType `yaml:"type" json:"type"`

	Param  string `yaml:"param" json:"param"`
	CIDR   string `yaml:"cidr" json:"cidr"`
	IPFrom string `yaml:"ipFrom" json:"ipFrom"`
	IPTo   string `yaml:"ipTo" json:"ipTo"`
	// contains filtered or unexported fields
}

IP Range

func NewIPRangeConfig

func NewIPRangeConfig() *IPRangeConfig

获取新对象

func ParseIPRange

func ParseIPRange(s string) (*IPRangeConfig, error)

从字符串中分析

func (*IPRangeConfig) Contains

func (this *IPRangeConfig) Contains(ipString string) bool

是否包含某个IP

func (*IPRangeConfig) Validate

func (this *IPRangeConfig) Validate() error

校验

type IPRangeType

type IPRangeType = int

IP Range类型

const (
	IPRangeTypeRange    IPRangeType = 1
	IPRangeTypeCIDR     IPRangeType = 2
	IPRangeTypeAll      IPRangeType = 3
	IPRangeTypeWildcard IPRangeType = 4 // 通配符,可以使用*
)

type RequestCall

type RequestCall struct {
	Formatter         func(source string) string
	Request           *http.Request
	ResponseCallbacks []func(resp http.ResponseWriter)
	Options           maps.Map
}

请求调用

func NewRequestCall

func NewRequestCall() *RequestCall

获取新对象

func (*RequestCall) AddResponseCall

func (this *RequestCall) AddResponseCall(callback func(resp http.ResponseWriter))

添加响应回调

func (*RequestCall) CallResponseCallbacks

func (this *RequestCall) CallResponseCallbacks(resp http.ResponseWriter)

执行响应回调

func (*RequestCall) Reset

func (this *RequestCall) Reset()

重置

type RequestCond

type RequestCond struct {
	Id string `yaml:"id" json:"id"` // ID

	// 要测试的字符串
	// 其中可以使用跟请求相关的参数,比如:
	// ${arg.name}, ${requestPath}
	Param string `yaml:"param" json:"param"`

	// 运算符
	Operator RequestCondOperator `yaml:"operator" json:"operator"`

	// 对比
	Value string `yaml:"value" json:"value"`
	// contains filtered or unexported fields
}

重写条件定义

func NewRequestCond

func NewRequestCond() *RequestCond

取得新对象

func (*RequestCond) Match

func (this *RequestCond) Match(formatter func(source string) string) bool

将此条件应用于请求,检查是否匹配

func (*RequestCond) Validate

func (this *RequestCond) Validate() error

校验配置

type RequestCondOperator

type RequestCondOperator = string

运算符定义

const (
	// 正则
	RequestCondOperatorRegexp    RequestCondOperator = "regexp"
	RequestCondOperatorNotRegexp RequestCondOperator = "not regexp"

	// 数字相关
	RequestCondOperatorEqInt    RequestCondOperator = "eq int"   // 整数等于
	RequestCondOperatorEqFloat  RequestCondOperator = "eq float" // 浮点数等于
	RequestCondOperatorGtFloat  RequestCondOperator = "gt"
	RequestCondOperatorGteFloat RequestCondOperator = "gte"
	RequestCondOperatorLtFloat  RequestCondOperator = "lt"
	RequestCondOperatorLteFloat RequestCondOperator = "lte"

	// 取模
	RequestCondOperatorMod10  RequestCondOperator = "mod 10"
	RequestCondOperatorMod100 RequestCondOperator = "mod 100"
	RequestCondOperatorMod    RequestCondOperator = "mod"

	// 字符串相关
	RequestCondOperatorEqString          RequestCondOperator = "eq"
	RequestCondOperatorNeqString         RequestCondOperator = "not"
	RequestCondOperatorHasPrefix         RequestCondOperator = "prefix"
	RequestCondOperatorHasSuffix         RequestCondOperator = "suffix"
	RequestCondOperatorContainsString    RequestCondOperator = "contains"
	RequestCondOperatorNotContainsString RequestCondOperator = "not contains"
	RequestCondOperatorIn                RequestCondOperator = "in"
	RequestCondOperatorNotIn             RequestCondOperator = "not in"
	RequestCondOperatorFileExt           RequestCondOperator = "file ext"
	RequestCondOperatorFileMimeType      RequestCondOperator = "mime type"
	RequestCondOperatorVersionRange      RequestCondOperator = "version range"

	// IP相关
	RequestCondOperatorEqIP     RequestCondOperator = "eq ip"
	RequestCondOperatorGtIP     RequestCondOperator = "gt ip"
	RequestCondOperatorGteIP    RequestCondOperator = "gte ip"
	RequestCondOperatorLtIP     RequestCondOperator = "lt ip"
	RequestCondOperatorLteIP    RequestCondOperator = "lte ip"
	RequestCondOperatorIPRange  RequestCondOperator = "ip range"
	RequestCondOperatorIPMod10  RequestCondOperator = "ip mod 10"
	RequestCondOperatorIPMod100 RequestCondOperator = "ip mod 100"
	RequestCondOperatorIPMod    RequestCondOperator = "ip mod"

	// 文件相关
	RequestCondOperatorFileExist    RequestCondOperator = "file exist"
	RequestCondOperatorFileNotExist RequestCondOperator = "file not exist"
)

type Variable

type Variable struct {
	Name  string `yaml:"name" json:"name"`   // 变量名
	Value string `yaml:"value" json:"value"` // 变量值
}

变量

func NewVariable

func NewVariable(name string, value string) *Variable

创建新变量

Jump to

Keyboard shortcuts

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