aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTimo Teräs <timo.teras@iki.fi>2013-11-14 15:35:43 +0200
committerNatanael Copa <ncopa@alpinelinux.org>2013-11-14 14:49:02 +0000
commit03c9ccc812c60bad951f7b44d26bc5abfabbf001 (patch)
treed3fd599802e64886397ee4e7e928032eea42e542
parent073d94a37924574226a80ce08ed55a5bce6af6dd (diff)
downloadaports-03c9ccc812c60bad951f7b44d26bc5abfabbf001.tar.bz2
aports-03c9ccc812c60bad951f7b44d26bc5abfabbf001.tar.xz
main/varnish: add plugin to do maxmind geoip lookups
(cherry picked from commit d32344d735b978fb7d30a9fcc4ad1141879c5433)
-rw-r--r--main/varnish/APKBUILD27
-rw-r--r--main/varnish/maxminddb.vcl76
-rw-r--r--main/varnish/varnishd.confd3
-rw-r--r--main/varnish/varnishd.initd6
4 files changed, 102 insertions, 10 deletions
diff --git a/main/varnish/APKBUILD b/main/varnish/APKBUILD
index 8e000a3d2a..b5b556898b 100644
--- a/main/varnish/APKBUILD
+++ b/main/varnish/APKBUILD
@@ -3,7 +3,7 @@
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=varnish
pkgver=3.0.4
-pkgrel=1
+pkgrel=2
pkgdesc="High-performance HTTP accelerator"
url="http://www.varnish-cache.org/"
arch="all"
@@ -12,13 +12,14 @@ depends="gcc libc-dev"
depends_dev=
makedepends="$depends_dev pcre-dev ncurses-dev readline-dev"
install="varnish.pre-install"
-subpackages="$pkgname-dev $pkgname-doc $pkgname-libs"
+subpackages="$pkgname-dev $pkgname-doc $pkgname-libs $pkgname-geoip"
pkgusers="varnish"
pkggroups="varnish"
source="http://repo.varnish-cache.org/source/varnish-$pkgver.tar.gz
varnishd.initd
varnishd.confd
varnishd.logrotate
+ maxminddb.vcl
varnish-3.0.3-pthread-uclibc.patch"
_builddir="$srcdir"/varnish-$pkgver
@@ -70,18 +71,28 @@ libs() {
mv "$pkgdir"/usr/lib/*.so.* "$subpkgdir"/usr/lib/
}
+geoip() {
+ pkgdesc="Maxmind GeoIP2 lookup plugin for varnish"
+ depends="libmaxminddb-dev"
+
+ install -m755 -D "$srcdir"/maxminddb.vcl "$subpkgdir"/usr/lib/varnish/plugins/maxminddb.vcl
+}
+
md5sums="a130ce9c3504b9603a46542635e18701 varnish-3.0.4.tar.gz
-5aa262fc6da61ded082393fe10dbad2a varnishd.initd
-0482f094e6d8ca59adb5b031d6c56b08 varnishd.confd
+d470dd6f893c1848c2d3f08ccb63d0d7 varnishd.initd
+df8e6eb5591be4ab916ef4504b3ea05d varnishd.confd
a6cb8a43c9465699cf956dc992998225 varnishd.logrotate
+e0fe20e65072a3c3aca5d5455b65a216 maxminddb.vcl
8c5532869bea7f668e2ce5d4585359f8 varnish-3.0.3-pthread-uclibc.patch"
sha256sums="4e044ccb5c76222bddf89c808f13ea8f66977972675aecb48a920f6800ad0f79 varnish-3.0.4.tar.gz
-8e75973cc20a462988171adab5550a2b9dfa4ec2887312074aaf2a411846f7cd varnishd.initd
-f3bc8ba994085d459b83422101d3966d13212e96aba02df0c9bbb2118819090c varnishd.confd
+1f6473f3493a1ce9952218c96614e246445af17059fe06503f6532d5f5d65e3e varnishd.initd
+24a9938a1d410579afd86507d645b4a27c36c99b3e73d966cdef15bd7f3c74ed varnishd.confd
017173cb42bb60f853063b7fbc843120c547e501233ce2299e1066b5d81e4d5e varnishd.logrotate
+bdec255d444250c41ec899b21825c8d10785ea4bbc1dbebb5b9b4583a8d36eaa maxminddb.vcl
3e39323ff4d741a9a0337e84eee7ce9f8b5419722a3b905f09ccf2486a9ff24f varnish-3.0.3-pthread-uclibc.patch"
sha512sums="d6fa8d320050f659f341af20b24c4401609ffd77207a7889b98e5378c507db04eda310e2b953b5144d748a0b3c08884e87d5c36962f0a38f03014708ff7c78d0 varnish-3.0.4.tar.gz
-b48897b24e452a6e36fac25ebdd1ac272cb0a7f9c5b6ab895daa77777e8be6f8f4ed7555a06046daa4dc149e85f8129758367bf630cbb0b8c73f98629d8e07a8 varnishd.initd
-ba17b04682fc30ebf736647d248ce4db11dc53434c93344a74c605f516799c5d7ad01a9a358cc707fdf5be17f5e96fd4a91f68641f24f2edb82f40b9675b11a1 varnishd.confd
+595b017bf890eaf6ffb9a35eef04983a85a1d90ddbfca835b4a003a7e78e1cf38d9910048fca5bb6a518e3480d6f1e23d1c3be1c1a47e7aafeac9b864c08e7c5 varnishd.initd
+fd3d7e6fb8d0849533c0ad1ec0ba1d5221ac03bd2093052b72cd09f2276b16b888a145402507d18f4defd4ec4fd9b198ef5f87347d92e5fca43add30bb91956e varnishd.confd
8fb1cba86ede5eff28a494f6b1da1a651d66383cdeb63922104407f28903dea0c643155b6d7ac8353b8c63d480a6c5b43a70c7252bc51ee73317c33a1844c52c varnishd.logrotate
+166fe752710a62511d277f6c80ad2b7938d1e481cdf62d4af1064804099b9699d9c041135a7fb63f9eb43155348a1403d3dd50b32ef5b1057def6ffe460f9b12 maxminddb.vcl
c06cd005ba04725ec69a9b5cb138fe2487bbd0b0e0aa4c6a39314b6473b160874d0c8452fca466e4efa907a998752e65940288a65a33b88cc4f206f1180b68d8 varnish-3.0.3-pthread-uclibc.patch"
diff --git a/main/varnish/maxminddb.vcl b/main/varnish/maxminddb.vcl
new file mode 100644
index 0000000000..b1cd732232
--- /dev/null
+++ b/main/varnish/maxminddb.vcl
@@ -0,0 +1,76 @@
+# Maxmind GeoIP2 API bindings
+# Copyright (c) 2013 Timo Teräs
+# GPLv2 applies
+
+# Use from your VCL by adding to your vcl_recv():
+# call maxminddb_lookup;
+
+# You also need to modify the cc_command parameter to include
+# Maxmind database library (-lmaxminddb). In Alpine Linux this is
+# done by uncomminting the relevant VARNISHD_PLUGIN_CFLAGS line in
+# /etc/conf.d/varnishd.
+
+C{
+
+#include <maxminddb.h>
+#include <string.h>
+
+static MMDB_s mmdb;
+
+static void __attribute__((constructor)) __geoip_on_load(void)
+{
+ if (MMDB_open("/var/lib/libmaxminddb/GeoLite2-City.mmdb", MMDB_MODE_MMAP, &mmdb) != MMDB_SUCCESS)
+ mmdb.filename = NULL;
+}
+
+static void __attribute__((destructor)) __geoip_on_unload(void)
+{
+ if (mmdb.filename != NULL)
+ MMDB_close(&mmdb);
+}
+
+static inline void __geoip_set_header(struct sess *sp, const char *hdr, MMDB_lookup_result_s *res, char **path)
+{
+ MMDB_entry_data_s entry;
+ char buf[64];
+ int s;
+
+ s = MMDB_aget_value(&res->entry, &entry, path);
+ if (s != MMDB_SUCCESS || !entry.has_data)
+ return;
+
+ if (entry.type != MMDB_DATA_TYPE_UTF8_STRING)
+ return;
+
+ if (entry.data_size+1 >= sizeof(buf))
+ return;
+
+ memcpy(buf, entry.utf8_string, entry.data_size);
+ buf[entry.data_size] = 0;
+
+ VRT_SetHdr(sp, HDR_REQ, hdr, buf, vrt_magic_string_end);
+}
+
+static void __geoip_set_headers(struct sess *sp)
+{
+ static const char *continent_path[] = { "continent", "code", NULL };
+ static const char *country_path[] = { "country", "iso_code", NULL };
+ MMDB_lookup_result_s res;
+ int mmdb_error;
+
+ if (mmdb.filename == NULL)
+ return;
+
+ res = MMDB_lookup_sockaddr(&mmdb, (struct sockaddr*) VRT_r_client_ip(sp), &mmdb_error);
+ if (mmdb_error == MMDB_SUCCESS) {
+ __geoip_set_header(sp, "\022X-GeoIP-Continent:", &res, continent_path);
+ __geoip_set_header(sp, "\020X-GeoIP-Country:", &res, country_path);
+ }
+}
+
+}C
+
+sub maxminddb_lookup {
+ C{ __geoip_set_headers(sp); }C
+ return(lookup);
+}
diff --git a/main/varnish/varnishd.confd b/main/varnish/varnishd.confd
index 4d72cb7f96..354247fc74 100644
--- a/main/varnish/varnishd.confd
+++ b/main/varnish/varnishd.confd
@@ -19,6 +19,9 @@ VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"
# Varnish admin
ADMINHOSTPORT="127.0.0.1:65080"
+# If maxminddb.vcl is included, this adds the proper library support
+# VARNISHD_PLUGIN_CFLAGS="-lmaxminddb"
+
# options passed to varnish on startup
# please see the varnishd man page for more options
VARNISHD_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_PORT} -s ${VARNISH_STORAGE} -T $ADMINHOSTPORT"
diff --git a/main/varnish/varnishd.initd b/main/varnish/varnishd.initd
index 2b9dec79bc..7c39a03964 100644
--- a/main/varnish/varnishd.initd
+++ b/main/varnish/varnishd.initd
@@ -17,6 +17,7 @@ fi
default_conf="${instance:-default}.vcl"
: ${VARNISH_CONF:=${CFG_FILE:-"/etc/varnish/$default_conf"}}
: ${VARNISHD_PID_FILE:="/var/run/varnish/${SVCNAME}.pid"}
+: ${VARNISHD_CC_COMMAND:="exec cc -fpic -shared -Wl,-x ${VARNISHD_PLUGIN_CFLAGS} -o %o %s"}
: ${VARNISHNCSA_PID_FILE:="/var/run/varnish/varnishncsa${instance:+.}${instance}.pid"}
@@ -26,14 +27,14 @@ depend() {
}
checkconfig() {
- if ! $command -C -f ${VARNISH_CONF} >/dev/null 2>&1; then
+ if ! $command -C -f ${VARNISH_CONF} -p "cc_command=${VARNISHD_CC_COMMAND}" >/dev/null 2>&1; then
error "$SVCNAME has deteced an error in your setup:"
$command -C -f ${VARNISH_CONF}
fi
}
configtest() {
- ebegin "Checkinf ${SVCNAME} configuration"
+ ebegin "Checking ${SVCNAME} configuration"
checkconfig
eend $?
}
@@ -52,6 +53,7 @@ start() {
-f ${VARNISH_CONF} \
-P ${VARNISHD_PID_FILE} \
-u varnish -g varnish \
+ -p "cc_command=${VARNISHD_CC_COMMAND}" \
$instance_opt \
${VARNISHD_OPTS} &> /dev/null
eend $?