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