Skip to content

Commit

Permalink
support hostname while parse nodes.json
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasLi1024 committed Jan 26, 2024
1 parent 8b429c6 commit f287626
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
61 changes: 60 additions & 1 deletion bcos-gateway/bcos-gateway/GatewayConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <boost/throw_exception.hpp>
#include <algorithm>
#include <limits>
#include <regex>
#include <string>
#include <vector>

Expand Down Expand Up @@ -45,6 +46,32 @@ int64_t GatewayConfig::doubleMBToBit(double _d)
return (int64_t)_d;
}

bool GatewayConfig::isIPAddress(const std::string& _input)
{
const std::regex ipv4_regex("^([0-9]{1,3}\\.){3}[0-9]{1,3}$");
const std::regex ipv6_regex(
"^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|:|((([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?::("
"([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?))$");

return std::regex_match(_input, ipv4_regex) || std::regex_match(_input, ipv6_regex);
}

bool GatewayConfig::isHostname(const std::string& _input)
{
boost::asio::io_context io_context;
boost::asio::ip::tcp::resolver resolver(io_context);

try
{
boost::asio::ip::tcp::resolver::results_type results = resolver.resolve(_input, "80");
return true;
}
catch (const std::exception& e)
{
return false;
}
}

void GatewayConfig::hostAndPort2Endpoint(const std::string& _host, NodeIPEndpoint& _endpoint)
{
std::string ip;
Expand Down Expand Up @@ -85,7 +112,39 @@ void GatewayConfig::hostAndPort2Endpoint(const std::string& _host, NodeIPEndpoin
}

boost::system::error_code ec;
boost::asio::ip::address ip_address = boost::asio::ip::make_address(ip, ec);
boost::asio::ip::address ip_address;
// ip
if (isIPAddress(ip))
{
ip_address = boost::asio::ip::make_address(ip, ec);
}
// hostname
else if (isHostname(ip))
{
boost::asio::io_context io_context;
boost::asio::ip::tcp::resolver resolver(io_context);
boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), ip, "80");
boost::asio::ip::tcp::resolver::results_type results = resolver.resolve(query, ec);
if (!ec)
{
ip_address = results.begin()->endpoint().address();
}
else
{
GATEWAY_CONFIG_LOG(ERROR)
<< LOG_DESC("parse host name failed") << LOG_KV("host name", ip);
BOOST_THROW_EXCEPTION(InvalidParameter() << errinfo_comment(
"GatewayConfig: parse host name failed, host name=" + ip));
}
}
else
{
GATEWAY_CONFIG_LOG(ERROR) << "the host is not a valid ip or a hostname"
<< LOG_KV("host:", _host);
BOOST_THROW_EXCEPTION(
InvalidParameter() << errinfo_comment(
"GatewayConfig: the host is not a valid ip or a hostname, host=" + _host));
}
if (ec.value() != 0)
{
GATEWAY_CONFIG_LOG(ERROR) << LOG_DESC("the host is invalid, make_address failed")
Expand Down
2 changes: 2 additions & 0 deletions bcos-gateway/bcos-gateway/GatewayConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ class GatewayConfig : public bcos::ObjectCounter<GatewayConfig>
bool isValidIP(const std::string& _ip);
// MB to bit
int64_t doubleMBToBit(double _d);
bool isIPAddress(const std::string& _input);
bool isHostname(const std::string& _input);
void hostAndPort2Endpoint(const std::string& _host, NodeIPEndpoint& _endpoint);
void parseConnectedJson(const std::string& _json, std::set<NodeIPEndpoint>& _nodeIPEndpointSet);
// loads p2p configuration items from the configuration file
Expand Down

0 comments on commit f287626

Please sign in to comment.