From aeb6dddcb13f45aa5a558fe3508e8b67ad3c734b Mon Sep 17 00:00:00 2001 From: wipedlifepotato Date: Mon, 18 Aug 2025 19:43:52 +0700 Subject: [PATCH] feat: bandwidth with bytes/kbytes/gbytes seconds and fix for style.css --- daemon/Daemon.cpp | 34 ++++++++++++++++++++++++++++++++-- daemon/HTTPServer.cpp | 26 ++++++++++++++++---------- libi2pd/RouterContext.cpp | 3 +++ 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/daemon/Daemon.cpp b/daemon/Daemon.cpp index e2fdf2d4..5c4f9c97 100644 --- a/daemon/Daemon.cpp +++ b/daemon/Daemon.cpp @@ -8,6 +8,7 @@ #include #include +#include #include "Daemon.h" @@ -186,12 +187,41 @@ namespace util std::string bandwidth; i2p::config::GetOption("bandwidth", bandwidth); if (bandwidth.length () > 0) { + const auto NumBandwithRegex = std::regex(R"(^\d+$)"); + const auto BandwithRegex = std::regex(R"((\d+)(b|kb|mb|gb))"); + std::smatch bandWithMatch; + if (bandwidth.length () == 1 && ((bandwidth[0] >= 'K' && bandwidth[0] <= 'P') || bandwidth[0] == 'X' )) { i2p::context.SetBandwidth (bandwidth[0]); LogPrint(eLogInfo, "Daemon: Bandwidth set to ", i2p::context.GetBandwidthLimit (), "KBps"); } - else + else if (std::regex_match(bandwidth, bandWithMatch, BandwithRegex)) { + const auto number = bandWithMatch[1].str(); + const auto unit = bandWithMatch[2].str(); + int limit = std::atoi(number.c_str()); + std::cout << unit; + if (unit == "b") + { + limit /= 1000; + } + else if(unit == "mb") + { + limit *= 1000; + } else if(unit == "gb") + { + limit *= 1000000; + } + // if limit more than 32 bits then its will be negative + if (limit < 0) + { + LogPrint(eLogInfo, "Daemon: Unexpected bandwidth ", bandwidth, ". Set to 'low'"); + i2p::context.SetBandwidth (i2p::data::CAPS_FLAG_LOW_BANDWIDTH2); + } else { + i2p::context.SetBandwidth(limit); + } + } + else if(std::regex_search(bandwidth, NumBandwithRegex)) { auto value = std::atoi(bandwidth.c_str()); if (value > 0) @@ -204,7 +234,7 @@ namespace util LogPrint(eLogInfo, "Daemon: Unexpected bandwidth ", bandwidth, ". Set to 'low'"); i2p::context.SetBandwidth (i2p::data::CAPS_FLAG_LOW_BANDWIDTH2); } - } + } } else if (isFloodfill) { diff --git a/daemon/HTTPServer.cpp b/daemon/HTTPServer.cpp index 18fecce7..e5c69a81 100644 --- a/daemon/HTTPServer.cpp +++ b/daemon/HTTPServer.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -42,6 +43,7 @@ namespace i2p { namespace http { + static void LoadExtCSS (std::string fileName = "style.css") { std::stringstream s; @@ -166,19 +168,17 @@ namespace http { static void ShowPageHead (std::stringstream& s) { std::string webroot; i2p::config::GetOption("http.webroot", webroot); - std::string theme; i2p::config::GetOption("http.theme", theme); - if(theme != "light" && theme != "black" ) + + const auto isThemeRegex = std::regex("^(white|black|light)"); + if (!std::regex_search(theme, isThemeRegex)) { - if (theme =="white") { - theme = "light"; - } else { - LoadExtCSS(theme); - } - + LoadExtCSS(theme); } - - + else + { + LoadExtCSS(); + } // Page language std::string currLang = i2p::client::context.GetLanguage ()->GetLanguage(); // get current used language @@ -1482,6 +1482,12 @@ namespace http { } else if (cmd == HTTP_COMMAND_RELOAD_CSS) { + std::string theme; i2p::config::GetOption("http.theme", theme); + + if (theme != "light" && theme != "black" && theme !="white") + { + + } LoadExtCSS(); } else diff --git a/libi2pd/RouterContext.cpp b/libi2pd/RouterContext.cpp index 9eb0f9a5..cfb92f00 100644 --- a/libi2pd/RouterContext.cpp +++ b/libi2pd/RouterContext.cpp @@ -683,6 +683,9 @@ namespace i2p else if (limit > (int)i2p::data::LOW_BANDWIDTH_LIMIT) { SetBandwidth('M'); } else if (limit > 12) { SetBandwidth('L'); } else { SetBandwidth('K'); } + + + LogPrint(eLogInfo, "RouterContext: Set bandwidth ", limit, ". kb/s"); m_BandwidthLimit = limit; // set precise limit }