M2 networkmanager: the watcher hears the rating
uplink-tools watch hears the network manager's PropertiesChanged on the system bus, keeps sign-in-asked and sign-in-ended as the rules say, at most 256 of them, and subscribes again when the system bus's socket comes back or on rating-again; nothing reads the rating on a timer. Builds: design 08 sign-in-page §12, §14, §20, §21, §22
This commit is contained in:
@@ -0,0 +1,398 @@
|
||||
package main
|
||||
|
||||
// The watcher (design 08 sign-in-page §11, §14, §20 to §22): `uplink-tools watch`, a process the machine's service
|
||||
// manager starts at boot as the account and keeps running, needing no bus. It hears the network manager's
|
||||
// PropertiesChanged on the system bus for the rating, the active connection and the check, keeps the events of §21 in
|
||||
// the module's folder for the bundle to publish, and answers its socket. Nothing runs on a timer: it acts on a signal,
|
||||
// on the system bus's socket coming back (a file event of the kernel) and on an ask on its socket.
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// keptBound is the most kept events pending/ holds; past it the oldest goes, with a journal line (§20).
|
||||
const keptBound = 256
|
||||
|
||||
// The network manager's ratings of the network (NMConnectivityState).
|
||||
const (
|
||||
ratingUnknown uint32 = 0
|
||||
ratingNone uint32 = 1
|
||||
ratingPortal uint32 = 2
|
||||
ratingLimited uint32 = 3
|
||||
ratingFull uint32 = 4
|
||||
)
|
||||
|
||||
func ratingName(r uint32) string {
|
||||
switch r {
|
||||
case ratingNone:
|
||||
return "NONE"
|
||||
case ratingPortal:
|
||||
return "PORTAL"
|
||||
case ratingLimited:
|
||||
return "LIMITED"
|
||||
case ratingFull:
|
||||
return "FULL"
|
||||
}
|
||||
return "UNKNOWN"
|
||||
}
|
||||
|
||||
// Rating is what the watcher reads of the network manager: its rating, whether its check is on and where it goes, and
|
||||
// the active connection (its object, its name and its first device's interface; "" or "/" when none is active).
|
||||
type Rating struct {
|
||||
Connectivity uint32
|
||||
CheckEnabled bool
|
||||
CheckAvailable bool
|
||||
CheckURI string
|
||||
Connection string
|
||||
Network string
|
||||
Link string
|
||||
}
|
||||
|
||||
func (r Rating) checkOn() bool { return r.CheckEnabled && r.CheckAvailable }
|
||||
func (r Rating) active() bool { return r.Connection != "" && r.Connection != "/" }
|
||||
|
||||
// The events of node-uplink the watcher keeps, and why an ask ended.
|
||||
const (
|
||||
eventAsked = "sign-in-asked"
|
||||
eventEnded = "sign-in-ended"
|
||||
|
||||
whySignedIn = "signed-in"
|
||||
whyLeft = "left"
|
||||
whyNoLongerAsked = "no-longer-asked"
|
||||
)
|
||||
|
||||
// eventSubject is where a node-uplink event of node is published (§1).
|
||||
func eventSubject(event, node string) string {
|
||||
return "mesh.seat.node-uplink.event." + event + "." + node
|
||||
}
|
||||
|
||||
type signInAsked struct {
|
||||
Node string `json:"node"`
|
||||
Link string `json:"link"`
|
||||
Network string `json:"network"`
|
||||
Address string `json:"address"`
|
||||
At string `json:"at"`
|
||||
}
|
||||
|
||||
type signInEnded struct {
|
||||
Node string `json:"node"`
|
||||
Link string `json:"link"`
|
||||
Network string `json:"network"`
|
||||
At string `json:"at"`
|
||||
Why string `json:"why"`
|
||||
}
|
||||
|
||||
// signal is one thing the system bus said that the watcher hears: the network manager's rating changed, or logind
|
||||
// has a new session (its object path).
|
||||
type signal struct {
|
||||
Rating bool
|
||||
Session string
|
||||
}
|
||||
|
||||
// systemBus is the watcher's connection to the machine's system bus: subscribed, it hands every signal it hears on the
|
||||
// channel, which closes when the connection drops.
|
||||
type systemBus interface {
|
||||
listen() (<-chan signal, error)
|
||||
rating() (Rating, error)
|
||||
close()
|
||||
}
|
||||
|
||||
// openAsk is the sign-in asked now: the connection, its network and link, and since when.
|
||||
type openAsk struct {
|
||||
Network string `json:"network"`
|
||||
Link string `json:"link"`
|
||||
Since time.Time `json:"since"`
|
||||
connection string
|
||||
address string
|
||||
}
|
||||
|
||||
// watcherState is the watcher's answer to `state` (§23's read).
|
||||
type watcherState struct {
|
||||
Subscribed bool `json:"subscribed"`
|
||||
Rating string `json:"rating,omitempty"`
|
||||
RatingAt *time.Time `json:"rating_at,omitempty"`
|
||||
Ask *openAsk `json:"ask,omitempty"`
|
||||
AsksOpened int `json:"asks_opened"`
|
||||
AsksEnded int `json:"asks_ended"`
|
||||
DroppedPastBound int `json:"dropped_past_bound"`
|
||||
}
|
||||
|
||||
// asked is an ask on the watcher's socket, handed to its loop, and the answer it sends back.
|
||||
type asked struct {
|
||||
ask string
|
||||
reply chan askReply
|
||||
}
|
||||
|
||||
type askReply struct {
|
||||
answer any
|
||||
err error
|
||||
}
|
||||
|
||||
// Watcher is the watch: what it reads through, and the state its loop alone holds.
|
||||
type Watcher struct {
|
||||
Folder Folder
|
||||
// Dial connects to the system bus.
|
||||
Dial func() (systemBus, error)
|
||||
// SocketThere fires once the system bus's socket is there: at once when it is, else at the file event of its
|
||||
// folder; closing stop ends the wait.
|
||||
SocketThere func(stop <-chan struct{}) <-chan struct{}
|
||||
Now func() time.Time
|
||||
|
||||
asks chan asked
|
||||
bus systemBus
|
||||
signals <-chan signal
|
||||
socketAt <-chan struct{}
|
||||
stopWait chan struct{}
|
||||
started bool
|
||||
state watcherState
|
||||
rating *Rating
|
||||
ask *openAsk
|
||||
}
|
||||
|
||||
func newWatcher(f Folder) *Watcher {
|
||||
return &Watcher{Folder: f, Dial: dialSystemBus, SocketThere: systemSocketThere, Now: time.Now, asks: make(chan asked)}
|
||||
}
|
||||
|
||||
// watch is `uplink-tools watch`: the watcher, until its process is stopped.
|
||||
func watch([]string) error {
|
||||
f, err := theFolder()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if f.Node == "" {
|
||||
return errors.New("no node named: the node-engine names it in MESH_NODE")
|
||||
}
|
||||
if err := os.MkdirAll(f.Dir, 0o700); err != nil {
|
||||
return err
|
||||
}
|
||||
l, err := f.listen()
|
||||
if err != nil {
|
||||
return fmt.Errorf("the watcher's socket: %w", err)
|
||||
}
|
||||
w := newWatcher(f)
|
||||
go serveSocket(l, w.askLoop)
|
||||
w.run(nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
// askLoop hands one ask of the socket to the loop and waits for its answer.
|
||||
func (w *Watcher) askLoop(ask string) (any, error) {
|
||||
reply := make(chan askReply, 1)
|
||||
w.asks <- asked{ask: ask, reply: reply}
|
||||
r := <-reply
|
||||
return r.answer, r.err
|
||||
}
|
||||
|
||||
// run is the watcher's loop, until stop closes: it subscribes, then acts on what it hears, one thing at a time.
|
||||
func (w *Watcher) run(stop <-chan struct{}) {
|
||||
if w.asks == nil {
|
||||
w.asks = make(chan asked)
|
||||
}
|
||||
w.subscribe()
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
w.unsubscribe()
|
||||
return
|
||||
case s, ok := <-w.signals:
|
||||
if !ok {
|
||||
journal("the system bus connection dropped; subscribing again when its socket is there")
|
||||
w.unsubscribe()
|
||||
w.waitSocket()
|
||||
continue
|
||||
}
|
||||
w.heardSignal(s)
|
||||
case <-w.socketAt:
|
||||
w.socketAt = nil
|
||||
w.subscribe()
|
||||
case a := <-w.asks:
|
||||
answer, err := w.answer(a.ask)
|
||||
a.reply <- askReply{answer, err}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// subscribe connects to the system bus, listens, reads the rating and acts on it: at the watcher's start as its start,
|
||||
// after that as on a change (§14). One that fails waits for the system bus's socket.
|
||||
func (w *Watcher) subscribe() {
|
||||
w.unsubscribe()
|
||||
bus, err := w.Dial()
|
||||
if err == nil {
|
||||
var signals <-chan signal
|
||||
if signals, err = bus.listen(); err == nil {
|
||||
w.bus, w.signals, w.state.Subscribed = bus, signals, true
|
||||
} else {
|
||||
bus.close()
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
journal("subscribing on the system bus failed: %v; subscribing again when its socket is there", err)
|
||||
w.waitSocket()
|
||||
return
|
||||
}
|
||||
journal("subscribed on the system bus")
|
||||
w.readRating()
|
||||
}
|
||||
|
||||
func (w *Watcher) unsubscribe() {
|
||||
if w.stopWait != nil {
|
||||
close(w.stopWait)
|
||||
w.stopWait, w.socketAt = nil, nil
|
||||
}
|
||||
if w.bus != nil {
|
||||
w.bus.close()
|
||||
}
|
||||
w.bus, w.signals, w.state.Subscribed = nil, nil, false
|
||||
}
|
||||
|
||||
func (w *Watcher) waitSocket() {
|
||||
w.stopWait = make(chan struct{})
|
||||
w.socketAt = w.SocketThere(w.stopWait)
|
||||
}
|
||||
|
||||
func (w *Watcher) heardSignal(s signal) {
|
||||
if s.Rating {
|
||||
w.readRating()
|
||||
}
|
||||
}
|
||||
|
||||
// readRating reads the rating now and acts on it: as the watcher's start the first time, as a change after.
|
||||
func (w *Watcher) readRating() {
|
||||
if w.bus == nil {
|
||||
return
|
||||
}
|
||||
r, err := w.bus.rating()
|
||||
if err != nil {
|
||||
journal("reading the network manager's rating failed: %v", err)
|
||||
return
|
||||
}
|
||||
atStart := !w.started
|
||||
w.started = true
|
||||
w.heard(r, atStart)
|
||||
}
|
||||
|
||||
// heard acts on a rating read: the check address, a journal line for a change, and the events of §21.
|
||||
func (w *Watcher) heard(r Rating, atStart bool) {
|
||||
old := w.rating
|
||||
now := w.Now()
|
||||
w.rating, w.state.RatingAt = &r, &now
|
||||
w.state.Rating = ratingName(r.Connectivity)
|
||||
if atStart || old == nil || old.Connectivity != r.Connectivity || old.Connection != r.Connection {
|
||||
from := "none"
|
||||
if old != nil {
|
||||
from = ratingName(old.Connectivity)
|
||||
}
|
||||
journal("rating %s -> %s on %q", from, ratingName(r.Connectivity), r.Network)
|
||||
}
|
||||
if atStart || old == nil || old.CheckURI != r.CheckURI {
|
||||
if err := w.Folder.writeCheckAddress(r.CheckURI); err != nil {
|
||||
journal("writing the check address failed: %v", err)
|
||||
}
|
||||
}
|
||||
if r.checkOn() && !webAddress(r.CheckURI) {
|
||||
journal("the check address %q is not http or https: nothing kept, nothing opened", r.CheckURI)
|
||||
return
|
||||
}
|
||||
if atStart {
|
||||
if !r.checkOn() {
|
||||
return
|
||||
}
|
||||
if r.Connectivity == ratingPortal && r.active() {
|
||||
w.asked(r)
|
||||
return
|
||||
}
|
||||
w.ended(r.Link, r.Network, whyNoLongerAsked)
|
||||
return
|
||||
}
|
||||
if w.ask != nil {
|
||||
switch {
|
||||
case r.Connection != w.ask.connection:
|
||||
w.ended(w.ask.Link, w.ask.Network, whyLeft)
|
||||
case !r.checkOn() || (r.Connectivity != ratingPortal && r.Connectivity != ratingFull):
|
||||
w.ended(w.ask.Link, w.ask.Network, whyNoLongerAsked)
|
||||
case r.Connectivity == ratingFull:
|
||||
w.ended(w.ask.Link, w.ask.Network, whySignedIn)
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
if r.checkOn() && r.Connectivity == ratingPortal && r.active() {
|
||||
w.asked(r)
|
||||
}
|
||||
}
|
||||
|
||||
// webAddress says whether an address is one the watcher keeps and opens: http or https, with a host.
|
||||
func webAddress(address string) bool {
|
||||
u, err := url.Parse(address)
|
||||
return err == nil && (u.Scheme == "http" || u.Scheme == "https") && u.Host != ""
|
||||
}
|
||||
|
||||
// asked keeps sign-in-asked for the active connection and holds the ask.
|
||||
func (w *Watcher) asked(r Rating) {
|
||||
now := w.Now()
|
||||
w.ask = &openAsk{Network: r.Network, Link: r.Link, Since: now, connection: r.Connection, address: r.CheckURI}
|
||||
w.state.AsksOpened++
|
||||
w.keep(eventAsked, signInAsked{Node: w.Folder.Node, Link: r.Link, Network: r.Network, Address: r.CheckURI,
|
||||
At: stamp(now)})
|
||||
}
|
||||
|
||||
// ended keeps sign-in-ended and drops the ask.
|
||||
func (w *Watcher) ended(link, network, why string) {
|
||||
if w.ask != nil {
|
||||
w.state.AsksEnded++
|
||||
}
|
||||
w.ask = nil
|
||||
w.keep(eventEnded, signInEnded{Node: w.Folder.Node, Link: link, Network: network, At: stamp(w.Now()), Why: why})
|
||||
}
|
||||
|
||||
// stamp is a time as the events say it: RFC 3339 in UTC, to the second.
|
||||
func stamp(t time.Time) string { return t.UTC().Truncate(time.Second).Format(time.RFC3339) }
|
||||
|
||||
// keep writes one event into pending/ and holds the folder to its bound, the oldest dropped with a journal line.
|
||||
func (w *Watcher) keep(event string, body any) {
|
||||
k, err := w.Folder.keep(event, eventSubject(event, w.Folder.Node), body)
|
||||
if err != nil {
|
||||
journal("keeping %s failed: %v", event, err)
|
||||
return
|
||||
}
|
||||
journal("kept %s as %s", event, k.MessageID)
|
||||
kept, err := w.Folder.pending()
|
||||
if err != nil {
|
||||
journal("reading the kept events failed: %v", err)
|
||||
return
|
||||
}
|
||||
for i := 0; len(kept)-i > keptBound; i++ {
|
||||
if err := w.Folder.drop(kept[i]); err != nil {
|
||||
journal("dropping the kept event %s past the bound of %d failed: %v", kept[i].MessageID, keptBound, err)
|
||||
continue
|
||||
}
|
||||
w.state.DroppedPastBound++
|
||||
journal("dropped the kept event %s past the bound of %d", kept[i].MessageID, keptBound)
|
||||
}
|
||||
}
|
||||
|
||||
// answer answers an ask of the watcher's socket: its state, or rating-again, which subscribes again and acts on the
|
||||
// rating read now as on a change (§12).
|
||||
func (w *Watcher) answer(ask string) (any, error) {
|
||||
switch ask {
|
||||
case askState:
|
||||
s := w.state
|
||||
if w.ask != nil {
|
||||
a := *w.ask
|
||||
s.Ask = &a
|
||||
}
|
||||
return s, nil
|
||||
case askRatingAgain:
|
||||
journal("asked rating-again: subscribing again")
|
||||
w.subscribe()
|
||||
if !w.state.Subscribed {
|
||||
return nil, errors.New("subscribing on the system bus failed; waiting for its socket")
|
||||
}
|
||||
return w.state, nil
|
||||
}
|
||||
return nil, fmt.Errorf("the watcher has no ask %q", ask)
|
||||
}
|
||||
Reference in New Issue
Block a user