From 78c71fbc65f223479edfced1fea9adb2b88cc61a Mon Sep 17 00:00:00 2001 From: Jackarain Date: Wed, 4 Sep 2024 20:34:16 +0800 Subject: [PATCH] Specify user to rate limit --- proxy/include/proxy/proxy_server.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/proxy/include/proxy/proxy_server.hpp b/proxy/include/proxy/proxy_server.hpp index 98be9df88..83fe7a602 100644 --- a/proxy/include/proxy/proxy_server.hpp +++ b/proxy/include/proxy/proxy_server.hpp @@ -384,6 +384,10 @@ R"x*x*x( using auth_users = std::tuple; std::vector auth_users_; + // 指定用户限速设置. + // 其中表示:用户名对应的速率 + std::unordered_map users_rate_limit_; + // allow_regions/deny_regions 用于指定允许/拒绝的地区, 例如: // allow_regions_ = { "中国", "香港", "台湾" }; // deny_regions_ = { "美国", "日本" }; @@ -2130,6 +2134,7 @@ R"x*x*x( if (user == userid) { verify_passed = true; + user_rate_limit_config(user); break; } } @@ -2307,6 +2312,7 @@ R"x*x*x( if (uname == user && passwd == pwd) { verify_passed = true; + user_rate_limit_config(user); break; } } @@ -2808,6 +2814,7 @@ R"x*x*x( if (uname == user && passwd == pwd) { verify_passed = true; + user_rate_limit_config(user); break; } } @@ -4412,6 +4419,17 @@ R"x*x*x( co_return; } + inline void user_rate_limit_config(const std::string& user) + { + // 在这里使用用户指定的速率设置替换全局速率配置. + auto found = m_option.users_rate_limit_.find(user); + if (found != m_option.users_rate_limit_.end()) + { + auto& rate = *found; + m_option.tcp_rate_limit_ = rate.second; + } + } + inline void stream_expires_never(variant_stream_type& stream) { boost::variant2::visit([](auto& s) mutable