Documentation
¶
Overview ¶
Package webservice 实现 Web 服务模块:按域名把请求分发到反向代理、重定向、静态文件或固定文本, 支持 HTTPS(证书来自证书模块)、PROXY 协议、基本认证、网页认证与 IP 访问控制。
Index ¶
- Constants
- type CertSource
- 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 Settings
- type Site
- type SiteStats
- type WAFProvider
Constants ¶
const ( TypeProxy = "proxy" TypeRedirect = "redirect" TypeStatic = "static" TypeText = "text" )
站点类型
const ( IPModeOff = "" IPModeWhitelist = "whitelist" IPModeBlacklist = "blacklist" )
访问控制模式
const ( OAuthGitHub = "github" OAuthOIDC = "oidc" )
第三方登录类型
const ID = "webservice"
ID 是模块标识。
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CertSource ¶
type CertSource interface {
GetCertificate(*tls.ClientHelloInfo) (*tls.Certificate, error)
}
CertSource 提供按 SNI 选择证书的能力(由证书模块实现)。
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"`
}
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 ¶
type OAuthProvider struct {
// Key 同时作为身份前缀,例如 key 为 github 时身份写作 github:用户名
Key string `json:"key"`
Name string `json:"name"`
Type string `json:"type"`
ClientID string `json:"clientId"`
ClientSecret string `json:"clientSecret"`
// Issuer 为 OIDC 签发者地址,例如 https://accounts.google.com
Issuer string `json:"issuer"`
}
OAuthProvider 是一个自建的第三方登录配置(使用用户自己申请的 OAuth 应用,不经过任何中转服务器)。
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"`
}
Settings 是模块级配置。
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:用户名(或邮箱),提供方key:* 表示该提供方任意用户
OAuthUsers []string `json:"oauthUsers"`
IPMode string `json:"ipMode"`
IPList []string `json:"ipList"`
// 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 是监听器下按域名匹配的一个站点。