8224ae7ff3
feat(middleware/cors): add CORS middleware with server-level configuration
...
Implement Cross-Origin Resource Sharing (CORS) middleware following the
middleware.Middleware interface pattern.
New config under security.cors:
- enabled: toggle CORS handling (default false)
- allowed_origins: exact origin list or ["*"] wildcard
- allowed_methods: allowed HTTP methods for preflight
- allowed_headers: allowed request headers for preflight
- expose_headers: headers visible to frontend JS
- allow_credentials: send cookies (incompatible with wildcard origin)
- max_age: preflight cache duration in seconds
Validation:
- origins+credentials mutual exclusion per CORS spec
- max_age non-negative check
Integration:
- Registered after SecurityHeaders, before ErrorIntercept in middleware chain
- Preflight (OPTIONS) returns 204 with CORS headers, skips handler
- Actual requests add CORS headers after handler execution
- Non-matching origins pass through without CORS headers
- 16 unit tests covering all scenarios
2026-06-11 23:41:38 +08:00
88a2c1fc1b
feat(config): add Least Time and Sticky configuration support
...
- Add least_time and sticky to valid algorithms list
- Add LeastTimeConfig and StickyConfig structures
- Update default config generation with new options
- Add configuration validation for new fields
2026-06-08 17:57:06 +08:00
2ad056d0ca
fix(config): supplement missing fields and fix comments in -g template
...
Add missing fields to GenerateConfigYAML output:
- proxy.timeout.dial (TCP dial timeout)
- health_check.slow_start and health_check.match (status/body/headers)
- proxy.buffering.buffers (multi-buffer config)
- lua.scripts[].route and route_type (routing mode)
Fix coroutine_stack_size comment to show actual default (64).
2026-06-04 00:04:13 +08:00
728a9f454b
fix(server,app,config): address code review findings
...
- Fix FD leak in DupListener: close *os.File after net.FileListener
- Add cleanup of partially-duped listeners on DupListener failure
- Make reload timeout configurable via shutdown.reload_timeout
- Handle filepath.Abs errors in processIncludes instead of ignoring
- Use net.ParseIP in isAnyAddr for robust IPv6 support
2026-06-03 13:16:05 +08:00
9b8ce2a08a
fix(config): real circular include detection with visited set
...
Replace depth-only detection with path-based visited set tracking.
Detects cycles immediately on first revisit instead of after 10 depth
iterations. Supports diamond patterns (A->B->shared, A->C->shared)
via backtracking. Add self-include and diamond tests. Document that
only servers/stream/variables are merged in defaults.go.
2026-06-03 11:51:17 +08:00
25d93c25fa
refactor: remove unused code and fix formatting
...
- Remove unused benchmark/tools package
- Make ValidAlgorithms private (validAlgorithms) in loadbalance
- Remove dead code (_ = result) in lua/api_socket_tcp.go
- Fix code formatting with goimports
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-09 16:58:45 +08:00
f3f92c7922
feat(lua): add LState pool configuration and fix coroutine error handling
...
- Add LStatePoolInitialSize and LStatePoolMaxSize config fields
- Set pool defaults to 100 initial / 1000 max (matching MaxConcurrentCoroutines)
- Fix middleware to return 500 on coroutine init failure instead of continuing
- Pass pool config from server init to Lua engine with zero-value fallbacks
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-09 11:13:23 +08:00
144e101c09
feat(proxy): add configurable X-Forwarded-Host and X-Forwarded-Proto headers
...
Add `set_forwarded_host` and `set_forwarded_proto` options to control
whether the proxy automatically sets these headers. This fixes issues
with upstream servers that validate X-Forwarded-Host against known hosts.
Changes:
- Add SetForwardedHost/SetForwardedProto fields to ProxyHeaders struct
- Modify SetForwardedHeaders and WriteForwardedHeaders function signatures
- Update modifyRequestHeaders to read config and pass control parameters
- Update WebSocket call chain to support new config
- Add unit tests for new functionality
- Update default config generation (-g) to include new options
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-07 13:28:28 +08:00
b62a3f12da
feat(handler): add autoindex module for directory listing
...
Add nginx-like autoindex functionality with three output formats:
- HTML: styled directory listing with sortable columns
- JSON: structured API-friendly output
- XML: machine-readable format
Configuration options:
- auto_index: enable/disable directory listing
- auto_index_format: output format (html/json/xml)
- auto_index_localtime: use local time instead of GMT
- auto_index_exact_size: show exact bytes vs human-readable
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-30 15:11:34 +08:00
e32e96ee81
feat(config): change gzip_static default to true
...
Enable pre-compressed file serving by default for better performance.
This aligns with the common practice of serving .gz/.br files when available.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-30 14:24:23 +08:00
11e22c80b8
perf: 零分配优化与 Dial timeout 支持
...
- 添加 b2s/s2b 零分配字节-字符串转换工具函数
- WebSocket 数据转发使用 sync.Pool 复用 32KB buffer
- 条件化 Debug 日志避免非 Debug 级别的字符串分配
- 缓存键哈希计算直接写入 []byte 避免 string 转换
- 使用 bytes.EqualFold 替代 strings.ToLower 进行大小写不敏感比较
- generateETag 使用 strconv.AppendInt 避免 fmt.Sprintf
- 支持 Dial timeout 配置,区分 TCP 连接建立和总连接超时
- MaxConnsPerHost 默认值改为 512(fasthttp 推荐)
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-28 20:11:20 +08:00
d191e1865d
feat(static): 添加 expires 缓存控制支持
...
- StaticConfig 添加 Expires 字段,支持 nginx 兼容格式(30d, 1h, max, epoch)
- StaticHandler 添加 SetExpires 方法和缓存响应头设置
- serveFile 自动设置 Cache-Control 和 Expires 响应头
- nginx 转换器正确转换 expires 指令
- --generate-config 输出包含 expires 文档和示例
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-27 14:05:25 +08:00
07acfad146
feat(config): 添加 alias 配置支持
...
- StaticConfig 添加 Alias 字段,与 Root 互斥
- server.go 创建 handler 时设置 alias
- validate.go 添加 root/alias 互斥验证和路径安全检查
- converter.go nginx alias 指令正确转换为 Alias 字段
- defaults.go --generate-config 输出包含 alias 文档和示例
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-27 13:16:12 +08:00
be974b2e18
feat(proxy,config): 代理层集成 stale 缓存回退逻辑
...
上游请求失败时,根据错误类型(超时/其他)调用 GetStale 尝试返回
过期缓存。配置文件示例补充 stale_if_error 和 stale_if_timeout 字段。
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-24 10:06:27 +08:00
0a7f7170d5
feat(cache,proxy): 增强代理缓存功能
...
- 添加 min_uses 阈值支持,请求次数达标才缓存
- 添加 cache_lock_timeout 配置,防止缓存锁无限等待
- 添加条件请求支持 (If-Modified-Since/If-None-Match),处理 304 响应
- 添加 background_update_disable 配置,允许禁用后台更新
- 添加 cache_ignore_headers 配置,缓存时忽略指定响应头
- 添加 methods 配置,指定可缓存的 HTTP 方法
- 改进路径匹配逻辑,支持精确匹配和通配符匹配
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-21 18:23:20 +08:00
48d8c06e31
fix(config): 补充默认值并同步 GenerateConfigYAML 输出
...
为 GeoIP/AuthRequest 添加默认值,GenerateConfigYAML 中硬编码值改为从配置读取,
补充 types/limit_rate/proxy_bind/buffering/internal 等字段文档输出,
新增默认值和字段覆盖测试。
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-21 15:10:52 +08:00
5a5f733cb4
refactor(logging): 将日志格式参数从布尔值改为字符串,支持 json/text/console 格式
...
将 Init 接口从 pretty bool 参数改为 format string 参数,
支持 json(纯 JSON)、text(无颜色 ConsoleWriter)、console(带颜色)三种格式。
简化 AppLogger 实现,统一使用 zerolog 格式化输出。
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-17 17:26:08 +08:00
7a589fec18
fix(config): 设置 ClientMaxBodySize 默认值并使配置生成使用该值
...
- 将 ClientMaxBodySize 默认值设为 10MB(而非空值)
- 修改 GenerateConfigYAML 使用实际配置值而非硬编码的 1MB
- 使默认配置与生成配置保持一致
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-17 15:02:50 +08:00
83fc6beba3
refactor(config): 移除 Transport.MaxIdleConnsPerHost 配置字段
...
简化连接池配置,仅保留 MaxConnsPerHost 作为总连接上限。
MaxConnsPerHost 已涵盖活跃和空闲连接,无需单独控制空闲连接数。
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-17 11:31:34 +08:00
bbf8032e6a
refactor(config): 调整 include 配置示例位置
...
将 include 配置示例从 SSL 配置前移动到变量配置后的位置,
使配置示例的组织结构更加合理。
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-17 10:41:13 +08:00
1926bf34e0
feat(config): 添加配置引入、Unix socket 和多 server_name 支持
...
新增配置功能:
- include 机制:支持 glob 模式引入子配置文件,DAG-safe 循环检测
- unix_socket:Unix domain socket 权限和所有权配置
- server_names:多 server_name 支持(通配符和正则)
- location_type:proxy/static 的位置匹配类型配置
- location_name:命名 location 配置
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-17 09:26:30 +08:00
87cd41a81f
docs(config): 添加 default/cache_valid/proxy_ssl 配置示例注释
...
补充 GenerateConfigYAML 输出的配置示例:
- default: 虚拟主机模式下标记默认服务器
- cache_valid: 按 HTTP 状态码细分缓存时间
- proxy_ssl: 上游 SSL/TLS 连接配置(含 mTLS 支持)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 15:16:14 +08:00
5625fdccc6
feat(status): 添加 Enabled 配置项显式控制状态端点启用
...
StatusConfig 新增 Enabled 字段,默认为 false 需显式启用。
修改 server.go 仅在 Enabled=true 时注册状态端点。
更新测试文件适配新配置结构。
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 15:06:50 +08:00
0c20c62b5c
refactor(config): 优化结构体内存布局减少 padding
...
ServerConfig、ProxyConfig、ProxySSLConfig 字段按类型大小排序:
指针→切片→字符串→结构体→int64→int→bool
减少内存对齐浪费,提升访问效率。
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 09:53:58 +08:00
bf14282e40
feat(server): 添加高并发优化配置支持
...
- ServerConfig 新增 Concurrency/ReadBufferSize/WriteBufferSize/ReduceMemoryUsage 配置
- defaults.go 设置默认值:256K 并发连接、16KB 缓冲区
- server.go 在 singleMode 和 vhostMode 中应用配置
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-15 18:26:49 +08:00
a026277385
feat(config): 添加 redirect_rewrite 配置定义和验证
...
- 新增 RedirectRewriteConfig 配置结构体,支持三种模式:
- default: 自动从 target URL 生成改写规则(运行时)
- off: 禁用 Location/Refresh 头改写
- custom: 使用预定义规则列表(预编译)
- 新增 RedirectRewriteRule 规则结构体,支持正则(~ 前缀)和前缀匹配
- 添加 validateRedirectRewrite 验证函数:
- Mode 有效性检查
- custom 模式必须有规则
- 正则表达式预编译检查
- 更新 GenerateConfigYAML 添加配置示例文档
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-15 17:01:07 +08:00
d5b4509014
refactor(config,server): 移除 Config.Server 字段,完善 servers 多服务器配置
...
- 移除 Config.Server 单服务器字段,统一使用 Servers 列表
- 为 ServerConfig 添加 Default 标记支持虚拟主机默认主机
- 重命名 GetDefaultServer 为 GetDefaultServerFromList
- 更新验证逻辑确保 servers 列表必填
- 更新默认配置生成和测试适配
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-15 13:32:36 +08:00
39371c7f06
refactor(config): 移除旧版 server 配置迁移逻辑
...
移除 migrateLegacyConfig 函数及其调用,不再支持旧版单 server
配置格式。配置文件必须使用 servers 数组格式。
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-15 12:48:36 +08:00
168d34d58d
refactor(config,app): 默认配置迁移到 servers 格式
...
- DefaultConfig() 使用 Servers[0] 替代 Server
- GenerateConfigYAML() 生成 servers 列表格式
- App.Run() 支持多服务器监听地址日志输出
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-15 10:34:04 +08:00
75b0d0758b
fix(config): 添加缺失的 Lua 配置默认值
...
- 补充 LuaMiddlewareConfig 默认配置
- 添加 Status.Format 字段默认值
- 简化验证逻辑
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 13:15:31 +08:00
50a960c539
feat(http2): 实现服务器优雅关闭功能
...
添加 GracefulShutdownTimeout 配置项,实现连接池管理和优雅关闭逻辑:
- HTTP2Config 新增 graceful_shutdown_timeout 字段(默认30秒)
- Server 添加连接池和等待组跟踪活跃连接
- Stop() 方法等待所有连接完成或超时后再退出
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 11:38:01 +08:00
532aa55a9c
feat(config): 添加服务器关闭超时配置
...
添加 ShutdownConfig 结构,支持配置优雅停止和快速停止超时:
- graceful_timeout: SIGQUIT 信号优雅停止超时(默认 30s)
- fast_timeout: SIGINT/SIGTERM 快速停止超时(默认 5s)
包含完整的验证逻辑和 YAML 生成支持。
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 11:25:31 +08:00
a10377f76d
refactor(config): 提取常量并删除未使用代码
...
- 新增 DefaultPprofPath 常量替代硬编码路径
- 删除 validate.go 中未使用的 parseSize() 函数
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-10 11:20:27 +08:00
ce43d5c1d6
feat(cache): 新增缓存清理 API
...
支持通过 HTTP API 主动清理代理缓存,提供精确路径和通配符模式清理。
包含 IP 白名单和 Token 认证支持。
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-09 17:58:18 +08:00
83b2a35715
feat(config): 完善 Resolver/SSL 默认值及 YAML 输出
...
- 添加 Resolver/SessionTickets/ClientVerify 默认配置
- GenerateConfigYAML 输出改为非注释格式
- 新增 Resolver、SSL 默认值、YAML 可加载性测试
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-09 14:44:01 +08:00
a298ca7254
feat(config): 完善 GenerateConfigYAML HTTP/2 和认证配置示例输出
...
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-09 13:25:47 +08:00
e03546a580
feat(config): 完善 GenerateConfigYAML 配置示例输出
...
新增 Session Tickets、mTLS 客户端验证、auth_request 外部认证、
TCP SSL、DNS resolver 及全局变量配置的 YAML 示例生成
2026-04-09 13:10:15 +08:00
412bfebdd8
feat(http2): 新增 HTTP/2 支持,集成到服务器和应用
2026-04-09 12:18:52 +08:00
238fa20e41
docs(config): 在默认配置生成中添加已实现功能的注释示例
...
新增 client_max_body_size、try_files、next_upstream、error_page 等功能的配置注释
Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-08 10:35:45 +08:00
ee4f7a5ca9
refactor(config): 移除已废弃的配置字段
...
移除以下已标记废弃的字段:
- AuthConfig.MinPasswordLength (auth)
- FileCacheConfig.LRUEviction (file_cache)
- TransportConfig.MaxIdleConns (transport)
同时清理相关的验证逻辑、默认值设置、测试代码和文档。
Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-08 09:50:32 +08:00
b30c84a38b
feat(config): 标记废弃字段,新增负载均衡算法验证
...
- MinPasswordLength 和 MaxIdleConns 添加 Deprecated 注释
- 验证时对废弃字段输出警告提示
- 新增 stream 负载均衡算法有效性验证
- 添加负载均衡算法验证测试用例
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-08 09:33:55 +08:00
58e420b3ab
docs(config): 新增完整配置字段参考文档
...
- 添加 docs/config-reference.md,列出所有配置字段及枚举值
- 更新 defaults.go 注释,补充 client_max_body_size、try_files、error_page 字段说明
Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-07 22:22:21 +08:00
766e9255fa
feat(config,server,makefile): 新增 pprof 性能分析端点支持
...
- 新增 PprofConfig 配置结构,支持路径和 IP 访问控制
- 实现 PprofHandler 处理器,提供 CPU/heap/goroutine/block/mutex profile
- Makefile 新增 build-perf、build-pgo、pgo-collect 目标
- 支持 PGO (Profile-Guided Optimization) 构建
Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-07 18:21:03 +08:00
83e1fe38ba
feat(config,handler,server): 支持多静态目录配置,新增路径前缀匹配
...
- Static 配置从单对象改为数组,支持多个静态目录
- StaticConfig 新增 Path 字段用于路径前缀匹配
- 添加 validateStatics 和 validatePathConflicts 验证函数
- 删除 config.example.yaml 示例文件(配置可通过 --generate 生成)
Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-07 16:26:55 +08:00
f2352ab9cc
docs(config,stream,logging,handler,proxy,cache,server,ssl,middleware): 为核心模块添加详细 GoDoc 文档注释
...
- config: 为 Config 和所有子配置结构添加完整文档,包含使用示例和注意事项
- stream: 为负载均衡器和服务器添加详细的参数、返回值和功能说明
- logging: 为日志格式化和输出函数添加文档,说明支持的变量替换
- handler: 为路由器、静态文件和 sendfile 处理器添加文档
- proxy: 为健康检查器和代理功能添加完整文档
- cache/server/ssl/middleware: 补充相关模块的文档注释
- config.example.yaml: 添加可信代理配置、加密套件示例,更新压缩级别说明
Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-07 15:36:09 +08:00
038c0639fd
feat(middleware,http3,config): 增强 gzip_static 支持 Brotli,新增 HTTP/3 0-RTT 与性能配置验证
...
- gzip_static: 支持 .br 和 .gz 预压缩文件,按 br > gzip 优先级选择
- http3/server: 新增 Enable0RTT 配置,启用时输出安全警告
- config: 废弃 FileCache.LRUEviction 字段,新增 validatePerformance 验证
- 新增 gzip_static 完整测试套件覆盖编码优先级、路径遍历防护等场景
Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-07 14:25:28 +08:00
20016ebb59
feat(config): 增强配置验证功能,添加多项验证函数
...
新增 validateLogging、validateRewrite、validateSecurityHeaders 验证函数,
完善限流算法、滑动窗口模式、一致性哈希键格式验证,
在 YAML 默认配置中添加 trusted_proxies 字段说明。
Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-07 13:15:27 +08:00
cd2d1a8194
refactor: 优化字符串构建方式,统一测试错误处理风格
...
- 使用 fmt.Fprintf 替代冗余的 WriteString(fmt.Sprintf) 组合
- 测试中 nil 检查使用 t.Fatal 替代 t.Error 立即终止
- .gitignore 添加 html/ 目录忽略
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-03 17:49:11 +08:00
a2e12c7537
refactor(config): 优化配置生成代码,使用 fmt.Fprintf 替代冗余写法
...
使用 fmt.Fprintf 直接写入 buffer,替代 buf.WriteString(fmt.Sprintf(...))
的冗余模式,代码更简洁高效。
Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-03 17:36:37 +08:00
ec916d882d
feat(proxy,middleware,config): 集成配置与代码差异修复
...
- 集成一致性哈希负载均衡到 proxy.go,支持 hash_key 和 virtual_nodes 配置
- 集成滑动窗口限流算法到 ratelimit.go,支持 algorithm 选择
- 应用 Transport 连接池配置到 createHostClient
- 集成 HSTS 配置到安全头部中间件
- 补充 config.example.yaml 缺失配置(http3、gzip_static、sliding_window)
Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-03 16:08:45 +08:00