2023-11-01 22:11:11 +01:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-08-08 13:05:39 -05:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 12:27:56 -06: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 23:21:28 +03:00
|
|
|
#pragma once
|
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 15:04:45 +03:00
|
|
|
#error only libtransmission should #include this header.
|
2008-11-24 20:17:36 +00:00
|
|
|
#endif
|
|
|
|
|
2023-06-29 23:51:55 -05:00
|
|
|
#include <cstddef> // size_t
|
2022-01-17 10:20:12 -06:00
|
|
|
#include <memory>
|
2021-11-14 15:14:37 -06:00
|
|
|
#include <string>
|
2021-11-14 00:02:45 -06:00
|
|
|
#include <string_view>
|
2022-04-22 08:08:02 -05:00
|
|
|
#include <vector>
|
2021-11-14 00:02:45 -06:00
|
|
|
|
2023-07-08 23:24:03 +08:00
|
|
|
#include "libtransmission/transmission.h"
|
2021-11-14 00:02:45 -06:00
|
|
|
|
2023-07-08 23:24:03 +08:00
|
|
|
#include "libtransmission/net.h"
|
|
|
|
#include "libtransmission/quark.h"
|
2024-02-07 10:14:47 -06:00
|
|
|
#include "libtransmission/settings.h"
|
2023-07-08 23:24:03 +08:00
|
|
|
#include "libtransmission/utils-ev.h"
|
2021-11-14 00:02:45 -06:00
|
|
|
|
2023-06-19 16:51:20 -04:00
|
|
|
class tr_rpc_address;
|
2023-07-08 23:24:03 +08:00
|
|
|
struct tr_session;
|
|
|
|
struct tr_variant;
|
2022-01-17 10:20:12 -06:00
|
|
|
struct libdeflate_compressor;
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2022-10-01 09:12:49 -05:00
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
class Timer;
|
|
|
|
}
|
|
|
|
|
2021-11-14 15:14:37 -06:00
|
|
|
class tr_rpc_server
|
|
|
|
{
|
|
|
|
public:
|
2024-02-07 10:14:47 -06: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 11:31:09 -06: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 10:14:47 -06: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 11:31:09 -06:00
|
|
|
tr_mode_t socket_mode = 0750;
|
|
|
|
tr_port port = tr_port::from_host(TR_DEFAULT_RPC_PORT);
|
2024-02-07 10:14:47 -06: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 11:31:09 -06:00
|
|
|
tr_rpc_server(tr_session* session, Settings&& settings);
|
2021-11-14 15:14:37 -06:00
|
|
|
~tr_rpc_server();
|
|
|
|
|
2021-11-17 18:17:09 -06: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 11:31:09 -06:00
|
|
|
void load(Settings&& settings);
|
2024-02-07 10:14:47 -06:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr Settings const& settings() const
|
|
|
|
{
|
|
|
|
return settings_;
|
|
|
|
}
|
2022-11-01 19:32:26 -05:00
|
|
|
|
2022-04-21 10:58:13 -05:00
|
|
|
[[nodiscard]] constexpr tr_port port() const noexcept
|
|
|
|
{
|
2024-02-07 10:14:47 -06:00
|
|
|
return settings_.port;
|
2022-04-21 10:58:13 -05:00
|
|
|
}
|
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
void set_port(tr_port port) noexcept;
|
2022-04-21 10:58:13 -05:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
[[nodiscard]] constexpr auto is_enabled() const noexcept
|
2022-04-22 09:19:20 -05:00
|
|
|
{
|
2024-02-07 10:14:47 -06:00
|
|
|
return settings_.is_enabled;
|
2022-04-22 09:19:20 -05:00
|
|
|
}
|
2021-11-14 15:14:37 -06:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
void set_enabled(bool is_enabled);
|
2021-11-14 15:14:37 -06:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
[[nodiscard]] constexpr auto is_whitelist_enabled() const noexcept
|
2022-04-22 09:19:20 -05:00
|
|
|
{
|
2024-02-07 10:14:47 -06:00
|
|
|
return settings_.is_whitelist_enabled;
|
2022-04-22 09:19:20 -05:00
|
|
|
}
|
2021-11-14 15:14:37 -06:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
constexpr void set_whitelist_enabled(bool is_whitelist_enabled) noexcept
|
2022-04-22 09:19:20 -05:00
|
|
|
{
|
2024-02-07 10:14:47 -06:00
|
|
|
settings_.is_whitelist_enabled = is_whitelist_enabled;
|
2022-04-22 09:19:20 -05:00
|
|
|
}
|
2021-11-14 15:14:37 -06:00
|
|
|
|
2022-04-22 09:19:20 -05:00
|
|
|
[[nodiscard]] constexpr auto const& whitelist() const noexcept
|
|
|
|
{
|
2024-02-07 10:14:47 -06:00
|
|
|
return settings_.whitelist_str;
|
2022-04-22 09:19:20 -05:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
void set_whitelist(std::string_view whitelist);
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2022-04-22 09:19:20 -05:00
|
|
|
[[nodiscard]] constexpr auto const& username() const noexcept
|
|
|
|
{
|
2024-02-07 10:14:47 -06:00
|
|
|
return settings_.username;
|
2022-04-22 09:19:20 -05:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
void set_username(std::string_view username);
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
[[nodiscard]] constexpr auto is_password_enabled() const noexcept
|
2022-04-22 09:19:20 -05:00
|
|
|
{
|
2024-02-07 10:14:47 -06:00
|
|
|
return settings_.authentication_required;
|
2022-04-22 09:19:20 -05:00
|
|
|
}
|
2010-12-12 18:22:11 +00:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
void set_password_enabled(bool enabled);
|
2010-12-12 18:22:11 +00:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
[[nodiscard]] constexpr auto const& get_salted_password() const noexcept
|
2022-04-22 09:19:20 -05:00
|
|
|
{
|
2024-02-07 10:14:47 -06:00
|
|
|
return settings_.salted_password;
|
2022-04-22 09:19:20 -05:00
|
|
|
}
|
2008-06-04 17:14:58 +00:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
void set_password(std::string_view password) noexcept;
|
2008-10-01 22:59:29 +00:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
[[nodiscard]] constexpr auto is_anti_brute_force_enabled() const noexcept
|
2022-04-22 09:19:20 -05:00
|
|
|
{
|
2024-02-07 10:14:47 -06:00
|
|
|
return settings_.is_anti_brute_force_enabled;
|
2022-04-22 09:19:20 -05:00
|
|
|
}
|
2008-10-01 22:59:29 +00:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
void set_anti_brute_force_enabled(bool enabled) noexcept;
|
2018-01-11 10:00:41 -08:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
[[nodiscard]] constexpr auto get_anti_brute_force_limit() const noexcept
|
2022-04-22 09:19:20 -05:00
|
|
|
{
|
2024-02-07 10:14:47 -06:00
|
|
|
return settings_.anti_brute_force_limit;
|
2022-04-22 09:19:20 -05:00
|
|
|
}
|
2022-02-23 16:09:54 -05:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
constexpr void set_anti_brute_force_limit(int limit) noexcept
|
2022-04-22 09:19:20 -05:00
|
|
|
{
|
2024-02-07 10:14:47 -06:00
|
|
|
settings_.anti_brute_force_limit = limit;
|
2022-04-22 09:19:20 -05:00
|
|
|
}
|
2018-01-11 10:00:41 -08:00
|
|
|
|
2022-08-30 19:30:47 -05:00
|
|
|
std::unique_ptr<libdeflate_compressor, void (*)(libdeflate_compressor*)> compressor;
|
2008-06-05 16:23:03 +00:00
|
|
|
|
2022-04-22 09:19:20 -05:00
|
|
|
[[nodiscard]] constexpr auto const& url() const noexcept
|
|
|
|
{
|
2024-02-07 10:14:47 -06:00
|
|
|
return settings_.url;
|
2022-04-22 09:19:20 -05:00
|
|
|
}
|
2008-06-05 16:23:03 +00:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
void set_url(std::string_view url);
|
2008-06-05 16:23:03 +00:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
[[nodiscard]] std::string get_bind_address() const;
|
2008-06-05 16:23:03 +00:00
|
|
|
|
2023-05-05 23:11:05 -05:00
|
|
|
[[nodiscard]] constexpr auto socket_mode() const noexcept
|
2022-04-22 09:19:20 -05:00
|
|
|
{
|
2024-02-07 10:14:47 -06:00
|
|
|
return settings_.socket_mode;
|
2022-04-22 09:19:20 -05:00
|
|
|
}
|
2008-06-05 16:23:03 +00:00
|
|
|
|
2024-02-07 10:14:47 -06:00
|
|
|
Settings settings_;
|
2022-11-01 19:32:26 -05:00
|
|
|
|
|
|
|
std::vector<std::string> host_whitelist_;
|
2022-04-22 09:19:20 -05:00
|
|
|
std::vector<std::string> whitelist_;
|
2022-07-22 20:10:02 -05:00
|
|
|
std::string const web_client_dir_;
|
2008-08-15 20:28:43 +00:00
|
|
|
|
2023-07-08 23:24:03 +08:00
|
|
|
std::unique_ptr<tr_rpc_address> bind_address_;
|
2020-11-15 05:43:42 +11:00
|
|
|
|
2022-08-11 12:28:37 -05:00
|
|
|
std::unique_ptr<libtransmission::Timer> start_retry_timer;
|
2022-11-28 08:07:04 -06:00
|
|
|
libtransmission::evhelpers::evhttp_unique_ptr httpd;
|
2022-04-22 09:19:20 -05:00
|
|
|
tr_session* const session;
|
2020-11-15 05:43:42 +11:00
|
|
|
|
2022-11-01 19:32:26 -05:00
|
|
|
size_t login_attempts_ = 0U;
|
2022-04-22 09:19:20 -05:00
|
|
|
int start_retry_counter = 0;
|
|
|
|
};
|