From fde001b3ac39ce13295212827168b7469eb3c3f9 Mon Sep 17 00:00:00 2001 From: Matthew Landauer Date: Wed, 6 Nov 2024 12:07:48 +1100 Subject: [PATCH] First go at reverse proxy for plausible.io --- config/application.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/config/application.rb b/config/application.rb index 7d8196ee9..d5d520f21 100644 --- a/config/application.rb +++ b/config/application.rb @@ -22,6 +22,31 @@ def compress(string) end end +# This is for proxying requests to this server to plausible.io for analytics. +# For some reason didn't work putting this in config/initializers/proxy.rb +# TODO: Fix this +class PlausibleProxy < Rack::Proxy + def perform_request(env) + request = Rack::Request.new(env) + + # use rack proxy for anything hitting plausible analytics endpoints + if request.path =~ %r{^/js/script\.} || request.path =~ %r{^/api/event} + # most backends required host set properly, but rack-proxy doesn't set this for you automatically + # even when a backend host is passed in via the options + env["HTTP_HOST"] = "plausible.io" + + # don't send your sites cookies to target service, unless it is a trusted internal service that can parse all your cookies + env['HTTP_COOKIE'] = '' + + env['content-length'] = nil + + super(env) + else + @app.call(env) + end + end +end + module PlanningalertsApp class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. @@ -46,6 +71,7 @@ class Application < Rails::Application # config.i18n.default_locale = :de config.middleware.use Rack::Attack + config.middleware.use PlausibleProxy config.action_dispatch.tld_length = 2