From e9d32ad23d8b1bf65b468045f5ed3252596ae54e Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 13 Mar 2023 18:42:35 +0300 Subject: [PATCH] Add Query::get_peer_ip_address. --- telegram-bot-api/Client.cpp | 7 ++++--- telegram-bot-api/ClientManager.cpp | 10 ++-------- telegram-bot-api/Query.cpp | 13 +++++++++++-- telegram-bot-api/Query.h | 8 +++----- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index ea9d03c..4462120 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -41,7 +41,7 @@ using td_api::move_object_as; int Client::get_retry_after_time(td::Slice error_message) { td::Slice prefix = "Too Many Requests: retry after "; - if (begins_with(error_message, prefix)) { + if (td::begins_with(error_message, prefix)) { auto r_retry_after = td::to_integer_safe(error_message.substr(prefix.size())); if (r_retry_after.is_ok() && r_retry_after.ok() > 0) { return r_retry_after.ok(); @@ -154,7 +154,7 @@ void Client::fail_query_with_error(PromisedQueryPtr query, int32 error_code, td: return fail_query(400, PSLICE() << "Bad Request: " << error_message, std::move(query)); } - if (begins_with(error_message, prefix)) { + if (td::begins_with(error_message, prefix)) { return fail_query(error_code, error_message, std::move(query)); } else { td::string error_str = prefix.str(); @@ -10168,7 +10168,8 @@ void Client::do_get_updates(int32 offset, int32 limit, int32 timeout, PromisedQu if (need_warning && previous_get_updates_finish_time_ > 0) { LOG(WARNING) << "Found " << updates.size() << " updates out of " << (total_size + updates.size()) << " after last getUpdates call " << (query->start_timestamp() - previous_get_updates_finish_time_) - << " seconds ago in " << (td::Time::now() - query->start_timestamp()) << " seconds"; + << " seconds ago in " << (td::Time::now() - query->start_timestamp()) << " seconds from " + << query->get_peer_ip_address(); } else { LOG(DEBUG) << "Found " << updates.size() << " updates out of " << total_size << " from " << from; } diff --git a/telegram-bot-api/ClientManager.cpp b/telegram-bot-api/ClientManager.cpp index da4d637..210d215 100644 --- a/telegram-bot-api/ClientManager.cpp +++ b/telegram-bot-api/ClientManager.cpp @@ -90,13 +90,7 @@ void ClientManager::send(PromisedQueryPtr query) { auto id_it = token_to_id_.find(token); if (id_it == token_to_id_.end()) { - td::string ip_address; - if (query->peer_address().is_valid() && !query->peer_address().is_reserved()) { // external connection - ip_address = query->peer_address().get_ip_str().str(); - } else { - // invalid peer address or connection from the local network - ip_address = query->get_header("x-real-ip").str(); - } + td::string ip_address = query->get_peer_ip_address(); if (!ip_address.empty()) { td::IPAddress tmp; tmp.init_host_port(ip_address, 0).ignore(); @@ -105,7 +99,7 @@ void ClientManager::send(PromisedQueryPtr query) { ip_address = tmp.get_ip_str().str(); } } - LOG(DEBUG) << "Receive incoming query for new bot " << token << " from " << query->peer_address(); + LOG(DEBUG) << "Receive incoming query for new bot " << token << " from " << ip_address; if (!ip_address.empty()) { LOG(DEBUG) << "Check Client creation flood control for IP address " << ip_address; auto res = flood_controls_.emplace(std::move(ip_address), td::FloodControlFast()); diff --git a/telegram-bot-api/Query.cpp b/telegram-bot-api/Query.cpp index 3478e04..4162f8a 100644 --- a/telegram-bot-api/Query.cpp +++ b/telegram-bot-api/Query.cpp @@ -26,10 +26,10 @@ td::FlatHashMap> empty_paramet Query::Query(td::vector &&container, td::Slice token, bool is_test_dc, td::MutableSlice method, td::vector> &&args, td::vector> &&headers, td::vector &&files, - std::shared_ptr shared_data, const td::IPAddress &peer_address, bool is_internal) + std::shared_ptr shared_data, const td::IPAddress &peer_ip_address, bool is_internal) : state_(State::Query) , shared_data_(shared_data) - , peer_address_(peer_address) + , peer_ip_address_(peer_ip_address) , container_(std::move(container)) , token_(token) , is_test_dc_(is_test_dc) @@ -53,6 +53,15 @@ Query::Query(td::vector &&container, td::Slice token, bool is_t } } +td::string Query::get_peer_ip_address() const { + if (peer_ip_address_.is_valid() && !peer_ip_address_.is_reserved()) { // external connection + return peer_ip_address_.get_ip_str().str(); + } else { + // invalid peer IP address or connection from the local network + return get_header("x-real-ip").str(); + } +} + td::int64 Query::query_size() const { return std::accumulate( container_.begin(), container_.end(), td::int64{0}, diff --git a/telegram-bot-api/Query.h b/telegram-bot-api/Query.h index 66c8e55..0b7b51e 100644 --- a/telegram-bot-api/Query.h +++ b/telegram-bot-api/Query.h @@ -70,9 +70,7 @@ class Query final : public td::ListNode { return files_; } - const td::IPAddress &peer_address() const { - return peer_address_; - } + td::string get_peer_ip_address() const; td::BufferSlice &answer() { return answer_; @@ -103,7 +101,7 @@ class Query final : public td::ListNode { Query(td::vector &&container, td::Slice token, bool is_test_dc, td::MutableSlice method, td::vector> &&args, td::vector> &&headers, td::vector &&files, - std::shared_ptr shared_data, const td::IPAddress &peer_address, bool is_internal); + std::shared_ptr shared_data, const td::IPAddress &peer_ip_address, bool is_internal); Query(const Query &) = delete; Query &operator=(const Query &) = delete; Query(Query &&) = delete; @@ -129,7 +127,7 @@ class Query final : public td::ListNode { State state_; std::shared_ptr shared_data_; double start_timestamp_; - td::IPAddress peer_address_; + td::IPAddress peer_ip_address_; td::ActorId stat_actor_; // request