From c7769ea2991c5303eb57bee16861596697ca6534 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 10 Nov 2020 02:15:40 +0300 Subject: [PATCH] Add http-ip-address and http-stat-ip-address options. --- td | 2 +- telegram-bot-api/HttpServer.h | 11 ++++-- telegram-bot-api/telegram-bot-api.cpp | 51 ++++++++++++++++++--------- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/td b/td index 1d75237..1fa87ba 160000 --- a/td +++ b/td @@ -1 +1 @@ -Subproject commit 1d75237893c8a08d73ba95a7e76b8da517b8c1dc +Subproject commit 1fa87babb32ea85ff5a97ab93525ae13f66a0eef diff --git a/telegram-bot-api/HttpServer.h b/telegram-bot-api/HttpServer.h index 6bfcdff..3e10bc9 100644 --- a/telegram-bot-api/HttpServer.h +++ b/telegram-bot-api/HttpServer.h @@ -23,13 +23,15 @@ namespace telegram_bot_api { class HttpServer : public td::TcpListener::Callback { public: - HttpServer(int port, std::function()> creator) - : port_(port), creator_(std::move(creator)) { + HttpServer(td::string ip_address, int port, + std::function()> creator) + : ip_address_(std::move(ip_address)), port_(port), creator_(std::move(creator)) { flood_control_.add_limit(1, 1); // 1 in a second flood_control_.add_limit(60, 10); // 10 in a minute } private: + td::string ip_address_; td::int32 port_; std::function()> creator_; td::ActorOwn listener_; @@ -45,13 +47,15 @@ class HttpServer : public td::TcpListener::Callback { flood_control_.add_event(static_cast(now)); LOG(INFO) << "Create tcp listener " << td::tag("port", port_); listener_ = td::create_actor(PSLICE() << "TcpListener" << td::tag("port", port_), port_, - actor_shared(this, 1)); + actor_shared(this, 1), ip_address_); } + void hangup_shared() override { LOG(ERROR) << "TCP listener was closed"; listener_.release(); yield(); } + void accept(td::SocketFd fd) override { auto scheduler_count = td::Scheduler::instance()->sched_count(); auto scheduler_id = scheduler_count - 1; @@ -62,6 +66,7 @@ class HttpServer : public td::TcpListener::Callback { scheduler_id) .release(); } + void loop() override { if (listener_.empty()) { start_up(); diff --git a/telegram-bot-api/telegram-bot-api.cpp b/telegram-bot-api/telegram-bot-api.cpp index 3502e21..4a3b38b 100644 --- a/telegram-bot-api/telegram-bot-api.cpp +++ b/telegram-bot-api/telegram-bot-api.cpp @@ -34,6 +34,7 @@ #include "td/utils/MemoryLog.h" #include "td/utils/misc.h" #include "td/utils/OptionParser.h" +#include "td/utils/port/IPAddress.h" #include "td/utils/port/path.h" #include "td/utils/port/rlimit.h" #include "td/utils/port/signals.h" @@ -140,6 +141,8 @@ int main(int argc, char *argv[]) { bool need_print_usage = false; int http_port = 8081; int http_stat_port = 0; + td::string http_ip_address = "0.0.0.0"; + td::string http_stat_ip_address = "0.0.0.0"; td::string log_file_path; int verbosity_level = 0; td::int64 log_max_file_size = 2000000000; @@ -184,23 +187,39 @@ int main(int argc, char *argv[]) { options.add_option('d', "dir", "server working directory", td::OptionParser::parse_string(working_directory)); options.add_option('t', "temp-dir", "directory for storing HTTP server temporary files", td::OptionParser::parse_string(temporary_directory)); - options.add_checked_option( - '\0', "filter", "\"/\". Allow only bots with 'bot_user_id % modulo == remainder'", - [&](td::Slice rem_mod) { - td::Slice rem; - td::Slice mod; - std::tie(rem, mod) = td::split(rem_mod, '/'); - TRY_RESULT(rem_i, td::to_integer_safe(rem)); - TRY_RESULT(mod_i, td::to_integer_safe(mod)); - if (rem_i >= mod_i) { - return td::Status::Error("Wrong argument specified: ensure that remainder < modulo"); - } - token_range = {rem_i, mod_i}; - return td::Status::OK(); - }); + options.add_checked_option('\0', "filter", + "\"/\". Allow only bots with 'bot_user_id % modulo == remainder'", + [&](td::Slice rem_mod) { + td::Slice rem; + td::Slice mod; + std::tie(rem, mod) = td::split(rem_mod, '/'); + TRY_RESULT(rem_i, td::to_integer_safe(rem)); + TRY_RESULT(mod_i, td::to_integer_safe(mod)); + if (rem_i >= mod_i) { + return td::Status::Error("Wrong argument specified: ensure that remainder < modulo"); + } + token_range = {rem_i, mod_i}; + return td::Status::OK(); + }); options.add_checked_option('\0', "max-webhook-connections", "default value of the maximum webhook connections per bot", td::OptionParser::parse_integer(parameters->default_max_webhook_connections_)); + options.add_checked_option('\0', "http-ip-address", + "local IP address, HTTP connections to which will be accepted. By default, connections to " + "any local IPv4 address are accepted", + [&](td::Slice ip_address) { + TRY_STATUS(td::IPAddress::get_ip_address(ip_address.str())); + http_ip_address = ip_address.str(); + return td::Status::OK(); + }); + options.add_checked_option('\0', "http-stat-ip-address", + "local IP address, HTTP statistics connections to which will be accepted. By default, " + "statistics connections to any local IPv4 address are accepted", + [&](td::Slice ip_address) { + TRY_STATUS(td::IPAddress::get_ip_address(ip_address.str())); + http_stat_ip_address = ip_address.str(); + return td::Status::OK(); + }); options.add_option('l', "log", "path to the file where the log will be written", td::OptionParser::parse_string(log_file_path)); @@ -370,7 +389,7 @@ int main(int argc, char *argv[]) { sched.create_actor_unsafe(0, "ClientManager", std::move(parameters), token_range).release(); sched .create_actor_unsafe( - 0, "HttpServer", http_port, + 0, "HttpServer", http_ip_address, http_port, [client_manager, shared_data] { return td::ActorOwn( td::create_actor("HttpConnection", client_manager, shared_data)); @@ -379,7 +398,7 @@ int main(int argc, char *argv[]) { if (http_stat_port != 0) { sched .create_actor_unsafe( - 0, "HttpStatsServer", http_stat_port, + 0, "HttpStatsServer", http_stat_ip_address, http_stat_port, [client_manager] { return td::ActorOwn( td::create_actor("HttpStatConnection", client_manager));