- Device registry: add/edit name, child and platform, persisted in devices. - Stable per-device topic derived from name + id, unchanged by renames. - Desired state persisted in state; timed locks expire and are audited. - Dashboard cards: name, child, platform, status dot, state and time left. - Manual lock/unlock forms with CSRF; every change appends to audit (FR-15). - Append-only activity list on the dashboard. No hard deletes (rule 10). - Store and handler tests; version bumped to 0.2.0-phase2. Co-Authored-By: Paperclip <noreply@paperclip.ing>
147 lines
3.4 KiB
Go
147 lines
3.4 KiB
Go
// Command control is the Internet Use Control Control Service.
|
|
//
|
|
// It is the single source of truth for the family's device rules
|
|
// (SYSTEM_MAP.md). Phase 2: device registry, desired state in SQLite, manual
|
|
// lock/unlock from the dashboard, and the audit log.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"gitea.lab.audasmedia.com.au/sam/internet_use_control/internal/auth"
|
|
"gitea.lab.audasmedia.com.au/sam/internet_use_control/internal/config"
|
|
"gitea.lab.audasmedia.com.au/sam/internet_use_control/internal/store"
|
|
"gitea.lab.audasmedia.com.au/sam/internet_use_control/internal/web"
|
|
)
|
|
|
|
// version is injected with -ldflags at build time.
|
|
var version = "dev"
|
|
|
|
func main() {
|
|
if len(os.Args) > 1 {
|
|
switch os.Args[1] {
|
|
case "hash-password":
|
|
hashPassword(os.Args[2:])
|
|
return
|
|
case "version":
|
|
fmt.Println(version)
|
|
return
|
|
}
|
|
}
|
|
if err := run(); err != nil {
|
|
fmt.Fprintln(os.Stderr, "control:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run() error {
|
|
started := time.Now()
|
|
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cfg.Version = version
|
|
|
|
log := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: cfg.LogLevel}))
|
|
slog.SetDefault(log)
|
|
|
|
st, err := store.Open(cfg.DBPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer st.Close()
|
|
|
|
if err := st.Migrate(context.Background()); err != nil {
|
|
return err
|
|
}
|
|
|
|
am, err := auth.New(cfg.SessionSecret, cfg.SessionTTL, cfg.CookieSecure,
|
|
cfg.AdminUsername, cfg.AdminPasswordHash, cfg.AdminPassword)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !am.Hashed() {
|
|
log.Warn("admin password is plaintext; set CONTROL_ADMIN_PASSWORD_HASH to use bcrypt")
|
|
}
|
|
|
|
srv, err := web.New(cfg, st, am, log)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
httpServer := &http.Server{
|
|
Addr: cfg.Addr,
|
|
Handler: srv.Handler(),
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
ReadTimeout: 30 * time.Second,
|
|
WriteTimeout: 30 * time.Second,
|
|
IdleTimeout: 60 * time.Second,
|
|
}
|
|
|
|
errCh := make(chan error, 1)
|
|
go func() {
|
|
log.Info("control service listening",
|
|
"addr", cfg.Addr, "db", cfg.DBPath, "env", cfg.AppEnv, "version", version)
|
|
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
errCh <- err
|
|
}
|
|
}()
|
|
|
|
stop := make(chan os.Signal, 1)
|
|
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
select {
|
|
case err := <-errCh:
|
|
return err
|
|
case sig := <-stop:
|
|
log.Info("shutting down", "signal", sig.String())
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := httpServer.Shutdown(ctx); err != nil {
|
|
return fmt.Errorf("shutdown: %w", err)
|
|
}
|
|
log.Info("stopped", "uptime", time.Since(started).Round(time.Second).String())
|
|
return nil
|
|
}
|
|
|
|
// hashPassword prints a bcrypt hash for the password given as an argument or
|
|
// read from stdin. It never prints the password itself.
|
|
func hashPassword(args []string) {
|
|
var password string
|
|
if len(args) > 0 {
|
|
password = args[0]
|
|
} else {
|
|
b, err := io.ReadAll(os.Stdin)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "control:", err)
|
|
os.Exit(1)
|
|
}
|
|
password = strings.TrimRight(string(b), "\r\n")
|
|
}
|
|
if password == "" {
|
|
fmt.Fprintln(os.Stderr, "control: empty password")
|
|
os.Exit(2)
|
|
}
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "control:", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println(string(hash))
|
|
}
|