64 Commits

Author SHA1 Message Date
xfy
e8fbbf368c fix(config,server): merge defaults on Load and fix monitoring registration
Two related fixes that must land together:

1. config.Load() now starts from DefaultConfig() before unmarshaling
   YAML. This ensures missing top-level fields (Performance,
   Monitoring, Resolver) use their documented defaults instead of
   zero values. Most importantly, file_cache is no longer silently
   disabled when users omit the performance: section.

2. startSingleMode() now checks Monitoring.Status.Enabled instead of
   Path/Allow to decide whether to register the status endpoint.
   Without this change, fix #1 would have caused a regression where
   the status handler is registered even when monitoring is disabled,
   because DefaultConfig() sets Path and Allow defaults.

Also replace remaining log.Printf in status.go and lua/api_timer.go
with zerolog to follow project logging conventions.

Added tests:
- config/load_test.go: verifies defaults are applied, explicit values
  override defaults, and monitoring stays disabled by default.
- server/monitoring_registration_test.go: verifies /_status is only
  registered when enabled and remains reachable with static handler
  on path: /.
2026-06-11 15:08:57 +08:00
xfy
6f17bbad7e chore: remove trailing blank lines and clean up whitespace 2026-06-03 18:08:34 +08:00
xfy
1a6b5f9166 Merge origin/master into master 2026-06-03 16:36:23 +08:00
xfy
2734b04d8f refactor: remove 16.8k lines of dead code across all internal packages
- Delete unused files: tempfile subsystem, matcher variants, server/internal
- Remove 200+ unused functions across proxy, ssl, lua, http2/3, stream, variable
- Fix proxy test type errors (backgroundRefresh ctx→Request)
- Move bench/tools mock backend into internal/testutil
- Remove corresponding test functions for all deleted code
2026-06-03 16:15:43 +08:00
xfy911
fc1de2d445 docs: add documentation comments for more exported constants and variables
- Add comments for ssl/client_verify.go verification modes
- Add comments for security/auth.go hash algorithms
- Add comments for rewrite/rewrite.go flags
- Add comments for compression/compression.go algorithms
- Add comments for limitrate/limitrate.go strategies
- Include author attribution (xfy)
2026-06-03 15:28:53 +08:00
xfy
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
xfy
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
xfy
2e9ddc7400 feat(config): implement include directive with glob support
Support loading config fragments from external files via include
directive. Servers and streams are appended, variables merged with
main config priority. Includes glob expansion, nested includes
(depth limit 10), and circular include detection.
2026-06-03 10:20:33 +08:00
xfy
d9a7ab9cca cleanup(config): remove dead ProxyCachePathConfig and CachePath field
Disk cache implementation was previously removed but config structs
remained. Remove ProxyCachePathConfig, Config.CachePath field, e2e
WithCachePath helper, and docs reference.
2026-06-03 10:14:07 +08:00
xfy
179090fa34 fix(security): 修复 2 个 CRITICAL + 6 个 HIGH 安全与代码质量问题
安全修复:
- ConnLimiter Acquire() TOCTOU 竞态: atomic.AddInt64 替代 loadInt64+addInt64
- Cache Purge token 时序侧信道: 改用 subtle.ConstantTimeCompare
- Lua Cosocket SSRF: 新增 ip_guard 两层 IP 检查(字面量+解析后),拒绝私有/回环地址
- X-Accel-Redirect 路径遍历: urlpath.Clean + 前缀拒绝(/internal/ /admin/)
- CRLF 注入: containsCRLF 校验变量展开后的 header 值,logging.Warn 可观测
- Proxy URI 注入: bytes.ContainsAny 检查 path 中的 @\r\n 危险字符

代码质量:
- disk_cache.go Set() 7 处静默 return 改为 logging.Error 日志记录
- config.go 从 2392 行拆分为 9 个按域文件(config/server/proxy/security/ssl/cache/performance/monitoring/variable)

