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

This commit is contained in:
Andrea Cavalli 2020-11-10 16:34:21 +01:00
commit 25e10ef243
2 changed files with 43 additions and 19 deletions

View File

@ -23,13 +23,15 @@ namespace telegram_bot_api {
class HttpServer : public td::TcpListener::Callback {
public:
HttpServer(int port, std::function<td::ActorOwn<td::HttpInboundConnection::Callback>()> creator)
: port_(port), creator_(std::move(creator)) {
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)) {
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<td::ActorOwn<td::HttpInboundConnection::Callback>()> creator_;
td::ActorOwn<td::TcpListener> listener_;
@ -45,13 +47,15 @@ class HttpServer : public td::TcpListener::Callback {
flood_control_.add_event(static_cast<td::int32>(now));
LOG(INFO) << "Create tcp listener " << td::tag("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 {
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();

View File

@ -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", "\"<remainder>/<modulo>\". 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<td::uint64>(rem));
TRY_RESULT(mod_i, td::to_integer_safe<td::uint64>(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",
"\"<remainder>/<modulo>\". 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<td::uint64>(rem));
TRY_RESULT(mod_i, td::to_integer_safe<td::uint64>(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<ClientManager>(0, "ClientManager", std::move(parameters), token_range).release();
sched
.create_actor_unsafe<HttpServer>(
0, "HttpServer", http_port,
0, "HttpServer", http_ip_address, http_port,
[client_manager, shared_data] {
return td::ActorOwn<td::HttpInboundConnection::Callback>(
td::create_actor<HttpConnection>("HttpConnection", client_manager, shared_data));
@ -379,7 +398,7 @@ int main(int argc, char *argv[]) {
if (http_stat_port != 0) {
sched
.create_actor_unsafe<HttpServer>(
0, "HttpStatsServer", http_stat_port,
0, "HttpStatsServer", http_stat_ip_address, http_stat_port,
[client_manager] {
return td::ActorOwn<td::HttpInboundConnection::Callback>(
td::create_actor<HttpStatConnection>("HttpStatConnection", client_manager));