Simplify is_local_address to just look for localhost

Doing a DNS resolution here seems like overkill, and even if you get a
resolution to a non-public IP that doesn't mean it's local/trusted (for
example, you could resolve a .loki address, get 10.0.0.3, and then
specify that).  So just hard-code common ways of specifying localhost
instead, which is really the main point of this.
This commit is contained in:
Jason Rhinelander 2020-07-23 13:26:59 -03:00
parent 4f783d3273
commit ea9eeab899

View file

@ -43,7 +43,6 @@
#include "readline_buffer.h" #include "readline_buffer.h"
#include "string_util.h" #include "string_util.h"
#include <boost/asio.hpp>
#include <boost/format.hpp> #include <boost/format.hpp>
#include "i18n.h" #include "i18n.h"
@ -211,45 +210,12 @@ namespace tools
bool is_local_address(const std::string &address) bool is_local_address(const std::string &address)
{ {
// always assume Tor/I2P addresses to be untrusted by default return address == "localhost"sv
if (tools::ends_with(address, ".onion") || tools::ends_with(address, ".i2p")) || (tools::starts_with(address, "127."sv) && address.find_first_not_of("0123456789."sv) == std::string::npos)
{ || address == "::1"sv
MDEBUG("Address '" << address << "' is Tor/I2P, non local"); || address == "[::1]"sv; // There are other uncommon ways to specify localhost (e.g. 0::1, ::0001) but don't worry about them.
return false;
} }
// extract host
epee::net_utils::http::url_content u_c;
if (!epee::net_utils::parse_url(address, u_c))
{
MWARNING("Failed to determine whether address '" << address << "' is local, assuming not");
return false;
}
if (u_c.host.empty())
{
MWARNING("Failed to determine whether address '" << address << "' is local, assuming not");
return false;
}
// resolve to IP
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(u_c.host, "");
boost::asio::ip::tcp::resolver::iterator i = resolver.resolve(query);
while (i != boost::asio::ip::tcp::resolver::iterator())
{
const boost::asio::ip::tcp::endpoint &ep = *i;
if (ep.address().is_loopback())
{
MDEBUG("Address '" << address << "' is local");
return true;
}
++i;
}
MDEBUG("Address '" << address << "' is not local");
return false;
}
int vercmp(std::string_view v0, std::string_view v1) int vercmp(std::string_view v0, std::string_view v1)
{ {
auto f0 = tools::split_any(v0, ".-"); auto f0 = tools::split_any(v0, ".-");