Yesterday, we have been down for 30 minutes because one of our memcache server has been shutdown during a maintenance. The anti-scraping system was not protected against an error like this. When developing, we done a test with the memcache process halted but not the memcache server itself. (unpluged of lan) And it is not the same error for libmemcached.
Furthermore, the 0.39 version of libmemcached has a bug for some cases where connect would have issues with timeout.
We have deployed libmemcached 0.43 and we have done few adjustments in our code:
//Connect timeout 10ms
rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, 10);
if (rc != MEMCACHED_SUCCESS) syslog(LOG_INFO, “Memcached error: %s”, memcached_strerror(memc,rc) );
# Copyright (c) 2010 OUESTFRANCE-MULTIMEDIA
# All rights reserved.
#
# Author: Vincent ROBERT <v.robert@of2m.fr>
# Thanks to: Tony FOUCHARD <t.fouchard@of2m.fr>
# Thanks to: Cyrille MAHIEUX <c.mahieux@of2m.fr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
C{
#define HOSTNAME_MAX_SIZE 200
#include <string.h>
#include <libmemcached/memcached.h>
#include <syslog.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
}C
sub check_ip {
set bereq.http.whitelisted = "0";
if (bereq.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|js|css|swf|xml|flv)(\?(.)*)?$") {
set bereq.http.whitelisted = "1";
}
else {
if (bereq.http.host == "internalwebSite") {
set bereq.http.whitelisted = "1";
}
elsif ((bereq.http.X-Forwarded-For == "IPaddress1") || (bereq.http.X-Forwarded-For == "IPaddress2")) {
set bereq.http.whitelisted = "1";
}
}
if (bereq.http.whitelisted=="0") {
C{
void ip_to_hostname(char *ipaddr, char *hostname)
{
struct addrinfo * result;
int successfull=0;
int error;
error = getaddrinfo(ipaddr, NULL, NULL, &result);
if (error==0) {
error = getnameinfo(result->ai_addr, result->ai_addrlen, hostname, HOSTNAME_MAX_SIZE, NULL, 0, 0);
if (error==0) {
successfull=1;
}
}
freeaddrinfo(result);
if (!successfull){
memcpy(hostname, ipaddr, HOSTNAME_MAX_SIZE*sizeof(char));
hostname[HOSTNAME_MAX_SIZE-1]=0;
}
}
memcached_server_st *servers = NULL;
memcached_st *memc;
memcached_return rc;
char hostname[HOSTNAME_MAX_SIZE]="";
char key[500]= "varnish_";
strcat(key, VRT_GetHdr(sp, HDR_REQ, "\005host:"));
strcat(key, "_");
strcat(key, VRT_IP_string(sp, VRT_r_client_ip(sp)));
memc= memcached_create(NULL);
servers= memcached_server_list_append(servers, "memcacheServer", 11211, &rc);
rc= memcached_server_push(memc, servers);
if (rc == MEMCACHED_SUCCESS) {
//Connect timeout 10ms
rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, 10);
uint64_t intval;
rc= memcached_increment(memc, key, strlen(key), (uint64_t)1, &intval);
if (rc != MEMCACHED_SUCCESS) {
rc= memcached_set(memc, key, strlen(key), "1", 1, (time_t)60, (uint32_t)0);
if (rc != MEMCACHED_SUCCESS) syslog(LOG_INFO, "Memcached error: %s", memcached_strerror(memc,rc) );
}
else
if (intval>30) {
ip_to_hostname(VRT_IP_string(sp, VRT_r_client_ip(sp)), hostname);
VRT_SetHdr(sp, HDR_BEREQ, "\013X-Scraping:", "1", vrt_magic_string_end);
VRT_SetHdr(sp, HDR_BEREQ, "\013X-Hostname:", hostname, vrt_magic_string_end);
VRT_SetHdr(sp, HDR_BEREQ, "\013X-HostOrig:", VRT_GetHdr(sp, HDR_REQ, "\005host:"), vrt_magic_string_end);
if (intval<300) {
rc= memcached_set(memc, key, strlen(key), "500", 3, (time_t)600, (uint32_t)0);
if (rc != MEMCACHED_SUCCESS) syslog(LOG_INFO, "Memcached error: %s", memcached_strerror(memc,rc) );
}
}
}
memcached_free(memc);
memcached_server_list_free(servers);
}C
if (bereq.http.X-Scraping=="1") {
if (!(bereq.http.X-Hostname ~ "(\.(yahoo\.(net|com))|(exabot\.com)|(googlebot\.com)|(search\.msn\.com))$")) {
C{
syslog(LOG_INFO, "Scraping detected from %s %s on %s with %s",VRT_IP_string(sp, VRT_r_client_ip(sp)), VRT_GetHdr(sp, HDR_BEREQ, "\013X-Hostname:"), VRT_GetHdr(sp, HDR_REQ, "\005host:"), VRT_GetHdr(sp, HDR_REQ, "\013User-Agent:"));
}C
}
}
}
}
sub vcl_miss {
call check_ip;
fetch;
}
sub vcl_pipe {
call check_ip;
pipe;
}
sub vcl_pass {
call check_ip;
pass;
}