mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-04 23:16:37 +08:00
4be44733e8
- Updated README.md to reflect the new support_dir for auxiliary files. - Refactored agent main.go to use support_dir instead of cert_dir. - Modified config.go to replace cert_dir with support_dir and added legacy support. - Adjusted config tests to validate support_dir usage. - Changed nginx manager to utilize support_dir for file paths. - Updated server configuration to use support_dir for SSL certificates. - Revised documentation to clarify the new configuration parameters. - Enhanced security checks for support file paths to prevent traversal attacks.
100 lines
3.2 KiB
Go
100 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"log/slog"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"atsflare-agent/internal/agent"
|
|
"atsflare-agent/internal/config"
|
|
"atsflare-agent/internal/heartbeat"
|
|
"atsflare-agent/internal/httpclient"
|
|
"atsflare-agent/internal/logging"
|
|
"atsflare-agent/internal/nginx"
|
|
"atsflare-agent/internal/state"
|
|
syncservice "atsflare-agent/internal/sync"
|
|
"atsflare-agent/internal/updater"
|
|
)
|
|
|
|
func main() {
|
|
logging.Setup()
|
|
|
|
configPath := flag.String("config", "./agent.json", "agent config path")
|
|
flag.Parse()
|
|
|
|
cfg, err := config.Load(*configPath)
|
|
if err != nil {
|
|
slog.Error("load agent config failed", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
cfg.NginxVersion = nginx.DetectVersion(
|
|
context.Background(),
|
|
nginx.ExecutorOptions{
|
|
NginxPath: cfg.OpenrestyPath,
|
|
DockerBinary: cfg.DockerBinary,
|
|
ContainerName: cfg.OpenrestyContainerName,
|
|
Image: cfg.OpenrestyDockerImage,
|
|
MainConfigPath: cfg.MainConfigPath,
|
|
RouteConfigPath: cfg.RouteConfigPath,
|
|
SupportDir: cfg.SupportDir,
|
|
NginxSupportDir: cfg.OpenrestySupportDir,
|
|
OpenrestyObservabilityPort: cfg.OpenrestyObservabilityPort,
|
|
},
|
|
)
|
|
slog.Info("agent config loaded",
|
|
"server", cfg.ServerURL,
|
|
"node", cfg.NodeName,
|
|
"ip", cfg.NodeIP,
|
|
"heartbeat_interval", cfg.HeartbeatInterval,
|
|
"route_config", cfg.RouteConfigPath,
|
|
"support_dir", cfg.SupportDir,
|
|
)
|
|
|
|
client := httpclient.New(cfg.ServerURL, cfg.InitialAuthToken(), cfg.RequestTimeout.Duration())
|
|
stateStore := state.NewStore(cfg.StatePath)
|
|
runtimeRouteConfigPath := cfg.RouteConfigPath
|
|
if cfg.OpenrestyPath == "" {
|
|
runtimeRouteConfigPath = nginx.DockerRouteConfigPath
|
|
}
|
|
runtimeManager := &nginx.Manager{
|
|
MainConfigPath: cfg.MainConfigPath,
|
|
RouteConfigPath: cfg.RouteConfigPath,
|
|
RuntimeRouteConfigPath: runtimeRouteConfigPath,
|
|
SupportDir: cfg.SupportDir,
|
|
NginxSupportDir: cfg.OpenrestySupportDir,
|
|
OpenrestyObservabilityPort: cfg.OpenrestyObservabilityPort,
|
|
Executor: nginx.NewExecutor(nginx.ExecutorOptions{
|
|
NginxPath: cfg.OpenrestyPath,
|
|
DockerBinary: cfg.DockerBinary,
|
|
ContainerName: cfg.OpenrestyContainerName,
|
|
Image: cfg.OpenrestyDockerImage,
|
|
MainConfigPath: cfg.MainConfigPath,
|
|
RouteConfigPath: cfg.RouteConfigPath,
|
|
SupportDir: cfg.SupportDir,
|
|
NginxSupportDir: cfg.OpenrestySupportDir,
|
|
OpenrestyObservabilityPort: cfg.OpenrestyObservabilityPort,
|
|
}),
|
|
}
|
|
runner := &agent.Runner{
|
|
Config: cfg,
|
|
StateStore: stateStore,
|
|
HeartbeatService: heartbeat.New(client),
|
|
SyncService: syncservice.New(client, runtimeManager, stateStore),
|
|
Updater: updater.New(),
|
|
RuntimeManager: runtimeManager,
|
|
}
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
slog.Info("agent process started")
|
|
|
|
if err = runner.Run(ctx); err != nil && err != context.Canceled {
|
|
slog.Error("agent process exited with error", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
slog.Info("agent process stopped")
|
|
}
|