Skip to content

Commit 1a9da28

Browse files
committed
imp: use adblock2privoxy instead of privoxy-blocklist
1 parent ed39e68 commit 1a9da28

File tree

10 files changed

+294
-1175
lines changed

10 files changed

+294
-1175
lines changed

Dockerfile

Lines changed: 108 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,14 @@
1-
FROM alpine:latest
1+
FROM alpine:latest AS build-privoxy
22

33
ARG PRIVOXY_VERSION=4.0.0
44
ARG PRIVOXY_SRC_SHA1SUM=d302cb0bf23536e67a1b5505d01486a335d9c4c0
55
ARG PRIVOXY_CONFIG_OPTIONS="--disable-toggle --disable-editor --disable-force --with-openssl --with-brotli"
66
ARG PRIVOXY_BUILD_EXTRA="openssl-dev brotli-dev"
7-
ARG SYSTEM_EXTRA_PKGS="openssl brotli net-tools"
87

98
SHELL ["/bin/ash", "-eo", "pipefail", "-c"]
109

11-
# Create Privoxy User
12-
RUN set -ex; \
13-
addgroup --gid 7777 --system privoxy; \
14-
adduser \
15-
--disabled-password \
16-
--home /var/lib/privoxy/ \
17-
--ingroup privoxy \
18-
--no-create-home \
19-
--system \
20-
--uid 7777 \
21-
privoxy; \
22-
mkdir /var/lib/privoxy/; \
23-
chown privoxy:privoxy /var/lib/privoxy/;
10+
WORKDIR /build
2411

25-
# Build Privoxy
2612
RUN set -eux; \
2713
apk add --no-cache --virtual build-tools \
2814
gcc \
@@ -33,72 +19,138 @@ RUN set -eux; \
3319
libc-dev \
3420
zlib-dev \
3521
pcre2-dev \
36-
$PRIVOXY_BUILD_EXTRA; \
37-
wget -qO /var/lib/privoxy/privoxy-src.tar.gz https://sourceforge.net/projects/ijbswa/files/Sources/${PRIVOXY_VERSION}%20%28stable%29/privoxy-${PRIVOXY_VERSION}-stable-src.tar.gz/download; \
38-
echo "${PRIVOXY_SRC_SHA1SUM} /var/lib/privoxy/privoxy-src.tar.gz" | sha1sum -c; \
39-
tar -zxvf /var/lib/privoxy/privoxy-src.tar.gz -C /var/lib/privoxy/; \
40-
cd /var/lib/privoxy/privoxy-${PRIVOXY_VERSION}-stable; \
22+
$PRIVOXY_BUILD_EXTRA;
23+
24+
RUN set -eux; \
25+
wget -qO privoxy-src.tar.gz https://sourceforge.net/projects/ijbswa/files/Sources/${PRIVOXY_VERSION}%20%28stable%29/privoxy-${PRIVOXY_VERSION}-stable-src.tar.gz/download; \
26+
echo "${PRIVOXY_SRC_SHA1SUM} privoxy-src.tar.gz" | sha1sum -c; \
27+
tar -zxvf privoxy-src.tar.gz; \
28+
cd privoxy-${PRIVOXY_VERSION}-stable; \
4129
autoheader; \
4230
autoconf; \
43-
./configure $PRIVOXY_CONFIG_OPTIONS; \
31+
./configure --prefix=/usr/local $PRIVOXY_CONFIG_OPTIONS; \
4432
make; \
45-
make -s install USER=privoxy GROUP=privoxy; \
46-
chown -R privoxy:privoxy /usr/local/etc/privoxy/; \
47-
rm -rf /var/lib/privoxy/privoxy-src.tar.gz /var/lib/privoxy/privoxy-${PRIVOXY_VERSION}-stable; \
48-
apk del build-tools build-deps;
33+
make install; \
34+
privoxy --version;
35+
36+
37+
FROM alpine:latest AS build-adblock2privoxy
38+
39+
ARG ADBLOCK2PRIVOXY_RESOLVER=lts-21.25
40+
41+
SHELL ["/bin/ash", "-eo", "pipefail", "-c"]
42+
43+
WORKDIR /build
44+
45+
RUN set -eux; \
46+
apk add --no-cache --virtual build-tools \
47+
gcc \
48+
g++ \
49+
make \
50+
curl \
51+
gmp \
52+
git; \
53+
apk add --no-cache --virtual build-deps \
54+
musl-dev \
55+
zlib-dev \
56+
gmp-dev \
57+
ncurses-libs \
58+
ncurses-dev; \
59+
curl -sSL https://get.haskellstack.org/ | sh;
60+
61+
RUN set -eux; \
62+
git clone https://github.com/essandess/adblock2privoxy.git . --depth=1; \
63+
export STACK_ROOT=/usr/local/etc/.stack; \
64+
cd adblock2privoxy; \
65+
stack setup --allow-different-user --resolver $ADBLOCK2PRIVOXY_RESOLVER; \
66+
stack build --allow-different-user --resolver $ADBLOCK2PRIVOXY_RESOLVER --allow-newer; \
67+
stack install --allow-different-user --local-bin-path /usr/local/bin --resolver $ADBLOCK2PRIVOXY_RESOLVER --allow-newer; \
68+
adblock2privoxy --version;
69+
70+
71+
FROM alpine:latest AS runtime
72+
73+
ARG SYSTEM_EXTRA_PKGS="brotli net-tools"
74+
75+
SHELL ["/bin/ash", "-eo", "pipefail", "-c"]
76+
77+
# Create Privoxy User
78+
RUN set -ex; \
79+
addgroup --gid 7777 --system privoxy; \
80+
adduser \
81+
--disabled-password \
82+
--home /var/lib/privoxy/ \
83+
--ingroup privoxy \
84+
--no-create-home \
85+
--system \
86+
--uid 7777 \
87+
privoxy; \
88+
mkdir /var/lib/privoxy/; \
89+
chown privoxy:privoxy /var/lib/privoxy/;
4990

