-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvmod_ip2proxy.c
More file actions
108 lines (96 loc) · 2.84 KB
/
Copy pathvmod_ip2proxy.c
File metadata and controls
108 lines (96 loc) · 2.84 KB
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
/*
* IP2Proxy Varnish library is distributed under LGPL version 3
* Copyright (c) 2013-2020 IP2Proxy.com. support at ip2location dot com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not see <http://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <string.h>
#include <IP2Proxy.h>
#include "cache/cache.h"
static void
ip2proxy_free(VRT_CTX, void *ptr)
{
IP2Proxy_close((IP2Proxy *)ptr);
}
static const struct vmod_priv_methods ip2p_methods[1] = {{
.magic = VMOD_PRIV_METHODS_MAGIC,
.type = "vmod_std_ip2proxy",
.fini = ip2proxy_free
}};
VCL_VOID
vmod_init_db(VRT_CTX, struct vmod_priv *priv, char *filename, char *memtype)
{
IP2Proxy *IP2ProxyObj;
enum IP2Proxy_lookup_mode mtype;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
AN(priv);
AN(memtype);
if (strcmp(memtype, "IP2PROXY_FILE_IO") == 0)
mtype = IP2PROXY_FILE_IO;
else if (strcmp(memtype, "IP2PROXY_SHARED_MEMORY") == 0)
mtype = IP2PROXY_SHARED_MEMORY;
else if (strcmp(memtype, "IP2PROXY_CACHE_MEMORY") == 0)
mtype = IP2PROXY_CACHE_MEMORY;
else {
VRT_fail(ctx, "IP2Proxy: invalid memtype (%s)", memtype);
return;
}
if (priv->priv != NULL)
IP2Proxy_close((IP2Proxy *)priv->priv);
IP2ProxyObj = IP2Proxy_open(filename);
if (!IP2ProxyObj) {
VRT_fail(ctx, "IP2Proxy: can't open database (%s)", filename);
return;
}
IP2Proxy_open_mem(IP2ProxyObj, mtype);
priv->priv = IP2ProxyObj;
priv->methods = ip2p_methods;
}
#define FUNC(lower, field) \
VCL_STRING \
vmod_ ## lower(VRT_CTX, struct vmod_priv *priv, char * ip) \
{ \
char *result; \
IP2ProxyRecord *r; \
\
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); \
AN(priv); \
\
if (!ip || !priv->priv) \
return ("-"); \
\
r = IP2Proxy_get_all((IP2Proxy *)priv->priv, ip); \
if (!r) \
return ("-"); \
\
result = WS_Copy(ctx->ws, r->field, -1); \
IP2Proxy_free_record(r); \
\
return (result); \
}
FUNC(country_short, country_short)
FUNC(country_long, country_long)
FUNC(region, region)
FUNC(city, city)
FUNC(isp, isp)
FUNC(domain, domain)
FUNC(usage_type, usage_type)
FUNC(proxy_type, proxy_type)
FUNC(asn, asn)
FUNC(as, as_)
FUNC(last_seen, last_seen)
FUNC(is_proxy, is_proxy)
FUNC(threat, threat)