31 #ifndef NETWORK_TORCONTROL_HPP_ 32 #define NETWORK_TORCONTROL_HPP_ 36 #include "../Main/Exception.hpp" 37 #include "../Timer/Simple.hpp" 45 #include <string_view> 87 std::string_view controlServer,
88 std::uint16_t controlPort,
89 std::string_view controlPassword
97 [[nodiscard]]
bool active()
const noexcept;
152 const bool isActive{
false};
153 const std::string server;
154 const std::uint16_t port{};
155 const std::string password;
156 std::uint64_t newIdentityNotBefore{};
157 std::uint64_t newIdentityAfter{};
160 asio::io_context context;
161 asio::ip::tcp::socket socket;
166 std::uint64_t elapsedMin{};
167 std::uint64_t elapsedMax{};
187 std::string_view controlServer,
188 std::uint16_t controlPort,
189 std::string_view controlPassword
190 ) : isActive(!controlServer.empty()),
191 server(controlServer),
193 password(controlPassword),
194 socket(this->context) {}
201 if(this->socket.is_open()) {
202 asio::error_code error;
204 this->socket.shutdown(asio::ip::tcp::socket::shutdown_both, error);
207 std::cerr <<
"TorControl::~TorControl(): " 220 return this->isActive;
234 this->newIdentityNotBefore = seconds;
237 this->elapsedMin = 0;
239 this->minTimer.
tick();
251 this->newIdentityAfter = seconds;
254 this->elapsedMax = 0;
256 this->maxTimer.
tick();
276 if(!(this->isActive)) {
277 throw Exception(
"No TOR control server/port set");
281 if(this->newIdentityNotBefore > 0) {
282 this->elapsedMin += this->minTimer.
tick();
284 if(this->elapsedMin / millisecondsPerSecond < this->newIdentityNotBefore) {
288 this->elapsedMin = 0;
292 asio::ip::tcp::resolver resolver(this->context);
296 asio::ip::tcp::resolver::results_type endpoints{
299 std::to_string(this->port),
300 asio::ip::tcp::resolver::numeric_service
305 asio::connect(this->socket, endpoints);
308 const std::string auth(
"AUTHENTICATE \"" + this->password +
"\"\n");
310 asio::write(this->socket, asio::buffer(auth.data(), auth.size()));
313 std::array<char, responseCodeLength> response{};
318 if(response[0] !=
'2' || response[1] !=
'5' || response[2] !=
'0') {
319 throw Exception(
"Authentification failed");
323 const std::string command(
"SIGNAL NEWNYM\r\n");
325 asio::write(this->socket, asio::buffer(command.data(), command.size()));
328 if(this->newIdentityAfter > 0) {
329 this->elapsedMax = 0;
331 this->maxTimer.
tick();
334 catch(
const asio::system_error& e) {
348 if(this->isActive && this->newIdentityAfter > 0) {
350 this->elapsedMin += this->minTimer.
tick();
351 this->elapsedMax += this->maxTimer.
tick();
359 this->elapsedMax = 0;
361 this->maxTimer.
tick();
Class for TOR control exceptions.
Definition: TorControl.hpp:129
TorControl & operator=(TorControl &)=delete
Deleted copy assignment operator.
#define MAIN_EXCEPTION_CLASS()
Macro used to easily define classes for general exceptions.
Definition: Exception.hpp:50
TorControl(std::string_view controlServer, std::uint16_t controlPort, std::string_view controlPassword)
Constructor creating context and socket for the connection to the TOR control server/port.
Definition: TorControl.hpp:186
bool newIdentity()
Requests a new TOR identity via the set TOR control server/port.
Definition: TorControl.hpp:274
void setNewIdentityMax(std::uint64_t seconds)
Sets the time (in seconds) after which to automatically request a new TOR identity.
Definition: TorControl.hpp:250
constexpr auto millisecondsPerSecond
The number of milliseconds per second.
Definition: TorControl.hpp:60
A simple timer.
Definition: Simple.hpp:53
Controls a TOR service via a TOR control server/port, if available.
Definition: TorControl.hpp:81
void tick()
Checks whether to request a new TOR identity.
Definition: TorControl.hpp:346
void setNewIdentityMin(std::uint64_t seconds)
Sets the time (in seconds) in which to ignore requests for a new identity.
Definition: TorControl.hpp:233
void write(const std::string &fileName, const std::string &content, bool binary)
Writes the given content to the given file.
Definition: File.hpp:137
std::uint64_t tick()
Timer tick returning the number of milliseconds passed.
Definition: Simple.hpp:98
bool active() const noexcept
Gets whether a TOR control server/port is set.
Definition: TorControl.hpp:219
constexpr auto responseCodeLength
The length of a HTTP response code.
Definition: TorControl.hpp:57
Namespace for networking classes.
Definition: Config.hpp:45
virtual ~TorControl()
Destructor shutting down remaining connections to the TOR control server/port if necessary.
Definition: TorControl.hpp:200