mirror of
https://bitbucket.org/King_DuckZ/keepupnpup.git
synced 2025-01-19 21:36:36 +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:
|
udp:
|
||||||
- 49164
|
- 49164
|
||||||
desc: RoutArm redir
|
desc: RoutArm redir
|
||||||
|
autoremove: true
|
||||||
|
|
18
src/main.cpp
18
src/main.cpp
|
@ -26,7 +26,8 @@ namespace {
|
||||||
enum CurrentMappingType {
|
enum CurrentMappingType {
|
||||||
NoMapping,
|
NoMapping,
|
||||||
PresentWithSameIP,
|
PresentWithSameIP,
|
||||||
PresentWithDifferentIP
|
PresentWithDifferentIP,
|
||||||
|
IgnoreMapping
|
||||||
};
|
};
|
||||||
|
|
||||||
CurrentMappingType has_mapping (const std::vector<kuu::Redirection>& parRedirs, const std::string& parAddr, uint16_t parPort, kuu::Protocol parProtocol) {
|
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;
|
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();
|
const std::vector<kuu::Redirection> redirs; // = upnp.redirections();
|
||||||
|
|
||||||
for (auto& mapping : parPortMappings) {
|
for (auto& mapping : parPortMappings) {
|
||||||
|
@ -46,8 +53,10 @@ namespace {
|
||||||
try {
|
try {
|
||||||
const CurrentMappingType curr_mapping =
|
const CurrentMappingType curr_mapping =
|
||||||
has_mapping(redirs, parHost, mapping.port, mapping.protocol);
|
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:
|
case PresentWithDifferentIP:
|
||||||
parUpnp.remove_port_mapping(mapping.port, parHost, mapping.protocol);
|
parUpnp.remove_port_mapping(mapping.port, parHost, mapping.protocol);
|
||||||
|
|
||||||
|
@ -56,6 +65,7 @@ namespace {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PresentWithSameIP:
|
case PresentWithSameIP:
|
||||||
|
case IgnoreMapping:
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -70,7 +80,7 @@ namespace {
|
||||||
|
|
||||||
kuu::RedirectSetting settings = kuu::load_redirect_settings("keepupnpup.yml", upnp.lanaddr());
|
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
|
} //unnamed namespace
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include <yaml-cpp/yaml.h>
|
#include <yaml-cpp/yaml.h>
|
||||||
#include <ciso646>
|
#include <ciso646>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <boost/algorithm/string.hpp>
|
||||||
|
|
||||||
namespace kuu {
|
namespace kuu {
|
||||||
namespace {
|
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
|
} //unnamed namespace
|
||||||
|
|
||||||
RedirectSetting load_redirect_settings (const std::string& parPath, const std::string& parLanAddr) {
|
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>();
|
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;
|
return redir_settings;
|
||||||
}
|
}
|
||||||
} //namespace kuu
|
} //namespace kuu
|
||||||
|
|
|
@ -41,6 +41,7 @@ namespace kuu {
|
||||||
std::string host;
|
std::string host;
|
||||||
std::string desc;
|
std::string desc;
|
||||||
PortMappingsList port_mappings;
|
PortMappingsList port_mappings;
|
||||||
|
bool autoremove_old;
|
||||||
};
|
};
|
||||||
|
|
||||||
RedirectSetting load_redirect_settings (const std::string& parPath, const std::string& parLanAddr);
|
RedirectSetting load_redirect_settings (const std::string& parPath, const std::string& parLanAddr);
|
||||||
|
|
Loading…
Add table
Reference in a new issue