5091
# Add system tools
5192
RUN set -eux; \
5293
apk add --no-cache --virtual runtime-deps \
53-
python3 \
54-
pcre2 \
55-
bash \
56-
sed \
57-
$SYSTEM_EXTRA_PKGS;
94+
python3 \
95+
pcre2 \
96+
openssl \
97+
nginx \
98+
gmp \
99+
ncurses \
100+
$SYSTEM_EXTRA_PKGS;
58101

59-
# Enable Privoxy HTTPS inspection
60-
# hadolint ignore=SC1003
61-
RUN set -ex; \
62-
mv /usr/local/etc/privoxy/config /usr/local/etc/privoxy/config.orig; \
63-
sed -i '/^+set-image-blocker{pattern}/a +https-inspection \\' /usr/local/etc/privoxy/match-all.action;
102+
# Docker Entry Point
103+
COPY docker-entrypoint.sh /usr/local/sbin/
104+
RUN sed -i 's/\r$//' /usr/local/sbin/docker-entrypoint.sh && \
105+
chmod +x /usr/local/sbin/docker-entrypoint.sh;
64106

65-
# Copy project scripts/configs
107+
# Privman
66108
COPY data/rules/ /usr/local/etc/privoxy/privman-rules/
67-
COPY data/config /usr/local/etc/privoxy/
68-
COPY data/privoxy-blocklist.conf /var/lib/privoxy/
69-
RUN set -eux; \
70-
# Remove CRLF (dos2unix) and ensure LF-only
71-
sed -i 's/\r$//' /var/lib/privoxy/privoxy-blocklist.conf
72109
COPY bin/privman.py /var/lib/privoxy/privman.py
73110
RUN set -ex; \
74111
sed -i 's/\r$//' /var/lib/privoxy/privman.py; \
75112
head -1 /var/lib/privoxy/privman.py | grep -q '^#!' || \
76113
sed -i '1i #!/usr/bin/env python3' /var/lib/privoxy/privman.py; \
77114
chmod +x /var/lib/privoxy/privman.py; \
78115
ln -sf /var/lib/privoxy/privman.py /usr/local/sbin/privman;
79-
COPY bin/privoxy-blocklist.sh /var/lib/privoxy/privoxy-blocklist.sh
80-
RUN set -eux; \
81-
sed -i 's/\r$//' /var/lib/privoxy/privoxy-blocklist.sh; \
82-
chmod +x /var/lib/privoxy/privoxy-blocklist.sh; \
83-
ln -sf /var/lib/privoxy/privoxy-blocklist.sh /usr/local/sbin/privoxy-blocklist;
84-
COPY docker-entrypoint.sh /usr/local/sbin/
85-
RUN sed -i 's/\r$//' /usr/local/sbin/docker-entrypoint.sh && \
86-
chmod +x /usr/local/sbin/docker-entrypoint.sh;
87116

