Karta.Tracking.Hosts (Karta v1.0.0)

Tracking hosts of sending domains (Karta.Tracking.TrackingHost): a domain's own name for the links, the open pixel and the List-Unsubscribe URL in its mail, such as click.example.com, instead of Karta's tracking host (KARTA_TRACKING_URL). Links in mail then point at a name of the sender's, whose reputation is the sender's own.

Proving it in DNS. A tracking host is a CNAME record at the name that points to the target (target/0): cname_target in config :karta, Karta.Tracking (KARTA_TRACKING_CNAME), by default the host of the tracking URL. check/2 looks it up, without the DNS cache; a host is verified while the CNAME is found. A host is added only to a verified domain (add/2), so a name can be claimed only by the organisation that has verified its domain; it is removed with the domain.

Which host a message uses (url_for/1, at delivery): the tracking host of its author's domain, else the one its server uses (assign_server/2), each only while it is verified and its domain too; else the tracking URL. The links, the open pixel and the List-Unsubscribe URL of each recipient's copy all use it.

TLS for many names. The reverse proxy in front of the tracking endpoint gets a certificate for each tracking host when it is first asked for, after asking Karta whether the name is one of its own (approved?/1, served as GET /tls/ask?domain=<name> on the tracking endpoint for Caddy's on_demand_tls).

Adding, checking and removing a host is announced on its domain's topic (:tracking_host, Karta.Changes). Every lookup is scoped by domain (and so by organisation).

Summary

Functions

Adds a tracking host to a verified domain. attrs: name, a label (click) or the whole name (click.example.com). It is checked later (check/2); until then it is not verified.

True when the reverse proxy may get a TLS certificate for name: the host of the tracking URL, or a tracking host in use (verified, of a verified domain, while a target is configured). Any case, with or without a final dot.

Sets the tracking host the server's mail uses when its author's domain has none: a host of the server's organisation, or nil for the tracking URL.

Looks up the CNAME record at the host's name and records the result (Karta.Tracking.TrackingHost). Options: dns (options for Karta.DNS.lookup/3; the cache is never used), target (instead of target/0) and now. {:error, :not_configured} without a target.

Looks up the CNAME record at name (dns: options for Karta.DNS.lookup/3) and compares it with target: {:ok, nil}, {:missing, nil}, {:mismatch, found} or {:failed, reason}.

The domain's tracking host, or nil.

A tracking host by id, whatever its domain (for jobs), or nil.

The ids of every tracking host, those checked longest ago first.

The tracking hosts of the organisation's domains, by name.

The names of the tracking hosts in use, by name: none without a target.

Removes a tracking host; its domain's mail goes back to the tracking URL.

The name a tracking host's CNAME record points to, or nil when neither cname_target nor the tracking URL is configured (no tracking host can then be verified or used).

The tracking URL of a message's links (https:// and a host name), or nil for the configured tracking URL: the tracking host of the domain of its from_address, else its server's, each only while it is verified and its domain is a verified domain of the server's organisation, and while a target is configured.

Functions

add(domain, attrs)

@spec add(Karta.Domains.Domain.t(), map()) ::
  {:ok, Karta.Tracking.TrackingHost.t()}
  | {:error, Ecto.Changeset.t() | :domain_not_verified}

Adds a tracking host to a verified domain. attrs: name, a label (click) or the whole name (click.example.com). It is checked later (check/2); until then it is not verified.

approved?(name)

@spec approved?(term()) :: boolean()

True when the reverse proxy may get a TLS certificate for name: the host of the tracking URL, or a tracking host in use (verified, of a verified domain, while a target is configured). Any case, with or without a final dot.

assign_server(server, arg2)

@spec assign_server(Karta.Servers.Server.t(), Karta.Tracking.TrackingHost.t() | nil) ::
  {:ok, Karta.Servers.Server.t()} | {:error, :not_found | Ecto.Changeset.t()}

Sets the tracking host the server's mail uses when its author's domain has none: a host of the server's organisation, or nil for the tracking URL.

check(host, opts \\ [])

@spec check(Karta.Tracking.TrackingHost.t(), keyword()) ::
  {:ok, Karta.Tracking.TrackingHost.t()}
  | {:error, :not_configured | Ecto.Changeset.t()}

Looks up the CNAME record at the host's name and records the result (Karta.Tracking.TrackingHost). Options: dns (options for Karta.DNS.lookup/3; the cache is never used), target (instead of target/0) and now. {:error, :not_configured} without a target.

cname(name, target, dns)

@spec cname(String.t(), String.t(), keyword()) ::
  {:ok | :missing | :mismatch | :failed, String.t() | nil}

Looks up the CNAME record at name (dns: options for Karta.DNS.lookup/3) and compares it with target: {:ok, nil}, {:missing, nil}, {:mismatch, found} or {:failed, reason}.

for_domain(domain)

@spec for_domain(Karta.Domains.Domain.t()) :: Karta.Tracking.TrackingHost.t() | nil

The domain's tracking host, or nil.

get(id)

@spec get(Ecto.UUID.t()) :: Karta.Tracking.TrackingHost.t() | nil

A tracking host by id, whatever its domain (for jobs), or nil.

ids()

@spec ids() :: [Ecto.UUID.t()]

The ids of every tracking host, those checked longest ago first.

list(organisation_id)

The tracking hosts of the organisation's domains, by name.

names_in_use()

@spec names_in_use() :: [String.t()]

The names of the tracking hosts in use, by name: none without a target.

remove(host)

Removes a tracking host; its domain's mail goes back to the tracking URL.

target()

@spec target() :: String.t() | nil

The name a tracking host's CNAME record points to, or nil when neither cname_target nor the tracking URL is configured (no tracking host can then be verified or used).

url_for(message)

@spec url_for(Karta.Messages.Message.t()) :: String.t() | nil

The tracking URL of a message's links (https:// and a host name), or nil for the configured tracking URL: the tracking host of the domain of its from_address, else its server's, each only while it is verified and its domain is a verified domain of the server's organisation, and while a target is configured.