front

package module
v0.1.20240309181159 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2024 License: MIT Imports: 27 Imported by: 0

README

front

这是一个简单的http/https/ws/wss均衡负载、故障转移路由/代理,通过定时刷新配置不中断地:

  • 动态后端
  • 路径继承
  • 自定权重
  • 故障转移
  • 自定义头
  • 请求头过滤
  • 请求数据过滤

支持嵌入到其他项目中/独立运行

配置为json数组格式[],下面为数组中的其中一个{},注意此级不会动态增加/移除

config:

  • addr: string 监听端口 例:0.0.0.0:8081
  • matchRule: string 匹配规则 prefix:当未匹配到时,返回最近的/匹配, all:当未匹配到时,返回404
  • copyBlocks: int 转发的块数量,默认1000
  • tls: {} 启用tls
    • pub: string 公钥pem路径
    • key: string 私钥pem路径
  • routes: [] 路由, 可以动态增加/删除
    • path: string 路径
    • splicing: int 当客户端支持cookie时,将会固定使用后端多少秒
    • pathAdd: bool 将客户端访问的路径附加在path上 例:/api/req => /ws => /ws/api/req
    • matcher 将会附加到每个backs前
    • backs: [] 后端, 可以动态增加/删除
      • name: string 后端名称,将在日志中显示
      • to: string 后端地址,例"s://www.baidu.com",会根据客户端自动添加http or ws在地址前
      • weight: int 权重,按routes中的全部back的权重比分配,当权重变为0时,将停止新请求的进入
      • errBanSec: int 当后端错误时(指连接失败,不指后端错误响应),将会禁用若干秒
      • matcher

matcher:

  • reqHeader: [] 请求后端前,请求头处理器, 可以动态增加/删除
    • action: string 可选access、deny、replace、add、del、set。
    • key: string 具体处理哪个头
    • matchExp: string access时不匹配将结束请求。deny时匹配将结束请求。replace时结合value进行替换
    • value: string replace时结合matchExp进行替换。add时将附加值。set时将覆盖值。
  • resHeader: [] 返回后端的响应前,请求头处理器, 可以动态增加/删除
    • action: string 可选access、deny、add、del、set。
    • key: string 具体处理哪个头
    • matchExp: string access时不匹配将结束请求。deny时匹配将结束请求。replace时结合value进行替换
    • value: string replace时结合matchExp进行替换。add时将附加值。set时将覆盖值。
  • reqBody: [] 请求后端前,请求数据过滤器, 可以动态增加/删除
    • action: string 可选access、deny。
    • reqSize:string 限定请求数据大小,默认为"1M"
    • matchExp: string access时如不匹配将结束请求。deny时如匹配将结束请求。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRedirect        = errors.New("ErrRedirect")
	ErrNoHttp          = errors.New("ErrNoHttp")
	ErrNoWs            = errors.New("ErrNoWs")
	ErrCopy            = errors.New("ErrCopy")
	ErrReqCreFail      = errors.New("ErrReqCreFail")
	ErrReqDoFail       = errors.New("ErrReqDoFail")
	ErrResDoFail       = errors.New("ErrResDoFail")
	ErrHeaderCheckFail = errors.New("ErrHeaderCheckFail")
	ErrBodyCheckFail   = errors.New("ErrBodyCheckFail")
)

Functions

func BodyMatchs added in v0.1.20240305172524

func BodyMatchs(matchBody []Body, r *http.Request) (reader io.ReadCloser, e error)

func DialContext added in v0.1.20231214143418

func DialContext(ctx context.Context, urlStr string, requestHeader http.Header) (net.Conn, *http.Response, error)

func HeaderMatchs added in v0.1.20240305172524

func HeaderMatchs(matchHeader []Header, r *http.Request) bool

func LoadPeriod

func LoadPeriod(ctx context.Context, buf []byte, configF File, configS *[]Config, logger Logger) error

加载

func Test

func Test(ctx context.Context, port int, logger Logger)

测试

func Upgrade added in v0.1.20231214143418

func Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (net.Conn, error)

Types

type Back

type Back struct {
	Name      string `json:"name"`
	To        string `json:"to"`
	Weight    int    `json:"weight"`
	ErrBanSec int    `json:"errBanSec"`

	Splicing int  `json:"-"`
	PathAdd  bool `json:"-"`
	Matcher
	// contains filtered or unexported fields
}

func (*Back) Disable added in v0.1.20231203025759

func (t *Back) Disable()

func (*Back) Id added in v0.1.20240305005058

func (t *Back) Id() string

func (*Back) IsLive added in v0.1.20231203025759

func (t *Back) IsLive() bool

type Body added in v0.1.20240305172524

type Body struct {
	Action   string `json:"action"`
	ReqSize  string `json:"reqSize"`
	MatchExp string `json:"matchExp"`
}

func (*Body) Match added in v0.1.20240305172524

func (t *Body) Match(r io.ReadCloser) (d io.ReadCloser, err error)

type Config

type Config struct {
	Addr string `json:"addr"`
	TLS  struct {
		Pub string `json:"pub"`
		Key string `json:"key"`
	} `json:"tls"`
	MatchRule  string               `json:"matchRule"`
	CopyBlocks int                  `json:"copyBlocks"`
	BlocksI    pslice.BlocksI[byte] `json:"-"`

	Routes []Route `json:"routes"`
	// contains filtered or unexported fields
}

func (*Config) Run added in v0.1.20240305005058

func (t *Config) Run(ctx context.Context, logger Logger)

func (*Config) SwapSign added in v0.1.20240303164122

func (t *Config) SwapSign(ctx context.Context, logger Logger)

type File added in v0.1.20231209073216

type File interface {
	Read(data []byte) (int, error)
}

type HandshakeError added in v0.1.20231214143418

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

func (HandshakeError) Error added in v0.1.20231214143418

func (t HandshakeError) Error() string
type Header struct {
	Action   string `json:"action"`
	Key      string `json:"key"`
	MatchExp string `json:"matchExp"`
	Value    string `json:"value"`
}

func (*Header) Match added in v0.1.20240305172524

func (t *Header) Match(value string) bool

type Logger added in v0.1.20231209073216

type Logger interface {
	Error(msg string, args ...any)
	Debug(msg string, args ...any)
	Info(msg string, args ...any)
	Warn(msg string, args ...any)
}

type Matcher added in v0.1.20240305172524

type Matcher struct {
	ReqHeader []Header `json:"reqHeader"`
	ResHeader []Header `json:"resHeader"`
	ReqBody   []Body   `json:"reqBody"`
}

type Route

type Route struct {
	Path string `json:"path"`

	Splicing int  `json:"splicing"`
	PathAdd  bool `json:"pathAdd"`
	Matcher

	Backs []Back `json:"backs"`
	// contains filtered or unexported fields
}

func (*Route) FiliterBackByRequest added in v0.1.20240305005058

func (t *Route) FiliterBackByRequest(r *http.Request) []*Back

func (*Route) Id added in v0.1.20240305172524

func (t *Route) Id() string

func (*Route) SwapSign

func (t *Route) SwapSign(add func(string, *Back), del func(string, *Back), logger Logger)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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