Files
demo/config/config.go
2025-03-14 18:50:49 +00:00

112 lines
7.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package config
import (
"log/slog"
"time"
)
// Config 代理配置
type Config struct {
// 基本配置
ListenAddr string `json:"listen_addr" yaml:"listen_addr" toml:"listen_addr"` // 监听地址
TargetAddr string `json:"target_addr" yaml:"target_addr" toml:"target_addr"` // 目标地址
DecryptHTTPS bool `json:"decrypt_https" yaml:"decrypt_https" toml:"decrypt_https"` // 是否启用HTTPS解密
CACert string `json:"ca_cert" yaml:"ca_cert" toml:"ca_cert"` // CA证书文件路径(用于生成动态证书)
CAKey string `json:"ca_key" yaml:"ca_key" toml:"ca_key"` // CA密钥文件路径(用于生成动态证书)
UseECDSA bool `json:"use_ecdsa" yaml:"use_ecdsa" toml:"use_ecdsa"` // 是否使用ECDSA生成证书默认使用RSA
TLSCert string `json:"tls_cert" yaml:"tls_cert" toml:"tls_cert"` // TLS证书文件路径
TLSKey string `json:"tls_key" yaml:"tls_key" toml:"tls_key"` // TLS密钥文件路径
// 连接配置
DisableKeepAlive bool `json:"disable_keep_alive" yaml:"disable_keep_alive" toml:"disable_keep_alive"` // 是否禁用连接复用
RequestTimeout time.Duration `json:"request_timeout" yaml:"request_timeout" toml:"request_timeout"` // 请求超时时间
EnableCache bool `json:"enable_cache" yaml:"enable_cache" toml:"enable_cache"` // 是否启用响应缓存
IdleTimeout time.Duration `json:"idle_timeout" yaml:"idle_timeout" toml:"idle_timeout"` // 连接空闲超时时间
MaxIdleConns int `json:"max_idle_conns" yaml:"max_idle_conns" toml:"max_idle_conns"` // 最大空闲连接数
// 缓存配置
DNSCacheTTL time.Duration `json:"dns_cache_ttl" yaml:"dns_cache_ttl" toml:"dns_cache_ttl"` // DNS缓存过期时间
CacheTTL time.Duration `json:"cache_ttl" yaml:"cache_ttl" toml:"cache_ttl"` // 缓存过期时间
// 重试配置
EnableRetry bool `json:"enable_retry" yaml:"enable_retry" toml:"enable_retry"` // 是否启用重试机制
MaxRetries int `json:"max_retries" yaml:"max_retries" toml:"max_retries"` // 最大重试次数
BaseBackoff time.Duration `json:"base_backoff" yaml:"base_backoff" toml:"base_backoff"` // 重试间隔基数
MaxBackoff time.Duration `json:"max_backoff" yaml:"max_backoff" toml:"max_backoff"` // 最大重试间隔
// 限流配置
RateLimit float64 `json:"rate_limit" yaml:"rate_limit" toml:"rate_limit"` // 每秒请求速率限制
// 其他配置
EnableCORS bool `json:"enable_cors" yaml:"enable_cors" toml:"enable_cors"` // 是否自动添加CORS头
// 负载均衡配置
EnableLoadBalancing bool `json:"enable_load_balancing" yaml:"enable_load_balancing" toml:"enable_load_balancing"` // 是否启用负载均衡
Backends []string `json:"backends" yaml:"backends" toml:"backends"` // 负载均衡后端列表
EnableRateLimit bool `json:"enable_rate_limit" yaml:"enable_rate_limit" toml:"enable_rate_limit"` // 是否启用限流
MaxBurst int `json:"max_burst" yaml:"max_burst" toml:"max_burst"` // 并发请求峰值限制
MaxConnections int `json:"max_connections" yaml:"max_connections" toml:"max_connections"` // 最大连接数
EnableConnectionPool bool `json:"enable_connection_pool" yaml:"enable_connection_pool" toml:"enable_connection_pool"` // 是否启用连接池
ConnectionPoolSize int `json:"connection_pool_size" yaml:"connection_pool_size" toml:"connection_pool_size"` // 连接池大小
EnableHealthCheck bool `json:"enable_health_check" yaml:"enable_health_check" toml:"enable_health_check"` // 是否启用健康检查
HealthCheckInterval time.Duration `json:"health_check_interval" yaml:"health_check_interval" toml:"health_check_interval"` // 健康检查间隔时间
HealthCheckTimeout time.Duration `json:"health_check_timeout" yaml:"health_check_timeout" toml:"health_check_timeout"` // 健康检查超时时间
EnableMetrics bool `json:"enable_metrics" yaml:"enable_metrics" toml:"enable_metrics"` // 是否启用监控指标
EnableTracing bool `json:"enable_tracing" yaml:"enable_tracing" toml:"enable_tracing"` // 是否启用请求追踪
WebSocketIntercept bool `json:"websocket_intercept" yaml:"websocket_intercept" toml:"websocket_intercept"` // 是否拦截WebSocket
ReverseProxy bool `json:"reverse_proxy" yaml:"reverse_proxy" toml:"reverse_proxy"` // 是否作为反向代理
ReverseProxyRulesFile string `json:"reverse_proxy_rules_file" yaml:"reverse_proxy_rules_file" toml:"reverse_proxy_rules_file"` // 反向代理规则文件路径
PreserveClientIP bool `json:"preserve_client_ip" yaml:"preserve_client_ip" toml:"preserve_client_ip"` // 是否保留客户端IP
EnableCompression bool `json:"enable_compression" yaml:"enable_compression" toml:"enable_compression"` // 是否启用压缩
RewriteHostHeader bool `json:"rewrite_host_header" yaml:"rewrite_host_header" toml:"rewrite_host_header"` // 重写Host头
AddXForwardedFor bool `json:"add_x_forwarded_for" yaml:"add_x_forwarded_for" toml:"add_x_forwarded_for"` // 是否添加X-Forwarded-For头
AddXRealIP bool `json:"add_x_real_ip" yaml:"add_x_real_ip" toml:"add_x_real_ip"` // 是否添加X-Real-IP头
SupportWebSocketUpgrade bool `json:"support_websocket_upgrade" yaml:"support_websocket_upgrade" toml:"support_websocket_upgrade"` // 是否支持Websocket升级
InsecureSkipVerify bool `json:"insecure_skip_verify" yaml:"insecure_skip_verify" toml:"insecure_skip_verify"` // 是否跳过TLS证书验证
Logger *slog.Logger `json:"-" yaml:"-" toml:"-"` // 日志记录器
}
// DefaultConfig 返回默认配置
func DefaultConfig() *Config {
return &Config{
ListenAddr: ":8080",
DecryptHTTPS: false,
UseECDSA: false,
RequestTimeout: 30 * time.Second,
IdleTimeout: 90 * time.Second,
EnableCache: false,
MaxIdleConns: 100,
DNSCacheTTL: 5 * time.Minute,
CacheTTL: 5 * time.Minute,
EnableRetry: true,
MaxRetries: 3,
BaseBackoff: time.Second,
MaxBackoff: 10 * time.Second,
RateLimit: 0, // 0 表示不限流
EnableCORS: true,
EnableLoadBalancing: false,
Backends: []string{},
EnableRateLimit: false,
MaxBurst: 50,
MaxConnections: 1000,
EnableConnectionPool: true,
ConnectionPoolSize: 100,
EnableHealthCheck: false,
HealthCheckInterval: 10 * time.Second,
HealthCheckTimeout: 5 * time.Second,
EnableMetrics: false,
EnableTracing: false,
WebSocketIntercept: false,
ReverseProxy: false,
ReverseProxyRulesFile: "",
PreserveClientIP: true,
EnableCompression: false,
RewriteHostHeader: false,
AddXForwardedFor: true,
AddXRealIP: true,
SupportWebSocketUpgrade: true,
InsecureSkipVerify: false,
Logger: slog.Default(),
}
}