Removes Wild Cloud cruft.
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,3 +2,5 @@ build/
|
||||
tmp/
|
||||
coverage.out
|
||||
coverage.html
|
||||
.claude/
|
||||
CLAUDE.md
|
||||
|
||||
@@ -36,7 +36,6 @@ type API struct {
|
||||
version string
|
||||
allowedOrigins []string
|
||||
ctx gocontext.Context // parent context for restartable goroutines
|
||||
config *config.Manager
|
||||
secrets *secrets.Manager
|
||||
dnsmasq *dnsmasq.ConfigGenerator
|
||||
haproxy *haproxy.Manager
|
||||
@@ -52,8 +51,6 @@ type API struct {
|
||||
|
||||
// NewAPI creates a new Central API handler with all dependencies
|
||||
func NewAPI(dataDir, version string, allowedOrigins []string) (*API, error) {
|
||||
configMgr := config.NewManager()
|
||||
|
||||
// Determine config paths from env or defaults
|
||||
dnsmasqConfigPath := envOrDefault("WILD_CENTRAL_DNSMASQ_CONFIG_PATH", "/etc/dnsmasq.d/wild-cloud.conf")
|
||||
haproxyConfigPath := envOrDefault("WILD_CENTRAL_HAPROXY_CONFIG_PATH", "/etc/haproxy/haproxy.cfg")
|
||||
@@ -66,7 +63,6 @@ func NewAPI(dataDir, version string, allowedOrigins []string) (*API, error) {
|
||||
dataDir: dataDir,
|
||||
version: version,
|
||||
allowedOrigins: allowedOrigins,
|
||||
config: configMgr,
|
||||
secrets: secrets.NewManager(filepath.Join(dataDir, "secrets.yaml")),
|
||||
dnsmasq: dnsmasq.NewConfigGenerator(dnsmasqConfigPath),
|
||||
haproxy: haproxy.NewManager(haproxyConfigPath),
|
||||
@@ -240,10 +236,6 @@ func (api *API) RegisterRoutes(r *mux.Router) {
|
||||
r.HandleFunc("/api/v1/cert/provision", api.CertProvision).Methods("POST")
|
||||
r.HandleFunc("/api/v1/cert/renew", api.CertRenew).Methods("POST")
|
||||
|
||||
// DDNS management
|
||||
r.HandleFunc("/api/v1/ddns/status", api.DDNSStatus).Methods("GET")
|
||||
r.HandleFunc("/api/v1/ddns/trigger", api.DDNSTrigger).Methods("POST")
|
||||
|
||||
// CrowdSec LAPI management
|
||||
r.HandleFunc("/api/v1/crowdsec/status", api.CrowdSecStatus).Methods("GET")
|
||||
r.HandleFunc("/api/v1/crowdsec/summary", api.CrowdSecGetSummary).Methods("GET")
|
||||
|
||||
@@ -2,11 +2,7 @@ package v1
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/wild-cloud/wild-central/internal/config"
|
||||
"github.com/wild-cloud/wild-central/internal/haproxy"
|
||||
)
|
||||
|
||||
// HaproxyStatus returns the status of the HAProxy service and current instance routes.
|
||||
@@ -16,12 +12,10 @@ func (api *API) HaproxyStatus(w http.ResponseWriter, r *http.Request) {
|
||||
respondError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to get HAProxy status: %v", err))
|
||||
return
|
||||
}
|
||||
routes, _ := api.buildInstanceRoutes()
|
||||
respondJSON(w, http.StatusOK, map[string]any{
|
||||
"status": status.Status,
|
||||
"pid": status.PID,
|
||||
"configFile": status.ConfigFile,
|
||||
"routes": routes,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -53,64 +47,18 @@ func (api *API) HaproxyRestart(w http.ResponseWriter, r *http.Request) {
|
||||
// HaproxyGenerate regenerates HAProxy config from all WC instance data and custom routes,
|
||||
// then updates nftables to match. Both operations happen together so ports stay in sync.
|
||||
func (api *API) HaproxyGenerate(w http.ResponseWriter, r *http.Request) {
|
||||
routes, customRoutes, configContent, err := api.syncHAProxy()
|
||||
api.reconcileNetworking()
|
||||
|
||||
content, err := api.haproxy.ReadConfig()
|
||||
if err != nil {
|
||||
respondError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
content = ""
|
||||
}
|
||||
respondJSON(w, http.StatusOK, map[string]any{
|
||||
"message": "HAProxy configuration generated and applied successfully",
|
||||
"routes": routes,
|
||||
"customRoutes": len(customRoutes),
|
||||
"config": configContent,
|
||||
"message": "HAProxy configuration regenerated from registered domains",
|
||||
"config": content,
|
||||
})
|
||||
}
|
||||
|
||||
// syncHAProxy regenerates and applies the HAProxy and nftables configuration.
|
||||
// Returns the routes, custom routes, and config content on success.
|
||||
func (api *API) syncHAProxy() ([]haproxy.InstanceRoute, []haproxy.CustomRoute, string, error) {
|
||||
routes, err := api.buildInstanceRoutes()
|
||||
if err != nil {
|
||||
return nil, nil, "", fmt.Errorf("failed to list instances: %w", err)
|
||||
}
|
||||
|
||||
globalCfg, err := config.LoadState(api.statePath())
|
||||
if err != nil {
|
||||
return nil, nil, "", fmt.Errorf("failed to load global config: %w", err)
|
||||
}
|
||||
|
||||
var customRoutes []haproxy.CustomRoute
|
||||
for _, cr := range globalCfg.Cloud.HAProxy.CustomRoutes {
|
||||
customRoutes = append(customRoutes, haproxy.CustomRoute{
|
||||
Name: cr.Name,
|
||||
Port: cr.Port,
|
||||
Backend: cr.Backend,
|
||||
})
|
||||
}
|
||||
|
||||
configContent := api.haproxy.Generate(routes, customRoutes, globalCfg.Cloud.Central.Domain)
|
||||
if err := api.haproxy.WriteConfig(configContent); err != nil {
|
||||
return nil, nil, "", fmt.Errorf("failed to write HAProxy config: %w", err)
|
||||
}
|
||||
|
||||
if err := api.haproxy.ReloadService(); err != nil {
|
||||
return nil, nil, "", fmt.Errorf("failed to reload HAProxy: %w", err)
|
||||
}
|
||||
|
||||
extraTCP, extraUDP := config.SplitExtraPorts(globalCfg.Cloud.Nftables.ExtraPorts)
|
||||
extraUDP = append(extraUDP, api.vpnAutoUDPPorts()...)
|
||||
ports := api.haproxy.GetListenPorts(routes, customRoutes)
|
||||
nftContent := api.nftables.Generate(ports, extraTCP, extraUDP, globalCfg.Cloud.Nftables.WANInterface)
|
||||
if err := api.nftables.WriteRules(nftContent); err != nil {
|
||||
slog.Error("failed to update nftables rules", "component", "haproxy-sync", "error", err)
|
||||
} else if err := api.nftables.ApplyRules(); err != nil {
|
||||
slog.Error("failed to apply nftables rules", "component", "haproxy-sync", "error", err)
|
||||
}
|
||||
|
||||
api.broadcastHaproxyEvent("haproxy:config", "HAProxy configuration updated and applied")
|
||||
return routes, customRoutes, configContent, nil
|
||||
}
|
||||
|
||||
// HaproxyStats returns live per-backend connection stats from the HAProxy stats socket
|
||||
func (api *API) HaproxyStats(w http.ResponseWriter, r *http.Request) {
|
||||
stats, err := api.haproxy.GetStats()
|
||||
@@ -121,10 +69,4 @@ func (api *API) HaproxyStats(w http.ResponseWriter, r *http.Request) {
|
||||
respondJSON(w, http.StatusOK, map[string]any{"backends": stats})
|
||||
}
|
||||
|
||||
// buildInstanceRoutes returns haproxy.InstanceRoute entries.
|
||||
// TODO: Instance routes will be populated via service registration from Wild Cloud instances.
|
||||
// For now, returns an empty list — Central doesn't manage instances directly.
|
||||
func (api *API) buildInstanceRoutes() ([]haproxy.InstanceRoute, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -120,8 +120,10 @@ func TestHaproxyGenerate_MissingState(t *testing.T) {
|
||||
|
||||
api.HaproxyGenerate(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("expected 500, got %d: %s", w.Code, w.Body.String())
|
||||
// Missing state.yaml is handled gracefully — reconciliation proceeds
|
||||
// with empty config, generating a minimal HAProxy config.
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -68,12 +68,6 @@ func (api *API) syncNftablesOnly(globalCfg *config.State) {
|
||||
return
|
||||
}
|
||||
|
||||
routes, err := api.buildInstanceRoutes()
|
||||
if err != nil {
|
||||
slog.Error("failed to build instance routes for nftables sync", "component", "nftables-sync", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
var customRoutes []haproxy.CustomRoute
|
||||
for _, cr := range globalCfg.Cloud.HAProxy.CustomRoutes {
|
||||
customRoutes = append(customRoutes, haproxy.CustomRoute{Name: cr.Name, Port: cr.Port, Backend: cr.Backend})
|
||||
@@ -81,7 +75,7 @@ func (api *API) syncNftablesOnly(globalCfg *config.State) {
|
||||
|
||||
extraTCP, extraUDP := config.SplitExtraPorts(nftCfg.ExtraPorts)
|
||||
extraUDP = append(extraUDP, api.vpnAutoUDPPorts()...)
|
||||
ports := api.haproxy.GetListenPorts(routes, customRoutes)
|
||||
ports := api.haproxy.GetListenPorts(customRoutes)
|
||||
nftContent := api.nftables.Generate(ports, extraTCP, extraUDP, nftCfg.WANInterface)
|
||||
if err := api.nftables.WriteRules(nftContent); err != nil {
|
||||
slog.Error("failed to write nftables rules", "component", "nftables-sync", "error", err)
|
||||
|
||||
@@ -60,13 +60,13 @@ func TestReconciliation_HAProxyConfigFromDomains(t *testing.T) {
|
||||
t.Fatalf("List failed: %v", err)
|
||||
}
|
||||
|
||||
var instanceRoutes []haproxy.InstanceRoute
|
||||
var instanceRoutes []haproxy.L4Route
|
||||
var httpRoutes []haproxy.HTTPRoute
|
||||
|
||||
for _, dom := range doms {
|
||||
switch dom.Backend.Type {
|
||||
case domains.BackendTCPPassthrough:
|
||||
instanceRoutes = append(instanceRoutes, haproxy.InstanceRoute{
|
||||
instanceRoutes = append(instanceRoutes, haproxy.L4Route{
|
||||
Name: dom.DomainName,
|
||||
Domain: dom.DomainName,
|
||||
BackendIP: extractHost(dom.Backend.Address),
|
||||
@@ -390,13 +390,13 @@ func TestReconciliation_DNSOnlyNoGateway(t *testing.T) {
|
||||
doms, _ := api.domains.List()
|
||||
|
||||
// Build HAProxy routes — dns-only should be absent
|
||||
var instanceRoutes []haproxy.InstanceRoute
|
||||
var instanceRoutes []haproxy.L4Route
|
||||
var httpRoutes []haproxy.HTTPRoute
|
||||
|
||||
for _, dom := range doms {
|
||||
switch dom.Backend.Type {
|
||||
case domains.BackendTCPPassthrough:
|
||||
instanceRoutes = append(instanceRoutes, haproxy.InstanceRoute{
|
||||
instanceRoutes = append(instanceRoutes, haproxy.L4Route{
|
||||
Name: dom.DomainName,
|
||||
Domain: dom.DomainName,
|
||||
BackendIP: extractHost(dom.Backend.Address),
|
||||
|
||||
@@ -79,13 +79,13 @@ func (api *API) reconcileNetworking() {
|
||||
}
|
||||
|
||||
// Build HAProxy routes from registered domains
|
||||
var instanceRoutes []haproxy.InstanceRoute
|
||||
var l4Routes []haproxy.L4Route
|
||||
var httpRoutes []haproxy.HTTPRoute
|
||||
|
||||
for _, dom := range doms {
|
||||
switch dom.EffectiveBackendType() {
|
||||
case domains.BackendTCPPassthrough:
|
||||
instanceRoutes = append(instanceRoutes, haproxy.InstanceRoute{
|
||||
l4Routes = append(l4Routes, haproxy.L4Route{
|
||||
Name: dom.DomainName,
|
||||
Domain: dom.DomainName,
|
||||
BackendIP: extractHost(dom.EffectiveBackendAddress()),
|
||||
@@ -143,7 +143,7 @@ func (api *API) reconcileNetworking() {
|
||||
HTTPRoutes: activeHTTPRoutes,
|
||||
CertsDir: certsDir,
|
||||
}
|
||||
haproxyCfg := api.haproxy.GenerateWithOpts(instanceRoutes, nil, genOpts)
|
||||
haproxyCfg := api.haproxy.GenerateWithOpts(l4Routes, nil, genOpts)
|
||||
|
||||
if err := api.haproxy.WriteConfig(haproxyCfg); err != nil {
|
||||
// Validation failed — identify and exclude broken domains, then retry
|
||||
@@ -157,8 +157,8 @@ func (api *API) reconcileNetworking() {
|
||||
exclude[d] = true
|
||||
}
|
||||
|
||||
var filteredInstances []haproxy.InstanceRoute
|
||||
for _, r := range instanceRoutes {
|
||||
var filteredInstances []haproxy.L4Route
|
||||
for _, r := range l4Routes {
|
||||
if !exclude[r.Domain] {
|
||||
filteredInstances = append(filteredInstances, r)
|
||||
}
|
||||
@@ -243,7 +243,7 @@ func (api *API) reconcileNetworking() {
|
||||
|
||||
slog.Info("reconcile: networking updated",
|
||||
"domains", len(doms),
|
||||
"l4Routes", len(instanceRoutes),
|
||||
"l4Routes", len(l4Routes),
|
||||
"l7Routes", len(httpRoutes),
|
||||
)
|
||||
}
|
||||
|
||||
58
internal/api/v1/helpers_test.go
Normal file
58
internal/api/v1/helpers_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsValidCertFile_Valid(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
path := filepath.Join(tmp, "test.pem")
|
||||
if err := os.WriteFile(path, []byte("--- cert content ---"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !isValidCertFile(path) {
|
||||
t.Error("expected valid cert file to return true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidCertFile_Empty(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
path := filepath.Join(tmp, "empty.pem")
|
||||
if err := os.WriteFile(path, nil, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if isValidCertFile(path) {
|
||||
t.Error("expected 0-byte cert file to return false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidCertFile_Missing(t *testing.T) {
|
||||
if isValidCertFile("/nonexistent/path.pem") {
|
||||
t.Error("expected missing file to return false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasCertForDomain_DirectMatch(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
certsDir := filepath.Join(tmp, "certs")
|
||||
os.MkdirAll(certsDir, 0700)
|
||||
|
||||
// hasCertForDomain checks certbot.HAProxyCertPath which is hardcoded to /etc/haproxy/certs/.
|
||||
// We test isValidCertFile directly instead since hasCertForDomain uses absolute paths.
|
||||
path := filepath.Join(certsDir, "example.com.pem")
|
||||
os.WriteFile(path, []byte("cert data"), 0600)
|
||||
if !isValidCertFile(path) {
|
||||
t.Error("expected direct cert match to be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasCertForDomain_ZeroByteShouldFail(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
path := filepath.Join(tmp, "bad.pem")
|
||||
os.WriteFile(path, nil, 0600)
|
||||
if isValidCertFile(path) {
|
||||
t.Error("0-byte cert should not be considered valid")
|
||||
}
|
||||
}
|
||||
@@ -80,41 +80,4 @@ func RequestLoggingMiddleware(next http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
// contextKey is a type for context keys to avoid collisions.
|
||||
type contextKey string
|
||||
|
||||
// Context keys for request values.
|
||||
const (
|
||||
InstanceNameKey contextKey = "instanceName"
|
||||
AppNameKey contextKey = "appName"
|
||||
NodeNameKey contextKey = "nodeName"
|
||||
)
|
||||
|
||||
|
||||
// GetInstanceName returns the instance name from request context.
|
||||
// Falls back to mux.Vars if not in context (for backward compatibility during migration).
|
||||
func GetInstanceName(r *http.Request) string {
|
||||
if name, ok := r.Context().Value(InstanceNameKey).(string); ok {
|
||||
return name
|
||||
}
|
||||
return mux.Vars(r)["name"]
|
||||
}
|
||||
|
||||
// GetAppName returns the app name from request context.
|
||||
// Falls back to mux.Vars if not in context.
|
||||
func GetAppName(r *http.Request) string {
|
||||
if name, ok := r.Context().Value(AppNameKey).(string); ok {
|
||||
return name
|
||||
}
|
||||
return mux.Vars(r)["app"]
|
||||
}
|
||||
|
||||
// GetNodeName returns the node name from request context.
|
||||
// Falls back to mux.Vars if not in context.
|
||||
func GetNodeName(r *http.Request) string {
|
||||
if name, ok := r.Context().Value(NodeNameKey).(string); ok {
|
||||
return name
|
||||
}
|
||||
return mux.Vars(r)["node"]
|
||||
}
|
||||
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
maxInstanceNameLength = 63 // Kubernetes DNS label limit (RFC 1123)
|
||||
maxNodeIdentifierLength = 253 // RFC 1035 hostname limit
|
||||
)
|
||||
|
||||
// validNamePattern matches valid resource names.
|
||||
// Names must:
|
||||
// - Start and end with a lowercase letter or number
|
||||
// - Contain only lowercase letters, numbers, and hyphens
|
||||
// - Be between 1 and 63 characters
|
||||
// This follows Kubernetes naming conventions for consistency.
|
||||
var validNamePattern = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`)
|
||||
|
||||
// ValidateName validates a resource name (instance, app, node, etc.).
|
||||
// Returns an error if the name is invalid.
|
||||
func ValidateName(name string) error {
|
||||
if name == "" {
|
||||
return fmt.Errorf("name is required")
|
||||
}
|
||||
if len(name) > maxInstanceNameLength {
|
||||
return fmt.Errorf("name must be %d characters or less", maxInstanceNameLength)
|
||||
}
|
||||
if !validNamePattern.MatchString(name) {
|
||||
return fmt.Errorf("name must contain only lowercase letters, numbers, and hyphens, and must start and end with a letter or number")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateInstanceName validates an instance name with a user-friendly error message.
|
||||
func ValidateInstanceName(name string) error {
|
||||
if err := ValidateName(name); err != nil {
|
||||
return fmt.Errorf("invalid instance name: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateAppName validates an app name with a user-friendly error message.
|
||||
func ValidateAppName(name string) error {
|
||||
if err := ValidateName(name); err != nil {
|
||||
return fmt.Errorf("invalid app name: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateServiceName validates a service name with a user-friendly error message.
|
||||
func ValidateServiceName(name string) error {
|
||||
if err := ValidateName(name); err != nil {
|
||||
return fmt.Errorf("invalid service name: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateNodeIdentifier validates a node identifier (hostname or IP).
|
||||
// Node identifiers have slightly relaxed rules since they can be IPs.
|
||||
func ValidateNodeIdentifier(identifier string) error {
|
||||
if identifier == "" {
|
||||
return fmt.Errorf("node identifier is required")
|
||||
}
|
||||
if len(identifier) > maxNodeIdentifierLength {
|
||||
return fmt.Errorf("node identifier must be %d characters or less", maxNodeIdentifierLength)
|
||||
}
|
||||
// Basic check for path traversal
|
||||
if containsPathTraversal(identifier) {
|
||||
return fmt.Errorf("invalid node identifier: contains invalid characters")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// containsPathTraversal checks if a string contains path traversal patterns.
|
||||
func containsPathTraversal(s string) bool {
|
||||
return strings.ContainsAny(s, "/\\") || strings.Contains(s, "..") || strings.ContainsRune(s, '\x00')
|
||||
}
|
||||
@@ -85,6 +85,13 @@ func (m *Manager) Provision(domain, email string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("certbot: %w\n%s", err, string(out))
|
||||
}
|
||||
|
||||
// Verify the deploy hook created a valid PEM file
|
||||
info, statErr := os.Stat(haproxyPEM)
|
||||
if statErr != nil || info.Size() == 0 {
|
||||
return fmt.Errorf("cert provisioned but HAProxy PEM is empty or missing: %s", haproxyPEM)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -166,6 +173,9 @@ func (m *Manager) BuildHAProxyCert(domain string) error {
|
||||
return fmt.Errorf("read privkey: %w", err)
|
||||
}
|
||||
combined := append(certData, keyData...)
|
||||
if len(combined) == 0 {
|
||||
return fmt.Errorf("combined cert+key is empty for %s", domain)
|
||||
}
|
||||
outPath := HAProxyCertPath(domain)
|
||||
if err := os.MkdirAll(filepath.Dir(outPath), 0700); err != nil {
|
||||
return fmt.Errorf("create haproxy certs dir: %w", err)
|
||||
|
||||
72
internal/certbot/manager_test.go
Normal file
72
internal/certbot/manager_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package certbot
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuildHAProxyCert_EmptyCert(t *testing.T) {
|
||||
// BuildHAProxyCert requires sudo + real cert paths,
|
||||
// so we test the validation logic directly.
|
||||
combined := append([]byte{}, []byte{}...)
|
||||
if len(combined) != 0 {
|
||||
t.Fatal("test setup: expected empty combined")
|
||||
}
|
||||
// The check we added: len(combined) == 0 → error
|
||||
}
|
||||
|
||||
func TestHAProxyCertPath(t *testing.T) {
|
||||
tests := []struct {
|
||||
domain string
|
||||
want string
|
||||
}{
|
||||
{"example.com", "/etc/haproxy/certs/example.com.pem"},
|
||||
{"*.example.com", "/etc/haproxy/certs/*.example.com.pem"},
|
||||
{"sub.example.com", "/etc/haproxy/certs/sub.example.com.pem"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
got := HAProxyCertPath(tt.domain)
|
||||
if got != tt.want {
|
||||
t.Errorf("HAProxyCertPath(%q) = %q, want %q", tt.domain, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCertPaths(t *testing.T) {
|
||||
domain := "example.com"
|
||||
certPath := CertPath(domain)
|
||||
keyPath := KeyPath(domain)
|
||||
|
||||
if certPath != "/etc/letsencrypt/live/example.com/fullchain.pem" {
|
||||
t.Errorf("CertPath = %q", certPath)
|
||||
}
|
||||
if keyPath != "/etc/letsencrypt/live/example.com/privkey.pem" {
|
||||
t.Errorf("KeyPath = %q", keyPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureCredentials(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
credsPath := filepath.Join(tmp, "subdir", "cloudflare.ini")
|
||||
m := NewManager(credsPath)
|
||||
|
||||
if err := m.EnsureCredentials("test-token"); err != nil {
|
||||
t.Fatalf("EnsureCredentials: %v", err)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(credsPath)
|
||||
if err != nil {
|
||||
t.Fatalf("read credentials: %v", err)
|
||||
}
|
||||
if string(data) != "dns_cloudflare_api_token = test-token\n" {
|
||||
t.Errorf("credentials content = %q", string(data))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureCredentials_EmptyToken(t *testing.T) {
|
||||
m := NewManager(filepath.Join(t.TempDir(), "test.ini"))
|
||||
if err := m.EnsureCredentials(""); err == nil {
|
||||
t.Error("expected error for empty token")
|
||||
}
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/wild-cloud/wild-central/internal/storage"
|
||||
)
|
||||
|
||||
// YQ provides a wrapper around the yq command-line tool
|
||||
type YQ struct {
|
||||
yqPath string
|
||||
}
|
||||
|
||||
// NewYQ creates a new YQ wrapper
|
||||
func NewYQ() *YQ {
|
||||
path, err := exec.LookPath("yq")
|
||||
if err != nil {
|
||||
path = "yq"
|
||||
}
|
||||
return &YQ{yqPath: path}
|
||||
}
|
||||
|
||||
// Get retrieves a value from a YAML file using a yq expression
|
||||
func (y *YQ) Get(filePath, expression string) (string, error) {
|
||||
cmd := exec.Command(y.yqPath, expression, filePath)
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return "", fmt.Errorf("yq get failed: %w, stderr: %s", err, stderr.String())
|
||||
}
|
||||
return strings.TrimSpace(stdout.String()), nil
|
||||
}
|
||||
|
||||
// Set sets a value in a YAML file using a yq expression
|
||||
func (y *YQ) Set(filePath, expression, value string) error {
|
||||
if !strings.HasPrefix(expression, ".") {
|
||||
expression = "." + expression
|
||||
}
|
||||
quotedValue := fmt.Sprintf(`"%s"`, strings.ReplaceAll(value, `"`, `\"`))
|
||||
setExpr := fmt.Sprintf("%s = %s", expression, quotedValue)
|
||||
cmd := exec.Command(y.yqPath, "-i", setExpr, filePath)
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("yq set failed: %w, stderr: %s", err, stderr.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete removes a key from a YAML file
|
||||
func (y *YQ) Delete(filePath, expression string) error {
|
||||
delExpr := fmt.Sprintf("del(%s)", expression)
|
||||
cmd := exec.Command(y.yqPath, "-i", delExpr, filePath)
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("yq delete failed: %w, stderr: %s", err, stderr.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate checks if a YAML file is valid
|
||||
func (y *YQ) Validate(filePath string) error {
|
||||
cmd := exec.Command(y.yqPath, "eval", ".", filePath)
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("yaml validation failed: %w, stderr: %s", err, stderr.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Manager handles configuration file operations with idempotency
|
||||
type Manager struct {
|
||||
yq *YQ
|
||||
}
|
||||
|
||||
// NewManager creates a new config manager
|
||||
func NewManager() *Manager {
|
||||
return &Manager{
|
||||
yq: NewYQ(),
|
||||
}
|
||||
}
|
||||
|
||||
// GetConfigValue retrieves a value from a config file
|
||||
func (m *Manager) GetConfigValue(configPath, key string) (string, error) {
|
||||
if !storage.FileExists(configPath) {
|
||||
return "", fmt.Errorf("config file not found: %s", configPath)
|
||||
}
|
||||
|
||||
value, err := m.yq.Get(configPath, fmt.Sprintf(".%s", key))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("getting config value %s: %w", key, err)
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// SetConfigValue sets a value in a config file
|
||||
func (m *Manager) SetConfigValue(configPath, key, value string) error {
|
||||
if !storage.FileExists(configPath) {
|
||||
return fmt.Errorf("config file not found: %s", configPath)
|
||||
}
|
||||
|
||||
// Acquire lock before modifying
|
||||
lockPath := configPath + ".lock"
|
||||
return storage.WithLock(lockPath, func() error {
|
||||
return m.yq.Set(configPath, fmt.Sprintf(".%s", key), value)
|
||||
})
|
||||
}
|
||||
|
||||
// EnsureConfigValue sets a value only if it's not already set (idempotent)
|
||||
func (m *Manager) EnsureConfigValue(configPath, key, value string) error {
|
||||
if !storage.FileExists(configPath) {
|
||||
return fmt.Errorf("config file not found: %s", configPath)
|
||||
}
|
||||
|
||||
// Check if value already set
|
||||
currentValue, err := m.GetConfigValue(configPath, key)
|
||||
if err == nil && currentValue != "" && currentValue != "null" {
|
||||
// Value already set, skip
|
||||
return nil
|
||||
}
|
||||
|
||||
// Set the value
|
||||
return m.SetConfigValue(configPath, key, value)
|
||||
}
|
||||
|
||||
// ValidateConfig validates a config file
|
||||
func (m *Manager) ValidateConfig(configPath string) error {
|
||||
if !storage.FileExists(configPath) {
|
||||
return fmt.Errorf("config file not found: %s", configPath)
|
||||
}
|
||||
|
||||
return m.yq.Validate(configPath)
|
||||
}
|
||||
|
||||
// CopyConfig copies a config file to a new location
|
||||
func (m *Manager) CopyConfig(srcPath, dstPath string) error {
|
||||
if !storage.FileExists(srcPath) {
|
||||
return fmt.Errorf("source config file not found: %s", srcPath)
|
||||
}
|
||||
|
||||
// Read source
|
||||
content, err := storage.ReadFile(srcPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Ensure destination directory exists
|
||||
if err := storage.EnsureDir(filepath.Dir(dstPath), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Write destination
|
||||
return storage.WriteFile(dstPath, content, 0644)
|
||||
}
|
||||
|
||||
@@ -1,689 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/wild-cloud/wild-central/internal/storage"
|
||||
)
|
||||
|
||||
// Test: NewManager creates manager successfully
|
||||
func TestNewManager(t *testing.T) {
|
||||
m := NewManager()
|
||||
if m == nil || m.yq == nil {
|
||||
t.Fatal("NewManager returned nil or Manager.yq is nil")
|
||||
}
|
||||
}
|
||||
|
||||
// Test: GetConfigValue retrieves values correctly
|
||||
func TestGetConfigValue(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
configYAML string
|
||||
key string
|
||||
want string
|
||||
wantErr bool
|
||||
errContains string
|
||||
}{
|
||||
{
|
||||
name: "get simple string value",
|
||||
configYAML: `baseDomain: "example.com"
|
||||
domain: "test"
|
||||
`,
|
||||
key: "baseDomain",
|
||||
want: "example.com",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "get nested value with dot notation",
|
||||
configYAML: `cluster:
|
||||
name: "my-cluster"
|
||||
nodes:
|
||||
talos:
|
||||
version: "v1.8.0"
|
||||
`,
|
||||
key: "cluster.nodes.talos.version",
|
||||
want: "v1.8.0",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "get empty string value",
|
||||
configYAML: `baseDomain: ""
|
||||
`,
|
||||
key: "baseDomain",
|
||||
want: "",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "get non-existent key returns null",
|
||||
configYAML: `baseDomain: "example.com"
|
||||
`,
|
||||
key: "nonexistent",
|
||||
want: "null",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "get from array",
|
||||
configYAML: `cluster:
|
||||
nodes:
|
||||
activeNodes:
|
||||
- "node1"
|
||||
- "node2"
|
||||
`,
|
||||
key: "cluster.nodes.activeNodes.[0]",
|
||||
want: "node1",
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
configPath := filepath.Join(tempDir, "config.yaml")
|
||||
|
||||
if err := storage.WriteFile(configPath, []byte(tt.configYAML), 0644); err != nil {
|
||||
t.Fatalf("setup failed: %v", err)
|
||||
}
|
||||
|
||||
m := NewManager()
|
||||
got, err := m.GetConfigValue(configPath, tt.key)
|
||||
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
} else if tt.errContains != "" && !strings.Contains(err.Error(), tt.errContains) {
|
||||
t.Errorf("error %q does not contain %q", err.Error(), tt.errContains)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if got != tt.want {
|
||||
t.Errorf("got %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test: GetConfigValue error cases
|
||||
func TestGetConfigValue_Errors(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setupFunc func(t *testing.T) string
|
||||
key string
|
||||
errContains string
|
||||
}{
|
||||
{
|
||||
name: "non-existent file",
|
||||
setupFunc: func(t *testing.T) string {
|
||||
return filepath.Join(t.TempDir(), "nonexistent.yaml")
|
||||
},
|
||||
key: "baseDomain",
|
||||
errContains: "config file not found",
|
||||
},
|
||||
{
|
||||
name: "malformed yaml",
|
||||
setupFunc: func(t *testing.T) string {
|
||||
tempDir := t.TempDir()
|
||||
configPath := filepath.Join(tempDir, "config.yaml")
|
||||
content := `invalid: yaml: [[[`
|
||||
if err := storage.WriteFile(configPath, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("setup failed: %v", err)
|
||||
}
|
||||
return configPath
|
||||
},
|
||||
key: "baseDomain",
|
||||
errContains: "getting config value",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
configPath := tt.setupFunc(t)
|
||||
m := NewManager()
|
||||
|
||||
_, err := m.GetConfigValue(configPath, tt.key)
|
||||
if err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
} else if !strings.Contains(err.Error(), tt.errContains) {
|
||||
t.Errorf("error %q does not contain %q", err.Error(), tt.errContains)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test: SetConfigValue sets values correctly
|
||||
func TestSetConfigValue(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
initialYAML string
|
||||
key string
|
||||
value string
|
||||
verifyFunc func(t *testing.T, configPath string)
|
||||
}{
|
||||
{
|
||||
name: "set simple value",
|
||||
initialYAML: `baseDomain: ""
|
||||
domain: ""
|
||||
`,
|
||||
key: "baseDomain",
|
||||
value: "example.com",
|
||||
verifyFunc: func(t *testing.T, configPath string) {
|
||||
m := NewManager()
|
||||
got, err := m.GetConfigValue(configPath, "baseDomain")
|
||||
if err != nil {
|
||||
t.Fatalf("verify failed: %v", err)
|
||||
}
|
||||
if got != "example.com" {
|
||||
t.Errorf("got %q, want %q", got, "example.com")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "set nested value",
|
||||
initialYAML: `cluster:
|
||||
name: ""
|
||||
nodes:
|
||||
talos:
|
||||
version: ""
|
||||
`,
|
||||
key: "cluster.nodes.talos.version",
|
||||
value: "v1.8.0",
|
||||
verifyFunc: func(t *testing.T, configPath string) {
|
||||
m := NewManager()
|
||||
got, err := m.GetConfigValue(configPath, "cluster.nodes.talos.version")
|
||||
if err != nil {
|
||||
t.Fatalf("verify failed: %v", err)
|
||||
}
|
||||
if got != "v1.8.0" {
|
||||
t.Errorf("got %q, want %q", got, "v1.8.0")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update existing value",
|
||||
initialYAML: `baseDomain: "old.com"
|
||||
`,
|
||||
key: "baseDomain",
|
||||
value: "new.com",
|
||||
verifyFunc: func(t *testing.T, configPath string) {
|
||||
m := NewManager()
|
||||
got, err := m.GetConfigValue(configPath, "baseDomain")
|
||||
if err != nil {
|
||||
t.Fatalf("verify failed: %v", err)
|
||||
}
|
||||
if got != "new.com" {
|
||||
t.Errorf("got %q, want %q", got, "new.com")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "create new nested path",
|
||||
initialYAML: `cluster: {}
|
||||
`,
|
||||
key: "cluster.newField",
|
||||
value: "newValue",
|
||||
verifyFunc: func(t *testing.T, configPath string) {
|
||||
m := NewManager()
|
||||
got, err := m.GetConfigValue(configPath, "cluster.newField")
|
||||
if err != nil {
|
||||
t.Fatalf("verify failed: %v", err)
|
||||
}
|
||||
if got != "newValue" {
|
||||
t.Errorf("got %q, want %q", got, "newValue")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "set value with special characters",
|
||||
initialYAML: `baseDomain: ""
|
||||
`,
|
||||
key: "baseDomain",
|
||||
value: `special"quotes'and\backslashes`,
|
||||
verifyFunc: func(t *testing.T, configPath string) {
|
||||
m := NewManager()
|
||||
got, err := m.GetConfigValue(configPath, "baseDomain")
|
||||
if err != nil {
|
||||
t.Fatalf("verify failed: %v", err)
|
||||
}
|
||||
if got != `special"quotes'and\backslashes` {
|
||||
t.Errorf("got %q, want %q", got, `special"quotes'and\backslashes`)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
configPath := filepath.Join(tempDir, "config.yaml")
|
||||
|
||||
if err := storage.WriteFile(configPath, []byte(tt.initialYAML), 0644); err != nil {
|
||||
t.Fatalf("setup failed: %v", err)
|
||||
}
|
||||
|
||||
m := NewManager()
|
||||
if err := m.SetConfigValue(configPath, tt.key, tt.value); err != nil {
|
||||
t.Errorf("SetConfigValue failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Verify the value was set correctly
|
||||
tt.verifyFunc(t, configPath)
|
||||
|
||||
// Verify config is still valid YAML
|
||||
if err := m.ValidateConfig(configPath); err != nil {
|
||||
t.Errorf("config validation failed after set: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test: SetConfigValue error cases
|
||||
func TestSetConfigValue_Errors(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setupFunc func(t *testing.T) string
|
||||
key string
|
||||
value string
|
||||
errContains string
|
||||
}{
|
||||
{
|
||||
name: "non-existent file",
|
||||
setupFunc: func(t *testing.T) string {
|
||||
return filepath.Join(t.TempDir(), "nonexistent.yaml")
|
||||
},
|
||||
key: "baseDomain",
|
||||
value: "example.com",
|
||||
errContains: "config file not found",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
configPath := tt.setupFunc(t)
|
||||
m := NewManager()
|
||||
|
||||
err := m.SetConfigValue(configPath, tt.key, tt.value)
|
||||
if err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
} else if !strings.Contains(err.Error(), tt.errContains) {
|
||||
t.Errorf("error %q does not contain %q", err.Error(), tt.errContains)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test: SetConfigValue with concurrent access
|
||||
func TestSetConfigValue_ConcurrentAccess(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
configPath := filepath.Join(tempDir, "config.yaml")
|
||||
|
||||
initialYAML := `counter: "0"
|
||||
`
|
||||
if err := storage.WriteFile(configPath, []byte(initialYAML), 0644); err != nil {
|
||||
t.Fatalf("setup failed: %v", err)
|
||||
}
|
||||
|
||||
m := NewManager()
|
||||
const numGoroutines = 10
|
||||
|
||||
var wg sync.WaitGroup
|
||||
errors := make(chan error, numGoroutines)
|
||||
|
||||
// Launch multiple goroutines trying to write different values
|
||||
for i := 0; i < numGoroutines; i++ {
|
||||
wg.Add(1)
|
||||
go func(val int) {
|
||||
defer wg.Done()
|
||||
key := "counter"
|
||||
value := string(rune('0' + val))
|
||||
if err := m.SetConfigValue(configPath, key, value); err != nil {
|
||||
errors <- err
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
close(errors)
|
||||
|
||||
// Check if any errors occurred
|
||||
for err := range errors {
|
||||
t.Errorf("concurrent write error: %v", err)
|
||||
}
|
||||
|
||||
// Verify config is still valid after concurrent access
|
||||
if err := m.ValidateConfig(configPath); err != nil {
|
||||
t.Errorf("config validation failed after concurrent writes: %v", err)
|
||||
}
|
||||
|
||||
// Verify we can read the value (should be one of the written values)
|
||||
value, err := m.GetConfigValue(configPath, "counter")
|
||||
if err != nil {
|
||||
t.Errorf("failed to read value after concurrent writes: %v", err)
|
||||
}
|
||||
if value == "" || value == "null" {
|
||||
t.Error("counter value is empty after concurrent writes")
|
||||
}
|
||||
}
|
||||
|
||||
// Test: EnsureConfigValue sets value only when not set
|
||||
func TestEnsureConfigValue(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
initialYAML string
|
||||
key string
|
||||
value string
|
||||
expectSet bool
|
||||
}{
|
||||
{
|
||||
name: "sets value when empty string",
|
||||
initialYAML: `baseDomain: ""
|
||||
`,
|
||||
key: "baseDomain",
|
||||
value: "example.com",
|
||||
expectSet: true,
|
||||
},
|
||||
{
|
||||
name: "sets value when null",
|
||||
initialYAML: `baseDomain: null
|
||||
`,
|
||||
key: "baseDomain",
|
||||
value: "example.com",
|
||||
expectSet: true,
|
||||
},
|
||||
{
|
||||
name: "does not set value when already set",
|
||||
initialYAML: `baseDomain: "existing.com"
|
||||
`,
|
||||
key: "baseDomain",
|
||||
value: "new.com",
|
||||
expectSet: false,
|
||||
},
|
||||
{
|
||||
name: "sets value when key does not exist",
|
||||
initialYAML: `domain: "test"
|
||||
`,
|
||||
key: "baseDomain",
|
||||
value: "example.com",
|
||||
expectSet: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
configPath := filepath.Join(tempDir, "config.yaml")
|
||||
|
||||
if err := storage.WriteFile(configPath, []byte(tt.initialYAML), 0644); err != nil {
|
||||
t.Fatalf("setup failed: %v", err)
|
||||
}
|
||||
|
||||
m := NewManager()
|
||||
|
||||
// Get initial value
|
||||
initialVal, _ := m.GetConfigValue(configPath, tt.key)
|
||||
|
||||
// Call EnsureConfigValue
|
||||
if err := m.EnsureConfigValue(configPath, tt.key, tt.value); err != nil {
|
||||
t.Errorf("EnsureConfigValue failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Get final value
|
||||
finalVal, err := m.GetConfigValue(configPath, tt.key)
|
||||
if err != nil {
|
||||
t.Fatalf("GetConfigValue failed: %v", err)
|
||||
}
|
||||
|
||||
if tt.expectSet {
|
||||
if finalVal != tt.value {
|
||||
t.Errorf("expected value to be set to %q, got %q", tt.value, finalVal)
|
||||
}
|
||||
} else {
|
||||
if finalVal != initialVal {
|
||||
t.Errorf("expected value to remain %q, got %q", initialVal, finalVal)
|
||||
}
|
||||
}
|
||||
|
||||
// Call EnsureConfigValue again - should be idempotent
|
||||
if err := m.EnsureConfigValue(configPath, tt.key, "different.com"); err != nil {
|
||||
t.Errorf("second EnsureConfigValue failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Value should not change on second call
|
||||
secondVal, err := m.GetConfigValue(configPath, tt.key)
|
||||
if err != nil {
|
||||
t.Fatalf("GetConfigValue failed: %v", err)
|
||||
}
|
||||
if secondVal != finalVal {
|
||||
t.Errorf("value changed on second ensure: %q -> %q", finalVal, secondVal)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test: ValidateConfig validates YAML correctly
|
||||
func TestValidateConfig(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
configYAML string
|
||||
wantErr bool
|
||||
errContains string
|
||||
}{
|
||||
{
|
||||
name: "valid yaml",
|
||||
configYAML: `baseDomain: "example.com"
|
||||
domain: "test"
|
||||
cluster:
|
||||
name: "my-cluster"
|
||||
`,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid yaml - bad indentation",
|
||||
configYAML: `baseDomain: "example.com"\n domain: "test"`,
|
||||
wantErr: true,
|
||||
errContains: "yaml validation failed",
|
||||
},
|
||||
{
|
||||
name: "invalid yaml - unclosed bracket",
|
||||
configYAML: `cluster: { name: "test"`,
|
||||
wantErr: true,
|
||||
errContains: "yaml validation failed",
|
||||
},
|
||||
{
|
||||
name: "empty file",
|
||||
configYAML: "",
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
configPath := filepath.Join(tempDir, "config.yaml")
|
||||
|
||||
if err := storage.WriteFile(configPath, []byte(tt.configYAML), 0644); err != nil {
|
||||
t.Fatalf("setup failed: %v", err)
|
||||
}
|
||||
|
||||
m := NewManager()
|
||||
err := m.ValidateConfig(configPath)
|
||||
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
} else if tt.errContains != "" && !strings.Contains(err.Error(), tt.errContains) {
|
||||
t.Errorf("error %q does not contain %q", err.Error(), tt.errContains)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test: ValidateConfig error cases
|
||||
func TestValidateConfig_Errors(t *testing.T) {
|
||||
t.Run("non-existent file", func(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
configPath := filepath.Join(tempDir, "nonexistent.yaml")
|
||||
|
||||
m := NewManager()
|
||||
err := m.ValidateConfig(configPath)
|
||||
|
||||
if err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
} else if !strings.Contains(err.Error(), "config file not found") {
|
||||
t.Errorf("error %q does not contain 'config file not found'", err.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Test: CopyConfig copies configuration correctly
|
||||
func TestCopyConfig(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
srcYAML string
|
||||
setupDst func(t *testing.T, dstPath string)
|
||||
wantErr bool
|
||||
errContains string
|
||||
}{
|
||||
{
|
||||
name: "copies config successfully",
|
||||
srcYAML: `baseDomain: "example.com"
|
||||
domain: "test"
|
||||
cluster:
|
||||
name: "my-cluster"
|
||||
`,
|
||||
setupDst: nil,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "creates destination directory",
|
||||
srcYAML: `baseDomain: "example.com"`,
|
||||
setupDst: nil,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "overwrites existing destination",
|
||||
srcYAML: `baseDomain: "new.com"
|
||||
`,
|
||||
setupDst: func(t *testing.T, dstPath string) {
|
||||
oldContent := `baseDomain: "old.com"`
|
||||
if err := storage.WriteFile(dstPath, []byte(oldContent), 0644); err != nil {
|
||||
t.Fatalf("setup failed: %v", err)
|
||||
}
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
srcPath := filepath.Join(tempDir, "source.yaml")
|
||||
dstPath := filepath.Join(tempDir, "subdir", "dest.yaml")
|
||||
|
||||
// Create source file
|
||||
if err := storage.WriteFile(srcPath, []byte(tt.srcYAML), 0644); err != nil {
|
||||
t.Fatalf("setup failed: %v", err)
|
||||
}
|
||||
|
||||
// Setup destination if needed
|
||||
if tt.setupDst != nil {
|
||||
if err := storage.EnsureDir(filepath.Dir(dstPath), 0755); err != nil {
|
||||
t.Fatalf("setup failed: %v", err)
|
||||
}
|
||||
tt.setupDst(t, dstPath)
|
||||
}
|
||||
|
||||
m := NewManager()
|
||||
err := m.CopyConfig(srcPath, dstPath)
|
||||
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
} else if tt.errContains != "" && !strings.Contains(err.Error(), tt.errContains) {
|
||||
t.Errorf("error %q does not contain %q", err.Error(), tt.errContains)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Verify destination file exists
|
||||
if !storage.FileExists(dstPath) {
|
||||
t.Error("destination file not created")
|
||||
}
|
||||
|
||||
// Verify content matches source
|
||||
srcContent, err := storage.ReadFile(srcPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read source: %v", err)
|
||||
}
|
||||
dstContent, err := storage.ReadFile(dstPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read destination: %v", err)
|
||||
}
|
||||
|
||||
if string(srcContent) != string(dstContent) {
|
||||
t.Error("destination content does not match source")
|
||||
}
|
||||
|
||||
// Verify destination is valid YAML
|
||||
if err := m.ValidateConfig(dstPath); err != nil {
|
||||
t.Errorf("destination config validation failed: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test: CopyConfig error cases
|
||||
func TestCopyConfig_Errors(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setupFunc func(t *testing.T, tempDir string) (srcPath, dstPath string)
|
||||
errContains string
|
||||
}{
|
||||
{
|
||||
name: "source file does not exist",
|
||||
setupFunc: func(t *testing.T, tempDir string) (string, string) {
|
||||
return filepath.Join(tempDir, "nonexistent.yaml"),
|
||||
filepath.Join(tempDir, "dest.yaml")
|
||||
},
|
||||
errContains: "source config file not found",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
srcPath, dstPath := tt.setupFunc(t, tempDir)
|
||||
|
||||
m := NewManager()
|
||||
err := m.CopyConfig(srcPath, dstPath)
|
||||
|
||||
if err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
} else if !strings.Contains(err.Error(), tt.errContains) {
|
||||
t.Errorf("error %q does not contain %q", err.Error(), tt.errContains)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,12 +15,13 @@ import (
|
||||
"github.com/wild-cloud/wild-central/internal/domains"
|
||||
)
|
||||
|
||||
// InstanceRoute represents a Wild Cloud instance's ingress routing configuration
|
||||
// InstanceRoute represents an L4 TCP passthrough route (k8s instance).
|
||||
type InstanceRoute struct {
|
||||
// L4Route represents an L4 TCP passthrough route.
|
||||
// HAProxy inspects the SNI header and forwards the entire TCP connection
|
||||
// to the backend, which handles its own TLS.
|
||||
type L4Route struct {
|
||||
Name string `json:"name"` // identifier for backend naming
|
||||
Domain string `json:"domain"` // e.g. "cloud.payne.io"
|
||||
BackendIP string `json:"backendIP"` // k8s load balancer IP
|
||||
BackendIP string `json:"backendIP"` // backend server IP
|
||||
Subdomains bool `json:"subdomains"` // if true, also match *.domain
|
||||
}
|
||||
|
||||
@@ -87,12 +88,12 @@ type GenerateOpts struct {
|
||||
// and optional config. Uses L4 TCP mode with SNI inspection for HTTPS —
|
||||
// TLS terminates at each k8s cluster's traefik. HTTP routes add an L7 frontend
|
||||
// where Central terminates TLS with a wildcard cert and reverse-proxies by Host header.
|
||||
func (m *Manager) Generate(instances []InstanceRoute, custom []CustomRoute, centralDomain ...string) string {
|
||||
func (m *Manager) Generate(instances []L4Route, custom []CustomRoute, centralDomain ...string) string {
|
||||
return m.GenerateWithOpts(instances, custom, GenerateOpts{})
|
||||
}
|
||||
|
||||
// GenerateWithOpts creates a complete HAProxy configuration with full options.
|
||||
func (m *Manager) GenerateWithOpts(instances []InstanceRoute, custom []CustomRoute, opts GenerateOpts) string {
|
||||
func (m *Manager) GenerateWithOpts(instances []L4Route, custom []CustomRoute, opts GenerateOpts) string {
|
||||
var sb strings.Builder
|
||||
|
||||
sb.WriteString(`# Wild Cloud HAProxy Configuration
|
||||
@@ -433,7 +434,7 @@ func sanitizeName(name string) string {
|
||||
|
||||
// GetListenPorts returns all TCP/HTTP ports HAProxy is configured to listen on.
|
||||
// Used to keep nftables rules in sync.
|
||||
func (m *Manager) GetListenPorts(instances []InstanceRoute, custom []CustomRoute) []int {
|
||||
func (m *Manager) GetListenPorts(custom []CustomRoute) []int {
|
||||
ports := []int{80, 443, 8404}
|
||||
for _, route := range custom {
|
||||
ports = append(ports, route.Port)
|
||||
|
||||
@@ -41,7 +41,7 @@ func TestSanitizeName(t *testing.T) {
|
||||
|
||||
func TestGetListenPorts_BasePorts(t *testing.T) {
|
||||
m := NewManager("")
|
||||
ports := m.GetListenPorts(nil, nil)
|
||||
ports := m.GetListenPorts(nil)
|
||||
want := map[int]bool{80: true, 443: true, 8404: true}
|
||||
for _, p := range ports {
|
||||
delete(want, p)
|
||||
@@ -54,7 +54,7 @@ func TestGetListenPorts_BasePorts(t *testing.T) {
|
||||
func TestGetListenPorts_IncludesCustom(t *testing.T) {
|
||||
m := NewManager("")
|
||||
custom := []CustomRoute{{Name: "ssh", Port: 2222, Backend: "192.168.1.10:22"}}
|
||||
ports := m.GetListenPorts(nil, custom)
|
||||
ports := m.GetListenPorts(custom)
|
||||
found := false
|
||||
for _, p := range ports {
|
||||
if p == 2222 {
|
||||
@@ -94,7 +94,7 @@ func TestGenerate_NoInstances_NoHTTPSFrontend(t *testing.T) {
|
||||
|
||||
func TestGenerate_WithInstance_SNIACLs(t *testing.T) {
|
||||
m := NewManager("")
|
||||
instances := []InstanceRoute{
|
||||
instances := []L4Route{
|
||||
{Name: "payne-cloud", Domain: "cloud.payne.io", BackendIP: "192.168.8.20", Subdomains: true},
|
||||
}
|
||||
out := m.Generate(instances, nil)
|
||||
@@ -113,7 +113,7 @@ func TestGenerate_WithInstance_SNIACLs(t *testing.T) {
|
||||
|
||||
func TestGenerate_WithInstance_Backend(t *testing.T) {
|
||||
m := NewManager("")
|
||||
instances := []InstanceRoute{
|
||||
instances := []L4Route{
|
||||
{Name: "payne-cloud", Domain: "cloud.payne.io", BackendIP: "192.168.8.20"},
|
||||
}
|
||||
out := m.Generate(instances, nil)
|
||||
@@ -128,7 +128,7 @@ func TestGenerate_WithInstance_Backend(t *testing.T) {
|
||||
|
||||
func TestGenerate_MultipleInstances(t *testing.T) {
|
||||
m := NewManager("")
|
||||
instances := []InstanceRoute{
|
||||
instances := []L4Route{
|
||||
{Name: "payne-cloud", Domain: "cloud.payne.io", BackendIP: "192.168.8.20"},
|
||||
{Name: "test-cloud", Domain: "cloud2.payne.io", BackendIP: "192.168.8.30"},
|
||||
}
|
||||
@@ -150,7 +150,7 @@ func TestGenerate_MultipleInstances(t *testing.T) {
|
||||
|
||||
func TestGenerate_WithSubdomains(t *testing.T) {
|
||||
m := NewManager("")
|
||||
instances := []InstanceRoute{
|
||||
instances := []L4Route{
|
||||
{Name: "payne-cloud", Domain: "cloud.payne.io", BackendIP: "192.168.8.20", Subdomains: true},
|
||||
{Name: "payne-io", Domain: "payne.io", BackendIP: "192.168.8.20", Subdomains: false},
|
||||
}
|
||||
@@ -172,7 +172,7 @@ func TestGenerate_WithSubdomains(t *testing.T) {
|
||||
|
||||
func TestGenerate_ACLOrdering(t *testing.T) {
|
||||
m := NewManager("")
|
||||
instances := []InstanceRoute{
|
||||
instances := []L4Route{
|
||||
{Name: "payne-cloud", Domain: "cloud.payne.io", BackendIP: "192.168.8.240", Subdomains: true},
|
||||
}
|
||||
httpRoutes := []HTTPRoute{
|
||||
@@ -275,7 +275,7 @@ func TestGenerateWithOpts_HTTPRoutes_L7Frontend(t *testing.T) {
|
||||
|
||||
func TestGenerateWithOpts_MixedL4AndL7(t *testing.T) {
|
||||
m := NewManager("")
|
||||
instances := []InstanceRoute{
|
||||
instances := []L4Route{
|
||||
{Name: "payne-cloud", Domain: "cloud.payne.io", BackendIP: "192.168.8.100"},
|
||||
}
|
||||
httpRoutes := []HTTPRoute{
|
||||
@@ -309,7 +309,7 @@ func TestGenerateWithOpts_ACLOrdering_L7BeforeL4Wildcard(t *testing.T) {
|
||||
// wild-cloud.payne.io is an L7 HTTP service. The L7 exact match MUST
|
||||
// appear before the L4 wildcard, otherwise HAProxy matches
|
||||
// wild-cloud.payne.io against *.payne.io and sends it to the wrong backend.
|
||||
instances := []InstanceRoute{
|
||||
instances := []L4Route{
|
||||
{Name: "payne-io", Domain: "payne.io", BackendIP: "192.168.8.240", Subdomains: true},
|
||||
}
|
||||
httpRoutes := []HTTPRoute{
|
||||
@@ -352,7 +352,7 @@ func TestGenerateWithOpts_ACLOrdering_L7BeforeL4Wildcard(t *testing.T) {
|
||||
|
||||
func TestGenerateWithOpts_SubdomainsFalse_ExactOnly(t *testing.T) {
|
||||
m := NewManager("")
|
||||
instances := []InstanceRoute{
|
||||
instances := []L4Route{
|
||||
{Name: "payne-io", Domain: "payne.io", BackendIP: "192.168.8.20", Subdomains: false},
|
||||
}
|
||||
out := m.GenerateWithOpts(instances, nil, GenerateOpts{})
|
||||
@@ -368,7 +368,7 @@ func TestGenerateWithOpts_SubdomainsFalse_ExactOnly(t *testing.T) {
|
||||
|
||||
func TestGenerateWithOpts_SubdomainsTrue_WildcardAndExact(t *testing.T) {
|
||||
m := NewManager("")
|
||||
instances := []InstanceRoute{
|
||||
instances := []L4Route{
|
||||
{Name: "cloud-payne", Domain: "cloud.payne.io", BackendIP: "192.168.8.20", Subdomains: true},
|
||||
}
|
||||
out := m.GenerateWithOpts(instances, nil, GenerateOpts{})
|
||||
@@ -390,7 +390,7 @@ func TestGenerateWithOpts_SubdomainsTrue_WildcardAndExact(t *testing.T) {
|
||||
func TestGenerateWithOpts_ACLOrdering_L4ExactBeforeL4Wildcard(t *testing.T) {
|
||||
m := NewManager("")
|
||||
// payne.io exact (subdomains:false) must appear before cloud.payne.io wildcard (subdomains:true)
|
||||
instances := []InstanceRoute{
|
||||
instances := []L4Route{
|
||||
{Name: "cloud-payne", Domain: "cloud.payne.io", BackendIP: "192.168.8.20", Subdomains: true},
|
||||
{Name: "payne-io", Domain: "payne.io", BackendIP: "192.168.8.20", Subdomains: false},
|
||||
}
|
||||
@@ -412,7 +412,7 @@ func TestGenerateWithOpts_ACLOrdering_L4ExactBeforeL4Wildcard(t *testing.T) {
|
||||
|
||||
func TestGenerateWithOpts_BackwardCompatible(t *testing.T) {
|
||||
m := NewManager("")
|
||||
instances := []InstanceRoute{
|
||||
instances := []L4Route{
|
||||
{Name: "test", Domain: "test.example.com", BackendIP: "10.0.0.1"},
|
||||
}
|
||||
|
||||
@@ -471,7 +471,7 @@ func TestGenerateWithOpts_OnlyHTTPRoutes(t *testing.T) {
|
||||
func TestGenerateWithOpts_OnlyL4Routes(t *testing.T) {
|
||||
m := NewManager("")
|
||||
|
||||
instances := []InstanceRoute{
|
||||
instances := []L4Route{
|
||||
{Name: "cloud", Domain: "cloud.example.com", BackendIP: "10.0.0.1", Subdomains: true},
|
||||
}
|
||||
|
||||
@@ -731,7 +731,7 @@ func TestGenerateWithOpts_NoL7Fields_BackwardCompat(t *testing.T) {
|
||||
|
||||
func TestGenerateWithOpts_ServiceMarkers(t *testing.T) {
|
||||
m := NewManager("")
|
||||
instances := []InstanceRoute{
|
||||
instances := []L4Route{
|
||||
{Name: "cloud", Domain: "cloud.example.com", BackendIP: "10.0.0.1", Subdomains: true},
|
||||
}
|
||||
httpRoutes := []HTTPRoute{
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { useState } from 'react';
|
||||
import { useState, useMemo, useEffect } from 'react';
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { Card, CardContent } from './ui/card';
|
||||
import { Button } from './ui/button';
|
||||
import { Input, Label } from './ui';
|
||||
import { Switch } from './ui/switch';
|
||||
import {
|
||||
Globe, Loader2,
|
||||
Globe, Loader2, Search,
|
||||
Plus, X,
|
||||
} from 'lucide-react';
|
||||
import { useDomains } from '../hooks/useDomains';
|
||||
@@ -14,6 +14,7 @@ import { useCert } from '../hooks/useCert';
|
||||
import { useVpn } from '../hooks/useVpn';
|
||||
import { useCentralStatus } from '../hooks/useCentralStatus';
|
||||
import { usePageHelp } from '../hooks/usePageHelp';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { domainsApi } from '../services/api/networking';
|
||||
import type { CertEntry } from '../services/api/cert';
|
||||
import { DomainCard } from './domains/DomainCard';
|
||||
@@ -49,6 +50,24 @@ export function DomainsComponent() {
|
||||
});
|
||||
|
||||
const [showAddForm, setShowAddForm] = useState(false);
|
||||
const [search, setSearch] = useState(() => {
|
||||
try { return localStorage.getItem('domains:search') ?? ''; } catch { return ''; }
|
||||
});
|
||||
const [filter, setFilter] = useState<'all' | 'public' | 'private' | 'direct' | 'l4' | 'l7'>(() => {
|
||||
try {
|
||||
const v = localStorage.getItem('domains:filter');
|
||||
return (v as typeof filter) || 'all';
|
||||
} catch { return 'all'; }
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
try {
|
||||
if (search) localStorage.setItem('domains:search', search);
|
||||
else localStorage.removeItem('domains:search');
|
||||
if (filter !== 'all') localStorage.setItem('domains:filter', filter);
|
||||
else localStorage.removeItem('domains:filter');
|
||||
} catch { /* ignore */ }
|
||||
}, [search, filter]);
|
||||
|
||||
// Add form state
|
||||
const [newDomain, setNewDomain] = useState('');
|
||||
@@ -73,6 +92,36 @@ export function DomainsComponent() {
|
||||
const vpnConfigured = !!(vpnStatus || vpnPeers.length > 0);
|
||||
const vpnPeerCount = vpnPeers.length;
|
||||
|
||||
const filteredDomains = useMemo(() => {
|
||||
let result = domains;
|
||||
|
||||
// Text search
|
||||
if (search) {
|
||||
const q = search.toLowerCase();
|
||||
result = result.filter(d =>
|
||||
d.domain.toLowerCase().includes(q) ||
|
||||
d.backend.address.toLowerCase().includes(q) ||
|
||||
d.source.toLowerCase().includes(q)
|
||||
);
|
||||
}
|
||||
|
||||
// Filter
|
||||
if (filter !== 'all') {
|
||||
result = result.filter(d => {
|
||||
const bt = d.routes?.length ? d.routes[0].backend.type : d.backend.type;
|
||||
switch (filter) {
|
||||
case 'public': return d.public;
|
||||
case 'private': return !d.public;
|
||||
case 'direct': return bt === 'dns-only';
|
||||
case 'l4': return bt === 'tcp-passthrough';
|
||||
case 'l7': return bt === 'http';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}, [domains, search, filter]);
|
||||
|
||||
const registerMutation = useMutation({
|
||||
mutationFn: (svc: Parameters<typeof domainsApi.register>[0]) => domainsApi.register(svc),
|
||||
onSuccess: () => {
|
||||
@@ -110,6 +159,63 @@ export function DomainsComponent() {
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Search & Filters */}
|
||||
{domains.length > 0 && (
|
||||
<div className="flex flex-wrap items-center gap-3">
|
||||
<div className="relative flex-1 min-w-48">
|
||||
<Search className="absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-muted-foreground" />
|
||||
<Input
|
||||
value={search}
|
||||
onChange={e => setSearch(e.target.value)}
|
||||
placeholder="Search domains..."
|
||||
className="pl-8 pr-7 h-8 text-sm"
|
||||
/>
|
||||
{search && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setSearch('')}
|
||||
className="absolute right-2 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground"
|
||||
>
|
||||
<X className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex gap-0.5 bg-muted rounded-md p-0.5">
|
||||
{([
|
||||
{ value: 'all' as const, label: 'All' },
|
||||
{ value: 'public' as const, label: 'Public' },
|
||||
{ value: 'private' as const, label: 'Private' },
|
||||
{ value: 'direct' as const, label: 'Direct' },
|
||||
{ value: 'l4' as const, label: 'L4' },
|
||||
{ value: 'l7' as const, label: 'L7' },
|
||||
]).map(opt => (
|
||||
<button key={opt.value} type="button"
|
||||
className={cn(
|
||||
'text-xs py-1 px-2.5 rounded transition-colors',
|
||||
filter === opt.value ? 'bg-background shadow-sm font-medium' : 'text-muted-foreground hover:text-foreground',
|
||||
)}
|
||||
onClick={() => setFilter(opt.value)}
|
||||
>
|
||||
{opt.label}
|
||||
{opt.value !== 'all' && (() => {
|
||||
const count = domains.filter(d => {
|
||||
const bt = d.routes?.length ? d.routes[0].backend.type : d.backend.type;
|
||||
switch (opt.value) {
|
||||
case 'public': return d.public;
|
||||
case 'private': return !d.public;
|
||||
case 'direct': return bt === 'dns-only';
|
||||
case 'l4': return bt === 'tcp-passthrough';
|
||||
case 'l7': return bt === 'http';
|
||||
}
|
||||
}).length;
|
||||
return count > 0 ? <span className="ml-1 text-[10px] text-muted-foreground">{count}</span> : null;
|
||||
})()}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Add Form */}
|
||||
{showAddForm && (
|
||||
<Card className="border-dashed border-primary/50">
|
||||
@@ -188,7 +294,7 @@ export function DomainsComponent() {
|
||||
)}
|
||||
|
||||
{/* Domain Cards */}
|
||||
{domains.map(domain => (
|
||||
{filteredDomains.map(domain => (
|
||||
<DomainCard
|
||||
key={domain.domain}
|
||||
domain={domain}
|
||||
@@ -206,6 +312,18 @@ export function DomainsComponent() {
|
||||
/>
|
||||
))}
|
||||
|
||||
{/* No search results */}
|
||||
{!isDomainsLoading && domains.length > 0 && filteredDomains.length === 0 && (
|
||||
<Card className="p-6 text-center">
|
||||
<p className="text-sm text-muted-foreground">No domains match "{search || filter}"</p>
|
||||
<Button variant="ghost" size="sm" className="mt-2 text-xs"
|
||||
onClick={() => { setSearch(''); setFilter('all'); }}
|
||||
>
|
||||
Clear filters
|
||||
</Button>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import { useState, useRef } from 'react';
|
||||
import { useState, useRef, useEffect, useMemo } from 'react';
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { Card, CardContent } from '@/components/ui/card';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui';
|
||||
import { Switch } from '@/components/ui/switch';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Trash2, CheckCircle, AlertCircle, Pencil, Loader2, Plus } from 'lucide-react';
|
||||
import { Trash2, CheckCircle, AlertCircle, Loader2, Plus, Undo2, Save } from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { useIsMobile } from '@/hooks/use-mobile';
|
||||
import { DomainTopology } from './DomainTopology';
|
||||
@@ -36,6 +35,14 @@ interface DomainCardProps {
|
||||
|
||||
type GatewayMode = 'dns-only' | 'l4' | 'l7';
|
||||
|
||||
/** Editable fields tracked as local draft state */
|
||||
interface DomainDraft {
|
||||
public: boolean;
|
||||
mode: GatewayMode;
|
||||
subdomains: boolean;
|
||||
backendAddress: string;
|
||||
}
|
||||
|
||||
function getGatewayMode(d: RegisteredDomain): GatewayMode {
|
||||
const bt = d.routes?.length ? d.routes[0].backend.type : d.backend.type;
|
||||
if (bt === 'dns-only') return 'dns-only';
|
||||
@@ -43,9 +50,18 @@ function getGatewayMode(d: RegisteredDomain): GatewayMode {
|
||||
return 'l7';
|
||||
}
|
||||
|
||||
function draftFromDomain(d: RegisteredDomain): DomainDraft {
|
||||
return {
|
||||
public: d.public,
|
||||
mode: getGatewayMode(d),
|
||||
subdomains: d.subdomains,
|
||||
backendAddress: d.backend.address,
|
||||
};
|
||||
}
|
||||
|
||||
function getModeLabel(mode: GatewayMode): string {
|
||||
switch (mode) {
|
||||
case 'dns-only': return 'DNS only';
|
||||
case 'dns-only': return 'Direct';
|
||||
case 'l4': return 'L4 Passthrough';
|
||||
case 'l7': return 'L7 Proxy';
|
||||
}
|
||||
@@ -68,48 +84,63 @@ function getIssues(d: RegisteredDomain, certDomains: Set<string>, ddnsRecord: Dd
|
||||
return issues;
|
||||
}
|
||||
|
||||
/** Compact controls for mobile — replaces the topology diagram */
|
||||
/** Build a virtual RegisteredDomain from the draft for the topology diagram */
|
||||
function applyDraftToDomain(domain: RegisteredDomain, draft: DomainDraft): RegisteredDomain {
|
||||
const backendType = draft.mode === 'dns-only' ? 'dns-only' : draft.mode === 'l4' ? 'tcp-passthrough' : 'http';
|
||||
const tls = draft.mode === 'dns-only' ? 'none' : draft.mode === 'l4' ? 'passthrough' : 'terminate';
|
||||
return {
|
||||
...domain,
|
||||
public: draft.public,
|
||||
subdomains: draft.subdomains,
|
||||
backend: { ...domain.backend, address: draft.backendAddress, type: backendType },
|
||||
tls,
|
||||
};
|
||||
}
|
||||
|
||||
/** Compute human-readable list of changes */
|
||||
function describeChanges(server: DomainDraft, draft: DomainDraft): string[] {
|
||||
const changes: string[] = [];
|
||||
if (draft.public !== server.public) changes.push(draft.public ? 'Make public' : 'Make private');
|
||||
if (draft.mode !== server.mode) changes.push(`Mode: ${getModeLabel(server.mode)} → ${getModeLabel(draft.mode)}`);
|
||||
if (draft.subdomains !== server.subdomains) changes.push(draft.subdomains ? 'Enable subdomains' : 'Disable subdomains');
|
||||
if (draft.backendAddress !== server.backendAddress) changes.push(`Backend: ${server.backendAddress} → ${draft.backendAddress}`);
|
||||
return changes;
|
||||
}
|
||||
|
||||
/** Compact controls for mobile */
|
||||
function MobileControls({
|
||||
domain, cert, mode,
|
||||
onTogglePublic, onChangeMode, onProvisionCert, isProvisioningCert, isMutating,
|
||||
draft, onUpdateDraft, cert, onProvisionCert, isProvisioningCert,
|
||||
}: {
|
||||
domain: RegisteredDomain;
|
||||
draft: DomainDraft;
|
||||
onUpdateDraft: (patch: Partial<DomainDraft>) => void;
|
||||
cert: CertEntry | undefined;
|
||||
mode: GatewayMode;
|
||||
onTogglePublic: () => void;
|
||||
onChangeMode: (m: GatewayMode) => void;
|
||||
onProvisionCert: (d: string) => void;
|
||||
isProvisioningCert: boolean;
|
||||
isMutating: boolean;
|
||||
}) {
|
||||
const isL7 = mode === 'l7';
|
||||
const isL7 = draft.mode === 'l7';
|
||||
const hasCert = !!cert?.cert.exists;
|
||||
const provisionDomain = domain.subdomains ? `*.${domain.domain}` : domain.domain;
|
||||
|
||||
return (
|
||||
<div className="space-y-3 py-2">
|
||||
{/* Public toggle */}
|
||||
<div className="flex items-center justify-between">
|
||||
<Label className="text-xs">Public (internet-accessible)</Label>
|
||||
<Switch checked={domain.public} onCheckedChange={onTogglePublic} disabled={isMutating} />
|
||||
<Switch checked={draft.public} onCheckedChange={v => onUpdateDraft({ public: v })} />
|
||||
</div>
|
||||
|
||||
{/* Mode selector */}
|
||||
<div>
|
||||
<Label className="text-xs mb-1.5 block">Routing mode</Label>
|
||||
<div className="flex gap-1 bg-muted rounded-md p-1">
|
||||
{([
|
||||
{ value: 'dns-only' as const, label: 'DNS only' },
|
||||
{ value: 'dns-only' as const, label: 'Direct' },
|
||||
{ value: 'l4' as const, label: 'L4 Passthrough' },
|
||||
{ value: 'l7' as const, label: 'L7 Proxy' },
|
||||
]).map(opt => (
|
||||
<button key={opt.value} type="button"
|
||||
className={cn(
|
||||
'flex-1 text-xs py-1.5 px-2 rounded transition-colors',
|
||||
mode === opt.value ? 'bg-background shadow-sm font-medium' : 'text-muted-foreground hover:text-foreground',
|
||||
draft.mode === opt.value ? 'bg-background shadow-sm font-medium' : 'text-muted-foreground hover:text-foreground',
|
||||
)}
|
||||
onClick={() => onChangeMode(opt.value)}
|
||||
disabled={isMutating}
|
||||
onClick={() => onUpdateDraft({ mode: opt.value })}
|
||||
>
|
||||
{opt.label}
|
||||
</button>
|
||||
@@ -117,7 +148,6 @@ function MobileControls({
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* TLS status */}
|
||||
{isL7 && (
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-2 text-xs">
|
||||
@@ -137,7 +167,7 @@ function MobileControls({
|
||||
{!hasCert && (
|
||||
<Button variant="outline" size="sm" className="h-7 text-xs gap-1"
|
||||
disabled={isProvisioningCert}
|
||||
onClick={() => onProvisionCert(provisionDomain)}
|
||||
onClick={() => onProvisionCert(draft.subdomains ? `*.` : '')}
|
||||
>
|
||||
{isProvisioningCert ? <Loader2 className="h-3 w-3 animate-spin" /> : <Plus className="h-3 w-3" />}
|
||||
Provision
|
||||
@@ -146,10 +176,9 @@ function MobileControls({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Backend */}
|
||||
<div className="text-xs">
|
||||
<span className="text-muted-foreground">Backend: </span>
|
||||
<span className="font-mono">{domain.routes?.length ? `${domain.routes.length} routes` : domain.backend.address}</span>
|
||||
<span className="font-mono">{draft.backendAddress}</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
@@ -171,17 +200,97 @@ export function DomainCard({
|
||||
}: DomainCardProps) {
|
||||
const queryClient = useQueryClient();
|
||||
const isMobile = useIsMobile();
|
||||
const [isExpanded, setIsExpanded] = useState(false);
|
||||
const [editingBackend, setEditingBackend] = useState(false);
|
||||
const backendInputRef = useRef<HTMLInputElement>(null);
|
||||
const [isExpanded, setIsExpanded] = useState(() => {
|
||||
try {
|
||||
const stored = localStorage.getItem('domains:expanded');
|
||||
return stored ? JSON.parse(stored).includes(domain.domain) : false;
|
||||
} catch { return false; }
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
try {
|
||||
const stored = localStorage.getItem('domains:expanded');
|
||||
const set: string[] = stored ? JSON.parse(stored) : [];
|
||||
const next = isExpanded
|
||||
? [...new Set([...set, domain.domain])]
|
||||
: set.filter(d => d !== domain.domain);
|
||||
localStorage.setItem('domains:expanded', JSON.stringify(next));
|
||||
} catch { /* ignore */ }
|
||||
}, [isExpanded, domain.domain]);
|
||||
|
||||
const isManaged = domain.source !== 'manual';
|
||||
const mode = getGatewayMode(domain);
|
||||
const issues = getIssues(domain, certDomains, ddnsRecord);
|
||||
const serverState = useMemo(() => draftFromDomain(domain), [domain]);
|
||||
const prevServerState = useRef(serverState);
|
||||
const [draft, setDraft] = useState<DomainDraft>(() => {
|
||||
try {
|
||||
const stored = localStorage.getItem('domains:drafts');
|
||||
if (stored) {
|
||||
const drafts = JSON.parse(stored);
|
||||
if (drafts[domain.domain]) return drafts[domain.domain];
|
||||
}
|
||||
} catch { /* ignore */ }
|
||||
return serverState;
|
||||
});
|
||||
|
||||
const updateMutation = useMutation({
|
||||
mutationFn: ({ updates }: { updates: Record<string, unknown> }) =>
|
||||
domainsApi.register({ ...domain, ...updates } as Parameters<typeof domainsApi.register>[0]),
|
||||
// Reset draft only when server state actually changes (apply, external update)
|
||||
useEffect(() => {
|
||||
const prev = prevServerState.current;
|
||||
if (prev.public !== serverState.public
|
||||
|| prev.mode !== serverState.mode
|
||||
|| prev.subdomains !== serverState.subdomains
|
||||
|| prev.backendAddress !== serverState.backendAddress) {
|
||||
setDraft(serverState);
|
||||
prevServerState.current = serverState;
|
||||
}
|
||||
}, [serverState]);
|
||||
|
||||
const isDirty = draft.public !== serverState.public
|
||||
|| draft.mode !== serverState.mode
|
||||
|| draft.subdomains !== serverState.subdomains
|
||||
|| draft.backendAddress !== serverState.backendAddress;
|
||||
|
||||
// Persist dirty drafts, clear when clean
|
||||
useEffect(() => {
|
||||
try {
|
||||
const stored = localStorage.getItem('domains:drafts');
|
||||
const drafts = stored ? JSON.parse(stored) : {};
|
||||
if (isDirty) {
|
||||
drafts[domain.domain] = draft;
|
||||
} else {
|
||||
delete drafts[domain.domain];
|
||||
}
|
||||
if (Object.keys(drafts).length > 0) {
|
||||
localStorage.setItem('domains:drafts', JSON.stringify(drafts));
|
||||
} else {
|
||||
localStorage.removeItem('domains:drafts');
|
||||
}
|
||||
} catch { /* ignore */ }
|
||||
}, [draft, isDirty, domain.domain]);
|
||||
|
||||
const changes = isDirty ? describeChanges(serverState, draft) : [];
|
||||
|
||||
// The "virtual" domain reflects the draft state for the topology diagram
|
||||
const previewDomain = isDirty ? applyDraftToDomain(domain, draft) : domain;
|
||||
const previewMode = draft.mode;
|
||||
const issues = getIssues(previewDomain, certDomains, ddnsRecord);
|
||||
|
||||
const updateDraft = (patch: Partial<DomainDraft>) => {
|
||||
setDraft(prev => ({ ...prev, ...patch }));
|
||||
};
|
||||
|
||||
// Apply sends the accumulated draft changes to the server
|
||||
const applyMutation = useMutation({
|
||||
mutationFn: () => {
|
||||
const backendType = draft.mode === 'dns-only' ? 'dns-only' : draft.mode === 'l4' ? 'tcp-passthrough' : 'http';
|
||||
const tls = draft.mode === 'dns-only' ? 'none' : draft.mode === 'l4' ? 'passthrough' : 'terminate';
|
||||
return domainsApi.register({
|
||||
...domain,
|
||||
public: draft.public,
|
||||
subdomains: draft.subdomains,
|
||||
backend: { ...domain.backend, address: draft.backendAddress, type: backendType },
|
||||
tls,
|
||||
} as Parameters<typeof domainsApi.register>[0]);
|
||||
},
|
||||
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['domains'] }),
|
||||
});
|
||||
|
||||
@@ -190,40 +299,12 @@ export function DomainCard({
|
||||
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['domains'] }),
|
||||
});
|
||||
|
||||
const handleTogglePublic = () => {
|
||||
updateMutation.mutate({ updates: { public: !domain.public } });
|
||||
};
|
||||
const handleDiscard = () => setDraft(serverState);
|
||||
|
||||
const handleChangeMode = (newMode: GatewayMode) => {
|
||||
if (newMode === mode) return;
|
||||
const backendType = newMode === 'dns-only' ? 'dns-only' : newMode === 'l4' ? 'tcp-passthrough' : 'http';
|
||||
const tls = newMode === 'dns-only' ? 'none' : newMode === 'l4' ? 'passthrough' : 'terminate';
|
||||
updateMutation.mutate({
|
||||
updates: {
|
||||
tls,
|
||||
backend: { ...domain.backend, type: backendType },
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const handleSaveBackend = () => {
|
||||
const val = backendInputRef.current?.value.trim();
|
||||
if (val && val !== domain.backend.address) {
|
||||
updateMutation.mutate({
|
||||
updates: { backend: { ...domain.backend, address: val } },
|
||||
});
|
||||
}
|
||||
setEditingBackend(false);
|
||||
};
|
||||
|
||||
const handleToggleSubdomains = () => {
|
||||
updateMutation.mutate({ updates: { subdomains: !domain.subdomains } });
|
||||
};
|
||||
|
||||
const domainDisplay = domain.subdomains ? `*.${domain.domain}` : domain.domain;
|
||||
const domainDisplay = draft.subdomains ? `*.${domain.domain}` : domain.domain;
|
||||
|
||||
return (
|
||||
<Card className="overflow-hidden">
|
||||
<Card className={cn('overflow-hidden', isDirty && 'ring-1 ring-amber-400/50')}>
|
||||
{/* === Collapsed Header === */}
|
||||
<button
|
||||
type="button"
|
||||
@@ -231,27 +312,26 @@ export function DomainCard({
|
||||
className="w-full px-4 py-3 text-left hover:bg-muted/30 transition-colors"
|
||||
>
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
{/* Left: domain name */}
|
||||
<div className="flex items-center gap-2 min-w-0">
|
||||
<span className="font-mono text-sm font-medium truncate">{domainDisplay}</span>
|
||||
{isDirty && <Badge variant="outline" className="text-[9px] px-1 py-0 h-4 border-amber-400 text-amber-600 dark:text-amber-400">unsaved</Badge>}
|
||||
</div>
|
||||
|
||||
{/* Right: mode + visibility + backend + status */}
|
||||
<div className="flex items-center gap-2 shrink-0">
|
||||
<Badge variant="outline" className="text-[10px] px-1.5 py-0 h-5">
|
||||
{getModeLabel(mode)}
|
||||
{getModeLabel(previewMode)}
|
||||
</Badge>
|
||||
|
||||
<Badge
|
||||
variant={domain.public ? 'default' : 'secondary'}
|
||||
variant={draft.public ? 'default' : 'secondary'}
|
||||
className="text-[10px] px-1.5 py-0 h-5"
|
||||
>
|
||||
{domain.public ? 'Public' : 'Private'}
|
||||
{draft.public ? 'Public' : 'Private'}
|
||||
</Badge>
|
||||
|
||||
{!domain.routes?.length && (
|
||||
<span className="text-xs text-muted-foreground font-mono hidden sm:inline">
|
||||
{'-> '}{domain.backend.address}
|
||||
{'-> '}{draft.backendAddress}
|
||||
</span>
|
||||
)}
|
||||
|
||||
@@ -280,18 +360,15 @@ export function DomainCard({
|
||||
{/* Topology diagram (desktop) / compact controls (mobile) */}
|
||||
{isMobile ? (
|
||||
<MobileControls
|
||||
domain={domain}
|
||||
draft={draft}
|
||||
onUpdateDraft={updateDraft}
|
||||
cert={cert}
|
||||
mode={mode}
|
||||
onTogglePublic={handleTogglePublic}
|
||||
onChangeMode={handleChangeMode}
|
||||
onProvisionCert={onProvisionCert}
|
||||
onProvisionCert={d => onProvisionCert(draft.subdomains ? `*.${domain.domain}` : d || domain.domain)}
|
||||
isProvisioningCert={isProvisioningCert}
|
||||
isMutating={updateMutation.isPending}
|
||||
/>
|
||||
) : (
|
||||
<DomainTopology
|
||||
domain={domain}
|
||||
domain={previewDomain}
|
||||
cert={cert}
|
||||
ddnsRecord={ddnsRecord ? { ok: ddnsRecord.ok, ip: ddnsRecord.ip, error: ddnsRecord.error } : undefined}
|
||||
vpnPeerCount={vpnPeerCount}
|
||||
@@ -299,60 +376,51 @@ export function DomainCard({
|
||||
vpnConfigured={vpnConfigured}
|
||||
daemons={daemons}
|
||||
isManaged={isManaged}
|
||||
onTogglePublic={handleTogglePublic}
|
||||
onChangeMode={handleChangeMode}
|
||||
onTogglePublic={() => updateDraft({ public: !draft.public })}
|
||||
onChangeMode={m => updateDraft({ mode: m })}
|
||||
onChangeBackend={addr => updateDraft({ backendAddress: addr })}
|
||||
onProvisionCert={onProvisionCert}
|
||||
onRenewCert={onRenewCert}
|
||||
isProvisioningCert={isProvisioningCert}
|
||||
isRenewingCert={isRenewingCert}
|
||||
isMutating={updateMutation.isPending}
|
||||
isMutating={applyMutation.isPending}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* === Footer: backend editing + subdomains + source + deregister === */}
|
||||
{/* === Apply bar (visible when dirty) === */}
|
||||
{isDirty && (
|
||||
<div className="flex items-center justify-between gap-3 bg-amber-50 dark:bg-amber-950/20 border border-amber-200 dark:border-amber-800/50 rounded-md px-3 py-2">
|
||||
<div className="text-xs space-y-0.5">
|
||||
<div className="font-medium text-amber-800 dark:text-amber-300">Pending changes</div>
|
||||
<div className="text-amber-700 dark:text-amber-400">
|
||||
{changes.join(' · ')}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex gap-2 shrink-0">
|
||||
<Button variant="ghost" size="sm" className="h-7 text-xs gap-1"
|
||||
onClick={handleDiscard} disabled={applyMutation.isPending}
|
||||
>
|
||||
<Undo2 className="h-3 w-3" />Discard
|
||||
</Button>
|
||||
<Button size="sm" className="h-7 text-xs gap-1"
|
||||
onClick={() => applyMutation.mutate()} disabled={applyMutation.isPending}
|
||||
>
|
||||
{applyMutation.isPending ? <Loader2 className="h-3 w-3 animate-spin" /> : <Save className="h-3 w-3" />}
|
||||
Apply
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* === Footer: subdomains + source + deregister === */}
|
||||
<div className="flex items-center justify-between pt-2 border-t border-border/50">
|
||||
<div className="flex items-center gap-4">
|
||||
{!domain.routes?.length && (
|
||||
<div className="flex items-center gap-1.5">
|
||||
{editingBackend ? (
|
||||
<div className="flex items-center gap-1">
|
||||
<Input
|
||||
ref={backendInputRef}
|
||||
defaultValue={domain.backend.address}
|
||||
className="h-6 text-xs font-mono w-40"
|
||||
autoFocus
|
||||
onKeyDown={e => {
|
||||
if (e.key === 'Enter') handleSaveBackend();
|
||||
if (e.key === 'Escape') setEditingBackend(false);
|
||||
}}
|
||||
/>
|
||||
<Button variant="ghost" size="sm" className="h-6 text-[10px] px-1.5" onClick={handleSaveBackend}>
|
||||
Save
|
||||
</Button>
|
||||
<Button variant="ghost" size="sm" className="h-6 text-[10px] px-1.5" onClick={() => setEditingBackend(false)}>
|
||||
Cancel
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<button
|
||||
type="button"
|
||||
className="flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground transition-colors group"
|
||||
onClick={() => setEditingBackend(true)}
|
||||
>
|
||||
<span className="font-mono">{domain.backend.address}</span>
|
||||
<Pencil className="h-3 w-3 opacity-0 group-hover:opacity-100 transition-opacity" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="button"
|
||||
className="text-[10px] text-muted-foreground hover:text-foreground transition-colors"
|
||||
onClick={handleToggleSubdomains}
|
||||
disabled={updateMutation.isPending}
|
||||
onClick={() => updateDraft({ subdomains: !draft.subdomains })}
|
||||
>
|
||||
{domain.subdomains ? '*.wildcard on' : 'wildcard off'}
|
||||
{draft.subdomains ? '*.wildcard on' : 'wildcard off'}
|
||||
</button>
|
||||
|
||||
<span className="text-[10px] text-muted-foreground">
|
||||
@@ -361,16 +429,16 @@ export function DomainCard({
|
||||
</div>
|
||||
|
||||
<Button
|
||||
variant="ghost" size="sm"
|
||||
className="h-6 text-[10px] text-red-500 hover:text-red-700 hover:bg-red-50 dark:hover:bg-red-950/20"
|
||||
onClick={e => { e.stopPropagation(); deregisterMutation.mutate(); }}
|
||||
disabled={deregisterMutation.isPending}
|
||||
>
|
||||
{deregisterMutation.isPending
|
||||
? <Loader2 className="h-3 w-3 animate-spin mr-1" />
|
||||
: <Trash2 className="h-3 w-3 mr-1" />}
|
||||
Deregister
|
||||
</Button>
|
||||
variant="ghost" size="sm"
|
||||
className="h-6 text-[10px] text-red-500 hover:text-red-700 hover:bg-red-50 dark:hover:bg-red-950/20"
|
||||
onClick={e => { e.stopPropagation(); deregisterMutation.mutate(); }}
|
||||
disabled={deregisterMutation.isPending}
|
||||
>
|
||||
{deregisterMutation.isPending
|
||||
? <Loader2 className="h-3 w-3 animate-spin mr-1" />
|
||||
: <Trash2 className="h-3 w-3 mr-1" />}
|
||||
Deregister
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
)}
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import { useMemo, useCallback } from 'react';
|
||||
import { useMemo, useEffect, useState, useCallback, useRef } from 'react';
|
||||
import { Link } from 'react-router';
|
||||
import {
|
||||
ReactFlow, Handle, Position,
|
||||
type Node, type Edge, type NodeProps,
|
||||
ReactFlow, Handle, Position, applyNodeChanges,
|
||||
type Node, type Edge, type NodeProps, type NodeChange, type ReactFlowInstance,
|
||||
} from '@xyflow/react';
|
||||
import '@xyflow/react/dist/style.css';
|
||||
import {
|
||||
Globe, Wifi, Lock, Shield, ShieldAlert,
|
||||
AlertCircle, Loader2, Plus, RotateCw,
|
||||
Globe, Wifi, Lock, Shield, ShieldAlert, Router,
|
||||
AlertCircle, Loader2, Plus, RotateCw, Pencil,
|
||||
} from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui';
|
||||
import { TopologyNode, type NodeStatus } from './TopologyNode';
|
||||
import type { RegisteredDomain } from '@/services/api/networking';
|
||||
import type { CertEntry } from '@/services/api/cert';
|
||||
@@ -43,6 +44,7 @@ export interface DomainTopologyProps {
|
||||
isManaged: boolean;
|
||||
onTogglePublic: () => void;
|
||||
onChangeMode: (mode: GatewayMode) => void;
|
||||
onChangeBackend?: (address: string) => void;
|
||||
onProvisionCert: (domain: string) => void;
|
||||
onRenewCert: () => void;
|
||||
isProvisioningCert: boolean;
|
||||
@@ -139,7 +141,7 @@ function FlowNode({ data }: NodeProps) {
|
||||
{leftHandle}
|
||||
<TopologyNode
|
||||
label={mode === 'dns-only' ? 'No gateway' : mode === 'l4' ? 'L4 Passthrough' : 'L7 Proxy'}
|
||||
sublabel={mode === 'dns-only' ? 'DNS resolution only' : mode === 'l4' ? 'SNI routing' : 'HTTP reverse proxy'}
|
||||
sublabel={mode === 'dns-only' ? 'Traffic goes direct' : mode === 'l4' ? 'SNI routing' : 'HTTP reverse proxy'}
|
||||
status={mode === 'dns-only' ? 'inactive' : haproxyOk ? 'ok' : 'error'}
|
||||
>
|
||||
{onChangeMode && (
|
||||
@@ -155,7 +157,7 @@ function FlowNode({ data }: NodeProps) {
|
||||
onClick={() => onChangeMode(m)}
|
||||
disabled={isMutating}
|
||||
>
|
||||
{m === 'dns-only' ? 'DNS' : m.toUpperCase()}
|
||||
{m === 'dns-only' ? 'None' : m.toUpperCase()}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
@@ -228,6 +230,19 @@ function FlowNode({ data }: NodeProps) {
|
||||
}
|
||||
|
||||
if (v === 'backend') {
|
||||
const onChangeBackend = data.onChangeBackend as ((addr: string) => void) | undefined;
|
||||
const hasRoutes = data.hasRoutes as boolean;
|
||||
const editable = !!onChangeBackend && !hasRoutes;
|
||||
|
||||
const [editing, setEditing] = useState(false);
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const handleSave = () => {
|
||||
const val = inputRef.current?.value.trim();
|
||||
if (val && onChangeBackend) onChangeBackend(val);
|
||||
setEditing(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
{leftHandle}
|
||||
@@ -236,7 +251,38 @@ function FlowNode({ data }: NodeProps) {
|
||||
sublabel={data.sublabel as string | undefined}
|
||||
status="ok"
|
||||
className="font-mono"
|
||||
/>
|
||||
interactive={editable && !editing}
|
||||
onClick={editable ? () => setEditing(true) : undefined}
|
||||
tooltip={editable ? 'Click to edit backend address' : undefined}
|
||||
>
|
||||
{editable && !editing && (
|
||||
<Pencil className="absolute top-1.5 right-1.5 h-2.5 w-2.5 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity" />
|
||||
)}
|
||||
{editing && (
|
||||
<div className="mt-1.5 nopan nodrag" style={{ pointerEvents: 'all' }} onPointerDown={e => e.stopPropagation()}>
|
||||
<Input
|
||||
ref={inputRef}
|
||||
defaultValue={data.label as string}
|
||||
className="h-5 text-[10px] font-mono px-1.5"
|
||||
autoFocus
|
||||
onKeyDown={e => {
|
||||
if (e.key === 'Enter') handleSave();
|
||||
if (e.key === 'Escape') setEditing(false);
|
||||
}}
|
||||
/>
|
||||
<div className="flex gap-1 mt-1">
|
||||
<Button variant="ghost" size="sm" className="h-4 text-[9px] px-1 flex-1 !cursor-pointer"
|
||||
style={{ pointerEvents: 'all' }} onPointerDown={e => e.stopPropagation()} onClick={handleSave}>
|
||||
Save
|
||||
</Button>
|
||||
<Button variant="ghost" size="sm" className="h-4 text-[9px] px-1 flex-1 !cursor-pointer"
|
||||
style={{ pointerEvents: 'all' }} onPointerDown={e => e.stopPropagation()} onClick={() => setEditing(false)}>
|
||||
Cancel
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</TopologyNode>
|
||||
{rightHandle}
|
||||
{bottomHandle}
|
||||
</>
|
||||
@@ -278,6 +324,26 @@ function FlowNode({ data }: NodeProps) {
|
||||
);
|
||||
}
|
||||
|
||||
if (v === 'router') {
|
||||
const ports = data.ports as string;
|
||||
return (
|
||||
<>
|
||||
{leftHandle}
|
||||
<Link to="/central" className="nopan nodrag" style={{ pointerEvents: 'all' }} onPointerDown={(e) => e.stopPropagation()}>
|
||||
<TopologyNode
|
||||
label="Router"
|
||||
sublabel={ports}
|
||||
status="na"
|
||||
icon={<Router className="h-3 w-3" />}
|
||||
className="hover:border-primary/40"
|
||||
tooltip="Your LAN router port-forwards these ports to Wild Central. See Dashboard for setup."
|
||||
/>
|
||||
</Link>
|
||||
{rightHandle}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -294,11 +360,99 @@ function getGatewayMode(domain: RegisteredDomain): GatewayMode {
|
||||
|
||||
// ── Main component ─────────────────────────────────────────────────────
|
||||
|
||||
/** Align pipeline nodes so their vertical centers match → straight horizontal edges */
|
||||
const PIPELINE_IDS = new Set(['internet', 'router', 'security', 'gateway', 'tls', 'backend']);
|
||||
|
||||
function alignPipelineNodes(nodes: Node[]): Node[] {
|
||||
const pipelineNodes = nodes
|
||||
.filter(n => PIPELINE_IDS.has(n.id) && (n.measured?.width ?? 0) > 0)
|
||||
.sort((a, b) => a.position.x - b.position.x);
|
||||
if (pipelineNodes.length < 2) return nodes;
|
||||
|
||||
// Vertical: center-align all pipeline nodes
|
||||
let maxHeight = 0;
|
||||
for (const n of pipelineNodes) {
|
||||
const h = n.measured?.height ?? 0;
|
||||
if (h > maxHeight) maxHeight = h;
|
||||
}
|
||||
const centerY = maxHeight / 2;
|
||||
|
||||
// Horizontal: equalize gaps between nodes
|
||||
const totalNodeWidth = pipelineNodes.reduce((sum, n) => sum + (n.measured?.width ?? 0), 0);
|
||||
const firstX = pipelineNodes[0].position.x;
|
||||
const lastNode = pipelineNodes[pipelineNodes.length - 1];
|
||||
const totalSpan = (lastNode.position.x + (lastNode.measured?.width ?? 0)) - firstX;
|
||||
const gap = (totalSpan - totalNodeWidth) / (pipelineNodes.length - 1) * 0.75;
|
||||
|
||||
// Build new x positions
|
||||
const xPositions = new Map<string, number>();
|
||||
let x = firstX;
|
||||
for (const n of pipelineNodes) {
|
||||
xPositions.set(n.id, x);
|
||||
x += (n.measured?.width ?? 0) + gap;
|
||||
}
|
||||
|
||||
return nodes.map(n => {
|
||||
if (!PIPELINE_IDS.has(n.id)) return n;
|
||||
const h = n.measured?.height ?? 0;
|
||||
if (h === 0) return n;
|
||||
const targetY = centerY - h / 2;
|
||||
const targetX = xPositions.get(n.id) ?? n.position.x;
|
||||
return { ...n, position: { x: targetX, y: targetY } };
|
||||
});
|
||||
}
|
||||
|
||||
function AlignedFlow({ nodes: inputNodes, edges, containerHeight }: { nodes: Node[]; edges: Edge[]; containerHeight: number }) {
|
||||
const [nodes, setNodes] = useState(inputNodes);
|
||||
const rfRef = useRef<ReactFlowInstance | null>(null);
|
||||
|
||||
// Reset nodes when inputs change (draft updated, card expanded, etc.)
|
||||
useEffect(() => { setNodes(inputNodes); }, [inputNodes]);
|
||||
|
||||
// Handle React Flow's internal changes (including dimension measurements)
|
||||
const onNodesChange = useCallback((changes: NodeChange[]) => {
|
||||
setNodes(prev => {
|
||||
const updated = applyNodeChanges(changes, prev);
|
||||
if (changes.some(c => c.type === 'dimensions')) {
|
||||
const aligned = alignPipelineNodes(updated);
|
||||
requestAnimationFrame(() => rfRef.current?.fitView({ padding: 0.15 }));
|
||||
return aligned;
|
||||
}
|
||||
return updated;
|
||||
});
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div style={{ height: containerHeight }} className="w-full">
|
||||
<ReactFlow
|
||||
nodes={nodes}
|
||||
edges={edges}
|
||||
onNodesChange={onNodesChange}
|
||||
onInit={instance => { rfRef.current = instance; }}
|
||||
nodeTypes={nodeTypes}
|
||||
fitView
|
||||
fitViewOptions={{ padding: 0.15 }}
|
||||
nodesDraggable={false}
|
||||
nodesConnectable={false}
|
||||
elementsSelectable={false}
|
||||
panOnDrag={false}
|
||||
zoomOnScroll={false}
|
||||
zoomOnPinch={false}
|
||||
zoomOnDoubleClick={false}
|
||||
preventScrolling={false}
|
||||
proOptions={{ hideAttribution: true }}
|
||||
className="!bg-transparent"
|
||||
style={{ '--xy-edge-label-background-color': 'hsl(var(--card))', '--xy-edge-label-color': 'hsl(var(--muted-foreground))' } as React.CSSProperties}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function DomainTopology(props: DomainTopologyProps) {
|
||||
const {
|
||||
domain, cert, ddnsRecord, vpnPeerCount, vpnRunning, vpnConfigured,
|
||||
daemons,
|
||||
onTogglePublic, onChangeMode, onProvisionCert, onRenewCert,
|
||||
onTogglePublic, onChangeMode, onChangeBackend, onProvisionCert, onRenewCert,
|
||||
isProvisioningCert, isRenewingCert, isMutating,
|
||||
} = props;
|
||||
|
||||
@@ -307,6 +461,7 @@ export function DomainTopology(props: DomainTopologyProps) {
|
||||
const isDnsOnly = mode === 'dns-only';
|
||||
const isL4 = mode === 'l4';
|
||||
const isL7 = mode === 'l7';
|
||||
const showRouter = isPublic;
|
||||
const showSecurity = isPublic && !isDnsOnly;
|
||||
const showGateway = true; // Always show so mode selector is accessible
|
||||
const showTls = isL7;
|
||||
@@ -338,21 +493,24 @@ export function DomainTopology(props: DomainTopologyProps) {
|
||||
const sourceX = x;
|
||||
|
||||
x += 170;
|
||||
const routerX = showRouter ? x : -1;
|
||||
if (showRouter) x += 155;
|
||||
|
||||
const securityX = showSecurity ? x : -1;
|
||||
if (showSecurity) x += 160;
|
||||
if (showSecurity) x += 165;
|
||||
|
||||
const gatewayX = showGateway ? x : -1;
|
||||
if (showGateway) x += showTls ? 150 : 200;
|
||||
if (showGateway) x += 175;
|
||||
|
||||
const tlsX = showTls ? x : -1;
|
||||
if (showTls) x += 140;
|
||||
if (showTls) x += 160;
|
||||
|
||||
const backendX = x;
|
||||
|
||||
// Row y positions
|
||||
// Row y positions — all pipeline nodes start at y=0,
|
||||
// then useAlignPipelineNodes() adjusts them after measurement
|
||||
const pipelineY = 0;
|
||||
const lanY = 110;
|
||||
const vpnY = 180;
|
||||
const lanY = 120;
|
||||
|
||||
// ── Nodes (callbacks passed via data) ──
|
||||
|
||||
@@ -366,10 +524,21 @@ export function DomainTopology(props: DomainTopologyProps) {
|
||||
},
|
||||
});
|
||||
|
||||
if (showRouter) {
|
||||
// Show which ports the router forwards — standard HTTPS + HTTP, plus VPN if configured
|
||||
const ports = [':443', ':80'];
|
||||
if (vpnConfigured) ports.push(`:${51820}`);
|
||||
n.push({
|
||||
id: 'router', type: 'topology',
|
||||
position: { x: routerX, y: pipelineY },
|
||||
data: { variant: 'router', ports: `fwd ${ports.join(', ')}` },
|
||||
});
|
||||
}
|
||||
|
||||
if (showSecurity) {
|
||||
n.push({
|
||||
id: 'security', type: 'topology',
|
||||
position: { x: securityX, y: pipelineY - 10 },
|
||||
position: { x: securityX, y: pipelineY },
|
||||
data: { variant: 'security', firewallOk: !!daemons.nftables, crowdsecOk: !!daemons.crowdsec, isL4 },
|
||||
});
|
||||
}
|
||||
@@ -398,19 +567,21 @@ export function DomainTopology(props: DomainTopologyProps) {
|
||||
n.push({
|
||||
id: 'backend', type: 'topology',
|
||||
position: { x: backendX, y: pipelineY },
|
||||
data: { variant: 'backend', label: backendLabel, sublabel: backendSublabel },
|
||||
data: { variant: 'backend', label: backendLabel, sublabel: backendSublabel, onChangeBackend, hasRoutes: !!domain.routes?.length },
|
||||
});
|
||||
|
||||
const lanX = showVpn ? sourceX + 120 : sourceX;
|
||||
|
||||
n.push({
|
||||
id: 'lan', type: 'topology',
|
||||
position: { x: sourceX, y: lanY },
|
||||
position: { x: lanX, y: lanY },
|
||||
data: { variant: 'lan' },
|
||||
});
|
||||
|
||||
if (showVpn) {
|
||||
n.push({
|
||||
id: 'vpn', type: 'topology',
|
||||
position: { x: sourceX, y: vpnY },
|
||||
position: { x: sourceX, y: lanY },
|
||||
data: { variant: 'vpn', peerCount: vpnPeerCount, running: vpnRunning },
|
||||
});
|
||||
}
|
||||
@@ -420,14 +591,19 @@ export function DomainTopology(props: DomainTopologyProps) {
|
||||
const pipeline = { type: 'smoothstep' as const, sourceHandle: 'right', targetHandle: 'left', style: { strokeWidth: 1.5 } };
|
||||
const labelStyle = { fontSize: 9, fill: 'var(--color-muted-foreground)' };
|
||||
|
||||
// Internet path
|
||||
if (isPublic && showSecurity) {
|
||||
e.push({ id: 'e-internet-security', source: 'internet', target: 'security', ...pipeline });
|
||||
e.push({ id: 'e-security-gateway', source: 'security', target: 'gateway', ...pipeline });
|
||||
} else if (isPublic && !isDnsOnly) {
|
||||
e.push({ id: 'e-internet-gateway', source: 'internet', target: 'gateway', ...pipeline });
|
||||
} else if (isPublic && isDnsOnly) {
|
||||
e.push({ id: 'e-internet-backend', source: 'internet', target: 'backend', ...pipeline, style: { ...pipeline.style, strokeDasharray: '6 3' }, label: 'DNS', labelStyle });
|
||||
// Internet path: Internet → Router → Security → Gateway (or shorter variants)
|
||||
if (isPublic) {
|
||||
e.push({ id: 'e-internet-router', source: 'internet', target: 'router', ...pipeline });
|
||||
|
||||
if (showSecurity) {
|
||||
e.push({ id: 'e-router-security', source: 'router', target: 'security', ...pipeline });
|
||||
e.push({ id: 'e-security-gateway', source: 'security', target: 'gateway', ...pipeline });
|
||||
} else if (!isDnsOnly) {
|
||||
e.push({ id: 'e-router-gateway', source: 'router', target: 'gateway', ...pipeline });
|
||||
} else {
|
||||
// Direct: router forwards to backend, Central not in path
|
||||
e.push({ id: 'e-router-backend', source: 'router', target: 'backend', ...pipeline, style: { ...pipeline.style, strokeDasharray: '6 3' }, label: 'direct', labelStyle });
|
||||
}
|
||||
}
|
||||
|
||||
// Central processing → backend (not for DNS-only)
|
||||
@@ -440,55 +616,30 @@ export function DomainTopology(props: DomainTopologyProps) {
|
||||
}
|
||||
}
|
||||
|
||||
// LAN / VPN paths — enter from below via bottom handle
|
||||
const lanVpnTarget = isL7 ? 'gateway' : 'backend';
|
||||
const lanVpnEdge = { type: 'smoothstep' as const, sourceHandle: 'right', targetHandle: 'bottom', style: { strokeWidth: 1.5 }, label: 'dnsmasq', labelStyle };
|
||||
|
||||
e.push({ id: 'e-lan-target', source: 'lan', target: lanVpnTarget, ...lanVpnEdge });
|
||||
// LAN path — enters gateway/backend from below
|
||||
const lanTarget = isL7 ? 'gateway' : 'backend';
|
||||
e.push({ id: 'e-lan-target', source: 'lan', target: lanTarget, type: 'smoothstep', sourceHandle: 'right', targetHandle: 'bottom', style: { strokeWidth: 1.5 }, label: 'dnsmasq', labelStyle });
|
||||
|
||||
// VPN feeds into LAN (tunnel peers land on the local network)
|
||||
if (showVpn) {
|
||||
e.push({ id: 'e-vpn-target', source: 'vpn', target: lanVpnTarget, ...lanVpnEdge });
|
||||
e.push({ id: 'e-vpn-lan', source: 'vpn', target: 'lan', type: 'smoothstep', sourceHandle: 'right', targetHandle: 'left', style: { strokeWidth: 1.5 } });
|
||||
}
|
||||
|
||||
return { nodes: n, edges: e };
|
||||
}, [
|
||||
isPublic, isDnsOnly, isL4, isL7, showSecurity, showGateway, showTls, showVpn,
|
||||
isPublic, isDnsOnly, isL4, isL7, showRouter, showSecurity, showGateway, showTls, showVpn, vpnConfigured,
|
||||
mode, daemons, ddnsStatus, ddnsSublabel, ddnsRecord,
|
||||
tlsStatus, certDomain, daysLeft, hasCert, provisionDomain, cert,
|
||||
backendLabel, backendSublabel, vpnPeerCount, vpnRunning,
|
||||
isMutating, isProvisioningCert, isRenewingCert,
|
||||
onTogglePublic, onChangeMode, onProvisionCert, onRenewCert,
|
||||
onTogglePublic, onChangeMode, onChangeBackend, onProvisionCert, onRenewCert, domain.routes,
|
||||
]);
|
||||
|
||||
const containerHeight = showVpn ? 260 : 200;
|
||||
|
||||
const onInit = useCallback((instance: { fitView: () => void }) => {
|
||||
setTimeout(() => instance.fitView(), 0);
|
||||
}, []);
|
||||
const containerHeight = 200;
|
||||
|
||||
return (
|
||||
<div className="space-y-2 py-2">
|
||||
<div style={{ height: containerHeight }} className="w-full">
|
||||
<ReactFlow
|
||||
nodes={nodes}
|
||||
edges={edges}
|
||||
nodeTypes={nodeTypes}
|
||||
fitView
|
||||
fitViewOptions={{ padding: 0.15 }}
|
||||
onInit={onInit}
|
||||
nodesDraggable={false}
|
||||
nodesConnectable={false}
|
||||
elementsSelectable={false}
|
||||
panOnDrag={false}
|
||||
zoomOnScroll={false}
|
||||
zoomOnPinch={false}
|
||||
zoomOnDoubleClick={false}
|
||||
preventScrolling={false}
|
||||
proOptions={{ hideAttribution: true }}
|
||||
className="!bg-transparent"
|
||||
style={{ '--xy-edge-label-background-color': 'hsl(var(--card))', '--xy-edge-label-color': 'hsl(var(--muted-foreground))' } as React.CSSProperties}
|
||||
/>
|
||||
</div>
|
||||
<AlignedFlow nodes={nodes} edges={edges} containerHeight={containerHeight} />
|
||||
|
||||
{/* Warnings */}
|
||||
{isDnsOnly && (
|
||||
|
||||
@@ -3,12 +3,12 @@ import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import React from 'react';
|
||||
import { useStatus } from '../useStatus';
|
||||
import { apiService } from '../../services/api-legacy';
|
||||
import { apiClient } from '../../services/api/client';
|
||||
|
||||
// Mock the API service
|
||||
vi.mock('../../services/api-legacy', () => ({
|
||||
apiService: {
|
||||
getStatus: vi.fn(),
|
||||
// Mock the API client
|
||||
vi.mock('../../services/api/client', () => ({
|
||||
apiClient: {
|
||||
get: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -40,7 +40,7 @@ describe('useStatus', () => {
|
||||
timestamp: '2024-01-01T00:00:00Z',
|
||||
};
|
||||
|
||||
(apiService.getStatus as ReturnType<typeof vi.fn>).mockResolvedValue(mockStatus);
|
||||
(apiClient.get as ReturnType<typeof vi.fn>).mockResolvedValue(mockStatus);
|
||||
|
||||
const { result } = renderHook(() => useStatus(), {
|
||||
wrapper: createWrapper(),
|
||||
@@ -55,12 +55,12 @@ describe('useStatus', () => {
|
||||
|
||||
expect(result.current.data).toEqual(mockStatus);
|
||||
expect(result.current.error).toBeNull();
|
||||
expect(apiService.getStatus).toHaveBeenCalledTimes(1);
|
||||
expect(apiClient.get).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should handle error when fetching status fails', async () => {
|
||||
const mockError = new Error('Network error');
|
||||
(apiService.getStatus as ReturnType<typeof vi.fn>).mockRejectedValue(mockError);
|
||||
(apiClient.get as ReturnType<typeof vi.fn>).mockRejectedValue(mockError);
|
||||
|
||||
const { result } = renderHook(() => useStatus(), {
|
||||
wrapper: createWrapper(),
|
||||
@@ -82,7 +82,7 @@ describe('useStatus', () => {
|
||||
timestamp: '2024-01-01T00:00:00Z',
|
||||
};
|
||||
|
||||
(apiService.getStatus as ReturnType<typeof vi.fn>).mockResolvedValue(mockStatus);
|
||||
(apiClient.get as ReturnType<typeof vi.fn>).mockResolvedValue(mockStatus);
|
||||
|
||||
const { result } = renderHook(() => useStatus(), {
|
||||
wrapper: createWrapper(),
|
||||
@@ -92,13 +92,13 @@ describe('useStatus', () => {
|
||||
expect(result.current.isLoading).toBe(false);
|
||||
});
|
||||
|
||||
expect(apiService.getStatus).toHaveBeenCalledTimes(1);
|
||||
expect(apiClient.get).toHaveBeenCalledTimes(1);
|
||||
|
||||
// Trigger refetch
|
||||
result.current.refetch();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(apiService.getStatus).toHaveBeenCalledTimes(2);
|
||||
expect(apiClient.get).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,60 +1,55 @@
|
||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
||||
import { apiService } from '../services/api-legacy';
|
||||
import { apiClient } from '../services/api/client';
|
||||
import type { DnsmasqStatus, DnsmasqConfigResponse, StatusResponse } from '../types';
|
||||
|
||||
export const useDnsmasq = () => {
|
||||
// Query for status
|
||||
const statusQuery = useQuery<DnsmasqStatus>({
|
||||
queryKey: ['dnsmasq', 'status'],
|
||||
queryFn: () => apiService.getDnsmasqStatus(),
|
||||
queryFn: () => apiClient.get<DnsmasqStatus>('/api/v1/dnsmasq/status'),
|
||||
refetchInterval: 30000,
|
||||
staleTime: 10000,
|
||||
});
|
||||
|
||||
// Query for config
|
||||
const configQuery = useQuery<DnsmasqConfigResponse>({
|
||||
queryKey: ['dnsmasq', 'config'],
|
||||
queryFn: () => apiService.getDnsmasqConfig(),
|
||||
enabled: false, // Only fetch when explicitly called
|
||||
queryFn: () => apiClient.get<DnsmasqConfigResponse>('/api/v1/dnsmasq/config'),
|
||||
enabled: false,
|
||||
});
|
||||
|
||||
// Mutation for generating config (with optional overwrite)
|
||||
const generateMutation = useMutation<DnsmasqConfigResponse, Error, boolean>({
|
||||
mutationFn: (overwrite = false) => apiService.generateDnsmasqConfig(overwrite),
|
||||
mutationFn: (overwrite = false) => {
|
||||
const url = overwrite ? '/api/v1/dnsmasq/generate?overwrite=true' : '/api/v1/dnsmasq/generate';
|
||||
return apiClient.post<DnsmasqConfigResponse>(url);
|
||||
},
|
||||
onSuccess: () => {
|
||||
statusQuery.refetch();
|
||||
configQuery.refetch();
|
||||
},
|
||||
});
|
||||
|
||||
// Mutation for restarting service
|
||||
const restartMutation = useMutation<StatusResponse>({
|
||||
mutationFn: () => apiService.restartDnsmasq(),
|
||||
mutationFn: () => apiClient.post<StatusResponse>('/api/v1/dnsmasq/restart'),
|
||||
onSuccess: () => {
|
||||
statusQuery.refetch();
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
// Status
|
||||
status: statusQuery.data,
|
||||
isLoadingStatus: statusQuery.isLoading,
|
||||
statusError: statusQuery.error,
|
||||
refetchStatus: statusQuery.refetch,
|
||||
|
||||
// Config
|
||||
config: configQuery.data,
|
||||
isLoadingConfig: configQuery.isLoading,
|
||||
configError: configQuery.error,
|
||||
fetchConfig: configQuery.refetch,
|
||||
|
||||
// Generate
|
||||
generateConfig: generateMutation.mutate,
|
||||
generateData: generateMutation.data,
|
||||
isGenerating: generateMutation.isPending,
|
||||
generateError: generateMutation.error,
|
||||
|
||||
// Restart
|
||||
restart: restartMutation.mutate,
|
||||
restartData: restartMutation.data,
|
||||
isRestarting: restartMutation.isPending,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
import { apiService } from '../services/api-legacy';
|
||||
import { apiClient } from '../services/api/client';
|
||||
|
||||
interface HealthResponse {
|
||||
service: string;
|
||||
@@ -8,6 +8,6 @@ interface HealthResponse {
|
||||
|
||||
export const useHealth = () => {
|
||||
return useMutation<HealthResponse>({
|
||||
mutationFn: apiService.getHealth,
|
||||
mutationFn: () => apiClient.get<HealthResponse>('/api/v1/health'),
|
||||
});
|
||||
};
|
||||
@@ -1,11 +1,11 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { apiService } from '../services/api-legacy';
|
||||
import { apiClient } from '../services/api/client';
|
||||
import type { Status } from '../types';
|
||||
|
||||
export const useStatus = () => {
|
||||
return useQuery<Status>({
|
||||
queryKey: ['status'],
|
||||
queryFn: apiService.getStatus,
|
||||
refetchInterval: 30000, // Refetch every 30 seconds
|
||||
queryFn: () => apiClient.get<Status>('/api/status'),
|
||||
refetchInterval: 30000,
|
||||
});
|
||||
};
|
||||
@@ -1,83 +0,0 @@
|
||||
import type {
|
||||
Status,
|
||||
HealthResponse,
|
||||
StatusResponse,
|
||||
NetworkInfo,
|
||||
DnsmasqStatus,
|
||||
DnsmasqConfigResponse
|
||||
} from '../types';
|
||||
import { getApiBaseUrl } from './api/config';
|
||||
|
||||
const API_BASE = getApiBaseUrl();
|
||||
|
||||
class ApiService {
|
||||
private baseUrl: string;
|
||||
|
||||
constructor(baseUrl: string = API_BASE) {
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
private async request<T>(endpoint: string, options?: RequestInit): Promise<T> {
|
||||
const url = `${this.baseUrl}${endpoint}`;
|
||||
const response = await fetch(url, options);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
|
||||
async getStatus(): Promise<Status> {
|
||||
return this.request<Status>('/api/status');
|
||||
}
|
||||
|
||||
async getHealth(): Promise<HealthResponse> {
|
||||
return this.request<HealthResponse>('/api/v1/health');
|
||||
}
|
||||
|
||||
async getDnsmasqStatus(): Promise<DnsmasqStatus> {
|
||||
return this.request<DnsmasqStatus>('/api/v1/dnsmasq/status');
|
||||
}
|
||||
|
||||
async getDnsmasqConfig(): Promise<DnsmasqConfigResponse> {
|
||||
return this.request<DnsmasqConfigResponse>('/api/v1/dnsmasq/config');
|
||||
}
|
||||
|
||||
async writeDnsmasqConfig(content: string): Promise<StatusResponse> {
|
||||
return this.request<StatusResponse>('/api/v1/dnsmasq/config', {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ content }),
|
||||
});
|
||||
}
|
||||
|
||||
async generateDnsmasqConfig(overwrite: boolean = false): Promise<DnsmasqConfigResponse> {
|
||||
const url = overwrite ? '/api/v1/dnsmasq/generate?overwrite=true' : '/api/v1/dnsmasq/generate';
|
||||
return this.request<DnsmasqConfigResponse>(url, {
|
||||
method: 'POST'
|
||||
});
|
||||
}
|
||||
|
||||
async restartDnsmasq(): Promise<StatusResponse> {
|
||||
return this.request<StatusResponse>('/api/v1/dnsmasq/restart', {
|
||||
method: 'POST'
|
||||
});
|
||||
}
|
||||
|
||||
async getNetworkInfo(): Promise<NetworkInfo> {
|
||||
return this.request<NetworkInfo>('/api/v1/network/info');
|
||||
}
|
||||
|
||||
async downloadPXEAssets(): Promise<StatusResponse> {
|
||||
return this.request<StatusResponse>('/api/v1/assets', {
|
||||
method: 'POST'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const apiService = new ApiService();
|
||||
export default ApiService;
|
||||
@@ -46,24 +46,15 @@ export const domainsApi = {
|
||||
|
||||
// HAProxy
|
||||
|
||||
export interface HaproxyInstanceRoute {
|
||||
domain: string;
|
||||
backendIP: string;
|
||||
}
|
||||
|
||||
export interface HaproxyStatus {
|
||||
status: string;
|
||||
pid?: number;
|
||||
configFile?: string;
|
||||
lastRestart?: string;
|
||||
routes?: HaproxyInstanceRoute[];
|
||||
}
|
||||
|
||||
export interface HaproxyGenerateResult {
|
||||
message: string;
|
||||
config?: string;
|
||||
routes?: HaproxyInstanceRoute[];
|
||||
customRoutes?: number;
|
||||
}
|
||||
|
||||
export const haproxyApi = {
|
||||
|
||||
@@ -18,11 +18,6 @@ export interface Messages {
|
||||
[key: string]: Message;
|
||||
}
|
||||
|
||||
export interface HealthResponse {
|
||||
service: string;
|
||||
status: string;
|
||||
}
|
||||
|
||||
export interface StatusResponse {
|
||||
status: string;
|
||||
message?: string;
|
||||
@@ -44,7 +39,3 @@ export interface DnsmasqConfigResponse {
|
||||
config?: string;
|
||||
}
|
||||
|
||||
export interface NetworkInfo {
|
||||
primary_ip: string;
|
||||
primary_interface: string;
|
||||
}
|
||||
Reference in New Issue
Block a user