|
| 1 | +# Optional live IP-reputation policy for Berghain (issue #82). |
| 2 | +# |
| 3 | +# This is a complete alternative to examples/haproxy/haproxy.cfg. Berghain's |
| 4 | +# reputation service (embedded in cmd/spop via the `reputation:` config |
| 5 | +# section, or standalone as cmd/feedupdater) joins the peers mesh below as |
| 6 | +# "berghain_feed" and pushes per-IP reputation into the st_reputation |
| 7 | +# stick-tables live — no map files, no reloads. The gpt0 tag encodes the |
| 8 | +# action: 1 = block (silent-drop), >= 2 = minimum challenge level. |
| 9 | +# |
| 10 | +# HAProxy must know which peer entry is itself: run it with |
| 11 | +# haproxy -L haproxy_local -f examples/haproxy/haproxy-reputation.cfg |
| 12 | +# |
| 13 | +# The mesh survives more peers: add further HAProxy instances to the peers |
| 14 | +# section and they all learn the same reputation tables. |
| 15 | +# |
| 16 | +# Validate from the repository root with: |
| 17 | +# haproxy -c -L haproxy_local -f examples/haproxy/haproxy-reputation.cfg |
| 18 | + |
| 19 | +global |
| 20 | + log stdout format raw local0 |
| 21 | + |
| 22 | +defaults |
| 23 | + mode http |
| 24 | + log global |
| 25 | + timeout client 5s |
| 26 | + timeout server 5s |
| 27 | + timeout connect 5s |
| 28 | + option httplog |
| 29 | + |
| 30 | +listen stats |
| 31 | + bind 127.0.0.1:8000 |
| 32 | + stats enable |
| 33 | + stats uri / |
| 34 | + stats refresh 15s |
| 35 | + |
| 36 | +# The reputation service listens as the "berghain_feed" peer. HAProxy dials it |
| 37 | +# on startup to resync and keeps the session for live pushes. |
| 38 | +peers berghain |
| 39 | + peer haproxy_local 127.0.0.1:10000 |
| 40 | + peer berghain_feed 127.0.0.1:10001 |
| 41 | + |
| 42 | +frontend test |
| 43 | + bind *:8080 |
| 44 | + log-format "%ci:%cp\ [%t]\ %ft\ %b/%s\ %Th/%Ti/%TR/%Tq/%Tw/%Tc/%Tr/%Tt\ %ST\ %B\ %CC\ %CS\ %tsc\ %ac/%fc/%bc/%sc/%rc\ %sq/%bq\ %hr\ %hs\ %{+Q}r\ %ID spoa-error:\ %[var(txn.berghain.error)]" |
| 45 | + |
| 46 | + acl berghain_path path /cdn-cgi/challenge-platform/challenge |
| 47 | + |
| 48 | + # HAProxy issues the initial support ID; continuation requests carry it in their body. |
| 49 | + http-request set-var-fmt(txn.berghain.session) "bh@%[uuid()]" if berghain_path METH_GET |
| 50 | + |
| 51 | + # Individual-IP reputation is pushed live into the st_reputation tables by |
| 52 | + # the Berghain reputation service over the peers protocol. Stick-tables |
| 53 | + # key on exact addresses, so CIDR feeds (e.g. the generated |
| 54 | + # maps/cloudflare-ips.lst) would be matched with `src -f <file>` instead. |
| 55 | + acl src_is_v4 src -m ip 0.0.0.0/0 |
| 56 | + http-request set-var(req.rep) src,table_gpt0(st_reputation_v4) if src_is_v4 |
| 57 | + http-request set-var(req.rep) src,table_gpt0(st_reputation_v6) if !src_is_v4 |
| 58 | + |
| 59 | + # Reputation tag 1 (e.g. a CrowdSec ban): drop without an answer. Keep the |
| 60 | + # Berghain endpoint out so a ban lifted mid-challenge cannot wedge clients. |
| 61 | + http-request silent-drop if { var(req.rep) -m int eq 1 } |
| 62 | + |
| 63 | + http-request track-sc1 src table st_src |
| 64 | + |
| 65 | + filter spoe engine berghain config examples/haproxy/berghain.cfg |
| 66 | + |
| 67 | + # Base challenge level from request rate; rules are increasing so the |
| 68 | + # highest match wins. |
| 69 | + http-request set-var(req.berghain.level) int(1) if { sc1_http_req_rate gt 5 } |
| 70 | + http-request set-var(req.berghain.level) int(2) if { sc1_http_req_rate gt 10 } |
| 71 | + http-request set-var(req.berghain.level) int(3) if { sc1_http_req_rate gt 15 } |
| 72 | + |
| 73 | + # Flagged sources (reputation tag >= 2, e.g. a CrowdSec captcha decision or |
| 74 | + # a Tor exit) raise the MINIMUM level — only ever upward, via the ge guard. |
| 75 | + http-request set-var(req.berghain.level) int(3) if { var(req.rep) -m int ge 2 } !{ var(req.berghain.level) -m int ge 3 } |
| 76 | + |
| 77 | + acl berghain_active var(req.berghain.level) -m found |
| 78 | + |
| 79 | + http-request send-spoe-group berghain validate if !berghain_path berghain_active |
| 80 | + http-request return status 501 if { var(txn.berghain.error) -m found } |
| 81 | + |
| 82 | + acl berghain_valid var(txn.berghain.valid) -m bool |
| 83 | + acl is_ssl ssl_fc |
| 84 | + |
| 85 | + http-request return status 403 content-type "text/html" file "web/dist/default/index.html" if !berghain_valid !berghain_path berghain_active !is_ssl |
| 86 | + http-request return status 403 content-type "text/html" file "web/dist/native-crypto/index.html" if !berghain_valid !berghain_path berghain_active is_ssl |
| 87 | + http-request wait-for-body time 5s if berghain_path METH_POST |
| 88 | + use_backend berghain_http if berghain_path |
| 89 | + |
| 90 | + default_backend app_backend |
| 91 | + |
| 92 | +backend st_src |
| 93 | + stick-table type ipv6 size 1m expire 15m store http_req_rate(10s) |
| 94 | + |
| 95 | +# The reputation tables. Names, key types and the expire value must match the |
| 96 | +# reputation service configuration (table_v4/table_v6/table_expiry). |
| 97 | +backend st_reputation_v4 |
| 98 | + stick-table type ip size 1m expire 24h store gpt0 peers berghain |
| 99 | + |
| 100 | +backend st_reputation_v6 |
| 101 | + stick-table type ipv6 size 1m expire 24h store gpt0 peers berghain |
| 102 | + |
| 103 | +backend app_backend |
| 104 | + mode http |
| 105 | + http-request return status 200 content-type "text/plain" string "Hello World!" |
| 106 | + |
| 107 | +backend berghain_http |
| 108 | + mode http |
| 109 | + filter spoe engine berghain_challenge config examples/haproxy/berghain.cfg |
| 110 | + |
| 111 | + acl is_challenge_path path /cdn-cgi/challenge-platform/challenge |
| 112 | + |
| 113 | + http-request send-spoe-group berghain_challenge challenge if is_challenge_path |
| 114 | + http-request return status 501 if { var(txn.berghain.error) -m found } |
| 115 | + |
| 116 | + acl has_token var(txn.berghain.token) -m found |
| 117 | + |
| 118 | + http-after-response add-header set-cookie "berghain=%[var(txn.berghain.token)]; %[var(txn.berghain.domain)] path=/;" if has_token |
| 119 | + http-request return status 200 content-type "application/json" lf-string "%[var(txn.berghain.response)]" if is_challenge_path |
| 120 | + |
| 121 | + http-request return status 404 |
| 122 | + |
| 123 | +backend berghain_spop |
| 124 | + mode tcp |
| 125 | + option spop-check |
| 126 | + server localhost unix@./spop.sock check |
0 commit comments