plugins

BareProxy Geo Rules Plugin: Allow, Block or Route Requests by Country

Status: built, number 5 of 21 in BareProxy’s build order, and out in BareProxy 0.7.0 as geo.wasm on the releases page. Its manual is the README in the code repository.

Some decisions depend on where a visitor is. A service licensed only in some countries. A login page that never sees a legitimate user from outside two countries but sees brute-force attempts from everywhere. A site with separate sections for different markets. Each app can look up the country itself, or the proxy can do it once and pass the answer on.

What It Does

  • Looks up the country of each request’s address in a local database file, such as the free GeoLite2, DB-IP Lite or IPinfo Lite databases, kept in a folder the plugin may read. No lookup leaves the machine.
  • Allows or blocks by country, for the whole site or a path prefix, with a page of your own and a 403 or 451 for those turned away. Addresses with no country, such as a private network, can be let in or kept out.
  • Passes it on. It adds the country code to the request in a header, X-Country: DE, so the app can use it without a database of its own. A country header the visitor sends is always replaced, so it can’t be faked.
  • Routes by country, the BareProxy way. The plugin sets the header, and an ordinary route rule matches on it: route /* header X-Country=FR -> eu-app. The routing stays in the config, where plan and explain can see it.
  • Works behind a CDN. Given the CDN’s address ranges, it takes the visitor’s address from X-Forwarded-For, and ignores that header from anyone else.

Here is a config:

plugin geo /etc/bareproxy/plugins/geo.wasm
  config /etc/bareproxy/plugins/geo.conf
  read /var/lib/geoip
  body-limit 32MB

site example.com
  use geo
  route /* header X-Country=FR -> eu-app
  route /* -> app

And the plugin’s own:

# geo.conf
database dbip-country-lite.mmdb
block RU KP

path /checkout/
  allow DE AT CH

Its Own Database Reader

The country databases come as MaxMind DB files. The plugin reads them with a small reader of its own, written for BareProxy, that looks the address up in the file where it lies: loading a database costs no more than reading it, and every read is checked against the file’s size, so a broken file gives an error, never a crash. It’s tested against MaxMind’s own test databases. check loads the database too, so a missing or broken file stops before it goes live.

Limits Worth Knowing

Address-to-country data is good, not perfect: VPNs, mobile carriers and cloud providers move addresses around. Geo rules are a sensible filter, never proof of where someone is. The database file needs updating every few weeks; the plugin picks up a new file on its own, without a restart, and keeps the old one if the new one is broken.

Every visitor turned away carries a note in the request’s record: the country, the rule that turned them away and its line in the config.

The whole program is on the plugins page.