65 Commits

Author SHA1 Message Date
xfy
f73a761632 fix(server): protect accessLogMiddleware and accessControl from concurrent writes 2026-06-05 11:49:19 +08:00
xfy
2be6b67d0b fix(server): release MatchResult back to pool after use
Add matcher.ReleaseMatchResult(result) in the base handler to prevent
sync.Pool object leak. Every Match() call acquires from pool but the
caller never returned objects, causing unbounded pool growth.
2026-06-04 11:14:32 +08:00
xfy
1eeab88c98 perf(server): pass ctx.Path() directly to Match, eliminate string alloc
Removes the string(ctx.Path()) conversion that caused one heap
allocation per request in the routing hot path.
2026-06-04 11:06:00 +08:00
xfy
29752f62bd fix: resolve golangci-lint issues across multiple packages
- stream: fix atomic.Int64 usage in tests and benchmarks
- server: fix errcheck, goconst ("tcp" -> constant), and govet shadow
- app: add missing ServerModeAuto case in requiresFullRestart
- lua: fix nolintlint unused directive warnings
- proxy: use `any` instead of `interface{}`
2026-06-03 18:17:07 +08:00
xfy
5ee83f6a69 refactor: extract common functions from server startup modes 2026-06-03 18:03: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
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
f58f194752 fix(server): serialize listener creation in multi-server mode
Remove VHost fallback during graceful upgrade. Serialize listener
creation before parallel router/middleware setup to prevent concurrent
inherited listener consumption. Fix tcpAddrMatch to match when either
side is any-addr (0.0.0.0/::).
2026-06-03 11:44:14 +08:00
xfy
f3f78b24a8 feat(server,app): implement proper config hot reload via SIGHUP
createListener now checks pre-set s.listeners (Path 2) for hot reload,
not just upgradeManager.IsChild() (Path 1). Add DupListener to dup FDs
so old/new servers own independent listeners. Reload rebuilds HTTP/2
and HTTP/3. Add matchInheritedListener with TCP any-addr matching.
Add requiresFullRestart with VHost server count detection.
2026-06-03 11:42:45 +08:00
xfy
38bb743781 fix(server): handle LocationEngine registration errors properly
Add typed ConflictError for path conflicts, change register functions
to return errors, handle conflicts as warnings and fatal errors as
startup failures. Remove all 20 instances of ignored Add* return values.
2026-06-03 10:12:09 +08:00
xfy
ac66ea5534 fix(server): use atomic.Bool for Server.running to eliminate data race
Server.running was a plain bool accessed from multiple goroutines
(start/stop/signal handlers). Convert to atomic.Bool with
Store/Load to make all accesses safe for concurrent use.

Updates all test files to use the new atomic API.
2026-06-03 01:19:23 +08:00
xfy
8643a0aff9 refactor: improve error handling and fix linter warnings
- Add nolint comments for type assertion errcheck in gjson/encode.go
  (switch case guarantees type safety)
- Handle fasthttp.Serve errors in benchmark mock backends
- Rename error variables to avoid shadowing in server.go
- Use underscore for unused loop variables

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-09 17:32:06 +08:00
xfy
8f3c304837 fix(lua): register Lua routes in multi-server mode
Add registerLuaRoutes method for router-based route registration
and call it in startMultiServerMode to fix Lua routes not working
when multiple servers are configured.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-09 13:41:26 +08:00
xfy
a5b5a085cc feat(lua): add Lua route registration in server startup
Add call to registerLuaRoutesWithLocationEngine between proxy and static
route registration, ensuring correct routing order: proxy -> lua -> static.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-09 11:44:39 +08:00
xfy
9c46c8bab8 fix(server): register status and pprof handlers in multi-server mode
In startMultiServerMode, status and pprof handlers were not registered,
causing /_status and /debug/pprof endpoints to return 404. Now these
handlers are registered on the server with default: true, consistent
with startVHostMode behavior. Also fixed cache API registration to
use default server instead of first server.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-07 17:44:57 +08:00
xfy
d269940d8b style: fix formatting issues
- Add missing newlines at end of files
- Fix indentation in ssl.go
- Remove extra blank lines

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-30 13:42:53 +08:00
xfy
7eaea845e7 refactor(server): extract createFastServer helper
- 新增 createFastServer 统一 fasthttp.Server 创建
- 消除 startSingleMode/startVHostMode/startMultiServerMode 中的重复代码
- multi_server 模式现在也支持高并发优化配置

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-29 17:48:40 +08:00
xfy
cf2fcca7e8 refactor: 提取公共逻辑、消除重复代码、加强错误处理
- 提取 App 公共逻辑到 app_common.go,消除 app.go/app_windows.go 重复定义
- 提取 Server 生命周期/中间件/路由逻辑到独立文件(lifecycle.go/middleware_builder.go/router.go)
- 提取 Proxy 缓存处理/头部修改/目标选择到独立模块
- 提取 CheckIPAccess/CheckTokenAuth 到 utils/httperror.go,消除 status/purge 重复实现
- 修复 stream 双向转发:任一方向完成立即关闭双端,避免连接泄漏
- 修复 SSL/TLS 中静默忽略错误的问题,添加日志记录
- 统一日志消息为英文

