shrub/pkg/arvo/app/ping.hoon

396 lines
11 KiB
Plaintext
Raw Normal View History

2019-12-02 12:28:08 +03:00
:: Ping our sponsorship tree regularly for routing.
::
2019-12-01 01:45:21 +03:00
:: To traverse NAT, we need the response to come back from someone
:: we've sent a message to. We ping our sponsor so that they know
:: where we are. However, we also need to ping our galaxy because if
:: the other ship tries to respond directly, it may be blocked by our
:: firewall or NAT. Thus, the response must come from a ship we've
2019-12-02 12:28:08 +03:00
:: messaged directly, and the only one we can guarantee is our galaxy.
2019-12-01 01:45:21 +03:00
:: Note this issue manifests itself even for bootstrapping a planet to
:: talk to its own star.
::
2023-06-24 01:26:12 +03:00
/+ default-agent, verb, dbug
2020-12-08 03:47:06 +03:00
=* point point:kale
2019-08-10 04:24:47 +03:00
::
|%
2023-06-27 06:07:04 +03:00
:: How often to ping our sponsor when we might be behind a NAT.
::
:: NAT timeouts are often pretty short for UDP entries. 5 minutes is
:: a common value. We use 25 seconds, same as Wireguard.
::
++ nat-timeout ~s25
::
:: How often to check our IP when we know we're not behind a NAT.
::
++ ip-timeout ~m5
::
:: Chosen because it's run by Cloudflare, and others I tried were
:: inconsistently slow.
::
++ ip-reflector 'https://icanhazip.com'
::
+$ card card:agent:gall
+$ ship-state
$% [%idle ~]
[%poking ~]
2023-06-27 06:07:04 +03:00
[%http until=@da]
[%waiting until=@da]
==
2023-06-27 06:07:04 +03:00
+$ state-1
$: %1
ships=(set ship)
nonce=@ud
$= plan
$~ [%nat ~]
$% [%nat ~]
[%pub ip=(unit @if)]
==
==
--
::
2023-06-24 01:26:12 +03:00
%- agent:dbug
::
2023-06-27 06:07:04 +03:00
=| state=state-1
2019-08-10 04:24:47 +03:00
=> |%
2023-06-27 06:07:04 +03:00
:: Bind for the the writer monad on (quip effect state)
2019-08-10 04:24:47 +03:00
::
2023-06-27 06:07:04 +03:00
++ rind
|* [effect=mold state=*]
|* state-type=mold
|= $: m-b=(quip effect state-type)
fun=$-(state-type (quip effect state-type))
==
^- (quip effect state-type)
=^ effects-1=(list effect) state m-b
=^ effects-2=(list effect) state (fun state)
[(weld effects-1 effects-2) state]
::
2023-06-27 06:07:04 +03:00
++ once
|= =cord
=(cord (scot %uw nonce.state))
::
:: Subsystem to keep track of which ships to ping across breaches
:: and sponsorship changes
::
++ ships
|%
++ rind (^rind card state)
++ kick
|= [our=@p now=@da]
^- (quip card _state)
:: ?: =(%czar (clan:title our))
:: `state
::
:: NB: !! This includes our own ship, and for moons, this is
:: what has caused Jael to fetch our own rift from our parent.
:: This role may be taken by Ames's subscription to
:: %public-keys, but this must be tested before changing the
:: behavior here.
::
=/ new-ships (~(gas in *(set ship)) (saxo:title our now our))
=/ removed (~(dif in ships.state) new-ships)
=/ added (~(dif in new-ships) ships.state)
;< new-state=_state rind
?~ removed `state
[[%pass /jael %arvo %j %nuke removed]~ state]
=. state new-state
::
;< new-state=_state rind
?~ added `state
[[%pass /jael %arvo %j %public-keys added]~ state]
=. state new-state
::
:: Kick even if ships weren't added or removed
::
(kick-pings our now new-ships)
2019-12-17 09:57:11 +03:00
::
2023-06-27 06:07:04 +03:00
:: Kick whenever we get a response. We really care about
:: breaches and sponsorship changes.
::
2023-06-27 06:07:04 +03:00
:: Delay until next event in case of breach, so that ames can
:: clear its state.
::
2023-06-27 06:07:04 +03:00
++ take-jael
|= now=@da
^- (quip card _state)
[[%pass /jael/delay %arvo %b %wait now]~ state]
::
2023-06-27 06:07:04 +03:00
++ take-delay kick
--
2019-12-04 08:13:18 +03:00
::
2023-06-27 06:07:04 +03:00
:: Starts pinging a new set of `ships`.
::
2023-06-27 06:07:04 +03:00
++ kick-pings
|= [our=@p now=@da ships=(set ship)]
^- (quip card _state)
2023-06-27 06:07:04 +03:00
=: nonce.state +(nonce.state)
ships.state ships
==
::
?: ?=(%nat -.plan.state)
(kick:nat our)
(kick:pub our now)
::
:: Subsystem for pinging our sponsors when we might be behind a NAT
::
:: Ping each ship every 25 seconds to keep the pinhole open.
:: This is expensive, but if you don't do it and you are behind a
:: NAT, you will stop receiving packets from other ships except
:: during the 30 seconds following each packet you send.
::
++ nat
?> ?=(%nat -.plan.state)
|%
++ rind (^rind card state)
++ kick
|= our=@p
^- (quip card _state)
=/ ships ~(tap in ships.state)
|- ^- (quip card _state)
?~ ships `state
?: =(our i.ships) `state
;< new-state=_state rind (send-ping i.ships)
=. state new-state
$(ships t.ships)
::
2023-06-27 06:07:04 +03:00
++ send-ping
|= =ship
^- (quip card _state)
:_ state
=/ wire /nat/(scot %uw nonce.state)/ping/(scot %p ship)
[%pass wire %agent [ship %ping] %poke %noun !>(~)]~
::
2023-06-27 06:07:04 +03:00
++ take-ping
|= [now=@da =wire error=(unit tang)]
^- (quip card _state)
?. ?=([%nat @ %ping @ ~] wire) `state
?. (once i.t.wire) `state
=/ ship (slav %p i.t.t.t.wire)
%- (slog ?~(error ~ ['ping: got nack' >ship< u.error]))
:_ state
=/ wire /nat/(scot %uw nonce.state)/wait/(scot %p ship)
[%pass wire %arvo %b %wait (add nat-timeout now)]~
::
2023-06-27 06:07:04 +03:00
++ take-wait
|= =wire
^- (quip card _state)
?. ?=([%nat @ %wait @ ~] wire) `state
?. (once i.t.wire) `state
=/ ship (slav %p i.t.t.t.wire)
(send-ping ship)
--
::
2023-06-27 06:07:04 +03:00
:: Subsystem for pinging our sponsors when we know we're not behind a NAT
::
:: Check our IP address every minute, and only if it changes,
:: ping all our sponsors.
::
++ pub
?> ?=(%pub -.plan.state)
|%
++ rind (^rind card state)
++ kick
|= [our=@p now=@da]
^- (quip card _state)
;< new-state=_state rind (send-pings our)
=. state new-state
::
;< new-state=_state rind check-ip
=. state new-state
::
(set-timer now)
::
++ send-pings
|= our=@p
^- (quip card _state)
:_ state
%+ murn ~(tap in ships.state)
|= =ship
?: =(our ship)
~
=/ wire /pub/(scot %uw nonce.state)/ping/(scot %p ship)
`u=[%pass wire %agent [our %ping] %poke %noun !>(~)]
::
++ take-pings
|= [=wire error=(unit tang)]
^- (quip card _state)
?. ?=([%pub @ %ping @ ~] wire) `state
?. (once i.t.wire) `state
=/ ship (slav %p i.t.t.t.wire)
%- (slog ?~(error ~ ['ping: got nack' >ship< u.error]))
`state
::
2023-06-27 06:07:04 +03:00
++ check-ip
^- (quip card _state)
:_ state
=/ wire /pub/(scot %uw nonce.state)/ip
=/ =request:http [%'GET' ip-reflector ~ ~]
[%pass wire %arvo %i %request request *outbound-config:iris]~
::
2023-06-27 06:07:04 +03:00
++ take-ip
|= [our=@p =wire resp=client-response:iris]
^- (quip card _state)
?. ?=([%pub @ %ip ~] wire) `state
?. (once i.t.wire) `state
::
?. ?=(%finished -.resp) `state :: will retry in a minute
?. ?=(%200 status-code.response-header.resp)
=* s status-code.response-header.resp
%- (slog leaf+"ping: ip check failed: {<s>}" ~)
`state
::
?~ full-file.resp
%- (slog 'ping: ip check body empty' ~)
`state
::
=* body q.data.u.full-file.resp
=/ ip (slaw %if (cat 3 '.' (end [3 (dec (met 3 body))] body)))
?~ ip
%- %: slog
'ping: ip check body not an ip:'
>q.data.u.full-file.resp<
~
==
`state
::
?: =(ip.plan.state ip) `state
::
=. ip.plan.state ip
(send-pings our)
2023-06-15 09:28:37 +03:00
::
2023-06-27 06:07:04 +03:00
++ set-timer
|= now=@da
^- (quip card _state)
~& > %setting
=/ =wire /pub/(scot %uw nonce.state)/wait
[[%pass wire %arvo %b %wait (add ip-timeout now)]~ state]
::
2023-06-27 06:07:04 +03:00
++ take-wait
|= [our=@p now=@da =wire]
^- (quip card _state)
~& > %waiting
?. ?=([%pub @ %wait ~] wire) `state
?. (once i.t.wire) `state
;< new-state=_state rind check-ip
=. state new-state
::
(set-timer now)
--
2019-08-10 04:24:47 +03:00
--
2023-06-28 05:12:00 +03:00
%+ verb |
^- agent:gall
|_ =bowl:gall
+* this .
def ~(. (default-agent this %|) bowl)
2019-08-10 04:24:47 +03:00
::
:: +on-init: initializing on startup
2019-08-10 04:24:47 +03:00
::
++ on-init
^- [(list card) _this]
2023-06-27 06:07:04 +03:00
=. plan.state [%nat ~]
=^ cards state (kick:ships our.bowl now.bowl)
[cards this]
2019-08-10 04:24:47 +03:00
::
++ on-save !>(state)
++ on-load
2023-06-27 06:07:04 +03:00
|= old-vase=vase
|^
=/ old !<(state-any old-vase)
=? old ?=(%0 -.old) (state-0-to-1 old)
?> ?=(%1 -.old)
=. state old
=^ cards state (kick:ships our.bowl now.bowl)
[cards this]
::
+$ state-any $%(state-0 state-1)
+$ state-0 [%0 ships=(map ship [=rift =ship-state])]
::
++ state-0-to-1
|= old=state-0
^- state-1
[%1 ~ 0 %nat ~]
--
:: +on-poke: positively acknowledge pokes
2019-08-10 04:24:47 +03:00
::
++ on-poke
|= [=mark =vase]
2023-06-28 04:47:57 +03:00
?. =(our src):bowl :: don't crash, this is where pings are handled
`this
::
=^ cards state
?: =(q.vase %kick) :: NB: ames calls this on %born
(kick:ships our.bowl now.bowl)
?: =(q.vase %nat)
=. plan.state [%nat ~]
(kick:ships our.bowl now.bowl)
?: =(q.vase %no-nat)
=. plan.state [%pub ~]
(kick:ships our.bowl now.bowl)
`state
[cards this]
2019-08-10 04:24:47 +03:00
::
++ on-watch on-watch:def
++ on-leave on-leave:def
++ on-peek
|= =path
^- (unit (unit cage))
``noun+!>(state)
:: +on-agent: handle ames ack
2019-08-10 04:24:47 +03:00
::
++ on-agent
|= [=wire =sign:agent:gall]
^- [(list card) _this]
=^ cards state
2023-06-27 06:07:04 +03:00
?+ wire `state
[%nat *]
?. ?=(%nat -.plan.state) `state
?> ?=(%poke-ack -.sign)
(take-ping:nat now.bowl wire p.sign)
::
[%pub *]
?. ?=(%pub -.plan.state) `state
?> ?=(%poke-ack -.sign)
(take-pings:pub wire p.sign)
==
[cards this]
:: +on-arvo: handle timer firing
2019-08-10 04:24:47 +03:00
::
++ on-arvo
|= [=wire =sign-arvo]
^- [(list card) _this]
2023-06-27 06:07:04 +03:00
=^ cards state
?+ wire ((slog 'ping: strange wire' >wire< ~) `state)
[%jael %delay ~]
?> ?=(%wake +<.sign-arvo)
?^ error.sign-arvo
%- (slog 'ping: strange jael wake fail!' u.error.sign-arvo)
`state
(take-delay:ships our.bowl now.bowl)
::
2023-06-27 06:07:04 +03:00
[%jael ~]
?> ?=(%public-keys +<.sign-arvo)
(take-jael:ships now.bowl)
::
[%nat *]
?. ?=(%nat -.plan.state) `state
?> ?=(%wake +<.sign-arvo)
?^ error.sign-arvo
%- (slog 'ping: strange nat wake fail!' u.error.sign-arvo)
`state
(take-wait:nat wire)
::
[%pub @ %ip *]
?. ?=(%pub -.plan.state) `state
?> ?=(%http-response +<.sign-arvo)
(take-ip:pub our.bowl wire client-response.sign-arvo)
::
[%pub @ %wait *]
?. ?=(%pub -.plan.state) `state
?> ?=(%wake +<.sign-arvo)
(take-wait:pub our.bowl now.bowl wire)
==
[cards this]
::
++ on-fail on-fail:def
2019-08-10 04:24:47 +03:00
--