-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathbanned_ip_log.cpp
More file actions
76 lines (63 loc) · 2.75 KB
/
Copy pathbanned_ip_log.cpp
File metadata and controls
76 lines (63 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
This file is part of libhttpserver
Copyright (C) 2011-2026 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
// Demonstrates the solution to issue #332: log every banned-IP rejection.
//
// Configure the daemon with an ACCEPT default policy, block the IPs you
// want denied via ws.block_ip(), and register a connection-level
// `accept_decision` hook that logs every rejection to stderr.
//
// The hook is observation-only: throwing from it does not flip the
// decision (the daemon still rejects the connection per the policy
// callback's return), so it is safe to do I/O here.
//
// Run, then attempt to connect from one of the blocked IPs to see
// stderr show a "[BANNED] ..." line per attempt.
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <httpserver.hpp>
namespace hs = httpserver;
class hello_resource : public hs::http_resource {
public:
hs::http_response render_get(const hs::http_request&) override {
return hs::http_response::string("Hello, World!");
}
};
int main() {
hs::webserver ws{hs::create_webserver(8080)
.default_policy(hs::http::http_utils::ACCEPT)};
// Block whatever client IP you want denied. The shipping example
// uses an RFC-1918 placeholder; swap with your client's address.
ws.block_ip("10.0.0.1");
auto h = ws.add_hook(hs::hook_phase::accept_decision,
std::function<void(const hs::accept_ctx&)>(
[](const hs::accept_ctx& ctx) {
if (!ctx.accepted) {
std::cerr << "[BANNED] peer=" << ctx.peer.to_string()
<< " reason="
<< (ctx.reason.has_value()
? std::string(*ctx.reason)
: std::string("unspecified"))
<< std::endl;
}
}));
auto resource = std::make_shared<hello_resource>();
ws.register_path("/hello", resource);
ws.start(true); // blocking
return 0;
}