mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-08 08:36:37 +08:00
[优化] 配置渲染从 Server 转移到 Agent
This commit is contained in:
@@ -79,13 +79,11 @@ type ApplyLogCleanupResult struct {
|
||||
}
|
||||
|
||||
type AgentConfigResponse struct {
|
||||
Version string `json:"version"`
|
||||
Checksum string `json:"checksum"`
|
||||
MainConfig string `json:"main_config"`
|
||||
RouteConfig string `json:"route_config"`
|
||||
RenderedConfig string `json:"rendered_config"`
|
||||
SupportFiles []SupportFile `json:"support_files"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Version string `json:"version"`
|
||||
Checksum string `json:"checksum"`
|
||||
SourceConfigJSON string `json:"source_config_json"`
|
||||
SupportFiles []SupportFile `json:"support_files"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type AgentSettings struct {
|
||||
@@ -233,13 +231,11 @@ func GetActiveConfigForAgent() (*AgentConfigResponse, error) {
|
||||
}
|
||||
slog.Debug("agent fetched active config", "version", version.Version, "checksum", version.Checksum)
|
||||
return &AgentConfigResponse{
|
||||
Version: version.Version,
|
||||
Checksum: version.Checksum,
|
||||
MainConfig: version.MainConfig,
|
||||
RouteConfig: version.RenderedConfig,
|
||||
RenderedConfig: version.RenderedConfig,
|
||||
SupportFiles: supportFiles,
|
||||
CreatedAt: version.CreatedAt,
|
||||
Version: version.Version,
|
||||
Checksum: version.Checksum,
|
||||
SourceConfigJSON: version.SnapshotJSON,
|
||||
SupportFiles: sourceSupportFiles(supportFiles),
|
||||
CreatedAt: version.CreatedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -426,10 +422,6 @@ func normalizeApplyLogPageSize(pageSize int) int {
|
||||
return pageSize
|
||||
}
|
||||
|
||||
func upsertNode(payload AgentNodePayload) (*model.Node, error) {
|
||||
return nil, errors.New("不再支持匿名自动注册")
|
||||
}
|
||||
|
||||
func computeNodeStatus(node *model.Node) string {
|
||||
if node == nil {
|
||||
return NodeStatusOffline
|
||||
|
||||
@@ -33,18 +33,13 @@ func TestGetActiveConfigForAgentIncludesPoWConfig(t *testing.T) {
|
||||
t.Fatalf("GetActiveConfigForAgent failed: %v", err)
|
||||
}
|
||||
|
||||
foundPowConfig := false
|
||||
for _, file := range activeConfig.SupportFiles {
|
||||
if file.Path != "pow_config.json" {
|
||||
continue
|
||||
}
|
||||
foundPowConfig = true
|
||||
if file.Content == "" {
|
||||
t.Fatal("expected pow_config.json content to be populated")
|
||||
if file.Path == "pow_config.json" || file.Path == "waf_config.json" {
|
||||
t.Fatalf("agent config should not receive rendered runtime config file %s", file.Path)
|
||||
}
|
||||
}
|
||||
if !foundPowConfig {
|
||||
t.Fatal("expected agent config to include pow_config.json support file")
|
||||
if !strings.Contains(activeConfig.SourceConfigJSON, `"pow_enabled":true`) {
|
||||
t.Fatal("expected agent config source json to include PoW source configuration")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,13 +66,12 @@ func TestGetActiveConfigForAgentIncludesWAFConfig(t *testing.T) {
|
||||
|
||||
for _, file := range activeConfig.SupportFiles {
|
||||
if file.Path == "waf_config.json" {
|
||||
if !strings.Contains(file.Content, `"rule_groups"`) {
|
||||
t.Fatalf("expected waf_config.json content to include rule groups, got %s", file.Content)
|
||||
}
|
||||
return
|
||||
t.Fatal("agent config should not receive rendered waf_config.json")
|
||||
}
|
||||
}
|
||||
t.Fatal("expected agent config to include waf_config.json support file")
|
||||
if !strings.Contains(activeConfig.SourceConfigJSON, `"waf"`) {
|
||||
t.Fatal("expected agent config source json to include WAF source configuration")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetActiveConfigForAgentUsesTenMinutePoWSessionDefault(t *testing.T) {
|
||||
@@ -105,13 +99,12 @@ func TestGetActiveConfigForAgentUsesTenMinutePoWSessionDefault(t *testing.T) {
|
||||
|
||||
for _, file := range activeConfig.SupportFiles {
|
||||
if file.Path == "pow_config.json" {
|
||||
if !strings.Contains(file.Content, `"session_ttl":600`) {
|
||||
t.Fatalf("expected default PoW session TTL to be 600 seconds, got %s", file.Content)
|
||||
}
|
||||
return
|
||||
t.Fatal("agent config should not receive rendered pow_config.json")
|
||||
}
|
||||
}
|
||||
t.Fatal("expected agent config to include pow_config.json support file")
|
||||
if !strings.Contains(activeConfig.SourceConfigJSON, `"session_ttl":600`) {
|
||||
t.Fatalf("expected default PoW session TTL to be in source json, got %s", activeConfig.SourceConfigJSON)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterNodeWithAgentToken(t *testing.T) {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,51 +0,0 @@
|
||||
package service
|
||||
|
||||
import "fmt"
|
||||
|
||||
const (
|
||||
openRestyObservabilityInitLuaPath = "init.lua"
|
||||
openRestyObservabilityLogLuaPath = "log.lua"
|
||||
openRestyObservabilityReadLuaPath = "read.lua"
|
||||
)
|
||||
|
||||
func renderOpenRestyObservabilityTemplateBlock() string {
|
||||
return stringsJoinLines(
|
||||
" lua_shared_dict openflare_observability 10m;",
|
||||
" lua_shared_dict openflare_pow_config 1m;",
|
||||
" lua_shared_dict openflare_pow_challenges 10m;",
|
||||
" lua_shared_dict openflare_pow_sessions 20m;",
|
||||
" lua_shared_dict openflare_waf_config 2m;",
|
||||
fmt.Sprintf(" init_worker_by_lua_file %s/%s;", nginxLuaDirPlaceholder, openRestyObservabilityInitLuaPath),
|
||||
fmt.Sprintf(" log_by_lua_file %s/%s;", nginxLuaDirPlaceholder, openRestyObservabilityLogLuaPath),
|
||||
"",
|
||||
fmt.Sprintf(" server {"),
|
||||
fmt.Sprintf(" listen %s;", nginxObservabilityListenPlaceholder),
|
||||
" server_name openflare-observability;",
|
||||
" access_log off;",
|
||||
"",
|
||||
" location = /openflare/observability {",
|
||||
" default_type application/json;",
|
||||
fmt.Sprintf(" content_by_lua_file %s/%s;", nginxLuaDirPlaceholder, openRestyObservabilityReadLuaPath),
|
||||
" }",
|
||||
"",
|
||||
" location = /openflare/stub_status {",
|
||||
" stub_status;",
|
||||
" }",
|
||||
" }",
|
||||
"",
|
||||
)
|
||||
}
|
||||
|
||||
func stringsJoinLines(lines ...string) string {
|
||||
if len(lines) == 0 {
|
||||
return ""
|
||||
}
|
||||
result := ""
|
||||
for index, line := range lines {
|
||||
if index > 0 {
|
||||
result += "\n"
|
||||
}
|
||||
result += line
|
||||
}
|
||||
return result + "\n"
|
||||
}
|
||||
@@ -168,25 +168,3 @@ func rewriteOriginURLAddress(rawURL string, newAddress string) (string, error) {
|
||||
parsed.Host = formatOriginHost(address, port)
|
||||
return parsed.String(), nil
|
||||
}
|
||||
|
||||
func splitOriginURL(rawURL string) (scheme string, address string, port string, uri string, err error) {
|
||||
parsed, err := url.ParseRequestURI(strings.TrimSpace(rawURL))
|
||||
if err != nil {
|
||||
return "", "", "", "", err
|
||||
}
|
||||
scheme = parsed.Scheme
|
||||
address = normalizeOriginAddress(parsed.Hostname())
|
||||
port = parsed.Port()
|
||||
uri = parsed.EscapedPath()
|
||||
if uri == "" {
|
||||
uri = parsed.Path
|
||||
}
|
||||
if parsed.RawQuery != "" {
|
||||
if uri == "" {
|
||||
uri = "?" + parsed.RawQuery
|
||||
} else {
|
||||
uri = uri + "?" + parsed.RawQuery
|
||||
}
|
||||
}
|
||||
return scheme, address, port, uri, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user