1. ✅ 监控指标收集 2. ✅ 中间件机制 3. ✅ 配置热更新 4. ✅ 优雅关闭 5. ✅ 插件系统 6. ✅ API文档 7. ✅ 认证授权系统 8. ✅ 请求/响应压缩优化
263 lines
6.0 KiB
Go
263 lines
6.0 KiB
Go
package proxy
|
||
|
||
import (
|
||
"net/http"
|
||
"net/http/httptrace"
|
||
"time"
|
||
|
||
"github.com/darkit/goproxy/internal/cache"
|
||
"github.com/darkit/goproxy/internal/config"
|
||
"github.com/darkit/goproxy/internal/healthcheck"
|
||
"github.com/darkit/goproxy/internal/loadbalance"
|
||
"github.com/darkit/goproxy/internal/metrics"
|
||
)
|
||
|
||
// Option 用于配置代理选项的函数类型
|
||
type Option func(*Options)
|
||
|
||
// WithConfig 设置代理配置
|
||
func WithConfig(cfg *config.Config) Option {
|
||
return func(opt *Options) {
|
||
opt.Config = cfg
|
||
}
|
||
}
|
||
|
||
// WithDisableKeepAlive 设置连接是否重用
|
||
func WithDisableKeepAlive(disableKeepAlive bool) Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
// 在transport中设置DisableKeepAlives
|
||
}
|
||
}
|
||
|
||
// WithClientTrace 设置HTTP客户端跟踪
|
||
func WithClientTrace(t *httptrace.ClientTrace) Option {
|
||
return func(opt *Options) {
|
||
opt.ClientTrace = t
|
||
}
|
||
}
|
||
|
||
// WithDelegate 设置委托类
|
||
func WithDelegate(delegate Delegate) Option {
|
||
return func(opt *Options) {
|
||
opt.Delegate = delegate
|
||
}
|
||
}
|
||
|
||
// WithTransport 使用自定义HTTP传输
|
||
func WithTransport(t *http.Transport) Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
// 在New方法中处理transport
|
||
}
|
||
}
|
||
|
||
// WithDecryptHTTPS 启用中间人代理解密HTTPS
|
||
func WithDecryptHTTPS(c CertificateCache) Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
opt.Config.DecryptHTTPS = true
|
||
opt.CertCache = c
|
||
}
|
||
}
|
||
|
||
// WithEnableWebsocketIntercept 启用WebSocket拦截
|
||
func WithEnableWebsocketIntercept() Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
// WebSocket拦截在代理处理逻辑中实现
|
||
}
|
||
}
|
||
|
||
// WithHTTPCache 设置HTTP缓存
|
||
func WithHTTPCache(c cache.Cache) Option {
|
||
return func(opt *Options) {
|
||
opt.HTTPCache = c
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
opt.Config.EnableCache = true
|
||
}
|
||
}
|
||
|
||
// WithLoadBalancer 设置负载均衡器
|
||
func WithLoadBalancer(lb loadbalance.LoadBalancer) Option {
|
||
return func(opt *Options) {
|
||
opt.LoadBalancer = lb
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
opt.Config.EnableLoadBalancing = true
|
||
}
|
||
}
|
||
|
||
// WithHealthChecker 设置健康检查器
|
||
func WithHealthChecker(hc *healthcheck.HealthChecker) Option {
|
||
return func(opt *Options) {
|
||
opt.HealthChecker = hc
|
||
}
|
||
}
|
||
|
||
// WithMetrics 设置监控指标
|
||
func WithMetrics(m metrics.MetricsCollector) Option {
|
||
return func(opt *Options) {
|
||
opt.Metrics = m
|
||
}
|
||
}
|
||
|
||
// WithTLSCertAndKey 设置TLS证书和密钥
|
||
func WithTLSCertAndKey(certPath, keyPath string) Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
opt.Config.TLSCert = certPath
|
||
opt.Config.TLSKey = keyPath
|
||
}
|
||
}
|
||
|
||
// WithCACertAndKey 设置CA证书和密钥(用于生成动态证书)
|
||
func WithCACertAndKey(caCertPath, caKeyPath string) Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
opt.Config.CACert = caCertPath
|
||
opt.Config.CAKey = caKeyPath
|
||
}
|
||
}
|
||
|
||
// WithConnectionPoolSize 设置连接池大小
|
||
func WithConnectionPoolSize(size int) Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
opt.Config.ConnectionPoolSize = size
|
||
}
|
||
}
|
||
|
||
// WithIdleTimeout 设置空闲超时时间
|
||
func WithIdleTimeout(timeout time.Duration) Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
opt.Config.IdleTimeout = timeout
|
||
}
|
||
}
|
||
|
||
// WithRequestTimeout 设置请求超时时间
|
||
func WithRequestTimeout(timeout time.Duration) Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
opt.Config.RequestTimeout = timeout
|
||
}
|
||
}
|
||
|
||
// WithReverseProxy 启用反向代理模式
|
||
func WithReverseProxy(enable bool) Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
opt.Config.ReverseProxy = enable
|
||
}
|
||
}
|
||
|
||
// WithEnableRetry 启用请求重试
|
||
func WithEnableRetry(maxRetries int, baseBackoff, maxBackoff time.Duration) Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
opt.Config.EnableRetry = true
|
||
opt.Config.MaxRetries = maxRetries
|
||
opt.Config.RetryBackoff = baseBackoff
|
||
opt.Config.MaxRetryBackoff = maxBackoff
|
||
}
|
||
}
|
||
|
||
// WithRateLimit 设置请求限流
|
||
func WithRateLimit(rps float64) Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
opt.Config.EnableRateLimit = true
|
||
opt.Config.RateLimit = rps
|
||
}
|
||
}
|
||
|
||
// WithDNSCacheTTL 设置DNS缓存TTL
|
||
func WithDNSCacheTTL(ttl time.Duration) Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
opt.Config.DNSCacheTTL = ttl
|
||
}
|
||
}
|
||
|
||
// WithURLRewrite 启用URL重写
|
||
func WithURLRewrite(enable bool) Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
opt.Config.EnableURLRewrite = enable
|
||
}
|
||
}
|
||
|
||
// WithEnableCORS 启用CORS支持
|
||
func WithEnableCORS(enable bool) Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
opt.Config.EnableCORS = enable
|
||
}
|
||
}
|
||
|
||
// WithCertManager 设置证书管理器
|
||
// 这是一个内部函数,主要用于在New方法中设置CertManager
|
||
func WithCertManager(certManager *CertManager) Option {
|
||
return func(opt *Options) {
|
||
opt.CertManager = certManager
|
||
}
|
||
}
|
||
|
||
// WithEnableECDSA 启用ECDSA证书生成(默认使用RSA)
|
||
func WithEnableECDSA(enable bool) Option {
|
||
return func(opt *Options) {
|
||
if opt.Config == nil {
|
||
opt.Config = config.DefaultConfig()
|
||
}
|
||
opt.Config.UseECDSA = enable
|
||
}
|
||
}
|
||
|
||
// NewWithOptions 使用选项函数创建代理
|
||
func NewWithOptions(options ...Option) *Proxy {
|
||
opts := &Options{
|
||
Config: config.DefaultConfig(),
|
||
}
|
||
|
||
// 应用所有选项
|
||
for _, option := range options {
|
||
option(opts)
|
||
}
|
||
|
||
return New(opts)
|
||
}
|