front

package module
v0.1.20241122070921 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: MIT Imports: 35 Imported by: 0

README

front

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

  • 监听占用等待
  • 动态后端
  • 路径继承
  • 自定权重
  • 故障转移
  • 自定义头
  • 多种轮询
  • 请求路径过滤
  • 请求头过滤
  • 请求数据过滤

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

示例

[
    {
        "addr": "127.0.0.1:10000",
        "routes": [
            {
                "path": ["/"],
                "pathAdd": true,
                "filiter": {
                    "reqUri": {
                        "accessRule": "!{stop}",
                        "items": {
                            "stop": "(main\\.json)"
                        }
                    }
                },
                "backs": [
                    {
                        "name": "back1",
                        "to": "./",
                        "weight": "1"
                    }
                ]
            }
        ]
    }
]
curl http://127.0.0.1:10000/
<!doctype html>
<meta name="viewport" content="width=device-width">
<pre>
<a href="front.run">front.run</a>
<a href="main.json">main.json</a>
<a href="main.log">main.log</a>
</pre>

curl http://127.0.0.1:10000/main.json -I
HTTP/1.1 403 Forbidden
X-Front-Error: ErrPatherCheckFail
Date: Wed, 30 Oct 2024 17:50:22 GMT

curl http://127.0.0.1:10000/main.log -I
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 1700014
Content-Type: text/x-log; charset=utf-8
Last-Modified: Wed, 30 Oct 2024 17:51:23 GMT
Date: Wed, 30 Oct 2024 17:51:23 GMT

配置为json数组格式[],下面为数组中的其中一个{},下述字段倾斜的,表示不会根据配置动态加载

config:

  • addr: string 监听端口 例:0.0.0.0:8081
  • matchRule: string 匹配规则,默认prefixprefix:当未匹配到时,返回最近的/匹配, all:当未匹配到时,返回404
  • copyBlocks: int 转发的块数量,默认1000
  • retryBlocks: {} 重试, 当停用时,分配仅进行一次
    • sizeB: int 重试的块大小,默认1000000
    • num: int 重试的块数量,默认0,为0时停用重试
  • tls: {} 启用tls, 默认空
    • pub: string 公钥pem路径
    • key: string 私钥pem路径
  • routes: [] 路由
    • path: []string 路径
    • pathAdd: bool 将客户端访问的路径附加在path上 例:/api/req => /ws => /ws/api/req
    • rollRule: string 可选
      • order(按顺序)
      • loop(轮流)
      • disableC_MinFirst(禁用数较少的优先)
      • dealingC_MinFirst(连接数较少的优先)
      • chosenC_MinFirst(被选择较少的优先)
      • (使用rand.Shuffle随机,默认)
    • reqBody: 请求后端前,请求数据过滤器
      • action: string 可选accessdeny
      • reqSize: string 限定请求数据大小,默认为1M
      • matchExp: string access时如不匹配将结束请求。deny时如匹配将结束请求。
    • setting... 将会给backs默认值
    • backs: [] 后端
      • name: string 后端名称,将在日志中显示
      • to: string 后端地址,说明如下:
        • 含有://时,例s://www.baidu.com,会根据客户端自动添加http or ws在地址前
        • 不含://时,将会尝试解析成本地文件
      • weight: string uint 权重,按routes中的全部back的权重比分配,当权重为0时,将停止新请求的进入
      • alwaysUp: bool 总是在线, 当只有一个后端时,默认为true
      • setting...

