feat: Implement initial Codex Pool backend server with comprehensive APIs for configuration, S2A integration, owner management, batch processing, monitoring, and corresponding frontend pages.
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -98,6 +99,7 @@ func startServer(cfg *config.Config) {
|
||||
// 基础 API
|
||||
mux.HandleFunc("/api/health", api.CORS(handleHealth))
|
||||
mux.HandleFunc("/api/config", api.CORS(handleConfig))
|
||||
mux.HandleFunc("/api/proxy/test", api.CORS(handleProxyTest)) // 代理测试
|
||||
|
||||
// 日志 API
|
||||
mux.HandleFunc("/api/logs", api.CORS(handleGetLogs))
|
||||
@@ -126,8 +128,8 @@ func startServer(cfg *config.Config) {
|
||||
mux.HandleFunc("/api/upload/validate", api.CORS(api.HandleUploadValidate))
|
||||
|
||||
// 母号封禁检查 API
|
||||
mux.HandleFunc("/api/db/owners/ban-check", api.CORS(api.HandleManualBanCheck)) // 手动触发检查
|
||||
mux.HandleFunc("/api/db/owners/ban-check/status", api.CORS(api.HandleBanCheckStatus)) // 检查状态
|
||||
mux.HandleFunc("/api/db/owners/ban-check", api.CORS(api.HandleManualBanCheck)) // 手动触发检查
|
||||
mux.HandleFunc("/api/db/owners/ban-check/status", api.CORS(api.HandleBanCheckStatus)) // 检查状态
|
||||
mux.HandleFunc("/api/db/owners/ban-check/settings", api.CORS(api.HandleBanCheckSettings)) // 配置
|
||||
|
||||
// 注册测试 API
|
||||
@@ -956,3 +958,84 @@ func handleCleanerSettings(w http.ResponseWriter, r *http.Request) {
|
||||
api.Error(w, http.StatusMethodNotAllowed, "不支持的方法")
|
||||
}
|
||||
}
|
||||
|
||||
// handleProxyTest POST /api/proxy/test - 测试代理连接
|
||||
func handleProxyTest(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
api.Error(w, http.StatusMethodNotAllowed, "仅支持 POST")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
ProxyURL string `json:"proxy_url"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
api.Error(w, http.StatusBadRequest, "请求格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
proxyURL := req.ProxyURL
|
||||
if proxyURL == "" {
|
||||
api.Error(w, http.StatusBadRequest, "代理地址不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
logger.Info(fmt.Sprintf("测试代理连接: %s", proxyURL), "", "proxy")
|
||||
|
||||
// 解析代理 URL
|
||||
proxyParsed, err := parseProxyURL(proxyURL)
|
||||
if err != nil {
|
||||
logger.Error(fmt.Sprintf("代理地址格式错误: %v", err), "", "proxy")
|
||||
api.Error(w, http.StatusBadRequest, fmt.Sprintf("代理地址格式错误: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
// 创建带代理的 HTTP 客户端
|
||||
client := &http.Client{
|
||||
Timeout: 15 * time.Second,
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyURL(proxyParsed),
|
||||
},
|
||||
}
|
||||
|
||||
// 测试请求 - 使用 httpbin.org/ip 获取出口 IP
|
||||
testURL := "https://httpbin.org/ip"
|
||||
resp, err := client.Get(testURL)
|
||||
if err != nil {
|
||||
logger.Error(fmt.Sprintf("代理连接失败: %v", err), "", "proxy")
|
||||
api.Error(w, http.StatusBadGateway, fmt.Sprintf("代理连接失败: %v", err))
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
logger.Error(fmt.Sprintf("代理测试失败: HTTP %d", resp.StatusCode), "", "proxy")
|
||||
api.Error(w, http.StatusBadGateway, fmt.Sprintf("代理测试失败: HTTP %d", resp.StatusCode))
|
||||
return
|
||||
}
|
||||
|
||||
// 解析响应获取出口 IP
|
||||
var ipResp struct {
|
||||
Origin string `json:"origin"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&ipResp); err != nil {
|
||||
logger.Warning(fmt.Sprintf("解析代理响应失败: %v", err), "", "proxy")
|
||||
}
|
||||
|
||||
logger.Success(fmt.Sprintf("代理连接成功, 出口IP: %s", ipResp.Origin), "", "proxy")
|
||||
|
||||
api.Success(w, map[string]interface{}{
|
||||
"connected": true,
|
||||
"message": "代理连接成功",
|
||||
"origin_ip": ipResp.Origin,
|
||||
})
|
||||
}
|
||||
|
||||
// parseProxyURL 解析代理 URL
|
||||
func parseProxyURL(proxyURL string) (*url.URL, error) {
|
||||
// 如果没有协议前缀,默认添加 http://
|
||||
if !strings.HasPrefix(proxyURL, "http://") && !strings.HasPrefix(proxyURL, "https://") && !strings.HasPrefix(proxyURL, "socks5://") {
|
||||
proxyURL = "http://" + proxyURL
|
||||
}
|
||||
return url.Parse(proxyURL)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user