Skip to content

Commit

Permalink
Added a _xff variant of country lookup
Browse files Browse the repository at this point in the history
This allows to consider X-Forwarded-For as the client IP.
Yes, it's limited, as XFF is not parsed, so it doesn't cover
every case.

This is what we need for now :-|
  • Loading branch information
Cosimo Streppone committed Dec 27, 2011
1 parent 44c7080 commit b55b6a7
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions geoip.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#define FALLBACK_COUNTRY "A6"

/* HTTP Header will be json-like */
#define HDR_MAXLEN 255
#define HDR_TMPL "city:%s, country:%s, lat:%f, lon:%f, ip:%s"
#define HEADER_MAXLEN 255
#define HEADER_TMPL "city:%s, country:%s, lat:%f, lon:%f, ip:%s"

pthread_mutex_t geoip_mutex = PTHREAD_MUTEX_INITIALIZER;
GeoIP* gi;
Expand Down Expand Up @@ -76,7 +76,6 @@ static int geoip_lookup(vcl_string *ip, vcl_string *resolved) {
}

static int geoip_lookup_country(vcl_string *ip, vcl_string *resolved) {
int lookup_success = 0;

pthread_mutex_lock(&geoip_mutex);

Expand All @@ -93,7 +92,8 @@ static int geoip_lookup_country(vcl_string *ip, vcl_string *resolved) {

pthread_mutex_unlock(&geoip_mutex);

return lookup_success;
/* Assume always success: we have FALLBACK_COUNTRY */
return 1;
}

#ifdef __VCL__
Expand Down Expand Up @@ -126,11 +126,19 @@ void vcl_geoip_set_header(const struct sess *sp) {
void vcl_geoip_country_set_header(const struct sess *sp) {
vcl_string hval[HDR_MAXLEN];
vcl_string *ip = VRT_IP_string(sp, VRT_r_client_ip(sp));
if (geoip_lookup_country(ip, hval)) {
geoip_lookup_country(ip, hval);
VRT_SetHdr(sp, HDR_REQ, "\011X-Geo-IP:", hval, vrt_magic_string_end);
}

/* XFF version: takes IP from X-Forwarded-For header */
void vcl_geoip_country_set_header_xff(const struct sess *sp) {
vcl_string hval[HEADER_MAXLEN];
vcl_string *ip = VRT_GetHdr(sp, HDR_REQ, "\020X-Forwarded-For:");
if (ip) {
geoip_lookup_country(ip, hval);
VRT_SetHdr(sp, HDR_REQ, "\011X-Geo-IP:", hval, vrt_magic_string_end);
}
else {
/* Send an empty header */
} else {
VCL_Log("geoip: no ip from X-Forwarded-For");
VRT_SetHdr(sp, HDR_REQ, "\011X-Geo-IP:", "", vrt_magic_string_end);
}
}
Expand Down

0 comments on commit b55b6a7

Please sign in to comment.