66 Commits

Author SHA1 Message Date
xfy
d0867bfe3e refactor(lua): remove unused mock engine and filter writer subsystem
- Delete mock_engine.go (331 lines): unused MockLuaEngine/MockCoroutine
- Delete filter_writer.go (811 lines): DelayedResponseWriter not integrated
- Delete filter_phase_test.go (1466 lines): tests for removed filter code
- Total: 2608 lines of dead code removed
2026-06-03 16:31:18 +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
c37364b309 style: format code and modernize loop syntax
- Align struct fields and constants in gjson/config.go
- Add missing newline at EOF in gjson/decode.go
- Remove trailing blank line in gjson/encode.go
- Remove extra blank line in internal/lua/coroutine.go
- Use modern for range syntax in internal/lua/pool.go

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-09 17:34:59 +08:00
xfy
c157be1ce5 refactor(cache): remove unused disk/tiered cache and add helper functions
Remove unused disk cache, tiered cache, purge, and config loader code.
Add HashPathWithMethod and MatchPattern helpers for future cache purge API.
Update test to use new mock backend API with ResponseBody field.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-09 17:26:19 +08:00
xfy
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
xfy
62be8bc557 feat(lua): enable package library and preload gjson module
Add glua.OpenPackage for require support and preload gjson module
to make JSON encoding/decoding available in Lua scripts.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-09 15:51:26 +08:00
xfy
fb655829e1 feat(lua): add LuaRouteHandler for route-based script execution
Create LuaRouteHandler that implements fasthttp.RequestHandler interface,
allowing Lua scripts to be registered as standalone route handlers.
Handles ngx.exit/ngx.redirect as normal exits, not errors.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-09 11:37:42 +08:00
xfy
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
xfy
6c7cf73c87 refactor(lua): replace single LState with LState pool architecture
Replace the single LState + coroutine model with an LState pool to
eliminate concurrent map read/write issues in gopher-lua. Each request
now gets a completely independent LState with its own Global table.

Key changes:
- Add LStatePool for managing pooled LState instances
- Remove shared Engine.L and coroutine-based execution
- Simplify coroutine.go: remove yield handling, use direct PCall
- Remove ngxRegisterMu lock (no longer needed with isolated LStates)
- Update config.go: add LStatePoolInitialSize/MaxSize settings
- Update tests to work with the new architecture

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-09 10:38:10 +08:00
xfy
b1e1547e36 fix(lint): resolve errcheck and goconst issues
- Add nolint comments for sync.Pool.Get() type assertions (pool always returns valid pointers)
- Extract TLS version strings to constants in sslutil/tlsconfig.go
- Extract expires directive strings to constants in handler/static.go

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-30 13:41:15 +08:00
xfy
f145a8770e refactor: modernize code with Go 1.22+ features
Apply modern Go patterns across the codebase:
- Replace `interface{}` with `any` (Go 1.18+)
- Use `for range n` instead of `for i := 0; i < n; i++` (Go 1.22+)
- Replace `sort.Slice` with `slices.Sort` from slices package
- Simplify sync.WaitGroup patterns with errgroup where appropriate
- Add Makefile targets for modernize analyzer

Total: 84 files updated, net reduction of 79 lines

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-30 10:37:45 +08:00
xfy
fdf04476e8 refactor(lua): merge variable getter functions
Make getVariable call getVariableLua and convert LValue to string,
eliminating ~85 lines of duplicate variable access logic.

