2023-09-10 13:15:34 +02:00
|
|
|
(local { : split : merge : hash : base64url } (require :anoia))
|
2023-09-05 23:42:25 +02:00
|
|
|
(local { : view } (require :fennel))
|
2023-09-08 22:17:42 +02:00
|
|
|
(local { : mktree : rmtree } (require :anoia.fs))
|
2023-09-05 23:42:25 +02:00
|
|
|
|
2023-09-08 21:47:01 +02:00
|
|
|
(local state-directory (assert (os.getenv "SERVICE_STATE")))
|
2023-09-08 22:17:42 +02:00
|
|
|
(mktree state-directory)
|
2023-05-25 00:01:50 +02:00
|
|
|
|
|
|
|
(fn write-value [name value]
|
2023-05-30 22:00:20 +02:00
|
|
|
(let [path (.. state-directory "/" name)]
|
|
|
|
(with-open [fout (io.open path :w)]
|
|
|
|
(when value (fout:write value)))))
|
2023-05-25 00:01:50 +02:00
|
|
|
|
|
|
|
(fn write-value-from-env [name]
|
|
|
|
(write-value name (os.getenv (string.upper name))))
|
|
|
|
|
2023-09-05 23:42:25 +02:00
|
|
|
(fn parse-address [str]
|
|
|
|
(fn parse-extra [s]
|
|
|
|
(let [out {}]
|
|
|
|
(each [name val (string.gmatch s ",(.-)=([^,]+)")]
|
|
|
|
(tset out name val))
|
|
|
|
out))
|
|
|
|
(let [(address len preferred valid extra)
|
|
|
|
(string.match str "(.-)/(%d+),(%d+),(%d+)(.*)$")]
|
|
|
|
(merge {: address : len : preferred : valid} (parse-extra extra))))
|
|
|
|
|
2023-09-08 17:49:52 +02:00
|
|
|
(fn write-addresses [prefix addresses]
|
2023-09-05 23:42:25 +02:00
|
|
|
(each [_ a (ipairs (split " " addresses))]
|
|
|
|
(let [address (parse-address a)
|
2023-09-10 13:15:34 +02:00
|
|
|
suffix (base64url (string.pack "n" (hash a)))
|
|
|
|
;; keydir should be a function of all the address
|
|
|
|
;; attributes: we want it to change whenever anything changes
|
|
|
|
;; so that clients can see which addresses are new without
|
|
|
|
;; deep table comparisons
|
|
|
|
keydir (..
|
|
|
|
prefix
|
|
|
|
(-> address.address
|
|
|
|
(: :gsub "::$" "")
|
|
|
|
(: :gsub ":" "-"))
|
|
|
|
"_"
|
|
|
|
suffix)]
|
2023-09-08 22:17:42 +02:00
|
|
|
(mktree (.. state-directory "/" keydir))
|
2023-09-05 23:42:25 +02:00
|
|
|
(each [k v (pairs address)]
|
|
|
|
(write-value (.. keydir "/" k) v)))))
|
|
|
|
|
2023-06-01 00:29:05 +02:00
|
|
|
;; we remove state before updating to ensure that consumers don't get
|
|
|
|
;; a half-updated snapshot
|
|
|
|
(os.remove (.. state-directory "/state"))
|
|
|
|
|
2023-09-08 21:47:01 +02:00
|
|
|
;; remove parsed addresses/prefixes from any previous run
|
|
|
|
(rmtree (.. state-directory "/prefix"))
|
|
|
|
(rmtree (.. state-directory "/address"))
|
|
|
|
|
2023-05-25 00:01:50 +02:00
|
|
|
(let [wanted
|
|
|
|
[
|
|
|
|
:addresses
|
|
|
|
:aftr
|
|
|
|
:cer
|
|
|
|
:domains
|
|
|
|
:lw406
|
|
|
|
:mape
|
|
|
|
:mapt
|
|
|
|
:ntp_fqdn
|
|
|
|
:ntp_ip
|
|
|
|
:option_1
|
|
|
|
:option_2
|
|
|
|
:option_3
|
|
|
|
:option_4
|
|
|
|
:option_5
|
|
|
|
:passthru
|
|
|
|
:prefixes
|
|
|
|
:ra_addresses
|
|
|
|
:ra_dns
|
|
|
|
:ra_domains
|
|
|
|
:ra_hoplimit
|
|
|
|
:ra_mtu
|
|
|
|
:ra_reachable
|
|
|
|
:ra_retransmit
|
|
|
|
:ra_routes
|
|
|
|
:rdnss
|
|
|
|
:server
|
|
|
|
:sip_domain
|
|
|
|
:sip_ip
|
|
|
|
:sntp_ip
|
|
|
|
:sntp_fqdn
|
|
|
|
]]
|
|
|
|
(each [_ n (ipairs wanted)]
|
2023-09-05 23:42:25 +02:00
|
|
|
(write-value-from-env n))
|
2023-09-08 17:49:09 +02:00
|
|
|
|
2023-09-08 17:49:52 +02:00
|
|
|
(write-addresses "address/" (os.getenv :ADDRESSES))
|
|
|
|
(write-addresses "prefix/" (os.getenv :PREFIXES)))
|
2023-05-26 19:36:44 +02:00
|
|
|
|
2023-05-30 22:00:20 +02:00
|
|
|
(let [[ifname state] arg
|
|
|
|
ready (match state
|
2023-05-26 19:36:44 +02:00
|
|
|
"started" false
|
|
|
|
"unbound" false
|
|
|
|
"stopped" false
|
|
|
|
_ true)]
|
2023-07-02 01:52:18 +02:00
|
|
|
(write-value ".lock" (tostring (os.time)))
|
2023-05-30 22:00:20 +02:00
|
|
|
(write-value "ifname" ifname)
|
2023-06-01 00:29:05 +02:00
|
|
|
(write-value "state" state)
|
2023-07-02 01:52:18 +02:00
|
|
|
(os.remove (.. state-directory "/.lock"))
|
2023-05-30 22:00:20 +02:00
|
|
|
(when ready
|
|
|
|
(with-open [fd (io.open "/proc/self/fd/10" :w)] (fd:write "\n"))))
|