✨ feat: AI配置支持自定义OpenAI兼容API与模型选择 - #3
Open
wooluo wants to merge 3 commits into
Open
Conversation
主分析管线(main.py→AIAnalyzer)云端 provider 原先写死为 deepseek,现新增通用 custom provider,可指向任意 OpenAI 兼容端点(OpenAI/通义千问/Moonshot/SiliconFlow/vLLM/Ollama 兼容口等)。增量改造,deepseek/ollama 完全向后兼容。 改动: - core/ai_analyzer.py: 新增 custom 分支与 _load_cloud_config(),URL 派生 _chat_url()/_models_url(),模块级 fetch_available_models() 供 GUI 拉模型 - core/config_manager.py: 枚举放行 custom,custom.base_url/timeout 校验,get_safe_config() 脱敏 custom.api_key,补 custom 默认段 - core/unified_config_manager.py: custom provider 校验分支 - gui_optimized.py: AI 面板新增 API 地址输入与自定义 provider 选项,刷新改为真实 GET /models 拉模型(后台线程),测试连接改为真实请求,新增保存AI配置(ruamel 保留注释写回)与启动回填 - config.yaml/config.example.yaml/.env.example: 新增 custom 段示例 - 顺带修复 config.yaml 中导致 yaml.safe_load 崩溃的 git 合并冲突标记 - 新增 11 个单元测试,零回归(原有失败均为改动前已存在) Co-Authored-By: Claude <noreply@anthropic.com>
云端 provider 原有 deepseek 与 custom(任意 OpenAI 兼容端点),现新增命名 provider zhipu,指向智谱 BigModel。智谱标准 API 与 GLM Coding Plan(编程套餐)均提供 OpenAI 兼容端点,故完全复用既有 OpenAI Chat Completions 协议链路(_analyze_with_cloud / _chat_url / _models_url / fetch_available_models 不动),仅在 provider 装配层加分支。增量改造,deepseek/custom/ollama 全部向后兼容。 端点(OpenAI 兼容 base,自动追加 /chat/completions): - 标准: https://open.bigmodel.cn/api/paas/v4 - 套餐: https://open.bigmodel.cn/api/coding/paas/v4 改动: - core/ai_analyzer.py: _load_cloud_config() 新增 zhipu 分支,按 coding_plan 派生默认 base_url;顺带修复 base_url 为空字符串时回退默认端点(section.get('base_url') or default),避免 base_url: '' 致端点派生失效 - core/config_manager.py: ai.cloud_provider 枚举放行 zhipu、zhipu 校验分支(要求 model)、_set_defaults 补 zhipu 段、get_safe_config 脱敏加 zhipu、range 校验加 zhipu.timeout - core/unified_config_manager.py: validate_config() 新增 zhipu 校验分支 - gui_optimized.py: AI 面板下拉加「🇨🇳 智谱 GLM」、_PROVIDER_MAP 加映射、新增「编程套餐」复选框(仅智谱可见,切换时在两个端点间换 base_url 并刷新模型列表)、预置 GLM 模型列表、启动回填 + 保存写回 zhipu 段(走 ruamel 注释保留) - config.yaml/config.example.yaml/.env.example: 新增 zhipu 段示例与注释 - 新增 8 个单元测试(智谱装配、coding_plan 端点切换、显式 base_url 优先、枚举/校验/默认/脱敏) 验证: 一次性 worktree 跑 HEAD 基线确认零回归(16 个失败用例改动前后完全一致,均为改动前已存在);8 个新 zhipu 单测全绿;core 导入正常、gui 编译通过、配置 YAML 合法;端到端冒烟测试通过。 Co-Authored-By: Claude <noreply@anthropic.com>
继 zhipu provider 之后,修复优化版 GUI 分析链路的一系列缺陷并补全占位功能。所有改动向后兼容。 核心修复: - core/event_bus.py: _normalize_priority() 把字符串优先级(如 'high')归一化为 EventPriority 枚举。修复 worker 第二次跑分析时注册重复事件处理器触发 'str' object has no attribute 'value' 崩溃。 - core/ai_analyzer.py: _analyze_with_cloud 在 content 为空时回退读 reasoning_content。修复 GLM-5 等推理模型把输出放 reasoning_content、content 返回空导致判定"AI返回内容为空"而退回备用模板。 GUI 分析链路修复(gui_optimized.py): - 威胁统计恒为 0:① run() 把统计写回 results['performance'];② 按 validation.threats 判定(NORMAL 级 is_valid 恒 True 会漏计)。 - 误报过载(761/778):内置规则按攻击载荷设计,对整行日志误伤(后端 192.168.x 触发 SSRF、Cookie 的 ;/UA 的 &# 触发命令注入)。改为只校验抽取出的请求目标(method+path+query),761→7 条真攻击。 - 切智谱/勾编程套餐时 base_url 不更新:切 provider 时重置继承地址、勾套餐直接切官方端点(此前继承了 deepseek 的 SiliconFlow 地址导致刷新/连接失败)。 - 刷新容错:智谱刷新前必须有 Key 否则提示并不清空;刷新失败恢复内置模型列表;内置模型列表更新为真实在售 lineup(含 glm-5-turbo)。 新增功能(gui_optimized.py): - 真实 AI 分析:用 AIAnalyzer.analyze_log 替换 _simulate_ai_analysis 模拟逻辑,结果实时输出到日志面板并写入威胁记录。 - HTML 报告导出:export_report 由占位改为真实生成自包含 HTML(统计卡片+威胁类型分布+每条威胁详情含AI深度分析),文件名带时间戳。分析完成时保存结果供导出。 配置: - core/config_manager.py + config.example.yaml: 智谱默认 timeout=120/max_tokens=4096(GLM-5 推理模型较慢需更长超时与更大空间);GUI 保存时用 max(已有,120/4096) 自动升级偏低值。 安全: - 取消跟踪 config.yaml 并加入 .gitignore(本地可能含真实 API Key);改由 config.example.yaml 作为模板。 验证:单元测试 zhipu/event_bus 相关通过、零回归;offscreen Qt 实测真实智谱分析(glm-5-turbo 返回 3267 字 SSRF 报告)、威胁计数 761→7、报告导出 HTML 有效。 Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
主分析管线(
main.py → AIAnalyzer,读config.yaml)的云端路径虽走标准 OpenAI Chat Completions 协议,但 provider 被硬编码为deepseek/ollama,无法指向任意 OpenAI 兼容端点。本次新增通用customprovider,支持自定义base_url+api_key+ 可选model,并可在 GUI 中选择 provider、连接后从GET /models拉取模型下拉、也允许手填模型名。增量改造,
deepseek/ollama完全向后兼容。改动
core/ai_analyzer.py:新增custom分支;抽取_load_cloud_config();URL 派生_chat_url()/_models_url()(兼容 base_url 含/不含/chat/completions);新增模块级fetch_available_models(base_url, api_key)供 GUI 拉模型。core/config_manager.py:枚举放行custom;custom.base_url/timeout校验;get_safe_config()对custom.api_key脱敏;补custom默认段。core/unified_config_manager.py:customprovider 校验分支。gui_optimized.py:AI 面板新增 API 地址输入框与「🌐 自定义 OpenAI 兼容」选项;刷新改为真实GET /models(后台线程不卡 UI);测试连接改为真实请求;新增「保存AI配置」按钮(ruamel 保留注释写回 config.yaml)与启动回填。config.yaml/config.example.yaml/.env.example新增custom:段。config.yaml中导致yaml.safe_load直接崩溃的 git 合并冲突标记。用法
```yaml
ai:
type: cloud
cloud_provider: custom # deepseek | custom
custom:
base_url: https://api.openai.com/v1
api_key: "" # 留空则用环境变量 SSLOGS_AI_API_KEY
model: gpt-4o-mini
```
验证
fetch_available_models、custom 校验通过/拒绝、custom 脱敏),全部通过。test_ai_analyzer.py13 个、test_config_manager.py3 个失败均为改动前已存在的旧问题(mock 错用session.post打真实网络、个别断言字符串错误),与本次无关。ConfigManager接受custom并脱敏;AIAnalyzer正确加载custom:段并派生 chat/models 端点;deepseek 旧行为不变;GUI 保存路径 ruamel 注释全保留。🤖 Generated with Claude Code