diff --git a/Sources/Gatekeeper/Request+Hostname.swift b/Sources/Gatekeeper/Request+Hostname.swift index 491d853..b7bd51f 100644 --- a/Sources/Gatekeeper/Request+Hostname.swift +++ b/Sources/Gatekeeper/Request+Hostname.swift @@ -2,6 +2,6 @@ import Vapor extension Request { var hostname: String? { - headers.first(name: .xForwardedFor) ?? remoteAddress?.hostname + headers.forwarded.first?.for ?? headers.first(name: .xForwardedFor) ?? remoteAddress?.hostname } } diff --git a/Tests/GatekeeperTests/GatekeeperTests.swift b/Tests/GatekeeperTests/GatekeeperTests.swift index 300c898..f4587f3 100644 --- a/Tests/GatekeeperTests/GatekeeperTests.swift +++ b/Tests/GatekeeperTests/GatekeeperTests.swift @@ -37,6 +37,27 @@ class GatekeeperTests: XCTestCase { }) } + func testGateKeeperForwardedSupported() throws { + let app = Application(.testing) + defer { app.shutdown() } + app.gatekeeper.config = .init(maxRequests: 10, per: .second) + + app.grouped(GatekeeperMiddleware()).get("test") { req -> HTTPStatus in + return .ok + } + + try app.test( + .GET, + "test", + beforeRequest: { req in + req.headers.forwarded = [HTTPHeaders.Forwarded(for: "\"[::1]\"")] + }, + afterResponse: { res in + XCTAssertEqual(res.status, .ok) + } + ) + } + func testGateKeeperCountRefresh() throws { let app = Application(.testing) defer { app.shutdown() }