Giter VIP home page Giter VIP logo

gatekeeper's Introduction

Gatekeeper 馃懏

Swift Version Vapor Version GitHub license

Gatekeeper is a middleware that restricts the number of requests from clients, based on their IP address (can be customized). It works by adding the clients identifier 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: "4.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

// [...]

app.caches.use(.memory)
app.gatekeeper.config = .init(maxRequests: 10, per: .second)

Add to routes

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

Specific routes in routes.swift:

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

For all requests in configure.swift:

// Register middleware
app.middlewares.use(GatekeeperMiddleware())

Customizing config

By default GatekeeperMiddleware uses app.gatekeeper.config as its configuration. However, you can pass a custom configuration to each GatekeeperMiddleware type via the initializer GatekeeperMiddleware(config:). This allows you to set configuration on a per-route basis.

Key Makers 馃攽

By default Gatekeeper uses the client's hostname (IP address) to identify them. This can cause issues where multiple clients are connected from the same network. Therefore, you can customize how Gatekeeper should identify the client by using the GatekeeperKeyMaker protocol.

GatekeeperHostnameKeyMaker is used by default.

You can configure which key maker Gatekeeper should use in configure.swift:

app.gatekeeper.keyMakers.use(.hostname) // default

Custom key maker

This is an example of a key maker that uses the user's ID to identify them.

struct UserIDKeyMaker: GatekeeperKeyMaker {
    public func make(for req: Request) -> EventLoopFuture<String> {
        let userID = try req.auth.require(User.self).requireID()        
        return req.eventLoop.future("gatekeeper_" + userID.uuidString)
    }
}
extension Application.Gatekeeper.KeyMakers.Provider {
    public static var userID: Self {
        .init { app in
            app.gatekeeper.keyMakers.use { _ in UserIDKeyMaker() }
        }
    }
}

configure.swift:

app.gatekeeper.keyMakers.use(.userID)

Cache 馃梽

Gatekeeper uses the same cache as configured by app.caches.use() from Vapor, by default. Therefore it is important to set up Vapor's cache if you're using this default behaviour. You can use an in-memory cache for Vapor like so:

configure.swift:

app.cache.use(.memory)

Custom cache

You can override which cache to use by creating your own type that conforms to the Cache protocol from Vapor. Use app.gatekeeper.caches.use() to configure which cache to use.

Credits 馃弳

This package is developed and maintained by the Vapor team at Nodes. The package owner for this project is Christian. Special thanks goes to madsodgaard for his work on the Vapor 4 version!

License 馃搫

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

gatekeeper's People

Contributors

brettrtoomey avatar cweinberger avatar madsodgaard avatar mauran avatar needleinajaystack avatar siemensikkema avatar steffendsommer avatar valen90 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gatekeeper's Issues

Check for scheme and X-Forwarded-For

When checking for SSL, it's never a good idea to look for the uri scheme.

Usually proxy servers will send the X-Forwarded-Proto header

So a better solution is first to check the scheme as now, if that's http, then check if X-Forwarded-Proto is https

Gatekeeper fails in testing

After adding Gatekeep to my Vapor app, my test harness fails due to the framework checking if there is a hostname on this line https://github.com/nodes-vapor/gatekeeper/blob/master/Sources/Gatekeeper/Gatekeeper.swift#L20 if there is a hostname attached to the request. XCTest doesn't seem to attach this to any request and thus tests won't pass. I don't seem to be able to add a hostname manually, so maybe a check to see if the env is testing and then manually set the hostname to localhost? I'd be happy to file a pull request if this would be a satisfactory solution

Update documentation

Update README to include how to add the middleware (look at our other packages for inspiration).

Vapor 4

Is gatekeeper Vapor 4 compliant?

Rate limit base on IP and Authentication

It would be a good idea to rate limit based on IP & Authentication if provided that way if many are using the Server from the same building (most likely the same IP address) they won't get rate limited because they are different users from the same IP

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.