2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2008-11-24 20:17:36 +00:00
|
|
|
#endif
|
|
|
|
|
2022-01-17 16:20:12 +00:00
|
|
|
#include <memory>
|
2021-11-14 21:14:37 +00:00
|
|
|
#include <string>
|
2021-11-14 06:02:45 +00:00
|
|
|
#include <string_view>
|
2022-04-22 13:08:02 +00:00
|
|
|
#include <vector>
|
2021-11-14 06:02:45 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
|
|
|
|
#include "net.h"
|
2022-11-28 14:07:04 +00:00
|
|
|
#include "utils-ev.h"
|
2021-11-14 06:02:45 +00:00
|
|
|
|
2022-01-17 16:20:12 +00:00
|
|
|
struct evhttp;
|
2021-11-09 03:30:03 +00:00
|
|
|
struct tr_variant;
|
2022-02-23 21:09:54 +00:00
|
|
|
struct tr_rpc_address;
|
2022-01-17 16:20:12 +00:00
|
|
|
struct libdeflate_compressor;
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2022-10-01 14:12:49 +00:00
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
class Timer;
|
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
#define RPC_SETTINGS_FIELDS(V) \
|
|
|
|
V(TR_KEY_anti_brute_force_enabled, is_anti_brute_force_enabled_, bool, false, "") \
|
|
|
|
V(TR_KEY_anti_brute_force_threshold, anti_brute_force_limit_, size_t, 100U, "") \
|
|
|
|
V(TR_KEY_rpc_authentication_required, authentication_required_, bool, false, "") \
|
|
|
|
V(TR_KEY_rpc_bind_address, bind_address_str_, std::string, "0.0.0.0", "") \
|
|
|
|
V(TR_KEY_rpc_enabled, is_enabled_, bool, false, "") \
|
|
|
|
V(TR_KEY_rpc_host_whitelist, host_whitelist_str_, std::string, "", "") \
|
|
|
|
V(TR_KEY_rpc_host_whitelist_enabled, is_host_whitelist_enabled_, bool, true, "") \
|
|
|
|
V(TR_KEY_rpc_port, port_, tr_port, tr_port::fromHost(TR_DEFAULT_RPC_PORT), "") \
|
|
|
|
V(TR_KEY_rpc_password, salted_password_, std::string, "", "") \
|
|
|
|
V(TR_KEY_rpc_socket_mode, socket_mode_, tr_mode_t, 0750, "") \
|
|
|
|
V(TR_KEY_rpc_url, url_, std::string, TR_DEFAULT_RPC_URL_STR, "") \
|
|
|
|
V(TR_KEY_rpc_username, username_, std::string, "", "") \
|
|
|
|
V(TR_KEY_rpc_whitelist, whitelist_str_, std::string, TR_DEFAULT_RPC_WHITELIST, "") \
|
|
|
|
V(TR_KEY_rpc_whitelist_enabled, is_whitelist_enabled_, bool, true, "")
|
|
|
|
|
2021-11-14 21:14:37 +00:00
|
|
|
class tr_rpc_server
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
tr_rpc_server(tr_session* session, tr_variant* settings);
|
|
|
|
~tr_rpc_server();
|
|
|
|
|
2021-11-18 00:17:09 +00:00
|
|
|
tr_rpc_server(tr_rpc_server&) = delete;
|
|
|
|
tr_rpc_server(tr_rpc_server&&) = delete;
|
|
|
|
tr_rpc_server& operator=(tr_rpc_server&) = delete;
|
|
|
|
tr_rpc_server& operator=(tr_rpc_server&&) = delete;
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void load(tr_variant* src);
|
|
|
|
void save(tr_variant* tgt) const;
|
|
|
|
static void defaultSettings(tr_variant* tgt);
|
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
[[nodiscard]] constexpr tr_port port() const noexcept
|
|
|
|
{
|
|
|
|
return port_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPort(tr_port) noexcept;
|
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
[[nodiscard]] constexpr auto isEnabled() const noexcept
|
|
|
|
{
|
|
|
|
return is_enabled_;
|
|
|
|
}
|
2021-11-14 21:14:37 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
void setEnabled(bool is_enabled);
|
2021-11-14 21:14:37 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
[[nodiscard]] constexpr auto isWhitelistEnabled() const noexcept
|
|
|
|
{
|
|
|
|
return is_whitelist_enabled_;
|
|
|
|
}
|
2021-11-14 21:14:37 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
constexpr void setWhitelistEnabled(bool is_whitelist_enabled) noexcept
|
|
|
|
{
|
|
|
|
is_whitelist_enabled_ = is_whitelist_enabled;
|
|
|
|
}
|
2021-11-14 21:14:37 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
[[nodiscard]] constexpr auto const& whitelist() const noexcept
|
|
|
|
{
|
|
|
|
return whitelist_str_;
|
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
void setWhitelist(std::string_view whitelist);
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
[[nodiscard]] constexpr auto const& username() const noexcept
|
|
|
|
{
|
|
|
|
return username_;
|
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
void setUsername(std::string_view username);
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
[[nodiscard]] constexpr auto isPasswordEnabled() const noexcept
|
|
|
|
{
|
|
|
|
return is_password_enabled_;
|
|
|
|
}
|
2010-12-12 18:22:11 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
void setPasswordEnabled(bool enabled);
|
2010-12-12 18:22:11 +00:00
|
|
|
|
2022-04-22 16:35:56 +00:00
|
|
|
[[nodiscard]] constexpr auto const& getSaltedPassword() const noexcept
|
2022-04-22 14:19:20 +00:00
|
|
|
{
|
|
|
|
return salted_password_;
|
|
|
|
}
|
2008-06-04 17:14:58 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void setPassword(std::string_view password) noexcept;
|
2008-10-01 22:59:29 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
[[nodiscard]] constexpr auto isAntiBruteForceEnabled() const noexcept
|
|
|
|
{
|
|
|
|
return is_anti_brute_force_enabled_;
|
|
|
|
}
|
2008-10-01 22:59:29 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
void setAntiBruteForceEnabled(bool enabled) noexcept;
|
2018-01-11 18:00:41 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
[[nodiscard]] constexpr auto getAntiBruteForceLimit() const noexcept
|
|
|
|
{
|
|
|
|
return anti_brute_force_limit_;
|
|
|
|
}
|
2022-02-23 21:09:54 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
constexpr void setAntiBruteForceLimit(int limit) noexcept
|
|
|
|
{
|
|
|
|
anti_brute_force_limit_ = limit;
|
|
|
|
}
|
2018-01-11 18:00:41 +00:00
|
|
|
|
2022-08-31 00:30:47 +00:00
|
|
|
std::unique_ptr<libdeflate_compressor, void (*)(libdeflate_compressor*)> compressor;
|
2008-06-05 16:23:03 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
[[nodiscard]] constexpr auto const& url() const noexcept
|
|
|
|
{
|
|
|
|
return url_;
|
|
|
|
}
|
2008-06-05 16:23:03 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
void setUrl(std::string_view url);
|
2008-06-05 16:23:03 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
[[nodiscard]] std::string getBindAddress() const;
|
2008-06-05 16:23:03 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
[[nodiscard]] constexpr auto socketMode() const noexcept
|
|
|
|
{
|
|
|
|
return socket_mode_;
|
|
|
|
}
|
2008-06-05 16:23:03 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
#define V(key, name, type, default_value, comment) type name = type{ default_value };
|
|
|
|
RPC_SETTINGS_FIELDS(V)
|
|
|
|
#undef V
|
|
|
|
|
|
|
|
std::vector<std::string> host_whitelist_;
|
2022-04-22 14:19:20 +00:00
|
|
|
std::vector<std::string> whitelist_;
|
2022-07-23 01:10:02 +00:00
|
|
|
std::string const web_client_dir_;
|
2008-08-15 20:28:43 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
std::unique_ptr<struct tr_rpc_address> bind_address_;
|
2020-11-14 18:43:42 +00:00
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
std::unique_ptr<libtransmission::Timer> start_retry_timer;
|
2022-11-28 14:07:04 +00:00
|
|
|
libtransmission::evhelpers::evhttp_unique_ptr httpd;
|
2022-04-22 14:19:20 +00:00
|
|
|
tr_session* const session;
|
2020-11-14 18:43:42 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
size_t login_attempts_ = 0U;
|
2022-04-22 14:19:20 +00:00
|
|
|
int start_retry_counter = 0;
|
2020-11-14 18:43:42 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
bool is_password_enabled_ = false;
|
|
|
|
};
|