Both functions now share the same switch-case implementation.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-29 18:25:41 +08:00
xfy
f82e363f58 refactor: 提取 Lua ngx 表 helpers 和统一验证函数
Batch 1 续:
- 新增 lua/helpers.go:GetOrCreateNgxTable/GetOrCreateNgxSubTable
- 重构 compression:提取 resettableWriteCloser 接口和 compressorPool
- 新增 validate.go:ValidateNonNegativeInt64/Duration/NoNullByte/PathTraversal
- 消除约 120 行重复代码

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-29 17:00:11 +08:00
xfy
247fa81c00 fix(lua): 修复 Lua 引擎并发安全问题
- 缓存 coroutine.yield/status 函数,避免并发读取全局 Lua 状态机
- 添加 ngxRegisterMu 锁保护共享 ngx 表的并发写入
- 各 API 注册函数检查字段是否已存在,避免重复写入
- TCPSocket.currentOp 字段添加锁保护

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-29 14:19:11 +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
2122067efb docs: 更新 AGENTS.md 文档,添加新模块说明
- 更新所有 AGENTS.md 时间戳至 2026-04-24
- 添加 converter、e2e、testutil 模块文档
- 更新 README.md:nginx 配置导入、stale 缓存功能说明
- 更新项目统计:132 Go 文件,157 测试文件

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-24 18:24:31 +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
f91a40cc1d test(lua): 添加边界场景和 Scheduler 模式测试
- 添加协程沙箱、定时器句柄、共享字典容量边界测试
- 添加 Scheduler 模式 API 安全限制测试

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-23 17:20:32 +08:00
xfy
5354dceaf7 fix(lua): 修复 remote_port 和 server_port 变量返回空值
- 实现 remote_port 从 RemoteAddr 正确解析端口
- 实现 server_port 从 LocalAddr 正确解析端口
- 使用 strings.LastIndex 兼容 IPv4/IPv6 地址格式

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-20 16:03:41 +08:00
xfy
1c3e04afdb docs(lua): 为 Lua API 模块添加标准化 godoc 注释
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-20 11:23:03 +08:00
xfy
ad177e9640 docs(lua): 为 Lua API 模块添加标准化 godoc 注释
为所有 Lua API 文件添加完整的包级和函数级文档注释:
- api_balancer: 负载均衡 API(set_current_peer, set_more_tries 等)
- api_ctx: 请求上下文存储 API(ngx.ctx)
- api_location: 子请求捕获 API(ngx.location.capture)
- api_log: 日志输出 API(ngx.log)
- api_req: 请求对象 API
- api_resp: 响应对象 API
- api_shared_dict: 共享字典 API
- api_socket_tcp: TCP socket API
- api_timer: 定时器 API
- api_var: 变量 API
- engine: Lua 引擎核心
- context: 请求上下文管理
- coroutine: 协程调度器
- middleware: 中间件集成
- filter_writer: 响应过滤器
- cache: Lua 脚本缓存
- shared_dict: 共享字典实现
- socket_manager: socket 连接管理

注释格式遵循 Go 官方风格,包含功能说明、参数说明和注意事项。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-20 10:59:17 +08:00
xfy
7ef16f2be1 test(lua): 添加 api_shared_dict 和 engine 测试覆盖
- api_shared_dict_test.go: 测试 shared_dict API 功能
- engine_test.go: 测试 Lua 引擎初始化和执行

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-20 08:27:24 +08:00
xfy
d856e3c570 fix(lua): 改进 dictReplace key 存在性检查逻辑
修复 shared_dict.replace() 方法对过期 key 的判断:
- 区分 key 不存在和 key 存在但已过期的情况
- Get() 返回 val="" 且 expired=false 表示 key 不存在
- 先检查不存在情况,再检查过期情况

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-20 08:26:50 +08:00
xfy
584ca9170d test(lua,security): 优化测试代码错误返回值处理
- api_socket_tcp_test.go: 使用空白标识符忽略非关键错误返回值
- auth_test.go: 使用空白标识符忽略不需要的 hash 返回值

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 18:24:05 +08:00
xfy
9d95aecd6f refactor(lua): 提取常量并优化结构体字段布局
- api_var.go: 提取 argPrefix 常量,消除魔法字符串
- coroutine.go: 按逻辑分组重排 LuaCoroutine 字段,改善可读性

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 18:24:05 +08:00
xfy
f123018f2d feat(lua): 增强变量 API 支持数值类型返回和测试覆盖
- 新增 getVariableLua 方法返回 Lua 类型而非字符串
- request_length 等变量返回数值类型而非字符串
- luaVarNewIndex 支持 nil 值转换为空字符串
- 添加 api_var 全面的单元测试覆盖
- 添加 api_ctx 上下文 API 测试
- 添加 api_socket_tcp TCP socket 测试

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 18:12:16 +08:00
xfy
8ed800271d test: 迁移基准测试循环到 Go 1.24 b.Loop() API
- 所有 *_bench_test.go 文件从 for i := 0; i < b.N; i++ 改为 for b.Loop()
- 部分测试文件从 for i := 0; i < N; ... 改为 for range N 或 for i := range N
- 涵盖模块: cache, handler, http2, http3, loadbalance, logging, lua,
  middleware/accesslog, middleware/bodylimit, middleware/rewrite,
  middleware/security, netutil, resolver, server, ssl, stream

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 13:50:15 +08:00
xfy
d2e72b25be perf(lua): 添加 LuaContext 和协程池复用效果验证基准测试
添加 BenchmarkLuaContextPoolReuse 测试完整请求生命周期下的池复用率,
添加 BenchmarkLuaCoroutinePoolThroughput 测试高频率协程创建销毁的吞吐量。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 11:44:31 +08:00
xfy
49f1e26760 refactor(lua): 对象池类型安全优化
- 使用 any 替代 interface{} (Go 1.18+)
- 添加类型断言检查防止 Pool 误用

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 11:13:53 +08:00
xfy
6dd651af5f perf(lua): LuaContext 对象池化优化
- 添加 luaContextPool 复用 LuaContext 对象
- 新增 AcquireContext 函数从池中获取
- Release 方法重置所有可变状态防止污染
- 添加状态隔离测试和多次复用测试
- 添加池化基准测试

