Documentation
¶
Overview ¶
Package webservice 实现 Web 服务模块:按域名把请求分发到反向代理、重定向、静态文件或固定文本, 支持 HTTPS(证书来自证书模块)、PROXY 协议、基本认证、网页认证与 IP 访问控制。
Index ¶
- Constants
- type BanInfo
- type CacheConfig
- type CertSource
- type HealthCheck
- type Listener
- type ListenerView
- type Module
- func (m *Module) ID() string
- func (m *Module) ImportLucky(doc luckyimport.Document, opts luckyimport.Options) (*luckyimport.Report, error)
- func (m *Module) Init(env module.Env) error
- func (m *Module) Routes(r module.Router)
- func (m *Module) Start(ctx context.Context) error
- func (m *Module) Stop() error
- func (m *Module) Summary() []module.SummaryItem
- func (m *Module) Title() string
- type OAuthProvider
- type PathRule
- type Point
- type RateLimit
- type Rewrite
- type Settings
- type ShareLink
- type Site
- type SiteStats
- type SiteStatsView
- type TopItem
- type UpstreamStatus
- type WAFProvider
Constants ¶
const ( CountryOff = "" CountryAllow = "allow" CountryDeny = "deny" )
国家过滤模式
const ( RuleAuthInherit = "" RuleAuthOff = "off" RuleAuthOn = "on" )
路径规则的认证覆盖
const ( TypeProxy = "proxy" TypeRedirect = "redirect" TypeStatic = "static" TypeText = "text" )
站点类型
const ( IPModeOff = "" IPModeWhitelist = "whitelist" IPModeBlacklist = "blacklist" )
访问控制模式
const ( LBRoundRobin = "" LBRandom = "random" LBIPHash = "ip_hash" LBLeastConn = "least_conn" )
负载均衡策略
const ID = "webservice"
ID 是模块标识。
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BanInfo ¶ added in v0.3.0
type BanInfo struct {
// Key 为被封禁的地址(IPv6 按 /64 聚合)
Key string `json:"key"`
Until time.Time `json:"until"`
Reason string `json:"reason"`
Manual bool `json:"manual"`
}
BanInfo 是封禁列表中的一项。
type CacheConfig ¶ added in v0.3.0
type CacheConfig struct {
Enable bool `json:"enable"`
// TTL 为上游未给出缓存指令时 200 响应的缓存秒数,0 表示只遵循上游的 Cache-Control
TTL int `json:"ttl"`
// MaxMB 为缓存总容量,默认 64
MaxMB int `json:"maxMb"`
}
CacheConfig 是反向代理的响应缓存(内存)。
type CertSource ¶
type CertSource interface {
GetCertificate(*tls.ClientHelloInfo) (*tls.Certificate, error)
}
CertSource 提供按 SNI 选择证书的能力(由证书模块实现)。
type HealthCheck ¶ added in v0.3.0
type HealthCheck struct {
Enable bool `json:"enable"`
// Mode 为 http(请求 Path,状态码 < 400 视为健康)或 tcp(只测试连接)
Mode string `json:"mode"`
Path string `json:"path"`
// Interval / Timeout 单位为秒
Interval int `json:"interval"`
Timeout int `json:"timeout"`
// Fails 连续失败多少次判为不健康,Passes 连续成功多少次恢复
Fails int `json:"fails"`
Passes int `json:"passes"`
}
HealthCheck 是后端健康检查设置。未启用时仍会做被动检查(连续连接失败后暂时摘除)。
type Listener ¶
type Listener struct {
Key string `json:"key"`
Name string `json:"name"`
Enable bool `json:"enable"`
// Network 为 tcp(双栈)、tcp4、tcp6
Network string `json:"network"`
ListenIP string `json:"listenIp"`
Port int `json:"port"`
TLS bool `json:"tls"`
// MinTLS 为 1.0 / 1.1 / 1.2 / 1.3
MinTLS string `json:"minTls"`
// AcceptProxyProtocol 接收上游(如 haproxy、frp)发送的 PROXY 协议头,仅信任全局设置中的来源
AcceptProxyProtocol bool `json:"acceptProxyProtocol"`
Sites []Site `json:"sites"`
// Default 在没有站点匹配时使用;未启用时返回 404
Default Site `json:"default"`
Remark string `json:"remark"`
}
Listener 是一个监听端口及其站点。
type ListenerView ¶
type ListenerView struct {
Listener
Running bool `json:"running"`
Error string `json:"error,omitempty"`
Stats map[string]map[string]int64 `json:"stats"`
// Upstreams 为各站点的后端健康状态,键为站点 key
Upstreams map[string][]UpstreamStatus `json:"upstreams,omitempty"`
}
ListenerView 是带运行状态的监听器。
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module 是 Web 服务模块。
func New ¶
func New(certs CertSource, waf WAFProvider) *Module
New 创建模块。certs 为证书来源,可为 nil(此时不能启用 HTTPS);waf 可为 nil(站点的 WAF 设置不生效)。
func (*Module) ImportLucky ¶
func (m *Module) ImportLucky(doc luckyimport.Document, opts luckyimport.Options) (*luckyimport.Report, error)
ImportLucky 实现 luckyimport.Importer,同时识别 lucky_base 中的 PROXY 协议可信来源。
func (*Module) Summary ¶
func (m *Module) Summary() []module.SummaryItem
Summary 实现 module.Summarizer。
type OAuthProvider ¶
OAuthProvider 是一个自建的第三方登录配置(使用用户自己申请的 OAuth 应用,不经过任何中转服务器)。
type PathRule ¶ added in v0.3.0
type PathRule struct {
Prefix string `json:"prefix"`
// Auth 为 ""(沿用站点)、off(无需认证)、on(必须认证)
Auth string `json:"auth"`
IPMode string `json:"ipMode"`
IPList []string `json:"ipList"`
}
PathRule 为某个路径前缀覆盖站点的认证与 IP 访问控制。最长前缀优先。
type Point ¶ added in v0.3.0
type Point struct {
Time int64 `json:"t"`
Requests int64 `json:"requests"`
Bytes int64 `json:"bytes"`
S4xx int64 `json:"s4xx"`
S5xx int64 `json:"s5xx"`
}
Point 是一个时间点的统计。
type RateLimit ¶ added in v0.3.0
type RateLimit struct {
Enable bool `json:"enable"`
// Rate 为每秒允许的请求数,Burst 为突发容量
Rate float64 `json:"rate"`
Burst int `json:"burst"`
// MaxConns 为单个 IP 的并发请求上限,0 表示不限
MaxConns int `json:"maxConns"`
}
RateLimit 按客户端 IP 限流。
type Rewrite ¶ added in v0.3.0
type Rewrite struct {
// Pattern 为匹配请求路径的正则表达式
Pattern string `json:"pattern"`
// To 为替换结果,可使用 $1 引用分组;Redirect 时为跳转目标
To string `json:"to"`
Redirect bool `json:"redirect"`
// Code 为重定向状态码,默认 302
Code int `json:"code"`
}
Rewrite 是路径重写或重定向规则,按顺序匹配,命中第一条即停止。
type Settings ¶
type Settings struct {
Enable bool `json:"enable"`
// TrustedProxies 为可信的 PROXY 协议来源(IP 或 CIDR)
TrustedProxies []string `json:"trustedProxies"`
// AuthSecret 用于签名网页认证 Cookie,首次启动自动生成
AuthSecret string `json:"authSecret"`
// OAuthProviders 为自建的第三方登录配置
OAuthProviders []OAuthProvider `json:"oauthProviders"`
// OAuthCallbackBase 为接收第三方登录回调的站点地址(需由 Gately 的某个监听提供),例如 https://auth.example.com
OAuthCallbackBase string `json:"oauthCallbackBase"`
// AutoBanSeconds 为认证连续失败后自动封禁来源 IP 的秒数,0 表示不自动封禁
AutoBanSeconds int `json:"autoBanSeconds"`
}
Settings 是模块级配置。
type ShareLink ¶ added in v0.3.0
type ShareLink struct {
// Token 是访问令牌,访问 https://域名/任意路径?_gs=令牌 即可兑换会话
MaxUses int `json:"maxUses"`
PathPrefix string `json:"pathPrefix"`
}
ShareLink 是站点的临时分享链接:持有令牌的访客无需账号即可在有效期内访问。
type Site ¶
type Site struct {
Key string `json:"key"`
Name string `json:"name"`
Enable bool `json:"enable"`
// Domains 支持精确域名、*.example.com 通配(匹配任意级子域名)与 *(匹配所有)
Domains []string `json:"domains"`
Type string `json:"type"`
// 反向代理
Targets []string `json:"targets"`
InsecureSkipVerify bool `json:"insecureSkipVerify"`
// UseTargetHost 为 true 时 Host 头使用目标地址,否则保留客户端请求的 Host
UseTargetHost bool `json:"useTargetHost"`
DisableKeepAlives bool `json:"disableKeepAlives"`
// SendProxyProtocol 向后端发送 PROXY v2 头传递真实客户端 IP(需要后端支持)
SendProxyProtocol bool `json:"sendProxyProtocol"`
// ForwardedHeaders 添加 X-Forwarded-For/Proto/Host
ForwardedHeaders bool `json:"forwardedHeaders"`
// 重定向:RedirectTo 为目标地址前缀,KeepPath 时附加原路径与查询串
RedirectTo string `json:"redirectTo"`
RedirectCode int `json:"redirectCode"`
KeepPath bool `json:"keepPath"`
// 静态文件
Root string `json:"root"`
ListDir bool `json:"listDir"`
// 固定文本
Text string `json:"text"`
ContentType string `json:"contentType"`
StatusCode int `json:"statusCode"`
// 认证:Users 每项为 用户名:密码(密码可为 bcrypt 哈希)
BasicAuth bool `json:"basicAuth"`
WebAuth bool `json:"webAuth"`
Users []string `json:"users"`
// OAuthUsers 为允许通过第三方登录访问的身份,格式 提供方key:标识(用户名、邮箱、稳定 ID 均可,行尾可用 " #" 加备注),提供方key:* 表示该提供方任意用户
OAuthUsers []string `json:"oauthUsers"`
IPMode string `json:"ipMode"`
IPList []string `json:"ipList"`
// CountryMode 为 ""(不限)、allow(仅允许列出的国家)、deny(拒绝列出的国家),需先在设置中导入 GeoIP 数据库
CountryMode string `json:"countryMode"`
// Countries 为两位 ISO 国家代码,如 CN、US
Countries []string `json:"countries"`
// AuthOptional 为 true 时,认证只作用于路径规则中标记为"必须认证"的路径,其余路径公开
AuthOptional bool `json:"authOptional"`
// Rules 为按路径前缀覆盖认证与 IP 访问控制的规则
Rules []PathRule `json:"rules"`
ShareLinks []ShareLink `json:"shareLinks"`
// 反向代理增强:Targets 每项可附 weight=N、backup;LBPolicy 为 ""(轮询)、random、ip_hash、least_conn
LBPolicy string `json:"lbPolicy"`
Health HealthCheck `json:"health"`
Compress bool `json:"compress"`
RateLimit RateLimit `json:"rateLimit"`
Cache CacheConfig `json:"cache"`
Rewrites []Rewrite `json:"rewrites"`
// RequestHeaders / ResponseHeaders 每项为 "Key: Value",Value 为空表示删除该头
RequestHeaders []string `json:"requestHeaders"`
ResponseHeaders []string `json:"responseHeaders"`
CORS bool `json:"cors"`
AccessLog bool `json:"accessLog"`
// WAF 为引用的 WAF 实例 key,留空不检查;实例不存在或停用时放行
WAF string `json:"waf"`
Remark string `json:"remark"`
// contains filtered or unexported fields
}
Site 是监听器下按域名匹配的一个站点。
type SiteStatsView ¶ added in v0.3.0
type SiteStatsView struct {
Series []Point `json:"series"`
Status map[string]int64 `json:"status"`
BytesOut int64 `json:"bytesOut"`
TopIPs []TopItem `json:"topIps"`
TopCountries []TopItem `json:"topCountries"`
Upstreams []UpstreamStatus `json:"upstreams,omitempty"`
}
SiteStatsView 是站点统计接口的输出。