[优化] 代码优化

This commit is contained in:
ryan
2026-05-31 20:21:46 +08:00
parent ceae693a20
commit 97c10b8d0b
10 changed files with 37 additions and 91 deletions
+2
View File
@@ -49,6 +49,7 @@ var AgentDiscoveryToken = ""
var NodeOfflineThreshold = 2 * time.Minute
// V3 operational settings (hot-reloadable via Option table)
var AgentHeartbeatInterval = 10000 // milliseconds
var AgentWebsocketUpgradeEnabled = true
var AgentUpdateRepo = "Rain-kl/OpenFlare"
@@ -57,6 +58,7 @@ var DatabaseAutoCleanupEnabled = false
var DatabaseAutoCleanupRetentionDays = 30
// V5 OpenResty performance settings (hot-reloadable via Option table)
var OpenRestyWorkerProcesses = "auto"
var OpenRestyWorkerConnections = 4096
var OpenRestyWorkerRlimitNofile = 65535
+2 -1
View File
@@ -3,7 +3,6 @@ package common
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"io"
"log/slog"
"os"
@@ -11,6 +10,8 @@ import (
"runtime"
"slices"
"strings"
"github.com/gin-gonic/gin"
)
type logLevel int
+2 -1
View File
@@ -2,10 +2,11 @@ package common
import (
"context"
"github.com/go-redis/redis/v8"
"log/slog"
"os"
"time"
"github.com/go-redis/redis/v8"
)
var RDB *redis.Client
+7 -7
View File
@@ -10,7 +10,7 @@ import (
// GetAccessLogs godoc
// @Summary List access logs
// @Tags AccessLogs
// @Produce json
// @Produce JSON
// @Security BearerAuth
// @Param node_id query string false "Node ID"
// @Param remote_addr query string false "Remote address"
@@ -34,7 +34,7 @@ func GetAccessLogs(c *gin.Context) {
// GetFoldedAccessLogs godoc
// @Summary List folded access logs
// @Tags AccessLogs
// @Produce json
// @Produce JSON
// @Security BearerAuth
// @Param node_id query string false "Node ID"
// @Param remote_addr query string false "Remote address"
@@ -61,7 +61,7 @@ func GetFoldedAccessLogs(c *gin.Context) {
// GetFoldedAccessLogIPs godoc
// @Summary List folded access log IP summaries
// @Tags AccessLogs
// @Produce json
// @Produce JSON
// @Security BearerAuth
// @Param node_id query string false "Node ID"
// @Param remote_addr query string false "Remote address"
@@ -98,7 +98,7 @@ func GetFoldedAccessLogIPs(c *gin.Context) {
// GetAccessLogIPSummaries godoc
// @Summary List access log IP summaries
// @Tags AccessLogs
// @Produce json
// @Produce JSON
// @Security BearerAuth
// @Param node_id query string false "Node ID"
// @Param remote_addr query string false "Remote address"
@@ -129,7 +129,7 @@ func GetAccessLogIPSummaries(c *gin.Context) {
// GetAccessLogIPTrend godoc
// @Summary Get access log IP trend
// @Tags AccessLogs
// @Produce json
// @Produce JSON
// @Security BearerAuth
// @Param node_id query string false "Node ID"
// @Param remote_addr query string true "Remote address"
@@ -156,8 +156,8 @@ func GetAccessLogIPTrend(c *gin.Context) {
// CleanupAccessLogs godoc
// @Summary Cleanup access logs by retention days
// @Tags AccessLogs
// @Accept json
// @Produce json
// @Accept JSON
// @Produce JSON
// @Security BearerAuth
// @Success 200 {object} map[string]interface{}
// @Router /api/access-logs/cleanup [post]
+2 -1
View File
@@ -1,8 +1,9 @@
package controller
import (
"github.com/gin-gonic/gin"
"openflare/model"
"github.com/gin-gonic/gin"
)
// GetDefaultAcmeAccount godoc
+2 -1
View File
@@ -1,8 +1,9 @@
package controller
import (
"github.com/gin-gonic/gin"
"openflare/model"
"github.com/gin-gonic/gin"
)
type DnsAccountInput struct {
+15 -69
View File
@@ -5,14 +5,14 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"log/slog"
"net/http"
"openflare/common"
"openflare/model"
"strconv"
"time"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
type GitHubOAuthResponse struct {
@@ -87,19 +87,13 @@ func GitHubOAuth(c *gin.Context) {
}
if !common.GitHubOAuthEnabled {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "管理员未开启通过 GitHub 登录以及注册",
})
respondFailure(c, "管理员未开启通过 GitHub 登录以及注册")
return
}
code := c.Query("code")
githubUser, err := getGitHubUserInfoByCode(code)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
respondFailure(c, err.Error())
return
}
user := model.User{
@@ -108,45 +102,16 @@ func GitHubOAuth(c *gin.Context) {
if model.IsGitHubIdAlreadyTaken(user.GitHubId) {
err := user.FillUserByGitHubId()
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
respondFailure(c, err.Error())
return
}
} else {
if common.RegisterEnabled {
user.Username = "github_" + strconv.Itoa(model.GetMaxUserId()+1)
if githubUser.Name != "" {
user.DisplayName = githubUser.Name
} else {
user.DisplayName = "GitHub User"
}
user.Email = githubUser.Email
user.Role = common.RoleCommonUser
user.Status = common.UserStatusEnabled
if err := user.Insert(); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
} else {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "管理员关闭了新用户注册",
})
return
}
respondFailure(c, "管理员关闭了新用户注册")
return
}
if user.Status != common.UserStatusEnabled {
c.JSON(http.StatusOK, gin.H{
"message": "用户已被封禁",
"success": false,
})
respondFailure(c, "用户已被封禁")
return
}
setupLogin(&user, c)
@@ -154,29 +119,20 @@ func GitHubOAuth(c *gin.Context) {
func GitHubBind(c *gin.Context) {
if !common.GitHubOAuthEnabled {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "管理员未开启通过 GitHub 登录以及注册",
})
respondFailure(c, "管理员未开启通过 GitHub 登录以及注册")
return
}
code := c.Query("code")
githubUser, err := getGitHubUserInfoByCode(code)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
respondFailure(c, err.Error())
return
}
user := model.User{
GitHubId: githubUser.Login,
}
if model.IsGitHubIdAlreadyTaken(user.GitHubId) {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "该 GitHub 账户已被绑定",
})
respondFailure(c, "该 GitHub 账户已被绑定")
return
}
session := sessions.Default(c)
@@ -185,24 +141,14 @@ func GitHubBind(c *gin.Context) {
user.Id = id.(int)
err = user.FillUserById()
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
respondFailure(c, err.Error())
return
}
user.GitHubId = githubUser.Login
err = user.Update(false)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
respondFailure(c, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "bind",
})
return
respondSuccessMessage(c, "bind")
}
@@ -1,9 +1,10 @@
package controller
import (
"github.com/gin-gonic/gin"
"openflare/service"
"strings"
"github.com/gin-gonic/gin"
)
// GetManagedDomains godoc
+2 -9
View File
@@ -85,17 +85,10 @@ func setLoginSession(user *model.User, c *gin.Context) (*model.User, error) {
func setupLogin(user *model.User, c *gin.Context) {
cleanUser, err := setLoginSession(user, c)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"message": "无法保存会话信息,请重试",
"success": false,
})
respondFailure(c, "无法保存会话信息,请重试")
return
}
c.JSON(http.StatusOK, gin.H{
"message": "",
"success": true,
"data": *cleanUser,
})
respondSuccess(c, *cleanUser)
}
func Logout(c *gin.Context) {
+1 -1
View File
@@ -247,7 +247,7 @@ func EnsureGlobalDiscoveryToken() (string, error) {
model.InitOptionMap()
}
common.OptionMapRWMutex.RLock()
token := strings.TrimSpace(common.OptionMap["AgentDiscoveryToken"])
token := strings.TrimSpace(common.AgentDiscoveryToken)
common.OptionMapRWMutex.RUnlock()
if token != "" {
return token, nil