Skip to content

Commit 9b1b17e

Browse files
committed
Upgrade to Caddy using trajano/google-fonts-proxy-docker
1 parent d4a711c commit 9b1b17e

9 files changed

Lines changed: 154 additions & 129 deletions

File tree

.github/workflows/docker-publish.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ jobs:
1212
- uses: actions/checkout@master
1313

1414
- name: Build and publish
15-
uses: elgohr/Publish-Docker-Github-Action@master
15+
uses: elgohr/Publish-Docker-Github-Action@v5
1616
with:
17-
name: citipo/docker-google-fonts-proxy/alpine
17+
name: citipo/docker-google-fonts-proxy
1818
username: citipo
1919
password: ${{ secrets.GITHUB_TOKEN }}
20-
registry: docker.pkg.github.com
20+
registry: ghcr.io
2121
dockerfile: Dockerfile

Caddyfile

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{
2+
admin off
3+
order replace before encode
4+
order replace after vars
5+
order replace after copy_response
6+
cache {
7+
# Force cache 30 days
8+
ttl 2592000s
9+
mode bypass
10+
default_cache_control public, max-age=2592000
11+
key {
12+
disable_scheme
13+
}
14+
nuts {
15+
configuration {
16+
Dir /data/nuts
17+
}
18+
}
19+
}
20+
}
21+
22+
:80
23+
@tracingEnabled expression {env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT} != ""
24+
25+
@resources {
26+
method GET HEAD
27+
path /css /css2 /icon
28+
}
29+
handle @resources {
30+
cache
31+
handle @tracingEnabled
32+
tracing {
33+
span {path}
34+
}
35+
36+
reverse_proxy @resources https://fonts.googleapis.com {
37+
header_up Host {upstream_hostport}
38+
header_up -X-Forwarded-For
39+
header_up -X-Forwarded-Host
40+
header_up -X-Forwarded-Proto
41+
header_up Accept-Encoding identity
42+
header_down -Cache-Control
43+
header_down -Report-To
44+
header_down -Content-Security-Policy-Report-Only
45+
header_down -Cross-Origin-Opener-Policy
46+
header_down -Cross-Origin-Resource-Policy
47+
header_down -Link
48+
}
49+
50+
replace stream {
51+
"https://fonts.gstatic.com/s" "//{env.FONTS_HOST}/s"
52+
}
53+
}
54+
55+
@static {
56+
method GET HEAD
57+
path /s/*
58+
}
59+
handle @static {
60+
cache
61+
handle @tracingEnabled
62+
tracing {
63+
span {path}
64+
}
65+
66+
reverse_proxy https://fonts.gstatic.com {
67+
header_up Host {upstream_hostport}
68+
header_up -X-Forwarded-For
69+
header_up -X-Forwarded-Host
70+
header_up -X-Forwarded-Proto
71+
header_up Accept-Encoding identity
72+
header_down -Cache-Control
73+
header_down -Report-To
74+
header_down -Content-Security-Policy-Report-Only
75+
header_down -Cross-Origin-Opener-Policy
76+
header_down -Cross-Origin-Resource-Policy
77+
header_down -Link
78+
}
79+
}
80+
81+
# Debug endpoint, to check if any private information gets leaked
82+
@debug {
83+
method GET
84+
path /debug
85+
}
86+
handle @debug {
87+
rewrite * /get
88+
reverse_proxy https://httpbin.org {
89+
header_up Host {upstream_hostport}
90+
header_up -X-Forwarded-For
91+
header_up -X-Forwarded-Host
92+
header_up -X-Forwarded-Proto
93+
header_up Accept-Encoding identity
94+
header_down -Cache-Control
95+
header_down -Report-To
96+
header_down -Content-Security-Policy-Report-Only
97+
header_down -Cross-Origin-Opener-Policy
98+
header_down -Cross-Origin-Resource-Policy
99+
header_down -Link
100+
}
101+
}
102+
103+
respond / 204
104+
respond 404

Dockerfile

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,11 @@
1-
FROM nginx:1.21-alpine AS builder
2-
3-
ENV SUBS_VERSION 1.1.15
4-
ENV FONTS_HOST 'http://localhost'
5-
6-
RUN apk add --no-cache --virtual .build-deps \
7-
git \
8-
gcc \
9-
libc-dev \
10-
make \
11-
openssl-dev \
12-
pcre2-dev \
13-
zlib-dev \
14-
linux-headers \
15-
libxslt-dev \
16-
gd-dev \
17-
geoip-dev \
18-
perl-dev \
19-
libedit-dev \
20-
bash \
21-
alpine-sdk \
22-
findutils
23-
24-
RUN wget "http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz" -O nginx.tar.gz
25-
RUN mkdir /tmp/src && CONFARGS=$(nginx -V 2>&1 | sed -n -e 's/^.*arguments: //p') tar -zxC /tmp/src -f nginx.tar.gz
26-
27-
RUN git clone git://github.com/yaoweibin/ngx_http_substitutions_filter_module.git
28-
29-
RUN SUBSDIR="$(pwd)/ngx_http_substitutions_filter_module" && \
30-
cd /tmp/src/nginx-$NGINX_VERSION && \
31-
./configure --with-compat $CONFARGS --add-dynamic-module=$SUBSDIR && \
32-
make && \
33-
make install
34-
35-
FROM nginx:1.21-alpine
36-
37-
# Extract the dynamic module NCHAN from the builder image
38-
COPY --from=builder /usr/local/nginx/modules/ngx_http_subs_filter_module.so /usr/local/nginx/modules/ngx_http_subs_filter_module.so
39-
40-
COPY ./conf/nginx.conf.template /etc/nginx/nginx.conf.template
41-
COPY ./docker-entrypoint.sh /
42-
RUN chmod a+x /docker-entrypoint.sh
43-
44-
ENTRYPOINT ["/docker-entrypoint.sh"]
45-
46-
CMD ["nginx", "-g", "daemon off;"]
1+
FROM caddy:builder AS builder
2+
RUN xcaddy build \
3+
--with github.com/caddyserver/replace-response \
4+
--with github.com/caddyserver/cache-handler \
5+
--with github.com/darkweak/storages/nuts/caddy
6+
7+
FROM busybox:1.37.0-uclibc
8+
COPY --from=builder /usr/bin/caddy /usr/bin/caddy
9+
COPY Caddyfile /etc/caddy/Caddyfile
10+
HEALTHCHECK --start-period=10s --start-interval=1s CMD wget -q --spider http://localhost/ || exit 1
11+
CMD [ "/usr/bin/caddy", "run", "--config", "/etc/caddy/Caddyfile" ]

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ receiving visits from European Union residents.
55

66
These new restrictions make it more illegal to use Google Fonts directly. This proxy is a plug-and-play
77
replacement for Google Fonts that can be hosted on a custom domain to proxy Google Fonts and thus comply
8-
with GDPR (as no personal data will be transfered to Google Fonts).
8+
with GDPR (as no personal data will be transferred to Google Fonts).
99

1010
This image only relies on nginx and its substitution module: it's extremely fast, privacy friendly and
1111
production ready.
1212

13+
> The latest version is based on https://github.com/trajano/google-fonts-proxy-docker.
14+
1315
## Usage
1416

1517
Once setup (for instance at Citipo we run an instance on https://fonts.citipo.com), all requests done to
1618
the configured URL will be forwarded to Google Fonts with the same exact URL path and CSS responses
1719
will be altered on-the-fly to change fonts files reference to use your own domain.
1820

19-
**This forwarding will be done fully anonymously so that the client IP and personnal data won't ever
21+
**This forwarding will be done fully anonymously so that the client IP and personal data won't ever
2022
be transmitted to Google.**
2123

2224
Once deployed, you can use the service by directly replacing the Google Fonts URL with your own:
@@ -27,27 +29,26 @@ Once deployed, you can use the service by directly replacing the Google Fonts UR
2729
<link href="https://fonts.citipo.com/css2?family=Assistant:ital,wght@0,400;0,700;0,800;1,400;1,700" rel="stylesheet" crossorigin="anonymous" />
2830
```
2931

30-
> In compliance to Google Fonts Terms of Service, this proxy doesn't cache the fonts locally, it only transfers
31-
> the requests (https://github.com/google/fonts/issues/1637).
32-
3332
## Setup
3433

3534
With docker-compose:
3635

3736
```yaml
38-
version: '3'
39-
4037
services:
4138
fonts:
42-
image: docker.pkg.github.com/citipo/docker-google-fonts-proxy/alpine
39+
image: ghcr.io/citipo/docker-google-fonts-proxy
4340
ports:
4441
# You can use a proxy (like https://github.com/nginx-proxy/nginx-proxy)
4542
# to provide SSL
4643
- '80:80'
4744
environment:
4845
# The host you use for fonts: references to Google Fonts
4946
# will be replaced to this host in CSS files
50-
- FONTS_HOST=fonts.citipo.com
47+
FONTS_HOST: fonts.citipo.com
48+
volumes:
49+
# The fonts and CSS files will be cached locally in this directory for 30 days
50+
# in order to avoid sending too many requests to Google Fonts
51+
- './storage:/data/nuts'
5152
```
5253
5354
With Docker CLI:
@@ -56,17 +57,17 @@ With Docker CLI:
5657
docker run -d \
5758
-p 80:80 \
5859
-e FONTS_HOST=fonts.citipo.com \
59-
docker.pkg.github.com/citipo/docker-google-fonts-proxy/alpine
60+
ghcr.io/citipo/docker-google-fonts-proxy
6061
```
6162

6263
## For developers: what to tell you Data Protection Officer?
6364

64-
Your Data Protection Officer (the person responsible of GDPR in your company) may be curious about the details
65+
Your Data Protection Officer (the person responsible for GDPR in your company) may be curious about the details
6566
of this tool, especially given the amount of media coverage the latest Google Fonts GDPR issues had.
6667

6768
To answer their questions, you can explain that this tool *anonymizes data* (in the GDPR meaning): it transforms
6869
personal, indirectly identifiable data into fully anonymous requests towards Google. As long as you host this
69-
service in your own infrastructure, no personal data is ever transfered to Google (or any other intermediate).
70+
service in your own infrastructure, no personal data is ever transferred to Google (or any other intermediate).
7071

71-
Data anonymization (which is different from pseudonymization) ensures that the data transfered to Google isn't
72+
Data anonymization (which is different from pseudonymization) ensures that the data transferred to Google isn't
7273
covered by GDPR, thus not being a compliance issue.

compose.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
fonts:
3+
build: .
4+
ports:
5+
- '4000:80'
6+
environment:
7+
FONTS_HOST: localhost:4000

conf/nginx.conf.template

Lines changed: 0 additions & 61 deletions
This file was deleted.

docker-entrypoint.sh

Lines changed: 0 additions & 6 deletions
This file was deleted.

fmt.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
MSYS_NO_PATHCONV=1 command docker run -v $(pwd):/mnt caddy caddy fmt --overwrite /mnt/Caddyfile

test.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -ex
3+
image_name=$(docker build . -q)
4+
MSYS_NO_PATHCONV=1 docker run -v $(pwd):/mnt $image_name caddy validate --config /mnt/Caddyfile
5+
MSYS_NO_PATHCONV=1 docker run -v $(pwd):/mnt $image_name caddy fmt --overwrite /mnt/Caddyfile
6+
PORT=$RANDOM
7+
container_name=$(docker run --rm -q -p $PORT:80 --env "STATIC_URL=http://localhost:$PORT/s" -d $image_name)
8+
while [ "$(docker inspect --format='{{.State.Health.Status}}' $container_name)" != "healthy" ]; do
9+
sleep 1
10+
done
11+
curl -v 'localhost:'$PORT'/'
12+
curl -v 'localhost:'$PORT'/css?family=Roboto:300,400,400i,700,700i&display=swap'
13+
curl -v 'localhost:'$PORT'/debug'

0 commit comments

Comments
 (0)