Swiftpack.co - Package - nodes-vapor/gatekeeper

Gatekeeper 👮

Swift Version Vapor Version Circle CI codebeat badge codecov Readme Score GitHub license

Gatekeeper is a middleware that restricts the number of requests from clients, based on their IP address. It works by adding the clients IP address to the cache and count how many requests the clients can make during the Gatekeeper's defined lifespan and give back an HTTP 429(Too Many Requests) if the limit has been reached. The number of requests left will be reset when the defined timespan has been reached.

Please take into consideration that multiple clients can be using the same IP address. eg. public wifi

📦 Installation

Update your Package.swift dependencies:

.package(url: "https://github.com/nodes-vapor/gatekeeper.git", from: "3.0.0"),

as well as to your target (e.g. "App"):

targets: [
    .target(name: "App", dependencies: [..., "Gatekeeper", ...]),
// ...
]

Getting started 🚀

Configuration

in configure.swift:

import Gatekeeper

// [...]

// Register providers first
try services.register(
    GatekeeperProvider(
        config: GatekeeperConfig(maxRequests: 10, per: .second),
        cacheFactory: { container -> KeyedCache in
            return try container.make()
        }
    )
)

Add to routes

You can add the GatekeeperMiddleware to specific routes or to all.

Specific routes in routes.swift:

let protectedRoutes = router.grouped(GatekeeperMiddleware.self)
protectedRoutes.get("protected/hello") { req in
    return "Protected Hello, World!"
}

For all requests in configure.swift:

// Register middleware
var middlewares = MiddlewareConfig() // Create _empty_ middleware config
middlewares.use(GatekeeperMiddleware.self)
services.register(middlewares)

Credits 🏆

This package is developed and maintained by the Vapor team at Nodes. The package owner for this project is Christian.

License 📄

This package is open-sourced software licensed under the MIT license

Github

link
Stars: 22
Help us keep the lights on

Dependencies

Releases

3.0.0 - May 6, 2019

Migrated to Vapor 3 🎉

2.0.1 - Dec 28, 2017

Fixed

  • Broken tests.

2.0.0 - Oct 24, 2017

Changed

  • Removed the SSL enforcing functionality to simplify the responsibility of this package (to deal with rate limiting)

1.0.0 - May 30, 2017

  • Vapor 2 released 🎉

0.1.0 - Jan 27, 2017

New:

  • Rate limiter
  • SSLEnforcer