-
Notifications
You must be signed in to change notification settings - Fork 0
Add proxy server support for outbound vendor API requests #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -91,7 +91,22 @@ func main() { | |||||||||||||||||
| } | ||||||||||||||||||
| log.Println("✅ 数据库写入队列已启动") | ||||||||||||||||||
|
|
||||||||||||||||||
| // 【修复】第三步:创建服务(现在可以安全使用数据库了) | ||||||||||||||||||
| // 【新增】第三步:初始化全局 HTTP 客户端(支持代理配置) | ||||||||||||||||||
| proxyConfig, err := services.GetProxyConfig() | ||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| log.Printf("⚠️ 读取代理配置失败,使用默认配置: %v", err) | ||||||||||||||||||
| proxyConfig = services.ProxyConfig{UseProxy: false} | ||||||||||||||||||
| } | ||||||||||||||||||
| if err := services.InitHTTPClient(proxyConfig); err != nil { | ||||||||||||||||||
| log.Fatalf("初始化 HTTP 客户端失败: %v", err) | ||||||||||||||||||
|
||||||||||||||||||
| log.Fatalf("初始化 HTTP 客户端失败: %v", err) | |
| log.Printf("⚠️ 初始化 HTTP 客户端失败,使用默认配置重试: %v", err) | |
| fallbackConfig := services.ProxyConfig{UseProxy: false} | |
| if fallbackErr := services.InitHTTPClient(fallbackConfig); fallbackErr != nil { | |
| log.Printf("❌ 使用默认配置初始化 HTTP 客户端也失败: %v", fallbackErr) | |
| } else { | |
| proxyConfig = fallbackConfig | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,17 +13,20 @@ import ( | |
| const ( | ||
| appSettingsDir = ".code-switch" // 【修复】修正拼写错误(原为 .codex-swtich) | ||
| appSettingsFile = "app.json" | ||
| oldSettingsDir = ".codex-swtich" // 旧的错误拼写 | ||
| oldSettingsDir = ".codex-swtich" // 旧的错误拼写 | ||
| migrationMarkerFile = ".migrated-from-codex-swtich" // 迁移标记文件 | ||
| ) | ||
|
|
||
| type AppSettings struct { | ||
| ShowHeatmap bool `json:"show_heatmap"` | ||
| ShowHomeTitle bool `json:"show_home_title"` | ||
| AutoStart bool `json:"auto_start"` | ||
| AutoUpdate bool `json:"auto_update"` | ||
| AutoConnectivityTest bool `json:"auto_connectivity_test"` | ||
| EnableSwitchNotify bool `json:"enable_switch_notify"` // 供应商切换通知开关 | ||
| ShowHeatmap bool `json:"show_heatmap"` | ||
| ShowHomeTitle bool `json:"show_home_title"` | ||
| AutoStart bool `json:"auto_start"` | ||
| AutoUpdate bool `json:"auto_update"` | ||
| AutoConnectivityTest bool `json:"auto_connectivity_test"` | ||
| EnableSwitchNotify bool `json:"enable_switch_notify"` // 供应商切换通知开关 | ||
| UseProxy bool `json:"use_proxy"` // 是否启用代理服务器 | ||
| ProxyAddress string `json:"proxy_address"` // 代理地址(如 http://127.0.0.1:1080) | ||
| ProxyType string `json:"proxy_type"` // 代理类型:http/https/socks5 | ||
| } | ||
|
|
||
| type AppSettingsService struct { | ||
|
|
@@ -139,9 +142,12 @@ func (as *AppSettingsService) defaultSettings() AppSettings { | |
| ShowHeatmap: true, | ||
| ShowHomeTitle: true, | ||
| AutoStart: autoStartEnabled, | ||
| AutoUpdate: true, // 默认开启自动更新 | ||
| AutoConnectivityTest: true, // 默认开启自动可用性监控(开箱即用) | ||
| EnableSwitchNotify: true, // 默认开启切换通知 | ||
| AutoUpdate: true, // 默认开启自动更新 | ||
| AutoConnectivityTest: true, // 默认开启自动可用性监控(开箱即用) | ||
| EnableSwitchNotify: true, // 默认开启切换通知 | ||
| UseProxy: false, // 默认不使用代理 | ||
| ProxyAddress: "", // 默认代理地址为空 | ||
| ProxyType: "http", // 默认代理类型为 HTTP | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -173,6 +179,18 @@ func (as *AppSettingsService) SaveAppSettings(settings AppSettings) (AppSettings | |
| if err := as.saveLocked(settings); err != nil { | ||
| return settings, err | ||
| } | ||
|
|
||
| // 【新增】同步代理配置到全局 HTTP 客户端 | ||
| proxyConfig := ProxyConfig{ | ||
| UseProxy: settings.UseProxy, | ||
| ProxyAddress: settings.ProxyAddress, | ||
| ProxyType: settings.ProxyType, | ||
| } | ||
| if err := UpdateHTTPClient(proxyConfig); err != nil { | ||
| // 代理配置更新失败不应阻止设置保存,只记录错误 | ||
| fmt.Printf("⚠️ 更新 HTTP 客户端代理配置失败: %v\n", err) | ||
| } | ||
|
||
|
|
||
|
Comment on lines
+183
to
+193
|
||
| return settings, nil | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The proxy address input uses
@blurevent for saving, which means changes are only persisted when the user clicks away from the input field. This can lead to data loss if the user enters a proxy address and immediately closes the settings without clicking elsewhere first. Consider using@inputwith debouncing or adding an explicit "Apply" button to ensure changes are saved reliably.