Show protocol enable state and which nodes have each protocol installed.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+36
-14
@@ -17,6 +17,7 @@ type Agent struct {
|
||||
name string
|
||||
secret string
|
||||
version string
|
||||
dataDir string
|
||||
startedAt time.Time
|
||||
mu sync.RWMutex
|
||||
config map[string]any
|
||||
@@ -30,7 +31,6 @@ func main() {
|
||||
}
|
||||
name := env("NODE_NAME", "vpn-node")
|
||||
version := env("AGENT_VERSION", defaultVersion)
|
||||
|
||||
dataDir := env("DATA_DIR", "/var/lib/vpn-node")
|
||||
_ = os.MkdirAll(dataDir, 0o755)
|
||||
|
||||
@@ -38,6 +38,7 @@ func main() {
|
||||
name: name,
|
||||
secret: secret,
|
||||
version: version,
|
||||
dataDir: dataDir,
|
||||
startedAt: time.Now().UTC(),
|
||||
config: map[string]any{},
|
||||
}
|
||||
@@ -77,23 +78,44 @@ func (a *Agent) auth(next http.HandlerFunc) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func stringList(cfg map[string]any, key string) []string {
|
||||
out := []string{}
|
||||
raw, ok := cfg[key]
|
||||
if !ok {
|
||||
return out
|
||||
}
|
||||
switch v := raw.(type) {
|
||||
case []any:
|
||||
for _, item := range v {
|
||||
if s, ok := item.(string); ok && s != "" {
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
case []string:
|
||||
out = append(out, v...)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (a *Agent) health(w http.ResponseWriter, _ *http.Request) {
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
protocols := []string{}
|
||||
if raw, ok := a.config["protocols"].([]any); ok {
|
||||
for _, p := range raw {
|
||||
if s, ok := p.(string); ok {
|
||||
protocols = append(protocols, s)
|
||||
}
|
||||
}
|
||||
enabled := stringList(a.config, "protocols_enabled")
|
||||
if len(enabled) == 0 {
|
||||
enabled = stringList(a.config, "protocols")
|
||||
}
|
||||
installed := stringList(a.config, "protocols_installed")
|
||||
if len(installed) == 0 {
|
||||
installed = append([]string{}, enabled...)
|
||||
}
|
||||
writeJSON(w, map[string]any{
|
||||
"ok": true,
|
||||
"name": a.name,
|
||||
"version": a.version,
|
||||
"uptime_sec": int64(time.Since(a.startedAt).Seconds()),
|
||||
"protocols": protocols,
|
||||
"ok": true,
|
||||
"name": a.name,
|
||||
"version": a.version,
|
||||
"uptime_sec": int64(time.Since(a.startedAt).Seconds()),
|
||||
"protocols": enabled,
|
||||
"protocols_installed": installed,
|
||||
"protocols_enabled": enabled,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -123,7 +145,7 @@ func (a *Agent) configHandler(w http.ResponseWriter, r *http.Request) {
|
||||
a.mu.Lock()
|
||||
a.config = cfg
|
||||
a.mu.Unlock()
|
||||
_ = a.saveConfig("/var/lib/vpn-node/config.json")
|
||||
_ = a.saveConfig(filepath.Join(a.dataDir, "config.json"))
|
||||
writeJSON(w, map[string]any{"ok": true})
|
||||
default:
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
|
||||
Reference in New Issue
Block a user