forked from cosimo/varnish-geoip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoip.c
175 lines (148 loc) · 4.63 KB
/
geoip.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Varnish-powered Geo IP lookup
*
* Idea and GeoIP code taken from
* http://svn.wikia-code.com/utils/varnishhtcpd/wikia.vcl
*
* Cosimo, 28/05/2013
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GeoIPCity.h>
#include <pthread.h>
#define vcl_string char
/* At Opera, we use the non-existent "A6" country
code to identify Geo::IP failed lookups */
#define FALLBACK_COUNTRY "A6"
/* HTTP Header will be json-like */
#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;
GeoIPRecord *record;
/* Init GeoIP code */
void geoip_init () {
if (!gi) {
gi = GeoIP_open_type(GEOIP_CITY_EDITION_REV1,GEOIP_MEMORY_CACHE);
assert(gi);
}
}
static int geoip_lookup(vcl_string *ip, vcl_string *resolved) {
int lookup_success = 0;
pthread_mutex_lock(&geoip_mutex);
if (!gi) {
geoip_init();
}
record = GeoIP_record_by_addr(gi, ip);
/* Lookup succeeded */
if (record) {
lookup_success = 1;
snprintf(resolved, HEADER_MAXLEN, HEADER_TMPL,
record->city,
record->country_code,
record->latitude,
record->longitude,
ip
);
}
/* Failed lookup */
else {
snprintf(resolved, HEADER_MAXLEN, HEADER_TMPL,
"", /* City */
FALLBACK_COUNTRY,
0.0f, /* Latitude */
0.0f, /* Longitude */
ip
);
}
pthread_mutex_unlock(&geoip_mutex);
return lookup_success;
}
static int geoip_lookup_country(vcl_string *ip, vcl_string *resolved) {
pthread_mutex_lock(&geoip_mutex);
if (!gi) {
geoip_init();
}
record = GeoIP_record_by_addr(gi, ip);
snprintf(resolved, HEADER_MAXLEN, "country:%s", record
? record->country_code
: FALLBACK_COUNTRY
);
pthread_mutex_unlock(&geoip_mutex);
/* Assume always success: we have FALLBACK_COUNTRY */
return 1;
}
#ifdef __VCL__
/* Returns the GeoIP info as synthetic response */
void vcl_geoip_send_synthetic(const struct sess *sp) {
vcl_string hval[HEADER_MAXLEN];
vcl_string *ip = VRT_IP_string(sp, VRT_r_client_ip(sp));
if (geoip_lookup(ip, hval)) {
VRT_synth_page(sp, 0, hval, vrt_magic_string_end);
}
else {
VRT_synth_page(sp, 0, "", vrt_magic_string_end);
}
}
/* Sets "X-Geo-IP" header with the geoip resolved information */
void vcl_geoip_set_header(const struct sess *sp) {
vcl_string hval[HEADER_MAXLEN];
vcl_string *ip = VRT_IP_string(sp, VRT_r_client_ip(sp));
if (geoip_lookup(ip, hval)) {
VRT_SetHdr(sp, HDR_REQ, "\011X-Geo-IP:", hval, vrt_magic_string_end);
}
else {
/* Send an empty header */
VRT_SetHdr(sp, HDR_REQ, "\011X-Geo-IP:", "", vrt_magic_string_end);
}
}
/* Sets "X-Geo-IP" header with the geoip resolved information */
/* XFF version: takes IP from X-Forwarded-For header */
void vcl_geoip_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) {
if (geoip_lookup(ip, hval)) {
VRT_SetHdr(sp, HDR_REQ, "\011X-Geo-IP:", hval, vrt_magic_string_end);
}
else {
/* Send an empty header */
VRT_SetHdr(sp, HDR_REQ, "\011X-Geo-IP:", "", vrt_magic_string_end);
}
} else {
VRT_SetHdr(sp, HDR_REQ, "\011X-Geo-IP:", "", vrt_magic_string_end);
}
}
/* Simplified version: sets "X-Geo-IP" header with the country only */
void vcl_geoip_country_set_header(const struct sess *sp) {
vcl_string hval[HEADER_MAXLEN];
vcl_string *ip = VRT_IP_string(sp, VRT_r_client_ip(sp));
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 {
/*VCL_Log("geoip: no ip from X-Forwarded-For");*/
VRT_SetHdr(sp, HDR_REQ, "\011X-Geo-IP:", "", vrt_magic_string_end);
}
}
#else
int main(int argc, char **argv) {
vcl_string resolved[HEADER_MAXLEN] = "";
if (argc == 2 && argv[1]) {
geoip_lookup(argv[1], resolved);
}
printf("%s\n", resolved);
return 0;
}
#endif /* __VCL__ */
/* vim: syn=c ts=4 et sts=4 sw=4 tw=0
*/