setting: json配置不需要setting

  • splicing: int 当客户端支持cookie时,将会固定使用后端多少秒,默认不启用

  • errToSec: float64 当后端响应超过(ws则指初次返回时间)指定秒,将会触发errBanSec

  • errBanSec: int 当后端错误时(指连接失败,不指后端错误响应),将会禁用若干秒

  • insecureSkipVerify: bool 忽略不安全的tls证书

  • verifyPeerCer: string 路径,校验服务器证书,使用intermediate_ca

  • filiter:

    • reqUri: 请求后端前,请求路径过滤器
      • accessRule: 布尔表达式,为true时才通过,例{id}|(!{id2}&{id3})
      • items: map[string]string
        • id: matchExp
    • reqHeader: 请求后端前,请求头处理器
      • accessRule: 布尔表达式,为true时才通过
      • items: map[string]{}
        • id:
          • key: string header头
          • matchExp: string
    • resHeader: 返回后端的响应前,请求头处理器
      • accessRule: 布尔表达式,为true时才通过
      • items: map[string]{}
        • id:
          • key: string header头
          • matchExp: string
  • dealer:

    • reqUri:[] 请求后端前,路径处理器
      • action: string 可选replace
      • matchExp: string replace时结合value进行替换
      • value: string replace时结合matchExp进行替换。
    • reqHeader:[] 请求后端前,请求头处理器
      • action: string 可选replaceadddelset
      • key: string 具体处理哪个头
      • matchExp: string replace时结合value进行替换
      • value: string replace时结合matchExp进行替换。add时将附加值。set时将覆盖值。
    • resHeader:[] 返回后端的响应前,请求头处理器
      • action: string 可选adddelset
      • key: string 具体处理哪个头
      • matchExp: string replace时结合value进行替换
      • value: string replace时结合matchExp进行替换。add时将附加值。set时将覆盖值。

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")
	ErrReqReBodyFail     = errors.New("ErrReqReBodyFail")
	ErrReqReBodyOverflow = errors.New("ErrReqReBodyOverflow")
	ErrReqCreFail        = errors.New("ErrReqCreFail")
	ErrReqDoFail         = errors.New("ErrReqDoFail")
	ErrResDoFail         = errors.New("ErrResDoFail")
	ErrResFail           = errors.New("ErrResFail")
	ErrResTO             = errors.New("ErrResTO")
	ErrUriTooLong        = errors.New("ErrUriTooLong")
	ErrPatherCheckFail   = errors.New("ErrPatherCheckFail")
	ErrHeaderCheckFail   = errors.New("ErrHeaderCheckFail")
	ErrBodyCheckFail     = errors.New("ErrBodyCheckFail")
	ErrAllBacksFail      = errors.New("ErrAllBacksFail")
	ErrBackFail          = errors.New("ErrBackFail")
	ErrNoRoute           = errors.New("ErrNoRoute")
	ErrDealReqUri        = errors.New("ErrDealReqUri")
	ErrDealReqHeader     = errors.New("ErrDealReqHeader")
	ErrDealResHeader     = errors.New("ErrDealResHeader")
	ErrCerVerify         = errors.New("ErrCerVerify")
)
View Source
var (
	ErrEmptyVerifyPeerCerByte = errors.New("ErrEmptyVerifyPeerCerByte")
)

Functions

func DialContext added in v0.1.20231214143418

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

func LoadPeriod

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

加载

func LoadX509PubKey added in v0.1.20240323030221

func LoadX509PubKey(certPEMBlock []byte) tls.Certificate

func MarkRetry added in v0.1.20241009015120

func MarkRetry(e error) 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   uint   `json:"weight,string"`
	AlwaysUp bool   `json:"alwaysUp"`

	Setting
	// 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

func (*Back) PathAdd added in v0.1.20231202185918

func (t *Back) PathAdd() bool

func (*Back) SwapSign added in v0.1.20231209195503

func (t *Back) SwapSign(logger Logger)

type Config

type Config struct {
	Addr string `json:"addr"`
	TLS  struct {
		Pub string `json:"pub"`
		Key string `json:"key"`
	} `json:"tls"`
	RetryBlocks struct {
		SizeB int `json:"sizeB"`
		Num   int `json:"num"`
	} `json:"retryBlocks"`
	RetryBlocksI pslice.BlocksI[byte] `json:"-"`
	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 ErrCanRetry added in v0.1.20241009015120

type ErrCanRetry struct {
	CanRetry bool
	// contains filtered or unexported fields
}

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 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 Route

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

	PathAdd  bool         `json:"pathAdd"`
	RollRule string       `json:"rollRule"`
	ReqBody  filiter.Body `json:"reqBody"`
	Setting

	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(logger Logger)

type Setting added in v0.1.20240321070814

type Setting struct {
	ErrToSec           float64         `json:"errToSec"`
	Splicing           int             `json:"splicing"`
	ErrBanSec          int             `json:"errBanSec"`
	InsecureSkipVerify bool            `json:"insecureSkipVerify"`
	VerifyPeerCer      string          `json:"verifyPeerCer"`
	Filiter            filiter.Filiter `json:"filiter"`
	Dealer             dealer.Dealer   `json:"dealer"`
	// contains filtered or unexported fields
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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