feat: Implement CodexAuth proxy pool management with a new frontend configuration page and a dedicated backend service for API, database, and proxy testing.
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"codex-pool/internal/database"
|
||||
"codex-pool/internal/proxyutil"
|
||||
)
|
||||
|
||||
// HandleCodexProxies 处理代理池请求
|
||||
@@ -15,6 +16,10 @@ func HandleCodexProxies(w http.ResponseWriter, r *http.Request) {
|
||||
case http.MethodGet:
|
||||
listCodexProxies(w, r)
|
||||
case http.MethodPost:
|
||||
if strings.HasSuffix(r.URL.Path, "/test") {
|
||||
testCodexProxy(w, r)
|
||||
return
|
||||
}
|
||||
addCodexProxy(w, r)
|
||||
case http.MethodDelete:
|
||||
deleteCodexProxy(w, r)
|
||||
@@ -170,3 +175,56 @@ func toggleCodexProxy(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Success(w, map[string]string{"message": "状态已切换"})
|
||||
}
|
||||
|
||||
// testCodexProxy 测试代理连通性
|
||||
func testCodexProxy(w http.ResponseWriter, r *http.Request) {
|
||||
idStr := r.URL.Query().Get("id")
|
||||
if idStr == "" {
|
||||
Error(w, http.StatusBadRequest, "缺少代理 ID")
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
Error(w, http.StatusBadRequest, "无效的代理 ID")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取代理详情
|
||||
proxies, err := database.Instance.GetCodexProxies()
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "获取代理失败")
|
||||
return
|
||||
}
|
||||
|
||||
var targetProxy *database.CodexProxy
|
||||
for _, p := range proxies {
|
||||
if p.ID == id {
|
||||
targetProxy = &p
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if targetProxy == nil {
|
||||
Error(w, http.StatusNotFound, "未找到代理")
|
||||
return
|
||||
}
|
||||
|
||||
// 执行测试 (异步执行以防前端超时,但用户想要同步结果,所以这里同步执行)
|
||||
// 因为是在管理后台点击,15s 超时是可以接受的
|
||||
result, err := proxyutil.TestProxy(targetProxy.ProxyURL)
|
||||
if err != nil {
|
||||
database.Instance.UpdateCodexProxyTestResult(id, "", false)
|
||||
Error(w, http.StatusInternalServerError, "测试过程出错: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 更新数据库
|
||||
err = database.Instance.UpdateCodexProxyTestResult(id, result.Location, result.Success)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "更新测试结果失败")
|
||||
return
|
||||
}
|
||||
|
||||
Success(w, result)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user