1
0
Fork 0
mirror of https://bitbucket.org/King_DuckZ/keepupnpup.git synced 2024-11-07 21:29:00 +00:00

Add an option to delete redirections to current host if they are not in the yaml

This commit is contained in:
King_DuckZ 2016-09-21 01:28:37 +02:00
parent 5c4d505205
commit 560b27b1bf
4 changed files with 41 additions and 4 deletions

View file

@ -7,3 +7,4 @@ redirect:
- 49164 - 49164
desc: RoutArm redir desc: RoutArm redir
autoremove: true autoremove: true
dropunused: true

View file

@ -21,6 +21,8 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <ciso646> #include <ciso646>
#include <boost/range/adaptor/filtered.hpp>
#include <algorithm>
namespace { namespace {
enum CurrentMappingType { enum CurrentMappingType {
@ -38,21 +40,47 @@ namespace {
return NoMapping; return NoMapping;
} }
void remove_unused_bindings(
kuu::UPNP& parUpnp,
const std::vector<kuu::Redirection>& parActiveRedirs,
const std::string& parHost,
const kuu::RedirectSetting::PortMappingsList& parPortMappings
) {
using boost::adaptors::filtered;
auto redirs_to_host = parActiveRedirs |
filtered([&](const kuu::Redirection& r) { return parHost == r.remote_host; });
for (const auto& redir : redirs_to_host) {
auto it_found = std::find_if(
parPortMappings.begin(),
parPortMappings.end(),
[&](const kuu::PortMapping& m) {
return redir.protocol == m.protocol and
redir.external_port == m.port and
redir.internal_port == m.internal_port;
}
);
if (it_found == parPortMappings.end()) {
parUpnp.remove_port_mapping(it_found->port, parHost, it_found->protocol);
}
}
}
void add_bindings ( void add_bindings (
kuu::UPNP& parUpnp, kuu::UPNP& parUpnp,
const std::vector<kuu::Redirection>& parActiveRedirs,
const std::string& parHost, const std::string& parHost,
const kuu::RedirectSetting::PortMappingsList& parPortMappings, const kuu::RedirectSetting::PortMappingsList& parPortMappings,
const std::string& parDesc, const std::string& parDesc,
bool parTryForcing bool parTryForcing
) { ) {
const std::vector<kuu::Redirection> redirs = parUpnp.redirections();
for (auto& mapping : parPortMappings) { for (auto& mapping : parPortMappings) {
//std::cout << "Adding " << mapping.port << " --> " << parHost << ":" << mapping.internal_port << " " << mapping.protocol << '\n'; //std::cout << "Adding " << mapping.port << " --> " << parHost << ":" << mapping.internal_port << " " << mapping.protocol << '\n';
bool try_add_mapping = true; bool try_add_mapping = true;
try { try {
const CurrentMappingType curr_mapping = const CurrentMappingType curr_mapping =
has_mapping(redirs, parHost, mapping.port, mapping.protocol); has_mapping(parActiveRedirs, parHost, mapping.port, mapping.protocol);
const CurrentMappingType decided_mapping = const CurrentMappingType decided_mapping =
(PresentWithDifferentIP == curr_mapping and not parTryForcing ? IgnoreMapping : curr_mapping); (PresentWithDifferentIP == curr_mapping and not parTryForcing ? IgnoreMapping : curr_mapping);
@ -79,8 +107,11 @@ namespace {
kuu::UPNP upnp; kuu::UPNP upnp;
kuu::RedirectSetting settings = kuu::load_redirect_settings("keepupnpup.yml", upnp.lanaddr()); kuu::RedirectSetting settings = kuu::load_redirect_settings("keepupnpup.yml", upnp.lanaddr());
const std::vector<kuu::Redirection> redirs = upnp.redirections();
add_bindings(upnp, settings.host, settings.port_mappings, settings.desc, settings.autoremove_old); add_bindings(upnp, redirs, settings.host, settings.port_mappings, settings.desc, settings.autoremove_old);
if (settings.drop_unused)
remove_unused_bindings(upnp, redirs, settings.host, settings.port_mappings);
} }
} //unnamed namespace } //unnamed namespace

View file

@ -86,6 +86,10 @@ namespace kuu {
redir_settings.autoremove_old = to_boolean(redirect_node["autoremove"].as<std::string>()); redir_settings.autoremove_old = to_boolean(redirect_node["autoremove"].as<std::string>());
else else
redir_settings.autoremove_old = false; redir_settings.autoremove_old = false;
if (redirect_node["dropunused"])
redir_settings.drop_unused = to_boolean(redirect_node["dropunused"].as<std::string>());
else
redir_settings.drop_unused = false;
return redir_settings; return redir_settings;
} }

View file

@ -42,6 +42,7 @@ namespace kuu {
std::string desc; std::string desc;
PortMappingsList port_mappings; PortMappingsList port_mappings;
bool autoremove_old; bool autoremove_old;
bool drop_unused;
}; };
RedirectSetting load_redirect_settings (const std::string& parPath, const std::string& parLanAddr); RedirectSetting load_redirect_settings (const std::string& parPath, const std::string& parLanAddr);