💘 Generated with Crush

Assisted-by: GLM 5.1 via Crush <crush@charm.land>
2026-04-28 18:00:48 +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
1e38fe9e90 fix: 显式忽略不需要处理的错误返回值
对 os.Remove、conn.Close 等清理操作的返回值使用 _ 忽略,
避免 errcheck 静态检查告警。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-24 10:41:35 +08:00
xfy
2bdc8f3b3b refactor(handler,middleware,server): 增强预压缩配置灵活性
- NewGzipStatic 增加 precompressedExtensions 参数,支持自定义预压缩扩展名
- SetGzipStatic 分离源文件扩展名和预压缩扩展名参数
- 更新相关测试用例

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-22 13:35:03 +08:00
xfy
2b63666ea5 feat(proxy,server): 实现 ProxyBind/Buffering/ProxyURI 和响应头控制
ProxyBind 支持指定本地地址出站连接,Buffering 控制响应缓冲模式,
ProxyURI 实现 nginx proxy_pass URI 替换语义,
响应头新增 HideResponse/PassResponse/IgnoreHeaders/Cookie 域路径重写,
健康检查集成 RecordFailure/RecordSuccess 软失败状态。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-21 11:28:32 +08:00
xfy
3033d57764 feat(server): 实现 server_tokens 和 types 配置
- getServerName: 根据 ServerTokens 控制版本号显示
- applyTypesConfig: 应用自定义 MIME 类型配置
- 更新 location 添加方法调用

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-20 18:08:25 +08:00
xfy
28be9e7e66 fix(server): 添加 shutdownServers nil ctx 防御性检查
防止 ctx 参数为 nil 时导致 panic:
- nil ctx 时使用 context.Background() 作为默认值
- 防御性检查确保函数健壮性

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-20 08:26:51 +08:00
xfy
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
xfy
5f60dc9ff7 feat(server): 为静态文件处理添加符号链接安全检查
在 registerStaticHandlersWithLocationEngine 和 registerStaticHandlers
两个方法中设置 SymlinkCheck 安全检查,防止通过符号链接访问敏感文件。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-17 10:41:13 +08:00
xfy
6281196dfd refactor(server): 优化结构体字段布局并使用 matcher 常量
- Server 和 VHostManager 结构体字段重新排序优化内存布局
- RegexHostMatcher 字段顺序调整
- 使用 matcher.LocationType* 常量替代硬编码字符串
- server_names 数组支持:遍历注册所有主机名
- 提取 createListener 方法支持 Unix socket

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-17 10:11:57 +08:00
xfy
359afb5e24 feat(server): 集成 LocationEngine 和 Unix socket 监听支持
- 使用 LocationEngine 替代 fasthttp/router 进行路由匹配
- 新增 createListener 支持 Unix domain socket 监听
- 支持热升级场景下的 Unix socket 继承
- 新增 registerProxyRoutesWithLocationEngine 和 registerStaticHandlersWithLocationEngine
- 添加 SetUpgradeManager 方法供 App 层注入

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-17 09:26:38 +08:00
xfy
bec8932561 feat(server): 添加缓存清理 API 支持
新增 PurgeHandler 处理器,支持:
- 按精确路径和通配符模式清理缓存
- HTTP 方法过滤(默认 GET)
- IP 白名单访问控制(CIDR/单 IP/localhost)
- Token 认证保护
- 三种启动模式路由注册

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 16:47:10 +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
470c82d940 style(proxy,server): 代码风格优化
- headers.go: 添加协议常量 protoHTTP/protoHTTPS
- redirect_rewrite.go: 添加模式常量,修正缩进
- proxy_ssl_test.go: 表格测试字段对齐
- server.go: 添加 ServerModeAuto 分支防御性处理

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 09:54:09 +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
fa55bfd497 feat(cache): 添加缓存 TTL 新鲜度验证优化
在 FileEntry 中新增 CachedAt 字段记录缓存时间,实现 TTL 窗口内的缓存新鲜度验证:
- TTL 内跳过 ModTime 验证,减少 os.Stat 调用
- TTL 过期后验证 ModTime,文件未修改时刷新 CachedAt
- 默认 TTL 设置为 5 秒

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-15 17:53:51 +08:00
xfy
b0380f8798 feat(server): 改用前缀匹配注册代理路由
使用 fasthttp/router 通配符路由替代精确匹配:
- path: / 匹配所有子路径如 /sorry/index
- path: /api/ 匹配 /api/* 所有子路径
- 路由格式: {path}/{path:*}

此改动确保代理路径下所有子请求都能正确路由到代理处理器,
支持 redirect_rewrite 功能正确处理子路径的重定向改写。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-15 17:01:30 +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
f04f804834 refactor(server): 移除废弃的 Stop 方法,更新测试使用 StopWithTimeout
移除 deprecated Stop() 方法,测试代码改用 StopWithTimeout。
testutil.go 更新为支持 servers 配置格式。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-15 12:48:49 +08:00
xfy
1f170b1951 refactor(server): 实现多服务器模式启动和关闭逻辑
- 新增 fastServers 列表支持多监听器
- 实现 startMultiServerMode() 并行启动多个服务器
- 添加 shutdownServers() 并行关闭多个 fasthttp.Server
- 重构 StopWithTimeout/GracefulStop 支持多服务器关闭
- startSingleMode 使用 Servers[0] 配置(迁移后)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-15 10:33:52 +08:00
xfy
9144dcbb06 refactor(server): 提取初始化逻辑到独立函数
- 将 Start() 中的 goroutine pool 初始化提取为 initGoroutinePool()
- 将 file cache 初始化提取为 initFileCache()
- 将 Lua engine 初始化提取为 initLuaEngine()
- 将 error page manager 初始化提取为 initErrorPageManager()
- 添加 init.go 存放提取的初始化函数
- 添加 init_test.go 测试初始化函数
- 添加 testutil.go 提供测试 mock 和工具
- 添加 lua_integration_test.go Lua 中间件集成测试
- 添加 start_integration_test.go Start() 集成测试
- 添加 server_test.go nil tlsManager 测试
- 添加 lua/mock_engine.go Lua 引擎 mock 实现
- 添加 lua/api_balancer_test.go Lua balancer API 测试

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 17:38:49 +08:00
xfy
5b41c3ab44 refactor(server): 使用 NewTargetFromConfig 简化目标创建 2026-04-13 16:40:50 +08:00
xfy
d21e27fbac fix(lint): 修复 golangci-lint 错误 (119 -> 0 issues)
主要修复:
- errcheck: defer Close 使用 //nolint:errcheck,类型断言改为 ok 检查
- govet fieldalignment: 调整结构体字段顺序优化内存布局
- revive unused-parameter: 将未使用参数改为 _
- exhaustive: 添加缺失的 switch case 或 default
- goconst: 提取重复字符串为常量 (accessAllow, accessDeny 等)
- staticcheck SA9003: 修复空分支逻辑
- gofmt: 运行 gofmt -w 格式化
- nolintlint: 修复 nolint 注释格式

其他改进:
- 更新 .golangci.yml 配置,启用更严格的检查
- 移除未使用的代码和导入
- 简化测试辅助函数调用

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 16:15:31 +08:00
xfy
0152dd1d35 feat(server): 添加 StopWithTimeout 方法支持自定义超时
新增 StopWithTimeout 方法,支持传入自定义超时参数:
- 替代原有固定 5s 超时的 Stop 方法
- timeout <= 0 时自动使用默认 5s
- 原 Stop 方法标记为 Deprecated,内部调用 StopWithTimeout

为配置化关闭超时提供基础设施。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 11:25:39 +08:00
xfy
344bc79f67 fix(server): 添加 Shutdown 超时和 CloseOnShutdown
为快速停止添加 5 秒超时防止无限等待,设置 CloseOnShutdown
确保连接在关闭时被正确清理。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 10:59:07 +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
153982121e feat(server): 集成 Lua 中间件到服务器生命周期
- 在 Server 结构添加 luaEngine 字段
- 实现 buildLuaMiddlewares 按阶段创建中间件
- 在 Start() 初始化 Lua 引擎,Stop/GracefulStop 关闭

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-11 14:31:48 +08:00
xfy
d22c20cbbb feat(server): 添加 AuthRequest 外部认证中间件
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-10 13:22:35 +08:00
xfy
7950f17da5 refactor(server): 适配变量系统重命名
适配 variable.NewContext/ReleaseContext 重命名

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-10 09:40:47 +08:00
xfy
9495a548e6 feat(proxy,server,logging): 集成变量系统和 DNS 解析器
变量系统集成:
- logging: 访问日志使用 variable 包展开模板
- rewrite: 重写规则支持变量展开
- proxy: 请求/响应头设置支持变量展开

DNS 解析器集成:
- app: 创建并启用 Resolver
- server: SetResolver/GetResolver 方法传递给 proxy
- proxy: SetResolver/Start 方法,后台 DNS 刷新协程
- proxy_dns.go: DNS 刷新逻辑和 IP 直连支持

新增集成测试:
- internal/integration/variable_test.go
- internal/integration/resolver_test.go

文档更新:
- docs/config-reference.md 配置参考文档

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-08 11:36:49 +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
23f5c08614 feat(handler,server): 静态文件新增 try_files 配置,集成新增中间件
- StaticHandler 支持 try_files 配置,用于 SPA 部署回退
- 支持 $uri 和 $uri/ 占位符解析
- Server 集成 bodylimit 中间件(全局和路径级别配置)
- Server 集成 errorintercept 中间件(错误页面拦截)
- Server 预加载错误页面到内存

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-07 17:50:38 +08:00