forked from cosimo/varnish-geoip
/
geoip.vcl
56 lines (46 loc) · 1.15 KB
/
geoip.vcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
C{
/*
* Varnish-powered Geo IP lookup
* Simplification made for Geo IP Country.
*
* Usage:
*
*
* Guy Bedford 14/05/2012, from the below version::
*
* ---
* https://github.com/cosimo/varnish-geoip
*
* Varnish-powered Geo IP lookup
*
* Idea and GeoIP code taken from
* http://svn.wikia-code.com/utils/varnishhtcpd/wikia.vcl
*
* Cosimo, 01/12/2011
* ---
*/
#include <stdlib.h>
#include <GeoIP.h>
#include <pthread.h>
pthread_mutex_t geoip_mutex = PTHREAD_MUTEX_INITIALIZER;
GeoIP* gi;
/* Init GeoIP code */
void geoip_init () {
if (!gi) {
//UPDATE THE PATH BELOW TO THE GEOIP FOLDER
gi = GeoIP_open("/var/GeoIP-1.4.8/data/GeoIP.dat", GEOIP_MEMORY_CACHE);
}
}
/* Simplified version: sets "X-Geo-IP" header with the country only */
void vcl_geoip_country_set_header(const struct sess *sp) {
pthread_mutex_lock(&geoip_mutex);
if (!gi) {
geoip_init();
}
char *ip = VRT_IP_string(sp, VRT_r_client_ip(sp));
char buff[2];
sprintf(buff, "%s", GeoIP_country_code_by_addr(gi, ip));
pthread_mutex_unlock(&geoip_mutex);
VRT_SetHdr(sp, HDR_REQ, "\011X-Geo-IP:", buff, vrt_magic_string_end);
}
}C