campaignd: Allow exempting hostmasks from download count bumps

This commit is contained in:
Ignacio R. Morelle 2017-10-24 02:53:35 -03:00
parent ae6ed8e713
commit 4f225f56e0
2 changed files with 21 additions and 1 deletions

View file

@ -111,6 +111,7 @@ server::server(const std::string& cfg_file)
, feedback_url_format_()
, blacklist_()
, blacklist_file_()
, stats_exempt_ips_()
, flush_timer_(io_service_)
{
@ -177,6 +178,8 @@ void server::load_config()
blacklist_file_ = cfg_["blacklist_file"].str();
load_blacklist();
stats_exempt_ips_ = utils::split(cfg_["stats_exempt_ips"].str());
// Load any configured hooks.
hooks_.emplace(std::string("hook_post_upload"), cfg_["hook_post_upload"]);
hooks_.emplace(std::string("hook_post_erase"), cfg_["hook_post_erase"]);
@ -422,6 +425,18 @@ void server::fire(const std::string& hook, const std::string& addon)
#endif
}
bool server::ignore_address_stats(const std::string& addr) const
{
for(const auto& mask : stats_exempt_ips_) {
// TODO: we want CIDR subnet mask matching here, not glob matching!
if(utils::wildcard_string_match(addr, mask)) {
return true;
}
}
return false;
}
void server::send_message(const std::string& msg, socket_ptr sock)
{
simple_wml::document doc;
@ -580,7 +595,7 @@ void server::handle_request_campaign(const server::request& req)
// Clients doing upgrades or some other specific thing shouldn't bump
// the downloads count. Default to true for compatibility with old
// clients that won't tell us what they are trying to do.
if(req.cfg["increase_downloads"].to_bool(true)) {
if(req.cfg["increase_downloads"].to_bool(true) && !ignore_address_stats(req.addr)) {
const int downloads = campaign["downloads"].to_int() + 1;
campaign["downloads"] = downloads;
}

View file

@ -95,6 +95,8 @@ private:
blacklist blacklist_;
std::string blacklist_file_;
std::vector<std::string> stats_exempt_ips_;
boost::asio::basic_waitable_timer<std::chrono::steady_clock> flush_timer_;
void handle_new_client(socket_ptr socket);
@ -147,6 +149,9 @@ private:
/** Retrieves the contents of the [server_info] WML node. */
config& server_info() { return cfg_.child("server_info"); }
/** Checks if the specified address should never bump download counts. */
bool ignore_address_stats(const std::string& addr) const;
//
// Request handling.
//