feat: Implement initial backend API server with configuration, S2A proxy, and data management, and add frontend Toast component and Config page.

This commit is contained in:
2026-02-01 03:57:51 +08:00
parent e27e36b0e0
commit 842a4ab4b2
3 changed files with 387 additions and 180 deletions

View File

@@ -217,6 +217,8 @@ func handleConfig(w http.ResponseWriter, r *http.Request) {
"group_ids": config.Global.GroupIDs,
"proxy_enabled": config.Global.ProxyEnabled,
"default_proxy": config.Global.DefaultProxy,
"proxy_test_status": getProxyTestStatus(),
"proxy_test_ip": getProxyTestIP(),
"site_name": config.Global.SiteName,
"mail_services_count": len(config.Global.MailServices),
"mail_services": config.Global.MailServices,
@@ -260,6 +262,11 @@ func handleConfig(w http.ResponseWriter, r *http.Request) {
}
if req.DefaultProxy != nil {
config.Global.DefaultProxy = *req.DefaultProxy
// 代理地址变更时,重置测试状态
if database.Instance != nil {
database.Instance.SetConfig("proxy_test_status", "unknown")
database.Instance.SetConfig("proxy_test_ip", "")
}
}
if req.SiteName != nil {
config.Global.SiteName = *req.SiteName
@@ -1010,6 +1017,11 @@ func handleProxyTest(w http.ResponseWriter, r *http.Request) {
if resp.StatusCode != http.StatusOK {
logger.Error(fmt.Sprintf("代理测试失败: HTTP %d", resp.StatusCode), "", "proxy")
// 保存失败状态
if database.Instance != nil {
database.Instance.SetConfig("proxy_test_status", "error")
database.Instance.SetConfig("proxy_test_ip", "")
}
api.Error(w, http.StatusBadGateway, fmt.Sprintf("代理测试失败: HTTP %d", resp.StatusCode))
return
}
@@ -1024,6 +1036,12 @@ func handleProxyTest(w http.ResponseWriter, r *http.Request) {
logger.Success(fmt.Sprintf("代理连接成功, 出口IP: %s", ipResp.Origin), "", "proxy")
// 保存成功状态到数据库
if database.Instance != nil {
database.Instance.SetConfig("proxy_test_status", "success")
database.Instance.SetConfig("proxy_test_ip", ipResp.Origin)
}
api.Success(w, map[string]interface{}{
"connected": true,
"message": "代理连接成功",
@@ -1031,6 +1049,26 @@ func handleProxyTest(w http.ResponseWriter, r *http.Request) {
})
}
// getProxyTestStatus 获取代理测试状态
func getProxyTestStatus() string {
if database.Instance == nil {
return "unknown"
}
if val, _ := database.Instance.GetConfig("proxy_test_status"); val != "" {
return val
}
return "unknown"
}
// getProxyTestIP 获取代理测试出口IP
func getProxyTestIP() string {
if database.Instance == nil {
return ""
}
val, _ := database.Instance.GetConfig("proxy_test_ip")
return val
}
// parseProxyURL 解析代理 URL
func parseProxyURL(proxyURL string) (*url.URL, error) {
// 如果没有协议前缀,默认添加 http://