Add http-ip-address and http-stat-ip-address options.

This commit is contained in:
levlam 2020-11-10 02:15:40 +03:00
parent 027807468a
commit c7769ea299
3 changed files with 44 additions and 20 deletions

2
td

@ -1 +1 @@
Subproject commit 1d75237893c8a08d73ba95a7e76b8da517b8c1dc Subproject commit 1fa87babb32ea85ff5a97ab93525ae13f66a0eef

View File

@ -23,13 +23,15 @@ namespace telegram_bot_api {
class HttpServer : public td::TcpListener::Callback { class HttpServer : public td::TcpListener::Callback {
public: public:
HttpServer(int port, std::function<td::ActorOwn<td::HttpInboundConnection::Callback>()> creator) HttpServer(td::string ip_address, int port,
: port_(port), creator_(std::move(creator)) { std::function<td::ActorOwn<td::HttpInboundConnection::Callback>()> 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(1, 1); // 1 in a second
flood_control_.add_limit(60, 10); // 10 in a minute flood_control_.add_limit(60, 10); // 10 in a minute
} }
private: private:
td::string ip_address_;
td::int32 port_; td::int32 port_;
std::function<td::ActorOwn<td::HttpInboundConnection::Callback>()> creator_; std::function<td::ActorOwn<td::HttpInboundConnection::Callback>()> creator_;
td::ActorOwn<td::TcpListener> listener_; td::ActorOwn<td::TcpListener> listener_;
@ -45,13 +47,15 @@ class HttpServer : public td::TcpListener::Callback {
flood_control_.add_event(static_cast<td::int32>(now)); flood_control_.add_event(static_cast<td::int32>(now));
LOG(INFO) << "Create tcp listener " << td::tag("port", port_); LOG(INFO) << "Create tcp listener " << td::tag("port", port_);
listener_ = td::create_actor<td::TcpListener>(PSLICE() << "TcpListener" << td::tag("port", port_), port_, listener_ = td::create_actor<td::TcpListener>(PSLICE() << "TcpListener" << td::tag("port", port_), port_,
actor_shared(this, 1)); actor_shared(this, 1), ip_address_);
} }
void hangup_shared() override { void hangup_shared() override {
LOG(ERROR) << "TCP listener was closed"; LOG(ERROR) << "TCP listener was closed";
listener_.release(); listener_.release();
yield(); yield();
} }
void accept(td::SocketFd fd) override { void accept(td::SocketFd fd) override {
auto scheduler_count = td::Scheduler::instance()->sched_count(); auto scheduler_count = td::Scheduler::instance()->sched_count();
auto scheduler_id = scheduler_count - 1; auto scheduler_id = scheduler_count - 1;
@ -62,6 +66,7 @@ class HttpServer : public td::TcpListener::Callback {
scheduler_id) scheduler_id)
.release(); .release();
} }
void loop() override { void loop() override {
if (listener_.empty()) { if (listener_.empty()) {
start_up(); start_up();

View File

@ -34,6 +34,7 @@
#include "td/utils/MemoryLog.h" #include "td/utils/MemoryLog.h"
#include "td/utils/misc.h" #include "td/utils/misc.h"
#include "td/utils/OptionParser.h" #include "td/utils/OptionParser.h"
#include "td/utils/port/IPAddress.h"
#include "td/utils/port/path.h" #include "td/utils/port/path.h"
#include "td/utils/port/rlimit.h" #include "td/utils/port/rlimit.h"
#include "td/utils/port/signals.h" #include "td/utils/port/signals.h"
@ -140,6 +141,8 @@ int main(int argc, char *argv[]) {
bool need_print_usage = false; bool need_print_usage = false;
int http_port = 8081; int http_port = 8081;
int http_stat_port = 0; 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; td::string log_file_path;
int verbosity_level = 0; int verbosity_level = 0;
td::int64 log_max_file_size = 2000000000; td::int64 log_max_file_size = 2000000000;
@ -184,8 +187,8 @@ int main(int argc, char *argv[]) {
options.add_option('d', "dir", "server working directory", td::OptionParser::parse_string(working_directory)); 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", options.add_option('t', "temp-dir", "directory for storing HTTP server temporary files",
td::OptionParser::parse_string(temporary_directory)); td::OptionParser::parse_string(temporary_directory));
options.add_checked_option( options.add_checked_option('\0', "filter",
'\0', "filter", "\"<remainder>/<modulo>\". Allow only bots with 'bot_user_id % modulo == remainder'", "\"<remainder>/<modulo>\". Allow only bots with 'bot_user_id % modulo == remainder'",
[&](td::Slice rem_mod) { [&](td::Slice rem_mod) {
td::Slice rem; td::Slice rem;
td::Slice mod; td::Slice mod;
@ -201,6 +204,22 @@ int main(int argc, char *argv[]) {
options.add_checked_option('\0', "max-webhook-connections", options.add_checked_option('\0', "max-webhook-connections",
"default value of the maximum webhook connections per bot", "default value of the maximum webhook connections per bot",
td::OptionParser::parse_integer(parameters->default_max_webhook_connections_)); 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", options.add_option('l', "log", "path to the file where the log will be written",
td::OptionParser::parse_string(log_file_path)); td::OptionParser::parse_string(log_file_path));
@ -370,7 +389,7 @@ int main(int argc, char *argv[]) {
sched.create_actor_unsafe<ClientManager>(0, "ClientManager", std::move(parameters), token_range).release(); sched.create_actor_unsafe<ClientManager>(0, "ClientManager", std::move(parameters), token_range).release();
sched sched
.create_actor_unsafe<HttpServer>( .create_actor_unsafe<HttpServer>(
0, "HttpServer", http_port, 0, "HttpServer", http_ip_address, http_port,
[client_manager, shared_data] { [client_manager, shared_data] {
return td::ActorOwn<td::HttpInboundConnection::Callback>( return td::ActorOwn<td::HttpInboundConnection::Callback>(
td::create_actor<HttpConnection>("HttpConnection", client_manager, shared_data)); td::create_actor<HttpConnection>("HttpConnection", client_manager, shared_data));
@ -379,7 +398,7 @@ int main(int argc, char *argv[]) {
if (http_stat_port != 0) { if (http_stat_port != 0) {
sched sched
.create_actor_unsafe<HttpServer>( .create_actor_unsafe<HttpServer>(
0, "HttpStatsServer", http_stat_port, 0, "HttpStatsServer", http_stat_ip_address, http_stat_port,
[client_manager] { [client_manager] {
return td::ActorOwn<td::HttpInboundConnection::Callback>( return td::ActorOwn<td::HttpInboundConnection::Callback>(
td::create_actor<HttpStatConnection>("HttpStatConnection", client_manager)); td::create_actor<HttpStatConnection>("HttpStatConnection", client_manager));