Add Query::get_peer_ip_address.

This commit is contained in:
levlam 2023-03-13 18:42:35 +03:00
parent 26854a6a3d
commit e9d32ad23d
4 changed files with 20 additions and 18 deletions

View File

@ -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<int>(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;
}

View File

@ -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());

View File

@ -26,10 +26,10 @@ td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> empty_paramet
Query::Query(td::vector<td::BufferSlice> &&container, td::Slice token, bool is_test_dc, td::MutableSlice method,
td::vector<std::pair<td::MutableSlice, td::MutableSlice>> &&args,
td::vector<std::pair<td::MutableSlice, td::MutableSlice>> &&headers, td::vector<td::HttpFile> &&files,
std::shared_ptr<SharedData> shared_data, const td::IPAddress &peer_address, bool is_internal)
std::shared_ptr<SharedData> 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<td::BufferSlice> &&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},

View File

@ -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<td::BufferSlice> &&container, td::Slice token, bool is_test_dc, td::MutableSlice method,
td::vector<std::pair<td::MutableSlice, td::MutableSlice>> &&args,
td::vector<std::pair<td::MutableSlice, td::MutableSlice>> &&headers, td::vector<td::HttpFile> &&files,
std::shared_ptr<SharedData> shared_data, const td::IPAddress &peer_address, bool is_internal);
std::shared_ptr<SharedData> 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<SharedData> shared_data_;
double start_timestamp_;
td::IPAddress peer_address_;
td::IPAddress peer_ip_address_;
td::ActorId<BotStatActor> stat_actor_;
// request