feat: soporte Android/Termux — battery_file + log_file (file-IPC sin exec), tail de logcat, workarounds DNS(1.1.1.1)+CA(SSL_CERT_FILE), procesos Android-safe

This commit is contained in:
Egutierrez
2026-06-07 14:25:45 +02:00
parent 2176e9d442
commit b7e3c80f4c
3 changed files with 209 additions and 4 deletions
+64 -2
View File
@@ -18,6 +18,7 @@ import (
"context"
"flag"
"log"
"net"
"os"
"os/signal"
"syscall"
@@ -26,11 +27,45 @@ import (
"fn-registry/functions/infra"
)
// androidWorkarounds fixes two things that break a standard cross-compiled Go
// binary on Android/Termux (no-op on Linux servers):
// - DNS: the pure-Go resolver reads /etc/resolv.conf, which on Android does
// not reflect the system DNS, so lookups hit ::1:53 and fail. We point the
// default resolver at a public DNS server explicitly.
// - TLS: Go's default CA bundle paths don't exist on Android, so HTTPS fails
// with "certificate signed by unknown authority". We point SSL_CERT_FILE at
// the Termux ca-certificates bundle (read lazily by crypto/x509 on first use).
func androidWorkarounds() {
if os.Getenv("ANDROID_ROOT") == "" && os.Getenv("ANDROID_DATA") == "" {
return
}
net.DefaultResolver = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{Timeout: 5 * time.Second}
return d.DialContext(ctx, "udp", "1.1.1.1:53")
},
}
if os.Getenv("SSL_CERT_FILE") == "" {
prefix := os.Getenv("PREFIX")
if prefix == "" {
prefix = "/data/data/com.termux/files/usr"
}
cert := prefix + "/etc/tls/cert.pem"
if _, err := os.Stat(cert); err == nil {
os.Setenv("SSL_CERT_FILE", cert)
}
}
log.Print("android: using 1.1.1.1 resolver + Termux CA bundle")
}
func main() {
configPath := flag.String("config", "", "path to JSON config file")
once := flag.Bool("once", false, "collect and push a single time, then exit (useful for testing)")
flag.Parse()
androidWorkarounds()
cfg, err := loadConfig(*configPath)
if err != nil {
log.Fatalf("config: %v", err)
@@ -58,9 +93,10 @@ func main() {
cancel()
}()
// Optional: ship systemd journal logs to Loki in the background.
// Optional: ship logs to Loki in the background (journald on Linux,
// logcat on Android/Termux).
if cfg.LokiURL != "" {
go shipJournald(ctx, cfg)
go shipLogs(ctx, cfg)
}
ticker := time.NewTicker(time.Duration(cfg.IntervalSec) * time.Second)
@@ -89,6 +125,10 @@ func pushOnce(cfg Config) error {
if err != nil {
return err
}
// Battery metrics are best-effort. On Android/Termux the agent cannot exec
// termux-battery-status (seccomp), so a shell helper writes its JSON to
// cfg.BatteryFile and we parse that here; elsewhere we collect directly.
samples = append(samples, batterySamples(cfg)...)
body := infra.FormatPromExposition(samples, time.Now().UnixMilli())
if err := infra.PushPromRemote(cfg.HubURL, cfg.User, cfg.Pass, body, map[string]string{"instance": cfg.Node}); err != nil {
return err
@@ -96,3 +136,25 @@ func pushOnce(cfg Config) error {
log.Printf("pushed %d samples", len(samples))
return nil
}
// batterySamples returns battery metrics, reading them from a JSON file when
// cfg.BatteryFile is set (Android path) or collecting them directly otherwise.
// Always best-effort: any error yields no samples rather than failing the push.
func batterySamples(cfg Config) []infra.PromSample {
if cfg.BatteryFile != "" {
data, err := os.ReadFile(cfg.BatteryFile)
if err != nil {
return nil
}
s, err := infra.BatterySamplesFromJSON(data)
if err != nil {
return nil
}
return s
}
s, err := infra.CollectBatteryMetrics()
if err != nil {
return nil
}
return s
}