From 0d1de5ec77483c6d0e1afc63f97c9aef4e901c93 Mon Sep 17 00:00:00 2001 From: lucasli <410567249@qq.com> Date: Mon, 29 Jan 2024 20:08:03 +0800 Subject: [PATCH] fix sdk parse domain name fail because boostssl don't support domain name --- .../bcos-boostssl/websocket/WsInitializer.cpp | 23 ++++++++++++++++--- .../bcos-boostssl/websocket/WsTools.h | 2 +- .../test/unittests/websocket/WsConfigTest.cpp | 2 +- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/bcos-boostssl/bcos-boostssl/websocket/WsInitializer.cpp b/bcos-boostssl/bcos-boostssl/websocket/WsInitializer.cpp index 8905b5b823..cd5768e134 100644 --- a/bcos-boostssl/bcos-boostssl/websocket/WsInitializer.cpp +++ b/bcos-boostssl/bcos-boostssl/websocket/WsInitializer.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -130,7 +131,7 @@ void WsInitializer::initWsService(WsService::Ptr _wsService) auto connectPeers = _config->connectPeers(); WEBSOCKET_INITIALIZER(INFO) << LOG_BADGE("initWsService") << LOG_DESC("start websocket service as client") - << LOG_KV("connected size", connectPeers ? connectPeers->size() : 0); + << LOG_KV("connected endpoints size", connectPeers ? connectPeers->size() : 0); if (connectPeers) { @@ -138,8 +139,24 @@ void WsInitializer::initWsService(WsService::Ptr _wsService) { if (!WsTools::validIP(peer.address())) { - BOOST_THROW_EXCEPTION(InvalidParameter() << errinfo_comment( - "invalid connected peer, value: " + peer.address())); + boost::system::error_code err; + + // test if the address domain name + boost::asio::ip::tcp::resolver::query qry( + peer.address(), boost::lexical_cast(0)); + resolver->resolve(qry, err); + + if (err) + { + BOOST_THROW_EXCEPTION(InvalidParameter() << errinfo_comment( + "invalid connection host, ipv4/ipv6/domain name " + "support, current: " + + peer.address())); + } + + WEBSOCKET_INITIALIZER(INFO) + << LOG_BADGE("initWsService") << LOG_DESC("domain name has been set") + << LOG_KV("host", peer.address()); } if (!WsTools::validPort(peer.port())) diff --git a/bcos-boostssl/bcos-boostssl/websocket/WsTools.h b/bcos-boostssl/bcos-boostssl/websocket/WsTools.h index 6919c0d287..8547cfcb10 100644 --- a/bcos-boostssl/bcos-boostssl/websocket/WsTools.h +++ b/bcos-boostssl/bcos-boostssl/websocket/WsTools.h @@ -42,7 +42,7 @@ class WsTools return !static_cast(ec); } - static bool validPort(uint16_t _port) { return _port > 1024; } + static bool validPort(uint16_t _port) { return _port > 0; } static bool stringToEndPoint(const std::string& peer, NodeIPEndpoint& _endpoint); diff --git a/bcos-boostssl/test/unittests/websocket/WsConfigTest.cpp b/bcos-boostssl/test/unittests/websocket/WsConfigTest.cpp index 1557ad7a0c..52e04c68f2 100644 --- a/bcos-boostssl/test/unittests/websocket/WsConfigTest.cpp +++ b/bcos-boostssl/test/unittests/websocket/WsConfigTest.cpp @@ -85,7 +85,7 @@ BOOST_AUTO_TEST_CASE(test_WsToolsTest) BOOST_CHECK_EQUAL(WsTools::validIP("::1"), true); BOOST_CHECK_EQUAL(WsTools::validPort(1111), true); - BOOST_CHECK_EQUAL(WsTools::validPort(10), false); + BOOST_CHECK_EQUAL(WsTools::validPort(10), true); BOOST_CHECK_EQUAL(WsTools::validPort(65535), true); }