cosimo / varnish-geoip Goto Github PK
View Code? Open in Web Editor NEWVarnish VCL "plugin" that geolocates client IP addresses (requires geoip)
Varnish VCL "plugin" that geolocates client IP addresses (requires geoip)
Varnish Geo-IP VCL =========================== Last updated: 28/05/2013 Cosimo Streppone <[email protected]> Opera Software ASA Here you will find a VCL config file for Varnish (http://varnish-cache.org) This VCL will transparently add a HTTP request header with Geo-IP information depending on the client IP address that made the request. *** WARNING *** This VCL consists of C code. Your Varnish might explode. YMMV. Don't use it in production if you don't know what you're doing. We are using it in production, but we _don't_ know what we're doing :). Why would you want this? ------------------------ So your backend application can just read a HTTP header and figure out where the client comes from, city, country and latitude/longitude. Of course this doesn't come for free. You need the city edition of Geo-IP. The rewritten header is "X-Geo-IP". X-Geo-IP header content ----------------------- the `X-Geo-IP` header will differ depending on which function you decide to apply, and what GeoIP database you are using, Country or City edition. In case of simple country lookup, you will have: X-Geo-IP: country:US whereas a failed lookup will return `country:A6`. That is an internal convention we adopted, of course YMMV. In case of City database, the header will look like: X-Geo-IP: city:Rome, country:IT, lat:44.2134, lon:12.241, ip:1.2.3.4 (numbers are made up), while in the lookup failed case, you will have: X-Geo-IP: city:, country:A6, lat:0.0, lon:0.0, ip:1.2.3.4 The invalid/internal IP case is covered by the geoip library itself, so it should be reported as unknown IP. Requirements ------------ - gcc, make - a recent perl, with `prove' Instructions ------------- 1) Run 'make && make test' You should see "All tests successful" at the end of the execution 2) Install the generated geoip.vcl in /etc/varnish/ 3) At the top of your main VCL file, add the following line: include "/etc/varnish/geoip.vcl" and then in your vcl_recv() add: C{ vcl_geoip_set_header(sp); }C 4) If you run Debian, look here: http://www.streppone.it/cosimo/blog/2011/11/how-to-start-up-varnish-with-a-custom-cc_command-on-debian/ for instructions on how to specify a custom "cc_command" in the Varnish init script. 5) Restart Varnish 6) ? 7) Profit !!
error message like:
Message from dlopen:
Compiled VCL program failed to load:
./vcl.pmCq19vo.so: undefined symbol: GeoIP_open_type
Running dlopen failed, exit 1
VCL compilation failed
i installed geoip like:
$ ll /usr/lib/libGeoIP.*
-rw-r--r-- 1 root root 1345150 Jul 13 16:21 /usr/lib/libGeoIP.a
-rwxr-xr-x 1 root root 921 Jul 13 16:21 /usr/lib/libGeoIP.la
lrwxrwxrwx 1 root root 17 Jul 13 16:21 /usr/lib/libGeoIP.so -> libGeoIP.so.1.6.5
lrwxrwxrwx 1 root root 17 Jul 13 16:21 /usr/lib/libGeoIP.so.1 -> libGeoIP.so.1.6.5
-rwxr-xr-x 1 root root 655553 Jul 13 16:21 /usr/lib/libGeoIP.so.1.6.5
$ ll /usr/include/GeoIP*
-rw-r--r-- 1 root root 20586 Jul 13 16:21 /usr/include/GeoIP.h
-rw-r--r-- 1 root root 2460 Jul 13 16:21 /usr/include/GeoIPCity.h
@cosimo thk for your attention.
Hi.
Since Varnish 4, the VMODs need to be changed, since the old sess struct has been split, some data is in vrt_ctx->req, and some is in vrt_vtx->req->sp.
varnishdCommand failed with error code 106
Message from C-compiler:
./vcl.7z2OalIg.c:1258:44: warning: ‘struct sess’ declared inside parameter list [enabled by default]
void vcl_geoip_send_synthetic(const struct sess *sp) {
^
./vcl.7z2OalIg.c:1258:44: warning: its scope is only this definition or declaration, which is probably not what you want [enabled by default]
./vcl.7z2OalIg.c: In function ‘vcl_geoip_send_synthetic’:
./vcl.7z2OalIg.c:1260:5: warning: passing argument 1 of ‘VRT_r_client_ip’ from incompatible pointer type [enabled by default]
vcl_string *ip = VRT_IP_string(sp, VRT_r_client_ip(sp));
^
./vcl.7z2OalIg.c:431:8: note: expected ‘const struct vrt_ctx *’ but argument is of type ‘const struct sess *’
VCL_IP VRT_r_client_ip(const struct vrt_ctx *);
[140 lines truncated]
Running C-compiler failed, exit 1
VCL compilation failed
Error: vcl.load root:dc384c58-0338-411b-8be8-2b306ca075fc /etc/varnish/default.vcl failed
failed!
Would be nice if it periodically checked if the DB has been updated such that it closed and opened the latest DB such that restarts are not required for DB updates.
What will this header look like? What format is it in? What happens when you can only get some information? What happens if the IP is invalid (e.g. a LAN ip)?
Internally, we use "A6" as special fallback code to mean "failed country lookup".
We should implement something like it.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.