plausible / analytics

Simple, open source, lightweight (< 1 KB) and privacy-friendly web analytics alternative to Google Analytics.
https://plausible.io
GNU Affero General Public License v3.0
18.95k stars 1.02k forks source link

HTTPClient not support HTTP/HTTPS proxy #4269

Open HADB opened 1 week ago

HADB commented 1 week ago

Past Issues Searched

Issue is a Bug Report

Using official Plausible Cloud hosting or self-hosting?

Self-hosting

Describe the bug

First of all, thank you for the great work on this project!

Currently, the HTTPClient seems not support using HTTP or HTTPS proxies set via environment variables (HTTP_PROXY and HTTPS_PROXY). This functionality is important for users who need to route traffic through a proxy for various reasons, such as network restrictions or privacy concerns.

In my case, I'm facing network restrictions while connecting to Google services or Gravartor avatars.

Expected behavior

The HTTPClient should respect the HTTP_PROXY / HTTPS_PROXY environment variables and route HTTP/HTTPS requests through the specified proxy.

Screenshots

No response

Environment

No response

ruslandoga commented 1 week ago

👋 @HADB

Where do the HTTP_PROXY and HTTPS_PROXY environment variables come from? Have they been documented somewhere? I don't think we have them anywhere in the code base.

HADB commented 1 week ago

@ruslandoga Hi, thanks for replying. HTTP_PROXY and HTTPS_PROXY are environment variables used to specify proxy servers for HTTP and HTTPS traffic respectively. Many applications and libraries recognize and use these variables to route their network traffic through specified proxy servers, such as:curl, wget and Python’s requests library.

ruslandoga commented 1 week ago

I see. We might be able to provide support for these env vars later, but for now we can at least make proxying configurable via custom config files. I'll open a PR for that later today with an example configuration.

ruslandoga commented 1 week ago

@HADB would you be able to try out the image I built from #4270?

Here're the changes you'd need to make:

docker-compose.yml

plausible:
- image: ghcr.io/plausible/community-edition:v2.1.1
+ image: ghcr.io/ruslandoga/plausible:build-allow-finch-config-9456444
+ volumes:
+   - ./my_config.exs:/app/my_config.exs

my_config.exs

import Config

# Plausible uses HTTPS for all outgoing requests, and our HTTP client (Mint) doesn't support HTTPS->HTTPS proxying
# https://hexdocs.pm/mint/Mint.HTTP.html#connect/4-proxying recommends HTTP->HTTPS in this case
config :plausible, Plausible.Finch, conn_opts: [proxy: {:http, _host = "some.proxy.com", _port = 1080, _opts = []}]

Note that you would need to provide your own proxy host/port pair. There is also an example config in #4270 where host and port are extracted from HTTP_PROXY env var.

plausible-conf.env

+ EXTRA_CONFIG_PATH=/app/my_config.exs
HADB commented 5 days ago

@ruslandoga I have tried this image and it works as expected! Great job! Thanks a lot!