mirror of
https://bitbucket.org/King_DuckZ/keepupnpup.git
synced 2024-11-07 21:29:00 +00:00
Add a setting to allow the program to try and delete any pre-existing rule.
This commit is contained in:
parent
9dea5d839e
commit
3c498f794e
4 changed files with 32 additions and 4 deletions
|
@ -6,3 +6,4 @@ redirect:
|
|||
udp:
|
||||
- 49164
|
||||
desc: RoutArm redir
|
||||
autoremove: true
|
||||
|
|
18
src/main.cpp
18
src/main.cpp
|
@ -26,7 +26,8 @@ namespace {
|
|||
enum CurrentMappingType {
|
||||
NoMapping,
|
||||
PresentWithSameIP,
|
||||
PresentWithDifferentIP
|
||||
PresentWithDifferentIP,
|
||||
IgnoreMapping
|
||||
};
|
||||
|
||||
CurrentMappingType has_mapping (const std::vector<kuu::Redirection>& parRedirs, const std::string& parAddr, uint16_t parPort, kuu::Protocol parProtocol) {
|
||||
|
@ -37,7 +38,13 @@ namespace {
|
|||
return NoMapping;
|
||||
}
|
||||
|
||||
void add_bindings (kuu::UPNP& parUpnp, const std::string& parHost, const kuu::RedirectSetting::PortMappingsList& parPortMappings, const std::string& parDesc) {
|
||||
void add_bindings (
|
||||
kuu::UPNP& parUpnp,
|
||||
const std::string& parHost,
|
||||
const kuu::RedirectSetting::PortMappingsList& parPortMappings,
|
||||
const std::string& parDesc,
|
||||
bool parTryForcing
|
||||
) {
|
||||
const std::vector<kuu::Redirection> redirs; // = upnp.redirections();
|
||||
|
||||
for (auto& mapping : parPortMappings) {
|
||||
|
@ -46,8 +53,10 @@ namespace {
|
|||
try {
|
||||
const CurrentMappingType curr_mapping =
|
||||
has_mapping(redirs, parHost, mapping.port, mapping.protocol);
|
||||
const CurrentMappingType decided_mapping =
|
||||
(PresentWithDifferentIP == curr_mapping and not parTryForcing ? IgnoreMapping : curr_mapping);
|
||||
|
||||
switch (curr_mapping) {
|
||||
switch (decided_mapping) {
|
||||
case PresentWithDifferentIP:
|
||||
parUpnp.remove_port_mapping(mapping.port, parHost, mapping.protocol);
|
||||
|
||||
|
@ -56,6 +65,7 @@ namespace {
|
|||
break;
|
||||
|
||||
case PresentWithSameIP:
|
||||
case IgnoreMapping:
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
@ -70,7 +80,7 @@ namespace {
|
|||
|
||||
kuu::RedirectSetting settings = kuu::load_redirect_settings("keepupnpup.yml", upnp.lanaddr());
|
||||
|
||||
add_bindings(upnp, settings.host, settings.port_mappings, settings.desc);
|
||||
add_bindings(upnp, settings.host, settings.port_mappings, settings.desc, settings.autoremove_old);
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include <yaml-cpp/yaml.h>
|
||||
#include <ciso646>
|
||||
#include <stdexcept>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
namespace kuu {
|
||||
namespace {
|
||||
|
@ -41,6 +42,16 @@ namespace kuu {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool to_boolean (const std::string& parValue) {
|
||||
using boost::iequals;
|
||||
|
||||
return iequals(parValue, std::string("on")) or
|
||||
iequals(parValue, std::string("true")) or
|
||||
parValue == "1" or
|
||||
iequals(parValue, std::string("enabled"))
|
||||
;
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
||||
RedirectSetting load_redirect_settings (const std::string& parPath, const std::string& parLanAddr) {
|
||||
|
@ -71,6 +82,11 @@ namespace kuu {
|
|||
redir_settings.desc = redirect_node["desc"].as<std::string>();
|
||||
}
|
||||
|
||||
if (redirect_node["autoremove"])
|
||||
redir_settings.autoremove_old = to_boolean(redirect_node["autoremove"].as<std::string>());
|
||||
else
|
||||
redir_settings.autoremove_old = false;
|
||||
|
||||
return redir_settings;
|
||||
}
|
||||
} //namespace kuu
|
||||
|
|
|
@ -41,6 +41,7 @@ namespace kuu {
|
|||
std::string host;
|
||||
std::string desc;
|
||||
PortMappingsList port_mappings;
|
||||
bool autoremove_old;
|
||||
};
|
||||
|
||||
RedirectSetting load_redirect_settings (const std::string& parPath, const std::string& parLanAddr);
|
||||
|
|
Loading…
Reference in a new issue