降低 GC 压力,减少高频请求下的对象分配

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 11:09:26 +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
fd4e164ae6 refactor(security): 滑动窗口限流器使用分段锁优化并发性能
将单一 counters map + 全局 mutex 改为 16 buckets 分段锁结构:

- 新增 limiterBucket 结构体,每个桶独立持有 RW 锁和计数器 map
- 使用 FNV-1a 哈希算法将键均匀分布到 16 个桶中
- 各方法修改为按 bucket 分发操作:
  - Allow() / allowApproximate() / allowPrecise()
  - Reset() / ResetAll() / Cleanup()
  - GetStats() / GetCount()

收益:
- 并发场景下锁竞争降低约 94% (16 个桶并行)
- 基准测试显示并行 Allow 操作约 89ns/op

测试验证:
- go test -race 通过并发安全测试
- 基准测试显示吞吐提升

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-14 14:26:01 +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
73ef7f4916 fix(lint): 修复剩余 lint 错误
- 统一八进制权限格式为 Go 1.13+ 风格 (0o644/0o755)
- 调整 Target 结构体字段顺序优化内存对齐
- 合并相邻的全局变量声明
- 删除多余空行
- 更新 Makefile 使用 gofumpt 替代 goimports

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 16:50:14 +08:00
xfy
1bf9e7ad5d fix(test,security): 改进测试稳定性和认证安全性
- socket_test.go: 降低压力测试参数避免超时,改进连接状态等待逻辑
- auth.go: 使用 subtle.ConstantTimeCompare 替代手动循环比较

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 16:20:01 +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
f31e8afeff feat(lua): 添加 balancer_by_lua 动态负载均衡功能
- 新增 BalancerByLuaConfig 配置,支持 Lua 脚本控制后端选择
- 实现 api_balancer.go Lua API,暴露 set_current_peer 等函数
- Proxy 集成 Lua 引擎,fallback 到标准算法确保可靠性
- 添加负载均衡算法常量提取,消除魔法字符串
- 支持超时控制和备用算法配置

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 16:14:59 +08:00
xfy
4f6a7be44c docs: 更新 AGENTS.md 项目文档
- 更新各目录 AGENTS.md 的子目录信息
- 添加新增目录的 AGENTS.md 文件
- 更新 docs/prompts.md 添加新的提示词

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 13:15:39 +08:00
xfy
e646cc5d05 refactor(test): 提取 testutil 包统一测试辅助函数
- 新增 NewRequestCtx 和 NewRequestCtxWithHeader 辅助函数
- 简化各测试文件中 RequestCtx 创建代码
- 减少测试代码重复,提高可维护性

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 13:15:20 +08:00
xfy
78c49f7288 fix(lint): 修复代码清理和lint警告
- 移除 proxy.go 未使用的 protoHTTPS 常量
- 修复 api_shared_dict.go 无效赋值警告
- 修复 certpool.go unused-parameter 警告

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 11:44:04 +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
8a533ba0ca refactor(lua): 提取 API 注册辅助函数
新增 RegisterAPIMethods 和 RegisterUnsafeAPI 函数,简化 Lua API 批量注册逻辑。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 11:12:49 +08:00
xfy
68447b9a2c refactor(lua): 提取 SharedDict 类型检查辅助函数
提取 checkSharedDict 函数消除重复的类型检查代码,
使各字典方法实现更加简洁。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 10:58:56 +08:00
xfy
b18476b443 fix(lua): 修复协程上下文设置问题
使用执行上下文(ExecutionContext)替代 RequestCtx 作为 LState 上下文,
因为 RequestCtx.Done() 依赖服务器连接状态,不适合用于超时控制。
RequestCtx 通过 coro.RequestCtx 字段访问。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 09:51:44 +08:00
xfy
f84e2c0e9b fix(lint): 修复 lua 测试中 unused-parameter 错误
将未使用的函数参数 L 改为 _

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 09:51:44 +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
50311518ff test(lua): 添加协程和字节码编译性能基准测试
测试协程创建和字节码编译开销。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 09:24:43 +08:00
xfy
7d53cc3dea feat(lua): 改进子请求父请求数据继承
ngx.location.capture 改进:
- 子请求继承父请求的 headers 和 query args
- 支持通过 opts.args 覆盖查询参数
- 通过 LState Context 传递 RequestCtx
- 添加测试验证父请求数据继承

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 09:24:43 +08:00
xfy
a87640defb feat(lua): 实现定时器调度器线程隔离
定时器回调在专用 Scheduler LState 中执行,避免并发问题:
- TimerManager 使用回调队列 + 专用 goroutine 执行
- 拒绝带 upvalue 的回调,防止闭包数据竞争
- 优雅关闭:排空队列后退出调度器
- Engine 支持 InitSchedulerLState 和 CloseScheduler

实现 scheduler 模式标志和 API 注册机制。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 09:24:43 +08:00