验证: go build + vet + golangci-lint(0 issues) + test(83.2% 无回归) + race detector 全部通过

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-28 10:13:47 +08:00
xfy
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
xfy
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
xfy
0cf943fede refactor(config,test): 优化 parseSize 为 switch 并适配 NewProxyCache 签名
将 parseSize 的 if-else 改为 switch 语句;更新集成测试中
NewProxyCache 调用以匹配新增的 stale_if_error/stale_if_timeout 参数。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-24 10:41:56 +08:00
xfy
91d67ad384 refactor(cache,config): 代码清理和优化
- disk_cache: 忽略 filepath.Walk 和 Delete 返回值
- tiered_cache: 忽略 l1.Delete 返回值,删除未使用的 revalidate 函数
- config: 简化 multiplier 变量声明

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-22 13:34:51 +08:00
xfy
92b7040a5f feat(proxy,config): 增强健康检查和缓存配置
健康检查增强:
- 添加 HealthMatch 接口支持自定义健康判断逻辑
- 支持状态码范围、响应体正则、响应头匹配
- 集成 SlowStartManager 实现慢启动

配置增强:
- HealthCheckConfig 新增 Match 和 SlowStart 字段
- ProxyBufferingConfig 支持 Buffers 配置字符串格式
- 新增 ProxyCachePathConfig 磁盘缓存路径配置
- 添加 StaleIfError/StaleIfTimeout 缓存配置

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-22 13:15:18 +08:00
xfy
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
xfy
8baee13503 fix(proxy,config): 修复 HealthChecker 重启支持和补充 random 算法文档
Stop 后重建 stopCh 以支持再次 Start;config 注释补充 random 算法。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-21 11:41:16 +08:00
xfy
c0b7e30bf0 feat(config,loadbalance): 添加上游服务器参数和 random 负载均衡算法
Target 新增 MaxConns/MaxFails/FailTimeout/Backup/Down/ProxyURI 字段,
实现 IsAvailable/RecordFailure/RecordSuccess 软失败机制,
filterHealthy 支持备份服务器优先级选择,
新增 random(Power of Two Choices)负载均衡算法。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-21 11:28:02 +08:00
xfy
d7e7b36048 feat(config): 添加 limit_rate、types、server_tokens 配置
- LimitRateConfig: 响应速率限制配置
- TypesConfig: 自定义 MIME 类型映射
- ServerTokens: 控制 Server 响应头版本号显示
- Internal: location 内部访问限制标记

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-20 18:07:20 +08:00
xfy
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
xfy
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
xfy
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
xfy
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
xfy
a644e551af feat(proxy): 添加上游 SSL 配置和缓存有效期分段配置
- ProxySSLConfig: 支持自定义 CA、客户端证书(mTLS)、SNI、TLS 版本控制
- ProxyCacheValidConfig: 按 HTTP 状态码分段配置缓存有效期
- proxy_ssl.go: 实现 CreateTLSConfig 和 TLS 版本解析
- proxy.go: 集成 SSL 配置到 HostClient,实现 getCacheDuration 分段缓存
- 测试文件适配新配置

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-15 18:27:50 +08:00
xfy
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
xfy
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
xfy
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
xfy
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
xfy
6007e96f69 refactor(config): 添加 ServerMode 类型和多服务器模式支持
- 新增 ServerMode 枚举类型 (single/vhost/multi_server/auto)
- 实现 migrateLegacyConfig() 自动迁移旧版 server 配置到 servers[0]
- 添加 validateMode() 和 validateListenConflicts() 验证函数
- 配置加载时自动迁移,保持向后兼容

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-15 10:33:40 +08:00
xfy
686b8c3239 refactor(lua): 调整配置字段顺序将协程池参数集中
将 CoroutinePoolWarmup 字段移至 CoroutineStackSize 后,
使协程相关配置字段集中在一起,便于理解和维护。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-14 14:53:12 +08:00
xfy
cf0ea6cc1f perf(lua): 完成协程栈优化配置集成
将 Lua 协程栈优化选项集成到配置系统中:
- CoroutineStackSize: 默认64,减少内存分配
- MinimizeStackMemory: 启用栈内存自动收缩
- CoroutinePoolWarmup: 默认预热4个协程

影响文件:
- internal/config/config.go: 添加配置项到 LuaGlobalSettings
- internal/lua/middleware_config.go: 中间件配置支持
- internal/server/init.go: 服务器初始化应用配置

