diff --git a/extra/lib/plausible/installation_support/browserless_config.ex b/extra/lib/plausible/installation_support/browserless_config.ex index 4dcac9579826..857879109bb5 100644 --- a/extra/lib/plausible/installation_support/browserless_config.ex +++ b/extra/lib/plausible/installation_support/browserless_config.ex @@ -16,6 +16,13 @@ defmodule Plausible.InstallationSupport.BrowserlessConfig do end on_ee do + def browserless_session_api_endpoint() do + config = Application.fetch_env!(:plausible, __MODULE__) + token = Keyword.fetch!(config, :token) + endpoint = Keyword.fetch!(config, :endpoint) + Path.join(endpoint, "session?token=#{token}") + end + def browserless_function_api_endpoint() do config = Application.fetch_env!(:plausible, __MODULE__) token = Keyword.fetch!(config, :token) diff --git a/extra/lib/plausible/installation_support/checks/detection.ex b/extra/lib/plausible/installation_support/checks/detection.ex index f76a89760e11..d6d8875b3e8b 100644 --- a/extra/lib/plausible/installation_support/checks/detection.ex +++ b/extra/lib/plausible/installation_support/checks/detection.ex @@ -54,7 +54,7 @@ defmodule Plausible.InstallationSupport.Checks.Detection do """ # We define a timeout for the browserless endpoint call to avoid waiting too long for a response - @endpoint_timeout_ms 2_000 + @endpoint_timeout_ms 3_000 # This timeout determines how long we wait for window.plausible to be initialized on the page, used for detecting whether v1 installed @plausible_window_check_timeout_ms 1_500 diff --git a/extra/lib/plausible/installation_support/checks/installation_v2.ex b/extra/lib/plausible/installation_support/checks/installation_v2.ex index 004e36689528..0005e2895233 100644 --- a/extra/lib/plausible/installation_support/checks/installation_v2.ex +++ b/extra/lib/plausible/installation_support/checks/installation_v2.ex @@ -91,20 +91,20 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 do def perform(%State{url: url} = state) do opts = [ headers: %{content_type: "application/json"}, - body: - JSON.encode!(%{ - code: @puppeteer_wrapper_code, - context: %{ - maxAttempts: @max_attempts, - timeoutMs: @plausible_window_check_timeout_ms, - timeoutBetweenAttemptsMs: @timeout_between_attempts_ms, - cspHostToCheck: PlausibleWeb.Endpoint.host(), - url: Plausible.InstallationSupport.URL.bust_url(url), - userAgent: Plausible.InstallationSupport.user_agent(), - debug: Application.get_env(:plausible, :environment) == "dev" - } - }), - params: %{timeout: @endpoint_timeout_ms}, + # body: + # JSON.encode!(%{ + # code: @puppeteer_wrapper_code, + # context: %{ + # maxAttempts: @max_attempts, + # timeoutMs: @plausible_window_check_timeout_ms, + # timeoutBetweenAttemptsMs: @timeout_between_attempts_ms, + # cspHostToCheck: PlausibleWeb.Endpoint.host(), + # url: Plausible.InstallationSupport.URL.bust_url(url), + # userAgent: Plausible.InstallationSupport.user_agent(), + # debug: Application.get_env(:plausible, :environment) == "dev" + # } + # }), + # params: %{timeout: @endpoint_timeout_ms}, retry: &BrowserlessConfig.retry_browserless_request/2, retry_log_level: :warning, max_retries: @max_retries @@ -113,10 +113,49 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 do extra_opts = Application.get_env(:plausible, __MODULE__)[:req_opts] || [] opts = Keyword.merge(opts, extra_opts) - case Req.post(BrowserlessConfig.browserless_function_api_endpoint(), opts) do - {:ok, %{body: body, status: status}} -> - handle_browserless_response(state, body, status) - + with {:ok, %{body: %{"id" => _session_id, "browserQL" => browser_ql_url, "stop" => stop_url}}} <- + Req.post( + BrowserlessConfig.browserless_session_api_endpoint(), + Keyword.merge(opts, + body: JSON.encode!(%{ttl: 20_000, stealth: true}) + ) + ), + {:ok, %{body: body, status: status}} <- + Req.post( + browser_ql_url, + Keyword.merge(opts, + body: + JSON.encode!(%{ + variables: %{ + eval1: "(() => {#{@verifier_code}})()", + eval2: + "JSON.stringify(await window.verifyPlausibleInstallation({cspHostToCheck: \"#{PlausibleWeb.Endpoint.host()}\", responseHeaders: {}, timeoutMs: 500, debug: true}))" + }, + query: """ + mutation Verify($eval1: String!, $eval2: String!) { + goto(url: "#{Plausible.InstallationSupport.URL.bust_url(url)}") { + status + } + title { + title + } + url { + url + } + evaluate1: evaluate(content: $eval1) { + value + } + evaluate2: evaluate(content: $eval2) { + value + } + } + """ + }) + ) + ), + {:ok, _} <- Req.post(stop_url, opts) do + handle_browserless_response(state, body, status) + else {:error, %{reason: reason}} -> Logger.warning(warning_message("Browserless request error: #{inspect(reason)}", state)) @@ -126,23 +165,30 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 do defp handle_browserless_response( state, - %{"data" => %{"completed" => completed} = data}, + %{"data" => %{"evaluate2" => %{"value" => eval2_value}}}, _status ) do - if completed do - put_diagnostics( - state, - parse_to_diagnostics(data) - ) - else - Logger.warning( - warning_message( - "Browserless function returned with completed: false, error.message: #{inspect(data["error"]["message"])}", - state + case JSON.decode(eval2_value) do + {:ok, %{"data" => data}} -> + if data["completed"] do + put_diagnostics(state, parse_to_diagnostics(data)) + else + Logger.warning( + warning_message( + "Browserless function returned with completed: false, error.message: #{inspect(data["error"]["message"])}", + state + ) + ) + + put_diagnostics(state, service_error: data["error"]["message"]) + end + + {:error, _} -> + Logger.warning( + warning_message("Browserless function returned with unparseable data", state) ) - ) - put_diagnostics(state, service_error: data["error"]["message"]) + put_diagnostics(state, service_error: "Unparseable verifyPlausibleInstallation result") end end diff --git a/extra/lib/plausible/installation_support/verification/checks.ex b/extra/lib/plausible/installation_support/verification/checks.ex index 2479f011a763..56f341e2ae49 100644 --- a/extra/lib/plausible/installation_support/verification/checks.ex +++ b/extra/lib/plausible/installation_support/verification/checks.ex @@ -11,8 +11,8 @@ defmodule Plausible.InstallationSupport.Verification.Checks do @checks [ Checks.Url, - Checks.InstallationV2, - Checks.InstallationV2CacheBust + Checks.InstallationV2 + # Checks.InstallationV2CacheBust ] @spec run(String.t(), String.t(), String.t(), Keyword.t()) :: :ok