88-
# Set the correct permissions
117+
# Privoxy
118+
COPY --from=build-privoxy /usr/local /usr/local
119+
COPY data/config /usr/local/etc/privoxy/
120+
# hadolint ignore=SC1003
89121
RUN set -ex; \
122+
#mv /usr/local/etc/privoxy/config /usr/local/etc/privoxy/config.orig; \
90123
mkdir -p /var/log/privoxy /usr/local/etc/privoxy/CA /usr/local/etc/privoxy/certs /usr/local/etc/privoxy/privman-rules; \
91-
chown -R privoxy:privoxy /var/log/privoxy/ /usr/local/etc/privoxy/config /usr/local/etc/privoxy/CA /usr/local/etc/privoxy/certs /usr/local/etc/privoxy/privman-rules /var/lib/privoxy/privoxy-blocklist.conf;
124+
chown -R privoxy:privoxy /var/log/privoxy /usr/local/etc/privoxy; \
125+
sed -i '/^+set-image-blocker{pattern}/a +https-inspection \\' /usr/local/etc/privoxy/match-all.action; \
126+
cp -a /usr/local/etc/privoxy /opt/privoxy-default;
127+
128+
# adblock2privoxy
129+
COPY --from=build-adblock2privoxy /usr/local/bin/adblock2privoxy /usr/local/bin/adblock2privoxy
130+
COPY --from=build-adblock2privoxy /build/adblock2privoxy/templates /opt/local/share/adblock2privoxy/templates
131+
COPY data/nginx.conf /etc/nginx/nginx.conf
132+
RUN set -ex; \
133+
mkdir -p /usr/local/etc/adblock2privoxy/css; \
134+
echo "# Dummy file" | tee -a /usr/local/etc/privoxy/ab2p.system.action /usr/local/etc/privoxy/ab2p.action /usr/local/etc/privoxy/ab2p.system.filter /usr/local/etc/privoxy/ab2p.filter; \
135+
chown -R privoxy:privoxy /usr/local/etc/privoxy/ab2p.system.action /usr/local/etc/privoxy/ab2p.action /usr/local/etc/privoxy/ab2p.system.filter /usr/local/etc/privoxy/ab2p.filter; \
136+
chown -R privoxy:privoxy /usr/local/etc/adblock2privoxy /etc/nginx /var/log/nginx /var/lib/nginx; \
137+
chmod 755 /usr/local/bin/adblock2privoxy; \
138+
chmod -R u+rw /etc/nginx /var/log/nginx /var/lib/nginx;
139+
140+
# Verifications
141+
RUN set -ex; \
142+
privoxy --version; \
143+
adblock2privoxy --version;
92144

93-
ENV ADBLOCK_URLS="" \
94-
ADBLOCK_FILTERS=""
145+
# Common
146+
ENV ADBLOCK_URLS=""
147+
ENV ADBLOCK_CSS_DOMAIN="172.17.0.2:8119"
95148

96149
ENTRYPOINT ["/usr/local/sbin/docker-entrypoint.sh"]
97150

98-
RUN cp -a /usr/local/etc/privoxy /opt/privoxy-default
99-
100151
VOLUME /usr/local/etc/privoxy
101152
EXPOSE 8118/tcp
153+
EXPOSE 8119/tcp
102154

