Giter Site home page Giter Site logo

libvmod-keystore's Introduction

Available drivers

  • redis
  • memcached

Prerequisites

How to build

With embedded drivers (statically linked)

cd /path/to/libvmod-keystore
cmake .

(if hiredis is found, redis driver will automatically be enabled ; same thing with libmemcached for memcached driver)

In top of your Varnish configuration, add:

import keystore;

With drivers as separate vmods

First build vmod_keystore alone:

cd /path/to/libvmod-keystore
cmake . -DWITH_REDIS:BOOL=OFF -DWITH_MEMCACHED:BOOL=OFF

Then build your driver(s). Example for redis:

cd /path/to/libvmod-keystore/drivers/redis
cmake .

To load redis driver, your varnish configuration should be similar to:

import keystore;
import keystore_redis; # add this line AFTER keystore

VMod methods

Instanciate a connection to your database:

new <variable name> = keystore.driver("<driver name>:host=<IP address or hostname or path to socket>;port=<port>;timeout=<timeout>");
  • STRING get(STRING key): fetch current value associated to key
  • BOOL add(STRING key, STRING value): add the given key if it does not already exist (returns FALSE if it already exists)
  • VOID set(STRING key, STRING value): add or replace (overwrites) the value associated to key
  • BOOL exists(STRING key): does key exist?
  • BOOL delete(STRING key): delete key
  • VOID expire(STRING key, DURATION ttl): set expiration of the given key (keys are inserted as persitent with 0 as TTL ; use 30s as value of ttl, for the key to expire in 30 seconds)
  • INT increment(STRING key): return value associated to key after incrementing it (of 1)
  • INT decrement(STRING key): return value associated to key after decrementing it (of 1)
  • STRING name() : return current driver name
  • STRING raw(STRING command) : execute an arbtrary command (redis only)

Examples

Prevent brute-force on http authentication

import std;
import keystore;
#import keystore_redis; # if compiled as a separate VMOD

sub vcl_init {
    new ipstore = keystore.driver("redis:host=localhost;port=6379");
}

sub vcl_recv {
    if (std.integer(ipstore.get("" + client.identity), 0) >= 5) {
        return(synth(403));
    }
    # ...
}

sub vcl_deliver {
    # ...
    if (401 == resp.status && req.http.Authorization) {
        if (ipstore.increment("" + client.identity) >= 5) {
            ipstore.expire("" + client.identity, 4h); # ban for 4 hours
            return(synth(429)); # return synth in vcl_deliver requires Varnish >= 4.0.2
        } else {
            ipstore.expire("" + client.identity, 1h); # reset attempts count after 1h
        }
    }
    # ...
}

libvmod-keystore's People

Contributors

julp avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.