优化效果(已在 engine.go 和 config.go 中实现):
- BenchmarkCoroutineCreation 内存减少18.7% (112KB->91KB)
- 延迟减少约27.6% (29us->21us)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-14 14:33:30 +08:00
xfy
ef652cdab1 feat(static): 添加符号链接安全检查功能 2026-04-13 16:40:50 +08:00
xfy
103e8ff0cf feat(geoip): 添加基于国家代码的 GeoIP 访问控制功能
- 新增 GeoIPConfig 配置结构,支持 MaxMind MMDB 数据库
- 实现 GeoIPLookup 查询器,带 LRU 缓存和 TTL 支持
- AccessControl 集成 GeoIP 检查,按国家代码过滤请求
- 支持私有 IP 特殊处理策略 (allow/deny)
- 添加完整的单元测试和配置验证测试
- 新增 stream-udp.conf 示例配置文档

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 16:14:44 +08:00
xfy
6a08a6ab4a fix(lint): 修复结构体字段对齐问题
调整 Config、Server、APIMethod 结构体字段顺序以优化内存布局,
解决 govet fieldalignment 检查警告。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 11:43:57 +08:00
xfy
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
xfy
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
xfy
8b382606df Merge branch 'lint-fix' - resolve sendfile.go conflict
Conflict: sendfile.go (!linux build tag) was incorrectly modified to
include linuxSendfile and getSocketFd functions which already exist
in sendfile_linux.go.

Resolution: Keep HEAD version (simple fallback returning ENOTSUP) as
Linux implementation is properly separated in sendfile_linux.go.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 09:26:48 +08:00
xfy
1e545717fb feat(config): 添加 Lua 中间件配置结构和验证
- 定义 LuaMiddlewareConfig、LuaScriptConfig、LuaGlobalSettings
- 添加 validateLua 验证脚本路径、阶段和超时配置
- 添加 lua_config_test.go 测试配置验证逻辑

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-11 14:31:43 +08:00
xfy
76c2a6028c feat(handler,config): 支持 try_files 动态后缀 ($uri.<ext>)
- 实现 $uri.<ext> 模式解析(如 $uri.html, $uri.json)
- 添加扩展名安全验证:白名单字符、危险后缀黑名单
- 根路径边界处理:避免生成 "/.html" 隐藏文件名
- 增强配置文档注释,说明 nginx 兼容性和安全限制
- 完整单元测试覆盖

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-10 18:30:28 +08:00
xfy
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
xfy
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
xfy
612208616c feat(status): 支持 Prometheus 格式输出
新增 Prometheus 格式的状态指标输出,用于监控系统集成。
包含连接数、请求数、缓存、Pool、Upstream、SSL、限流等指标。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-09 17:51:28 +08:00
xfy
412bfebdd8 feat(http2): 新增 HTTP/2 支持,集成到服务器和应用 2026-04-09 12:18:52 +08:00
xfy
9d49349ee1 feat(ssl,config): 新增 Session Tickets 和 mTLS 客户端证书验证
- SessionTicketsConfig 支持 TLS 1.3 会话恢复,密钥轮换和持久化
- ClientVerifyConfig 支持双向 TLS 认证,CA 证书池和 CRL
- TLSManager 集成 SessionTicketManager 和 ClientVerifier
- 新增完整测试覆盖密钥轮换和客户端验证逻辑

Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-08 14:36:47 +08:00
xfy
5a6f4d351f feat(resolver): 新增 DNS 解析器支持
新增 internal/resolver 包,提供动态 DNS 解析功能:
- Resolver 类型支持配置多个 DNS 服务器
- 内置缓存支持 TTL 和最大条目数限制
- 统计信息追踪查询次数和缓存命中率

配置层面:
- ResolverConfig 支持 addresses/valid/timeout/ipv4/ipv6/cache_size
- 添加配置验证逻辑

Target 增强:
- 新增 hostname/resolvedIPs 字段支持 IP 缓存
- NeedsResolve 方法判断是否需要重新解析
- NewTargetFromConfig 工厂函数

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-08 11:36:49 +08:00
xfy
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
xfy
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
xfy
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
xfy
f6245c19e0 feat(proxy,loadbalance,config): 新增故障转移 (next_upstream) 支持
- 配置新增 NextUpstreamConfig 支持 tries 和 http_codes 参数
- 负载均衡器新增 SelectExcluding 方法用于故障转移排除选择
- 代理请求失败时自动尝试下一个健康后端
- 健康检查新增 MarkHealthy 方法用于故障转移成功后恢复状态

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-07 17:49:10 +08:00
xfy
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