103155
USER privoxy
104156

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Alpine docker with [privoxy](https://www.privoxy.org) enabled and configured to work with HTTPS.
66

7-
It also includes the script made by '[Andrwe Lord Weber](https://github.com/Andrwe/privoxy-blocklist)' to translate adblock rules to privoxy.
7+
It also includes '[adblock2privoxy](https://github.com/essandess/adblock2privoxy)' to translate adblock rules to privoxy with CSS hidden elements & blackhole.
88

99
**The default configuration is intended for personal use only (ex. raspberry)**
1010

@@ -20,7 +20,7 @@ Privoxy Status Page: https://config.privoxy.org/show-status
2020
| Name | Description | Default |
2121
|----------------|-------------|-------------|
2222
| ADBLOCK_URLS | String of urls separated by spaces | "" |
23-
| ADBLOCK_FILTERS | String of filters separated by spaces | "" |
23+
| ADBLOCK_CSS_DOMAIN | A domain/IP that points to the container (IP:PORT) | 172.17.0.2:8119 |
2424

2525
- Can get urls from: https://easylist.to/
2626
- Can know the available filters with ```docker exec privoxy privoxy-blocklist --help```
@@ -39,9 +39,11 @@ services:
3939
container_name: privoxy
4040
ports:
4141
- 8118:8118
42+
- 8119:8119
4243
environment:
43-
- TZ=Europe/Madrid
44-
- ADBLOCK_URLS=https://easylist.to/easylist/easylist.txt
44+
TZ: Europe/Madrid
45+
ADBLOCK_URLS: https://easylist.to/easylist/easylist.txt
46+
ADBLOCK_CSS_DOMAIN: privoxy.local:8119
4547
volumes:
4648
- privoxy-ca:/usr/local/etc/privoxy/CA
4749
restart: unless-stopped
@@ -51,6 +53,8 @@ volumes:
5153
privoxy-ca:
5254
```
5355
56+
** privoxy.local must point to the container
57+
5458
### Get ca-bundle
5559
```sh
5660
docker cp privoxy:/usr/local/etc/privoxy/CA/privoxy-ca-bundle.crt .
@@ -61,7 +65,7 @@ docker cp privoxy:/usr/local/etc/privoxy/CA/privoxy-ca-bundle.crt .
6165
- Update the Trusted CA file: `docker exec privoxy privman --update-trusted-ca`
6266
- Regenerate the .crt bundle: `docker exec privoxy privman --regenerate-crt-bundle`
6367
- Update 'adblock' filters: `docker exec privoxy privman --update-adblock-filters`
64-
- Block a domain to the blocklist: `docker exec privoxy privman --add-blocklist .google. .facebook.`
68+
- Add a domain to the blocklist: `docker exec privoxy privman --add-blocklist .google. .facebook.`
6569
- Remove a domain from the blocklist: `docker exec privoxy privman --remove-blocklist .facebook.`
6670

6771
## :page_facing_up: Configuration highlight changes

bin/privman.py

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import argparse
77
import subprocess
88
import urllib.request
9+
import signal
10+
911

1012
BASE_LIB_DIR = "/var/lib/privoxy"
1113
BASE_DIR = "/usr/local/etc/privoxy"
@@ -52,22 +54,58 @@ def generate_crt_bundle(subj, forced=False):
5254
print_log("CRT Bundle", "Nothing to do. The file already exists.")
5355

5456

55-
def update_adblock_filters():
56-
adblock_filters = " ".join(
57-
map(lambda x: f'"{x}"', os.environ.get("ADBLOCK_FILTERS", "").split(" "))
58-
)
59-
adblock_urls = " ".join(
60-
map(lambda x: f'"{x}"', os.environ.get("ADBLOCK_URLS", "").split(" "))
57+
def init_adblock_filters():
58+
subprocess.run(
59+
[
60+
"adblock2privoxy",
61+
"-p",
62+
"/usr/local/etc/privoxy",
63+
"-w",
64+
"/usr/local/etc/adblock2privoxy/css",
65+
"-d",
66+
os.environ.get("ADBLOCK_CSS_DOMAIN", ""),
67+
"-t",
68+
"/usr/local/etc/privoxy/ab2p.task",
69+
os.environ.get("ADBLOCK_URLS", ""),
70+
]
6171
)
62-
lines = [f"URLS=({adblock_urls})", f"\nFILTERS=({adblock_filters})"]
63-
with open(ADBLOCK_DYN_FILE, "w") as f:
64-
f.writelines(lines)
72+
return True
73+
74+
75+
def update_adblock_filters():
6576
subprocess.run(
66-
["privoxy-blocklist", "-c", "/var/lib/privoxy/privoxy-blocklist.conf"]
77+
[
78+
"adblock2privoxy",
79+
"-t",
80+
"/usr/local/etc/privoxy/ab2p.task",
81+
]
6782
)
6883
return True
6984

7085

86+
def _get_privoxy_pid():
87+
for pid in os.listdir("/proc"):
88+
if not pid.isdigit():
89+
continue
90+
try:
91+
with open(f"/proc/{pid}/comm", "r") as f:
92+
proc_name = f.read().strip()
93+
if proc_name.lower() == "privoxy":
94+
return int(pid)
95+
except (IOError, FileNotFoundError):
96+
continue
97+
return None
98+
99+
100+
def restart_privoxy():
101+
privoxy_pid = _get_privoxy_pid()
102+
if privoxy_pid:
103+
os.kill(privoxy_pid, signal.SIGHUP)
104+
print_log("Privoxy", "Restarted successfully")
105+
else:
106+
print_log("Privoxy", "Can't found the PID of privoxy")
107+
108+
71109
def _get_section_index(rules, section):
72110
for index, rule in enumerate(rules):
73111
if rule.strip() == section:
@@ -125,11 +163,6 @@ def _remove_from(filename, section_name, url):
125163
return need_write
126164

127165

128-
def restart_privoxy():
129-
os.system("kill -HUP `cat /tmp/supervisord_privoxy.pid`")
130-
print_log("Privoxy", "Restarted successfully")
131-
132-
133166
def add_whitelist(urls, soft_mode=False):
134167
user_action_file = os.path.join(BASEDIR_RULES, "user.action")
135168
has_changes = False
@@ -270,8 +303,8 @@ def remove_blocklist(urls):
270303

271304
if args.init:
272305
update_trusted_ca()
273-
update_adblock_filters()
274306
generate_crt_bundle(args.crt_bundle_subj)
307+
init_adblock_filters()
275308
if args.update_trusted_ca:
276309
need_restart = update_trusted_ca(forced=True)
277310
if args.regenerate_crt_bundle:

0 commit comments

Comments
 (0)