From 9e0c5d67c7c4e6b92e0fa9971f3d0afa07f549f7 Mon Sep 17 00:00:00 2001 From: weko Date: Tue, 14 Feb 2023 14:58:11 +0300 Subject: [PATCH 1/2] Don't use reversed port for choise random port. (by whothefuckami) --- libi2pd/RouterContext.cpp | 8 +++++++- libi2pd/util.cpp | 16 ++++++++++++++++ libi2pd/util.h | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/libi2pd/RouterContext.cpp b/libi2pd/RouterContext.cpp index a91b9217..a0a0ab06 100644 --- a/libi2pd/RouterContext.cpp +++ b/libi2pd/RouterContext.cpp @@ -202,7 +202,13 @@ namespace i2p uint16_t RouterContext::SelectRandomPort () const { - uint16_t port = rand () % (30777 - 9111) + 9111; // I2P network ports range + uint16_t port; + do + { + port = rand () % (30777 - 9111) + 9111; // I2P network ports range + } + while(i2p::util::net::IsPortInReservedRange(port)); + if (port == 9150) port = 9151; // Tor browser return port; } diff --git a/libi2pd/util.cpp b/libi2pd/util.cpp index f0e9a7c6..48a8fab3 100644 --- a/libi2pd/util.cpp +++ b/libi2pd/util.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include "util.h" @@ -487,6 +488,21 @@ namespace net return IsYggdrasilAddress (addr.to_v6 ().to_bytes ().data ()); } + bool IsPortInReservedRange (const uint16_t port) noexcept + { + static const std::unordered_set reservedPorts{ + 9119,9306,9312,9389,9418,9535,9536,9695, + 9800,9899,10000,10050,10051,10110,10212, + 10933,11001,11112,11235,11371,12222,12223, + 13075,13400,13720,13721,13724,13782,13783, + 13785,13786,15345,17224,17225,17500,18104, + 19788,19812,19813,19814,19999,20000,24465, + 24554,26000,27000,27001,27002,27003,27004, + 27005,27006,27007,27008,27009,28000}; + + return (reservedPorts.find(port) != reservedPorts.end()); + } + boost::asio::ip::address_v6 GetYggdrasilAddress () { #if defined(_WIN32) diff --git a/libi2pd/util.h b/libi2pd/util.h index 248c2bad..7ba24694 100644 --- a/libi2pd/util.h +++ b/libi2pd/util.h @@ -224,6 +224,7 @@ namespace util bool IsLocalAddress (const boost::asio::ip::address& addr); bool IsInReservedRange (const boost::asio::ip::address& host); bool IsYggdrasilAddress (const boost::asio::ip::address& addr); + bool IsPortInReservedRange (const uint16_t port) noexcept; } } } From 76d39cc7c9fe5d26ebd37d2746c622ec7c1b6b98 Mon Sep 17 00:00:00 2001 From: weko Date: Tue, 14 Feb 2023 20:03:04 +0300 Subject: [PATCH 2/2] changed to tabs, move tor port to function --- libi2pd/RouterContext.cpp | 9 ++++----- libi2pd/util.cpp | 27 ++++++++++++++------------- libi2pd/util.h | 2 +- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/libi2pd/RouterContext.cpp b/libi2pd/RouterContext.cpp index a0a0ab06..72b723ea 100644 --- a/libi2pd/RouterContext.cpp +++ b/libi2pd/RouterContext.cpp @@ -204,12 +204,11 @@ namespace i2p { uint16_t port; do - { - port = rand () % (30777 - 9111) + 9111; // I2P network ports range - } - while(i2p::util::net::IsPortInReservedRange(port)); + { + port = rand () % (30777 - 9111) + 9111; // I2P network ports range + } + while(i2p::util::net::IsPortInReservedRange(port)); - if (port == 9150) port = 9151; // Tor browser return port; } diff --git a/libi2pd/util.cpp b/libi2pd/util.cpp index 48a8fab3..369f999e 100644 --- a/libi2pd/util.cpp +++ b/libi2pd/util.cpp @@ -488,20 +488,21 @@ namespace net return IsYggdrasilAddress (addr.to_v6 ().to_bytes ().data ()); } - bool IsPortInReservedRange (const uint16_t port) noexcept - { - static const std::unordered_set reservedPorts{ - 9119,9306,9312,9389,9418,9535,9536,9695, - 9800,9899,10000,10050,10051,10110,10212, - 10933,11001,11112,11235,11371,12222,12223, - 13075,13400,13720,13721,13724,13782,13783, - 13785,13786,15345,17224,17225,17500,18104, - 19788,19812,19813,19814,19999,20000,24465, - 24554,26000,27000,27001,27002,27003,27004, - 27005,27006,27007,27008,27009,28000}; + bool IsPortInReservedRange (const uint16_t port) noexcept + { + // https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers (Feb. 3, 2023) + Tor browser (9150) + static const std::unordered_set reservedPorts{ + 9119,9150,9306,9312,9389,9418,9535,9536,9695, + 9800,9899,10000,10050,10051,10110,10212, + 10933,11001,11112,11235,11371,12222,12223, + 13075,13400,13720,13721,13724,13782,13783, + 13785,13786,15345,17224,17225,17500,18104, + 19788,19812,19813,19814,19999,20000,24465, + 24554,26000,27000,27001,27002,27003,27004, + 27005,27006,27007,27008,27009,28000}; - return (reservedPorts.find(port) != reservedPorts.end()); - } + return (reservedPorts.find(port) != reservedPorts.end()); + } boost::asio::ip::address_v6 GetYggdrasilAddress () { diff --git a/libi2pd/util.h b/libi2pd/util.h index 7ba24694..e2037212 100644 --- a/libi2pd/util.h +++ b/libi2pd/util.h @@ -224,7 +224,7 @@ namespace util bool IsLocalAddress (const boost::asio::ip::address& addr); bool IsInReservedRange (const boost::asio::ip::address& host); bool IsYggdrasilAddress (const boost::asio::ip::address& addr); - bool IsPortInReservedRange (const uint16_t port) noexcept; + bool IsPortInReservedRange (const uint16_t port) noexcept; } } }