2023-09-11 01:07:11 +02:00
|
|
|
(local { : system } (require :anoia))
|
2023-07-09 00:07:07 +02:00
|
|
|
(local svc (require :anoia.svc))
|
|
|
|
|
|
|
|
(local bound-states
|
|
|
|
{ :bound true
|
|
|
|
:rebound true
|
|
|
|
:informed true
|
|
|
|
:updated true
|
|
|
|
:ra-updated true
|
|
|
|
})
|
|
|
|
|
|
|
|
(fn changes [old-addresses new-addresses]
|
|
|
|
(let [added {}
|
2023-09-11 01:07:11 +02:00
|
|
|
deleted {}]
|
|
|
|
(each [n address (pairs new-addresses)]
|
|
|
|
(if (not (. old-addresses n))
|
|
|
|
(table.insert added address)))
|
|
|
|
(each [n address (pairs old-addresses)]
|
|
|
|
(if (not (. new-addresses n))
|
|
|
|
(table.insert deleted address)))
|
2023-07-09 00:07:07 +02:00
|
|
|
(values added deleted)))
|
|
|
|
|
2023-09-11 01:07:11 +02:00
|
|
|
(fn update-addresses [wan-device addresses new-addresses]
|
|
|
|
(let [(added deleted) (changes addresses new-addresses)]
|
|
|
|
(each [_ p (ipairs added)]
|
|
|
|
(system
|
|
|
|
(.. "ip address add " p.address "/" p.len " dev " wan-device)))
|
|
|
|
(each [_ p (ipairs deleted)]
|
|
|
|
(system
|
|
|
|
(.. "ip address del " p.address "/" p.len " dev " wan-device)))
|
|
|
|
new-addresses))
|
|
|
|
|
2023-09-12 18:51:00 +02:00
|
|
|
(fn run []
|
|
|
|
(let [[state-directory wan-device] arg
|
|
|
|
dir (svc.open state-directory)]
|
2023-09-12 21:55:08 +02:00
|
|
|
(accumulate [addresses []
|
|
|
|
v (dir:events)]
|
2023-09-12 21:42:42 +02:00
|
|
|
;; we don't handle unbound or stopped, where we should
|
|
|
|
;; take the addresses away
|
|
|
|
(when (. bound-states (v:output "state"))
|
2023-09-12 21:55:08 +02:00
|
|
|
(update-addresses wan-device addresses (v:output "address"))))))
|
2023-09-11 01:07:11 +02:00
|
|
|
|
2023-09-12 18:51:00 +02:00
|
|
|
{ : update-addresses : changes : run }
|