2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © 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
|
|
|
|
|
2023-06-30 04:51:55 +00:00
|
|
|
#include <cstddef> // size_t
|
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
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2021-11-14 06:02:45 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/net.h"
|
|
|
|
#include "libtransmission/quark.h"
|
2024-02-07 16:14:47 +00:00
|
|
|
#include "libtransmission/settings.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/utils-ev.h"
|
2021-11-14 06:02:45 +00:00
|
|
|
|
2023-06-19 20:51:20 +00:00
|
|
|
class tr_rpc_address;
|
2023-07-08 15:24:03 +00:00
|
|
|
struct tr_session;
|
|
|
|
struct tr_variant;
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-14 21:14:37 +00:00
|
|
|
class tr_rpc_server
|
|
|
|
{
|
|
|
|
public:
|
2024-02-07 16:14:47 +00:00
|
|
|
class Settings final : public libtransmission::Settings
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Settings() = default;
|
|
|
|
|
|
|
|
explicit Settings(tr_variant const& src)
|
|
|
|
{
|
|
|
|
load(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: When adding a field here, you must also add it to
|
|
|
|
// fields() if you want it to be in session-settings.json
|
|
|
|
bool authentication_required = false;
|
|
|
|
bool is_anti_brute_force_enabled = false;
|
|
|
|
bool is_enabled = false;
|
|
|
|
bool is_host_whitelist_enabled = true;
|
|
|
|
bool is_whitelist_enabled = true;
|
2024-02-15 17:31:09 +00:00
|
|
|
size_t anti_brute_force_limit = 100U;
|
|
|
|
std::string bind_address_str = "0.0.0.0";
|
|
|
|
std::string host_whitelist_str = "";
|
2024-02-07 16:14:47 +00:00
|
|
|
std::string salted_password = "";
|
|
|
|
std::string url = TR_DEFAULT_RPC_URL_STR;
|
|
|
|
std::string username = "";
|
|
|
|
std::string whitelist_str = TR_DEFAULT_RPC_WHITELIST;
|
2024-02-15 17:31:09 +00:00
|
|
|
tr_mode_t socket_mode = 0750;
|
|
|
|
tr_port port = tr_port::from_host(TR_DEFAULT_RPC_PORT);
|
2024-02-07 16:14:47 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
[[nodiscard]] Fields fields() override
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
{ TR_KEY_anti_brute_force_enabled, &is_anti_brute_force_enabled },
|
|
|
|
{ TR_KEY_anti_brute_force_threshold, &anti_brute_force_limit },
|
|
|
|
{ TR_KEY_rpc_authentication_required, &authentication_required },
|
|
|
|
{ TR_KEY_rpc_bind_address, &bind_address_str },
|
|
|
|
{ TR_KEY_rpc_enabled, &is_enabled },
|
|
|
|
{ TR_KEY_rpc_host_whitelist, &host_whitelist_str },
|
|
|
|
{ TR_KEY_rpc_host_whitelist_enabled, &is_host_whitelist_enabled },
|
|
|
|
{ TR_KEY_rpc_port, &port },
|
|
|
|
{ TR_KEY_rpc_password, &salted_password },
|
|
|
|
{ TR_KEY_rpc_socket_mode, &socket_mode },
|
|
|
|
{ TR_KEY_rpc_url, &url },
|
|
|
|
{ TR_KEY_rpc_username, &username },
|
|
|
|
{ TR_KEY_rpc_whitelist, &whitelist_str },
|
|
|
|
{ TR_KEY_rpc_whitelist_enabled, &is_whitelist_enabled },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-02-15 17:31:09 +00:00
|
|
|
tr_rpc_server(tr_session* session, Settings&& settings);
|
2021-11-14 21:14:37 +00:00
|
|
|
~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;
|
|
|
|
|
2024-02-15 17:31:09 +00:00
|
|
|
void load(Settings&& settings);
|
2024-02-07 16:14:47 +00:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr Settings const& settings() const
|
|
|
|
{
|
|
|
|
return settings_;
|
|
|
|
}
|
2022-11-02 00:32:26 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
[[nodiscard]] constexpr tr_port port() const noexcept
|
|
|
|
{
|
2024-02-07 16:14:47 +00:00
|
|
|
return settings_.port;
|
2022-04-21 15:58:13 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_port(tr_port port) noexcept;
|
2022-04-21 15:58:13 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] constexpr auto is_enabled() const noexcept
|
2022-04-22 14:19:20 +00:00
|
|
|
{
|
2024-02-07 16:14:47 +00:00
|
|
|
return settings_.is_enabled;
|
2022-04-22 14:19:20 +00:00
|
|
|
}
|
2021-11-14 21:14:37 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_enabled(bool is_enabled);
|
2021-11-14 21:14:37 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] constexpr auto is_whitelist_enabled() const noexcept
|
2022-04-22 14:19:20 +00:00
|
|
|
{
|
2024-02-07 16:14:47 +00:00
|
|
|
return settings_.is_whitelist_enabled;
|
2022-04-22 14:19:20 +00:00
|
|
|
}
|
2021-11-14 21:14:37 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
constexpr void set_whitelist_enabled(bool is_whitelist_enabled) noexcept
|
2022-04-22 14:19:20 +00:00
|
|
|
{
|
2024-02-07 16:14:47 +00:00
|
|
|
settings_.is_whitelist_enabled = is_whitelist_enabled;
|
2022-04-22 14:19:20 +00:00
|
|
|
}
|
2021-11-14 21:14:37 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
[[nodiscard]] constexpr auto const& whitelist() const noexcept
|
|
|
|
{
|
2024-02-07 16:14:47 +00:00
|
|
|
return settings_.whitelist_str;
|
2022-04-22 14:19:20 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_whitelist(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
|
|
|
|
{
|
2024-02-07 16:14:47 +00:00
|
|
|
return settings_.username;
|
2022-04-22 14:19:20 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_username(std::string_view username);
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] constexpr auto is_password_enabled() const noexcept
|
2022-04-22 14:19:20 +00:00
|
|
|
{
|
2024-02-07 16:14:47 +00:00
|
|
|
return settings_.authentication_required;
|
2022-04-22 14:19:20 +00:00
|
|
|
}
|
2010-12-12 18:22:11 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_password_enabled(bool enabled);
|
2010-12-12 18:22:11 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] constexpr auto const& get_salted_password() const noexcept
|
2022-04-22 14:19:20 +00:00
|
|
|
{
|
2024-02-07 16:14:47 +00:00
|
|
|
return settings_.salted_password;
|
2022-04-22 14:19:20 +00:00
|
|
|
}
|
2008-06-04 17:14:58 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_password(std::string_view password) noexcept;
|
2008-10-01 22:59:29 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] constexpr auto is_anti_brute_force_enabled() const noexcept
|
2022-04-22 14:19:20 +00:00
|
|
|
{
|
2024-02-07 16:14:47 +00:00
|
|
|
return settings_.is_anti_brute_force_enabled;
|
2022-04-22 14:19:20 +00:00
|
|
|
}
|
2008-10-01 22:59:29 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_anti_brute_force_enabled(bool enabled) noexcept;
|
2018-01-11 18:00:41 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] constexpr auto get_anti_brute_force_limit() const noexcept
|
2022-04-22 14:19:20 +00:00
|
|
|
{
|
2024-02-07 16:14:47 +00:00
|
|
|
return settings_.anti_brute_force_limit;
|
2022-04-22 14:19:20 +00:00
|
|
|
}
|
2022-02-23 21:09:54 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
constexpr void set_anti_brute_force_limit(int limit) noexcept
|
2022-04-22 14:19:20 +00:00
|
|
|
{
|
2024-02-07 16:14:47 +00:00
|
|
|
settings_.anti_brute_force_limit = limit;
|
2022-04-22 14:19:20 +00:00
|
|
|
}
|
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
|
|
|
|
{
|
2024-02-07 16:14:47 +00:00
|
|
|
return settings_.url;
|
2022-04-22 14:19:20 +00:00
|
|
|
}
|
2008-06-05 16:23:03 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_url(std::string_view url);
|
2008-06-05 16:23:03 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] std::string get_bind_address() const;
|
2008-06-05 16:23:03 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] constexpr auto socket_mode() const noexcept
|
2022-04-22 14:19:20 +00:00
|
|
|
{
|
2024-02-07 16:14:47 +00:00
|
|
|
return settings_.socket_mode;
|
2022-04-22 14:19:20 +00:00
|
|
|
}
|
2008-06-05 16:23:03 +00:00
|
|
|
|
2024-02-07 16:14:47 +00:00
|
|
|
Settings settings_;
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
std::unique_ptr<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;
|
|
|
|
};
|