From f5a52f62b4bad4bd88e231fec4fe5f8f0404178b Mon Sep 17 00:00:00 2001 From: Jay Herron Date: Tue, 24 Aug 2021 10:25:08 -0600 Subject: [PATCH] =?UTF-8?q?Adds=20hostname=20extraction=20for=20=E2=80=98f?= =?UTF-8?q?orwarded=E2=80=99=20header?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Gatekeeper/Request+Hostname.swift | 2 +- Tests/GatekeeperTests/GatekeeperTests.swift | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) 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() }