2021-08-22 04:20:59 -06:00
|
|
|
/*
|
|
|
|
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
2023-01-04 05:07:55 -07:00
|
|
|
* Copyright (c) 2021-2023 SChernykh <https://github.com/SChernykh>
|
2021-08-22 04:20:59 -06:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "uv_util.h"
|
|
|
|
|
|
|
|
namespace p2pool {
|
|
|
|
|
|
|
|
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
|
|
|
|
class TCPServer : public nocopy_nomove
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct Client;
|
|
|
|
typedef Client* (*allocate_client_callback)();
|
|
|
|
|
2021-08-28 16:13:53 -06:00
|
|
|
explicit TCPServer(allocate_client_callback allocate_new_client);
|
2021-08-22 04:20:59 -06:00
|
|
|
virtual ~TCPServer();
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void parse_address_list(const std::string& address_list, T callback);
|
|
|
|
|
|
|
|
bool connect_to_peer(bool is_v6, const char* ip, int port);
|
|
|
|
|
2022-09-08 08:16:00 -06:00
|
|
|
void drop_connections_async() { if (m_finished.load() == 0) { uv_async_send(&m_dropConnectionsAsync); } }
|
2021-08-22 04:20:59 -06:00
|
|
|
void shutdown_tcp();
|
|
|
|
virtual void print_status();
|
|
|
|
|
|
|
|
uv_loop_t* get_loop() { return &m_loop; }
|
|
|
|
|
2022-10-28 06:06:26 -06:00
|
|
|
virtual int listen_port() const { return m_listenPort; }
|
2021-08-22 04:20:59 -06:00
|
|
|
|
|
|
|
bool connect_to_peer(bool is_v6, const raw_ip& ip, int port);
|
2022-08-31 08:37:33 -06:00
|
|
|
virtual void on_connect_failed(bool /*is_v6*/, const raw_ip& /*ip*/, int /*port*/) {}
|
2021-08-22 04:20:59 -06:00
|
|
|
|
|
|
|
void ban(const raw_ip& ip, uint64_t seconds);
|
2022-02-21 11:59:57 -07:00
|
|
|
virtual void print_bans();
|
2021-08-22 04:20:59 -06:00
|
|
|
|
|
|
|
struct Client
|
|
|
|
{
|
|
|
|
Client();
|
2022-04-08 15:14:08 -06:00
|
|
|
virtual ~Client() {}
|
2021-08-22 04:20:59 -06:00
|
|
|
|
2023-02-17 00:47:52 -07:00
|
|
|
virtual size_t size() const = 0;
|
|
|
|
|
2021-08-22 04:20:59 -06:00
|
|
|
virtual void reset();
|
|
|
|
virtual bool on_connect() = 0;
|
|
|
|
virtual bool on_read(char* data, uint32_t size) = 0;
|
2022-08-31 08:37:33 -06:00
|
|
|
bool on_proxy_handshake(char* data, uint32_t size);
|
2021-09-06 08:17:20 -06:00
|
|
|
virtual void on_read_failed(int /*err*/) {}
|
2021-09-07 01:53:38 -06:00
|
|
|
virtual void on_disconnected() {}
|
2021-08-22 04:20:59 -06:00
|
|
|
|
|
|
|
static void on_alloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
|
|
|
|
static void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);
|
|
|
|
static void on_write(uv_write_t* req, int status);
|
|
|
|
|
|
|
|
void close();
|
|
|
|
void ban(uint64_t seconds);
|
|
|
|
|
2022-08-31 08:37:33 -06:00
|
|
|
void init_addr_string();
|
2021-08-22 04:20:59 -06:00
|
|
|
|
2022-05-07 04:29:45 -06:00
|
|
|
alignas(8) char m_readBuf[READ_BUF_SIZE];
|
|
|
|
|
2021-08-22 04:20:59 -06:00
|
|
|
TCPServer* m_owner;
|
|
|
|
|
|
|
|
// Used to maintain connected clients list
|
|
|
|
Client* m_prev;
|
|
|
|
Client* m_next;
|
|
|
|
|
|
|
|
uv_tcp_t m_socket;
|
|
|
|
|
|
|
|
bool m_isV6;
|
|
|
|
bool m_isIncoming;
|
2022-05-07 04:29:45 -06:00
|
|
|
bool m_readBufInUse;
|
2022-07-06 06:17:09 -06:00
|
|
|
bool m_isClosing;
|
2022-05-07 04:29:45 -06:00
|
|
|
uint32_t m_numRead;
|
|
|
|
|
2021-08-22 04:20:59 -06:00
|
|
|
raw_ip m_addr;
|
|
|
|
int m_port;
|
2022-08-31 08:37:33 -06:00
|
|
|
char m_addrString[72];
|
|
|
|
|
|
|
|
enum class Socks5ProxyState {
|
|
|
|
Default,
|
|
|
|
MethodSelectionSent,
|
|
|
|
ConnectRequestSent,
|
|
|
|
} m_socks5ProxyState;
|
2021-08-22 04:20:59 -06:00
|
|
|
|
2022-05-09 11:13:34 -06:00
|
|
|
std::atomic<uint32_t> m_resetCounter;
|
2021-08-22 04:20:59 -06:00
|
|
|
};
|
|
|
|
|
2021-10-31 08:25:59 -06:00
|
|
|
struct WriteBuf
|
|
|
|
{
|
2022-02-25 07:31:35 -07:00
|
|
|
uv_write_t m_write = {};
|
2023-01-20 09:03:59 -07:00
|
|
|
Client* m_client = nullptr;
|
|
|
|
void* m_data = nullptr;
|
|
|
|
size_t m_dataCapacity = 0;
|
2021-10-31 08:25:59 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<WriteBuf*> m_writeBuffers;
|
|
|
|
|
2023-02-17 00:47:52 -07:00
|
|
|
WriteBuf* get_write_buffer();
|
|
|
|
void return_write_buffer(WriteBuf* buf);
|
|
|
|
|
2021-08-22 04:20:59 -06:00
|
|
|
struct SendCallbackBase
|
|
|
|
{
|
|
|
|
virtual ~SendCallbackBase() {}
|
2022-05-04 07:53:01 -06:00
|
|
|
virtual size_t operator()(void*, size_t) = 0;
|
2021-08-22 04:20:59 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct SendCallback : public SendCallbackBase
|
|
|
|
{
|
|
|
|
explicit FORCEINLINE SendCallback(T&& callback) : m_callback(std::move(callback)) {}
|
2022-05-04 07:53:01 -06:00
|
|
|
size_t operator()(void* buf, size_t buf_size) override { return m_callback(buf, buf_size); }
|
2021-08-22 04:20:59 -06:00
|
|
|
|
|
|
|
private:
|
|
|
|
SendCallback& operator=(SendCallback&&) = delete;
|
|
|
|
|
|
|
|
T m_callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
FORCEINLINE bool send(Client* client, T&& callback) { return send_internal(client, SendCallback<T>(std::move(callback))); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void loop(void* data);
|
|
|
|
static void on_new_connection(uv_stream_t* server, int status);
|
|
|
|
static void on_connection_close(uv_handle_t* handle);
|
2022-06-07 14:25:27 -06:00
|
|
|
static void on_connection_error(uv_handle_t* handle);
|
2021-08-22 04:20:59 -06:00
|
|
|
static void on_connect(uv_connect_t* req, int status);
|
|
|
|
void on_new_client(uv_stream_t* server);
|
2022-08-31 08:37:33 -06:00
|
|
|
void on_new_client(uv_stream_t* server, Client* client);
|
2021-08-22 04:20:59 -06:00
|
|
|
|
2022-08-31 08:37:33 -06:00
|
|
|
bool connect_to_peer(Client* client);
|
2021-08-22 04:20:59 -06:00
|
|
|
|
|
|
|
bool send_internal(Client* client, SendCallbackBase&& callback);
|
|
|
|
|
|
|
|
allocate_client_callback m_allocateNewClient;
|
|
|
|
|
2021-08-26 15:27:05 -06:00
|
|
|
void close_sockets(bool listen_sockets);
|
2021-08-22 04:20:59 -06:00
|
|
|
|
|
|
|
std::vector<uv_tcp_t*> m_listenSockets6;
|
|
|
|
std::vector<uv_tcp_t*> m_listenSockets;
|
|
|
|
uv_thread_t m_loopThread;
|
|
|
|
|
|
|
|
protected:
|
2021-08-26 15:27:05 -06:00
|
|
|
void start_listening(const std::string& listen_addresses);
|
|
|
|
|
2022-08-31 08:37:33 -06:00
|
|
|
std::string m_socks5Proxy;
|
|
|
|
bool m_socks5ProxyV6;
|
|
|
|
raw_ip m_socks5ProxyIP;
|
|
|
|
int m_socks5ProxyPort;
|
|
|
|
|
2021-09-06 01:02:35 -06:00
|
|
|
std::atomic<int> m_finished;
|
2021-08-22 04:20:59 -06:00
|
|
|
int m_listenPort;
|
|
|
|
|
|
|
|
uv_loop_t m_loop;
|
|
|
|
|
|
|
|
uv_mutex_t m_clientsListLock;
|
|
|
|
std::vector<Client*> m_preallocatedClients;
|
2023-02-17 00:47:52 -07:00
|
|
|
|
|
|
|
Client* get_client();
|
|
|
|
void return_client(Client* c);
|
|
|
|
|
2021-08-22 04:20:59 -06:00
|
|
|
Client* m_connectedClientsList;
|
2022-05-09 08:07:49 -06:00
|
|
|
std::atomic<uint32_t> m_numConnections;
|
|
|
|
std::atomic<uint32_t> m_numIncomingConnections;
|
2021-08-22 04:20:59 -06:00
|
|
|
|
|
|
|
uv_mutex_t m_bansLock;
|
2022-02-21 10:41:36 -07:00
|
|
|
unordered_map<raw_ip, std::chrono::steady_clock::time_point> m_bans;
|
2021-08-22 04:20:59 -06:00
|
|
|
|
2021-08-27 08:36:06 -06:00
|
|
|
bool is_banned(const raw_ip& ip);
|
|
|
|
|
2021-10-29 06:24:05 -06:00
|
|
|
unordered_set<raw_ip> m_pendingConnections;
|
2021-08-26 15:27:05 -06:00
|
|
|
|
|
|
|
uv_async_t m_dropConnectionsAsync;
|
|
|
|
static void on_drop_connections(uv_async_t* async) { reinterpret_cast<TCPServer*>(async->data)->close_sockets(false); }
|
|
|
|
|
2022-10-07 08:02:08 -06:00
|
|
|
virtual void on_shutdown() = 0;
|
|
|
|
|
2021-08-26 15:27:05 -06:00
|
|
|
uv_async_t m_shutdownAsync;
|
2022-10-18 10:41:58 -06:00
|
|
|
uv_prepare_t m_shutdownPrepare;
|
|
|
|
uv_timer_t m_shutdownTimer;
|
|
|
|
uint32_t m_shutdownCountdown;
|
|
|
|
uint32_t m_numHandles;
|
2022-06-04 05:16:05 -06:00
|
|
|
|
2022-10-18 10:41:58 -06:00
|
|
|
static void on_shutdown(uv_async_t* async);
|
2021-08-22 04:20:59 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace p2pool
|