diff --git a/lib/mix/tasks/obtain_ssl_certificates.ex b/lib/mix/tasks/obtain_ssl_certificates.ex new file mode 100644 index 000000000..8cae65248 --- /dev/null +++ b/lib/mix/tasks/obtain_ssl_certificates.ex @@ -0,0 +1,30 @@ +defmodule Mix.Tasks.ObtainSslCertificates do + use Plausible.Repo + + def run(_args) do + Application.ensure_all_started(:plausible) + execute() + end + + def execute(system \\ System) do + recent_custom_domains = Repo.all( + from cd in Plausible.Site.CustomDomain, + where: cd.updated_at > fragment("now() - '3 days'::interval"), + where: not cd.has_ssl_certificate + ) + + for domain <- recent_custom_domains do + system.cmd("ssh", ["-t", "ubuntu@custom.plausible.io", "sudo certbot certonly --nginx -n -d #{domain.domain}"]) + |> report_result(domain) + end + end + + defp report_result({_, 0}, domain) do + Ecto.Changeset.change(domain, has_ssl_certificate: true) |> Repo.update! + Plausible.Slack.notify("Obtained SSL cert for #{domain.domain}") + end + + defp report_result({error_msg, error_code}, domain) do + Sentry.capture_message("Error obtaining SSL certificate", extra: %{error_msg: error_msg, error_code: error_code, domain: domain.domain}) + end +end diff --git a/lib/plausible/site/custom_domain.ex b/lib/plausible/site/custom_domain.ex new file mode 100644 index 000000000..a7db64fce --- /dev/null +++ b/lib/plausible/site/custom_domain.ex @@ -0,0 +1,20 @@ +defmodule Plausible.Site.CustomDomain do + use Ecto.Schema + import Ecto.Changeset + @domain_name_regex ~r/(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/ + + schema "custom_domains" do + field :domain, :string + field :has_ssl_certificate, :boolean + belongs_to :site, Plausible.Site + + timestamps() + end + + def changeset(custom_domain, attrs) do + custom_domain + |> cast(attrs, [:domain, :site_id]) + |> validate_required([:domain, :site_id]) + |> validate_format(:domain, @domain_name_regex, message: "please enter a valid domain name") + end +end diff --git a/lib/plausible/site/schema.ex b/lib/plausible/site/schema.ex index 8f7f9082f..cdbcdc466 100644 --- a/lib/plausible/site/schema.ex +++ b/lib/plausible/site/schema.ex @@ -13,6 +13,7 @@ defmodule Plausible.Site do has_one :google_auth, GoogleAuth has_one :weekly_report, Plausible.Site.WeeklyReport has_one :monthly_report, Plausible.Site.MonthlyReport + has_one :custom_domain, Plausible.Site.CustomDomain timestamps() end diff --git a/lib/plausible/sites.ex b/lib/plausible/sites.ex index f64cd19ed..9a626e7ef 100644 --- a/lib/plausible/sites.ex +++ b/lib/plausible/sites.ex @@ -1,5 +1,6 @@ defmodule Plausible.Sites do use Plausible.Repo + alias Plausible.Site.CustomDomain def get_for_user!(user_id, domain) do Repo.one!( @@ -31,4 +32,11 @@ defmodule Plausible.Sites do where: sm.user_id == ^user_id and sm.site_id == ^site.id ) end + + def add_custom_domain(site, custom_domain) do + CustomDomain.changeset(%CustomDomain{}, %{ + site_id: site.id, + domain: custom_domain + }) |> Repo.insert + end end diff --git a/lib/plausible/slack.ex b/lib/plausible/slack.ex index 15d50db02..b6496eb6b 100644 --- a/lib/plausible/slack.ex +++ b/lib/plausible/slack.ex @@ -1,6 +1,7 @@ defmodule Plausible.Slack do @app_env System.get_env("APP_ENV") || "dev" - @feed_channel_url "https://hooks.slack.com/services/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" + @feed_channel_url "https://hooks.slack.com/services/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" + require Logger def notify(text) do Task.start(fn -> @@ -8,7 +9,7 @@ defmodule Plausible.Slack do "prod" -> HTTPoison.post!(@feed_channel_url, Poison.encode!(%{text: text})) _ -> - nil + Logger.debug(text) end end) end diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex index f39b3406b..367df4901 100644 --- a/lib/plausible_web/controllers/site_controller.ex +++ b/lib/plausible_web/controllers/site_controller.ex @@ -75,6 +75,7 @@ defmodule PlausibleWeb.SiteController do def settings(conn, %{"website" => website}) do site = Sites.get_for_user!(conn.assigns[:current_user].id, website) |> Repo.preload(:google_auth) + |> Repo.preload(:custom_domain) search_console_domains = if site.google_auth do Plausible.Google.Api.fetch_verified_properties(site.google_auth) @@ -290,6 +291,38 @@ defmodule PlausibleWeb.SiteController do redirect(conn, to: "/#{URI.encode_www_form(site.domain)}/settings#visibility") end + def new_custom_domain(conn, %{"website" => website}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + changeset = Plausible.Site.CustomDomain.changeset(%Plausible.Site.CustomDomain{}, %{}) + + render(conn, "new_custom_domain.html", site: site, changeset: changeset, layout: {PlausibleWeb.LayoutView, "focus.html"}) + end + + def custom_domain_dns_setup(conn, %{"website" => website}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + |> Repo.preload(:custom_domain) + + render(conn, "custom_domain_dns_setup.html", site: site, layout: {PlausibleWeb.LayoutView, "focus.html"}) + end + + def custom_domain_snippet(conn, %{"website" => website}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + |> Repo.preload(:custom_domain) + + render(conn, "custom_domain_snippet.html", site: site, layout: {PlausibleWeb.LayoutView, "focus.html"}) + end + + def add_custom_domain(conn, %{"website" => website, "custom_domain" => domain}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + + case Sites.add_custom_domain(site, domain["domain"]) do + {:ok, _custom_domain} -> + redirect(conn, to: "/sites/#{URI.encode_www_form(site.domain)}/custom-domains/dns-setup") + {:error, changeset} -> + render(conn, "new_custom_domain.html", site: site, changeset: changeset, layout: {PlausibleWeb.LayoutView, "focus.html"}) + end + end + defp insert_site(user_id, params) do site_changeset = Plausible.Site.changeset(%Plausible.Site{}, params) diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex index cb73832ec..baa45e05e 100644 --- a/lib/plausible_web/router.ex +++ b/lib/plausible_web/router.ex @@ -113,6 +113,11 @@ defmodule PlausibleWeb.Router do post "/sites/:website/shared-links", SiteController, :create_shared_link delete "/sites/:website/shared-links/:slug", SiteController, :delete_shared_link + get "/sites/:website/custom-domains/new", SiteController, :new_custom_domain + get "/sites/:website/custom-domains/dns-setup", SiteController, :custom_domain_dns_setup + get "/sites/:website/custom-domains/snippet", SiteController, :custom_domain_snippet + post "/sites/:website/custom-domains", SiteController, :add_custom_domain + get "/sites/:website/weekly-report/unsubscribe", UnsubscribeController, :weekly_report get "/sites/:website/monthly-report/unsubscribe", UnsubscribeController, :monthly_report diff --git a/lib/plausible_web/templates/site/custom_domain_dns_setup.html.eex b/lib/plausible_web/templates/site/custom_domain_dns_setup.html.eex new file mode 100644 index 000000000..de5a35264 --- /dev/null +++ b/lib/plausible_web/templates/site/custom_domain_dns_setup.html.eex @@ -0,0 +1,10 @@ +
<%= @site.custom_domain.domain %>custom.plausible.io. (including the dot)
+ Allow up to 4 hours for DNS changes to propagate and for us to obtain an SSL certificate for <%= @site.custom_domain.domain %>
+
+ The setup is working when <%= @site.custom_domain.domain %>/js/plausible.js loads the javascript tracker. +
++ To finish your setup, please update the tracking snippet on your site with your custom domain. +
+ ++ Problems? <%= link("Get help via email", to: "mailto:uku@plausible.io", class: "text-indigo-darker underline") %> +
+<% end %> diff --git a/lib/plausible_web/templates/site/new_custom_domain.html.eex b/lib/plausible_web/templates/site/new_custom_domain.html.eex new file mode 100644 index 000000000..70c532dea --- /dev/null +++ b/lib/plausible_web/templates/site/new_custom_domain.html.eex @@ -0,0 +1,15 @@ +<%= form_for @changeset, "/sites/#{URI.encode_www_form(@site.domain)}/custom-domains", [class: "max-w-sm w-full mx-auto bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %> +example.com you can use stats.example.com.
+ stats.
+ Include this snippet in the <head> of your website.
Paste this snippet in the <head> of your website.