Skip to content

Commit bd612a6

Browse files
author
Sebastiano Merlino
committed
Added partial support to ip ban
Added methods to unban ips and to unregister resources
1 parent e46559a commit bd612a6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Webserver.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,21 @@ void Webserver::registerResource(const string& resource, HttpResource* http_reso
400400
this->registeredResources[HttpEndpoint(resource, family, true)] = http_resource;
401401
}
402402

403+
void Webserver::unregisterResource(const string& resource)
404+
{
405+
this->registeredResources.erase(HttpEndpoint(resource));
406+
}
407+
408+
void Webserver::banIp(const string& ip)
409+
{
410+
this->bans.insert(ip);
411+
}
412+
413+
void Webserver::unbanIp(const string& ip)
414+
{
415+
this->bans.erase(ip);
416+
}
417+
403418
int Webserver::buildRequestHeader (void *cls, enum MHD_ValueKind kind, const char *key, const char *value)
404419
{
405420
HttpRequest* dhr = (HttpRequest*)(cls);
@@ -431,7 +446,11 @@ int Webserver::buildRequestArgs (void *cls, enum MHD_ValueKind kind, const char
431446

432447
int policyCallback (void *cls, const struct sockaddr* addr, socklen_t addrlen)
433448
{
434-
// TODO: Develop a system that allow to study a configurable policy callback
449+
if(((Webserver*)cls)->bans.count(get_ip_str(addr, addrlen)))
450+
return MHD_NO;
451+
#ifdef DEBUG
452+
cout << get_ip_str(addr, addrlen) << endl;
453+
#endif
435454
return MHD_YES;
436455
}
437456

src/httpserver/Webserver.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <cstring>
3737
#include <map>
3838
#include <vector>
39+
#include <set>
3940
#include <string>
4041
#include <utility>
4142
#include <memory>
@@ -204,6 +205,9 @@ class Webserver
204205
* @param family boolean indicating whether the resource is registered for the endpoint and its child or not.
205206
**/
206207
void registerResource(const std::string& resource, HttpResource* http_resource, bool family = false);
208+
void unregisterResource(const std::string& resource);
209+
void banIp(const std::string& ip);
210+
void unbanIp(const std::string& ip);
207211
/**
208212
* Method used to kill the webserver waiting for it to terminate
209213
**/
@@ -236,6 +240,7 @@ class Webserver
236240
bool running;
237241

238242
std::map<HttpEndpoint, HttpResource* > registeredResources;
243+
std::set<std::string> bans;
239244
struct MHD_Daemon *daemon;
240245
static int not_found_page
241246
(

0 commit comments

Comments
 (0)