2020-11-03 17:34:10 +01:00
|
|
|
//
|
2024-05-30 18:40:54 +02:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2020-11-03 17:34:10 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2023-07-25 21:26:12 +02:00
|
|
|
#include "telegram-bot-api/ClientParameters.h"
|
|
|
|
|
2020-11-03 17:34:10 +01:00
|
|
|
#include "td/net/HttpInboundConnection.h"
|
|
|
|
#include "td/net/TcpListener.h"
|
|
|
|
|
2022-01-25 16:18:44 +01:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2021-09-29 19:34:21 +02:00
|
|
|
#include "td/utils/BufferedFd.h"
|
2023-03-09 16:54:59 +01:00
|
|
|
#include "td/utils/common.h"
|
2020-11-03 17:34:10 +01:00
|
|
|
#include "td/utils/FloodControlFast.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/port/SocketFd.h"
|
2021-05-20 22:51:37 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2020-11-03 17:34:10 +01:00
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace telegram_bot_api {
|
|
|
|
|
2022-01-25 16:18:44 +01:00
|
|
|
class HttpServer final : public td::TcpListener::Callback {
|
2020-11-03 17:34:10 +01:00
|
|
|
public:
|
2020-11-10 00:15:40 +01:00
|
|
|
HttpServer(td::string ip_address, int port,
|
|
|
|
std::function<td::ActorOwn<td::HttpInboundConnection::Callback>()> creator)
|
|
|
|
: ip_address_(std::move(ip_address)), port_(port), creator_(std::move(creator)) {
|
2020-11-03 17:34:10 +01:00
|
|
|
flood_control_.add_limit(1, 1); // 1 in a second
|
|
|
|
flood_control_.add_limit(60, 10); // 10 in a minute
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-11-10 00:15:40 +01:00
|
|
|
td::string ip_address_;
|
2020-11-03 17:34:10 +01:00
|
|
|
td::int32 port_;
|
|
|
|
std::function<td::ActorOwn<td::HttpInboundConnection::Callback>()> creator_;
|
|
|
|
td::ActorOwn<td::TcpListener> listener_;
|
|
|
|
td::FloodControlFast flood_control_;
|
|
|
|
|
2022-01-25 16:18:44 +01:00
|
|
|
void start_up() final {
|
2020-11-03 17:34:10 +01:00
|
|
|
auto now = td::Time::now();
|
|
|
|
auto wakeup_at = flood_control_.get_wakeup_at();
|
|
|
|
if (wakeup_at > now) {
|
|
|
|
set_timeout_at(wakeup_at);
|
|
|
|
return;
|
|
|
|
}
|
2022-11-11 13:37:07 +01:00
|
|
|
flood_control_.add_event(now);
|
2023-03-09 16:54:59 +01:00
|
|
|
LOG(INFO) << "Create TCP listener " << td::tag("address", ip_address_) << td::tag("port", port_);
|
2020-11-20 16:11:23 +01:00
|
|
|
listener_ = td::create_actor<td::TcpListener>(
|
|
|
|
PSLICE() << "TcpListener" << td::tag("address", ip_address_) << td::tag("port", port_), port_,
|
|
|
|
actor_shared(this, 1), ip_address_);
|
2020-11-03 17:34:10 +01:00
|
|
|
}
|
2020-11-10 00:15:40 +01:00
|
|
|
|
2022-01-25 16:18:44 +01:00
|
|
|
void hangup_shared() final {
|
2020-11-03 17:34:10 +01:00
|
|
|
LOG(ERROR) << "TCP listener was closed";
|
|
|
|
listener_.release();
|
|
|
|
yield();
|
|
|
|
}
|
2020-11-10 00:15:40 +01:00
|
|
|
|
2022-01-25 16:18:44 +01:00
|
|
|
void accept(td::SocketFd fd) final {
|
2021-09-29 19:34:21 +02:00
|
|
|
td::create_actor<td::HttpInboundConnection>("HttpInboundConnection", td::BufferedFd<td::SocketFd>(std::move(fd)), 0,
|
2023-07-25 21:26:12 +02:00
|
|
|
50, 500, creator_(), SharedData::get_slow_incoming_http_scheduler_id())
|
2020-11-03 17:34:10 +01:00
|
|
|
.release();
|
|
|
|
}
|
2020-11-10 00:15:40 +01:00
|
|
|
|
2022-01-25 16:18:44 +01:00
|
|
|
void loop() final {
|
2020-11-03 17:34:10 +01:00
|
|
|
if (listener_.empty()) {
|
|
|
|
start_up();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace telegram_bot_api
|