urbit/app/dns.hoon

575 lines
14 KiB
Plaintext
Raw Normal View History

/- *dns
!:
2018-05-07 19:53:31 +03:00
::
:: moves and state
2018-05-07 19:53:31 +03:00
::
=> |%
+$ move (pair bone card)
+$ poke
$% [%dns-command command]
:: XX find some other notification channel?
::
[%helm-send-hi ship (unit tape)]
==
+$ card
$% [%hiss wire [~ ~] %httr %hiss hiss:eyre]
[%poke wire dock poke]
[%rule wire %turf %put turf]
[%wait wire @da]
==
:: +state: complete app state
2018-05-07 19:53:31 +03:00
::
+$ state
2018-05-07 19:53:31 +03:00
$: :: dom: the set of our bindings
::
dom=(set turf)
:: per: per-dependent ips &c
::
per=(map ship relay)
:: nem: authoritative state
::
nem=(unit nameserver)
==
--
::
:: helpers
::
=> |%
:: +join: dedup with :acme
::
++ join
|= [sep=@t hot=(list @t)]
^- @t
?> ?=(^ hot)
%+ rap 3
|- ^- (list @t)
?~ t.hot hot
[i.hot sep $(hot t.hot)]
:: +name: fully-qualified domain name
::
++ name
|= [him=ship dom=turf]
(cat 3 (join '.' [(crip +:(scow %p him)) (flop dom)]) '.')
:: +endpoint: append path to purl
::
++ endpoint
|= [bas=purl:eyre pat=path]
^+ bas
bas(q.q (weld q.q.bas pat))
--
::
:: service providers
::
=> |%
2018-06-18 22:59:36 +03:00
:: |gcloud: provider-specific functions
::
++ gcloud
|_ aut=authority
2018-06-18 22:59:36 +03:00
:: +base: provider service endpoint
::
++ base
(need (de-purl:html 'https://www.googleapis.com/dns/v1/projects'))
2018-06-18 22:59:36 +03:00
:: +record: JSON-formatted provider-specific dns record
::
++ record
|= [him=ship tar=target]
^- json
:: ?> ?=([%gcloud *] pro.aut)
=+ ^- [typ=cord dat=cord]
?: ?=(%direct -.tar)
['A' (crip +:(scow %if p.tar))]
['CNAME' (name p.tar dom.aut)]
:- %o %- my :~
name+s+(name him dom.aut)
type+s+typ
2018-06-18 22:59:36 +03:00
:: XX make configureable?
ttl+n+~.300
rrdatas+a+[s+dat ~]
==
2018-06-18 22:59:36 +03:00
:: +request: provider-specific record-creation request
::
++ request
=, eyre
|= [him=ship tar=target pre=(unit target)]
^- hiss
:: ?> ?=([%gcloud *] pro.aut)
=/ url=purl
%+ endpoint base
/[project.pro.aut]/['managedZones']/[zone.pro.aut]/changes
=/ hed=math
(my content-type+['application/json' ~] ~)
=/ bod=octs
%- as-octt:mimes:html
%- en-json:html
:- %o %- my
:- additions+a+[(record him tar) ~]
?~ pre ~
[deletions+a+[(record him u.pre) ~] ~]
[url %post hed `bod]
--
2018-05-07 19:53:31 +03:00
--
::
2018-06-18 22:59:36 +03:00
:: the app itself
::
2018-05-07 19:53:31 +03:00
|_ [bow=bowl:gall state]
:: +this: is sparta
::
2018-05-07 19:53:31 +03:00
++ this .
:: +poke-noun: debugging
2018-05-07 19:53:31 +03:00
::
++ poke-noun
|= a=*
^- (quip move _this)
~& +<+:this
[~ this]
:: +sigh-httr: accept http response
2018-05-07 19:53:31 +03:00
::
++ sigh-httr
|= [wir=wire rep=httr:eyre]
2018-06-18 21:42:57 +03:00
^- (quip move _this)
:: at least two segments in every wire
::
?. ?=([@ @ *] wir)
~& [%strange-http-response wire=wir response=rep]
[~ this]
?+ i.wir
:: print and ignore unrecognized responses
::
~& +<
[~ this]
:: responses for a nameserver
::
%authority
?~ nem
~& [%strange-authority wire=wir response=rep]
[~ this]
?+ i.t.wir
!!
:: response confirming a valid nameserver config
:: XX attempt to download existing ship.domain bindings for a breach?
::
%confirm
?. =(200 p.rep)
~& [%authority-confirm-fail rep]
[~ this(nem ~)]
:: XX anything to do here? parse body?
::
[~ this]
:: response to a binding creation request
::
%create
?> ?=([@ %for @ ~] t.t.wir)
?. =(200 p.rep)
:: XX set a retry timeout?
::
~& [%authority-create-fail wire=wir response=rep]
[~ this]
=/ him=ship (slav %p i.t.t.wir)
=/ for=ship (slav %p i.t.t.t.t.wir)
abet:(~(confirm bind u.nem) for him)
==
:: responses for a relay validating a binding target
::
%check
=/ him=ship (slav %p i.t.wir)
?: =(200 p.rep)
abet:bind:(tell him)
:: cttp timeout
:: XX backoff, refactor
::
?: =(504 p.rep)
:_ this :_ ~
[ost.bow %wait wir (add now.bow ~m10)]
:: XX specific messages per status code
::
~& %direct-confirm-fail
abet:(fail:(tell him) %failed-request)
:: responses for a relay validating an established binding
::
%check-bond
=/ him=ship (slav %p i.t.wir)
?: =(200 p.rep)
abet:bake:(tell him)
:: XX backoff, refactor
::
:_ this :_ ~
[ost.bow %wait wir (add now.bow ~m5)]
==
:: +sigh-tang: failed to make http request
::
++ sigh-tang
|= [wir=wire saw=tang]
^- (quip move _this)
~& [%sigh-tang wir]
?+ wir
[((slog saw) ~) this]
::
[%authority %confirm ~]
~& %authority-confirm-fail
[((slog saw) ~) this(nem ~)]
::
[%check @ ~]
~& %direct-confirm-fail
=/ him=ship (slav %p i.t.wir)
%- (slog saw)
abet:(fail:(tell him) %crash)
::
[%check-bond @ ~]
~& check-bond-fail+wir
:: XX backoff, refactor
::
:_ this :_ ~
[ost.bow %wait wir (add now.bow ~m10)]
==
:: +wake: timer callback
::
++ wake
|= [wir=wire ~]
^- (quip move _this)
?+ wir
~& [%strange-wake wir]
[~ this]
::
[%check @ ~]
=/ him=ship (slav %p i.t.wir)
abet:check:(tell him)
::
[%check-bond @ ~]
=/ him=ship (slav %p i.t.wir)
abet:recheck-bond:(tell him)
==
:: +poke-dns-command: act on command
2018-05-07 19:53:31 +03:00
::
++ poke-dns-command
|= com=command
2018-05-07 19:53:31 +03:00
^- (quip move _this)
?- -.com
:: configure self as an authority
::
:: [%authority authority]
::
%authority
~| %authority-reset-wat-do
?< ?=(^ nem)
~! com
abet:(init:bind aut.com)
:: create binding (if authority) and forward request
::
:: [%bind for=ship him=ship target]
::
%bind
~& [%bind src=src.bow +<.$]
=/ rac (clan:title him.com)
?: ?=(%czar rac)
~|(%bind-galazy !!)
?: ?& ?=(%king rac)
?=(%indirect -.tar.com)
==
~|(%bind-indirect-star !!)
:: always forward, there may be multiple authorities
::
=^ zom=(list move) ..this
abet:(forward:(tell him.com) [for tar]:com)
=^ zam=(list move) ..this
?~ nem [~ this]
abet:(~(create bind u.nem) [for him tar]:com)
[(weld zom zam) this]
:: process established dns binding
::
:: [%bond for=ship him=ship turf]
::
%bond
?: ?& =(our.bow for.com)
!=(our.bow src.bow)
==
~& [%bound-him him.com dom.com]
abet:(check-bond:(tell him.com) dom.com)
?: =(our.bow him.com)
~& [%bound-us dom.com]
:- [[ost.bow %rule /bound %turf %put dom.com] ~]
this(dom (~(put in dom) dom.com))
~& [%strange-bond com]
[~ this]
:: manually set our ip, request direct binding
::
:: [%ip %if addr=@if]
::
%ip
?. =(our.bow src.bow)
~& %dns-ip-no-foreign
[~ this]
abet:(hear:(tell our.bow) `addr.com)
:: meet sponsee, request indirect binding
::
:: [%meet him=ship]
::
%meet
?. =(our.bow (sein:title our.bow now.bow him.com))
~& %dns-meet-not-sponsored
[~ this]
abet:(hear:(tell him.com) ~)
==
:: +coup: general poke acknowledgement or error
2018-06-19 20:19:54 +03:00
::
++ coup
|= [wir=wire saw=(unit tang)]
?~ saw [~ this]
~& [%coup-fallthru wir]
[((slog u.saw) ~) this]
:: +prep: adapt state
2018-05-07 19:53:31 +03:00
::
:: ++ prep _[~ this]
2018-05-07 19:53:31 +03:00
++ prep
|= old=(unit state)
^- (quip move _this)
?^ old
[~ this(+<+ u.old)]
:: XX print :dns|ip config instructions for stars?
::
2018-05-07 19:53:31 +03:00
[~ this]
:: |bind: acting as zone authority
2018-05-07 19:53:31 +03:00
::
2018-06-18 22:59:36 +03:00
++ bind
2018-05-07 19:53:31 +03:00
=| moz=(list move)
|_ nam=nameserver
++ this .
:: +abet: finalize state changes, produce moves
2018-05-07 19:53:31 +03:00
::
++ abet
^- (quip move _^this)
[(flop moz) ^this(nem `nam)]
:: +emit: emit a move
2018-05-07 19:53:31 +03:00
::
++ emit
|= car=card
^+ this
this(moz [[ost.bow car] moz])
:: +init: establish zone authority (request confirmation)
::
2018-05-07 19:53:31 +03:00
++ init
|= aut=authority
:: ?> ?=(%gcloud pro.aut)
=/ wir=wire /authority/confirm
=/ url=purl:eyre
%+ endpoint base:gcloud
/[project.pro.aut]/['managedZones']/[zone.pro.aut]
~& url
%- emit(nam [aut ~ ~])
[%hiss wir [~ ~] %httr %hiss url %get ~ ~]
:: +create: bind :him, on behalf of :for
2018-05-07 19:53:31 +03:00
::
++ create
|= [for=ship him=ship tar=target]
:: XX defer %indirect where target isn't yet bound
::
?> ?| ?=(%direct -.tar)
(~(has by bon.nam) p.tar)
==
:: ignore if binding is pending
::
=/ pending (~(get by pen.nam) him)
?: ?& ?=(^ pending)
=(tar u.pending)
==
this
:: re-notify if binding already exists
::
:: XX deduplicate with +confirm
::
=/ existing (~(get by bon.nam) him)
?: ?& ?=(^ existing)
=(tar cur.u.existing)
==
=/ wir=wire
/bound/(scot %p him)/for/(scot %p for)
=/ dom=turf
(weld dom.aut.nam /(crip +:(scow %p him)))
%- emit
[%poke wir [for dap.bow] %dns-command %bond for him dom]
:: create new or replace existing binding
::
=/ wir=wire
/authority/create/(scot %p him)/for/(scot %p for)
=/ pre=(unit target)
=/ bon=(unit bound) (~(get by bon.nam) him)
?~(bon ~ `cur.u.bon)
:: ?> ?=(%gcloud pro.aut.nam)
=/ req=hiss:eyre
(~(request gcloud aut.nam) him tar pre)
%- emit(pen.nam (~(put by pen.nam) him tar)) :: XX save for
[%hiss wir [~ ~] %httr %hiss req]
:: +confirm: successfully bound
2018-05-07 19:53:31 +03:00
::
++ confirm
|= [for=ship him=ship]
=/ tar=target (~(got by pen.nam) him)
=/ bon=(unit bound)
(~(get by bon.nam) him)
=/ nob=bound
[now.bow tar ?~(bon ~ [[wen.u.bon cur.u.bon] hit.u.bon])]
=. pen.nam (~(del by pen.nam) him)
=. bon.nam (~(put by bon.nam) him nob)
=/ wir=wire
/bound/(scot %p him)/for/(scot %p for)
=/ dom=turf
(weld dom.aut.nam /(crip +:(scow %p him)))
%- emit
[%poke wir [for dap.bow] %dns-command %bond for him dom]
2018-05-07 19:53:31 +03:00
--
:: |tell: acting as planet parent or relay
2018-05-07 19:53:31 +03:00
::
2018-06-18 22:59:36 +03:00
++ tell
|= him=ship
2018-05-07 19:53:31 +03:00
=| moz=(list move)
=/ rel=(unit relay) (~(get by per) him)
|%
2018-05-07 19:53:31 +03:00
++ this .
:: +abet: finalize state changes, produce moves
2018-05-07 19:53:31 +03:00
::
++ abet
^- (quip move _^this)
:- (flop moz)
=? per ?=(^ rel)
(~(put by per) him u.rel)
^this
:: +emit: emit a move
2018-05-07 19:53:31 +03:00
::
++ emit
|= car=card
2018-05-07 19:53:31 +03:00
^+ this
this(moz [[ost.bow car] moz])
:: +hear: hear ip address, maybe emit binding request
2018-05-07 19:53:31 +03:00
::
2018-06-18 22:59:36 +03:00
++ hear
|= addr=(unit @if)
2018-05-07 19:53:31 +03:00
^+ this
=/ tar=target
?: |(?=(~ addr) ?=(%duke (clan:title him)))
[%indirect our.bow]
[%direct %if u.addr]
:: re-notify if binding already exists
::
:: XX deduplicate with +confirm
::
?: ?& ?=(^ rel)
?=(^ dom.u.rel)
=(tar tar.u.rel)
==
=/ wir=wire
/bound/(scot %p him)/for/(scot %p our.bow)
%- emit
[%poke wir [him dap.bow] %dns-command %bond our.bow him u.dom.u.rel]
:: check binding target validity, store and forward
::
=. rel `[wen=now.bow addr dom=~ try=0 tar]
?:(?=(%indirect -.tar) bind check)
:: +check: confirm %direct target is accessible
::
++ check
^+ this
?> ?=(^ rel)
?> ?=(%direct -.tar.u.rel)
?: (reserved:eyre p.tar.u.rel)
(fail %reserved-ip)
?: (gth try.u.rel 2)
(fail %unreachable)
=. try.u.rel +(try.u.rel)
=/ wir=wire
/check/(scot %p him)
=/ url=purl:eyre
:- [sec=| por=~ host=[%| `@if`p.tar.u.rel]]
[[ext=`~.umd path=/static] query=~]
:: XX state mgmt
::
%- emit
[%hiss wir [~ ~] %httr %hiss url %get ~ ~]
:: +fail: %direct target is invalid or inaccessible
::
++ fail
|= err=@tas
^+ this
?> ?=(^ rel)
~& [%fail err him tar.u.rel]
=/ wir=wire
/fail/(scot %p him)
=/ msg=tape
?+ err
"dns binding failed"
::
%reserved-ip
?> ?=(%direct -.tar.u.rel)
"unable to create dns binding reserved address {(scow %if p.tar.u.rel)}"
==
:: XX state mgmt
::
%- emit
[%poke wir [our.bow %hood] %helm-send-hi him `msg]
:: +bind: request binding for target
::
:: Since we may be an authority, we poke ourselves.
::
++ bind
^+ this
?> ?=(^ rel)
:: XX state mgmt
=/ wir=wire
/bind/(scot %p him)/for/(scot %p our.bow)
%- emit
[%poke wir [our.bow dap.bow] %dns-command %bind our.bow him tar.u.rel]
:: +check-bond: confirm binding propagation
::
++ check-bond
|= dom=turf
^+ this
?> ?=(^ rel)
=/ wir=wire
/check-bond/(scot %p him)
=/ url=purl:eyre
:- [sec=| por=~ host=[%& dom]]
[[ext=`~.umd path=/static] query=~]
:: XX track bound-state per-domain
::
%- emit(dom.u.rel `dom)
[%hiss wir [~ ~] %httr %hiss url %get ~ ~]
:: +recheck-bond: re-attempt to confirm binding propagation
::
++ recheck-bond
^+ this
?> ?& ?=(^ rel)
?=(^ dom.u.rel)
==
(check-bond u.dom.u.rel)
:: +bake: successfully bound
2018-05-07 19:53:31 +03:00
::
2018-06-18 22:59:36 +03:00
++ bake
2018-05-07 19:53:31 +03:00
^+ this
?> ?=(^ rel)
?> ?=(^ dom.u.rel)
~& [%bake u.dom.u.rel]
=/ wir=wire
/forward/bound/(scot %p him)/for/(scot %p our.bow)
:: XX state mgmt
::
%- emit
[%poke wir [him dap.bow] %dns-command %bond our.bow him u.dom.u.rel]
:: +forward: sending binding request up the network
2018-05-07 19:53:31 +03:00
::
2018-06-18 22:59:36 +03:00
++ forward
|= [for=ship tar=target]
2018-06-18 21:42:57 +03:00
~& [%forward tar]
2018-05-07 19:53:31 +03:00
^+ this
?: ?=(%~zod our.bow) :: ~zod don't forward
~& [%zod-no-forward him tar]
this
=/ to=ship
?- (clan:title our.bow)
%czar ~zod
* (sein:title [our now our]:bow)
2018-05-07 19:53:31 +03:00
==
=/ wir=wire
/forward/bind/(scot %p him)/for/(scot %p for)
%- emit :: XX for
[%poke wir [to dap.bow] %dns-command %bind for him tar]
2018-05-07 19:53:31 +03:00
--
--