The Battle for Wesnoth  1.19.5+dev
server.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "config.hpp"
21 #include "server/wesnothd/ban.hpp"
25 
26 #include "utils/optional_fwd.hpp"
27 
28 #include <boost/asio/steady_timer.hpp>
29 
30 #include <chrono>
31 #include <random>
32 
33 namespace wesnothd
34 {
35 
36 class server : public server_base
37 {
38 public:
39  server(int port, bool keep_alive, const std::string& config_file);
40 
41  // We keep this flag for coroutines. Since they get their stack unwinding done after player_connections_
42  // is already destroyed they need to know to avoid calling remove_player() on invalid iterators.
43  bool destructed = false;
44  ~server() {
45  destructed = true;
46  }
47 
48 private:
49  void handle_new_client(socket_ptr socket);
51 
52  template<class SocketPtr> void login_client(boost::asio::yield_context yield, SocketPtr socket);
53  template<class SocketPtr> bool is_login_allowed(boost::asio::yield_context yield, SocketPtr socket, const simple_wml::node* const login, const std::string& username, bool& registered, bool& is_moderator);
54  template<class SocketPtr> bool authenticate(SocketPtr socket, const std::string& username, const std::string& password, bool name_taken, bool& registered);
55  template<class SocketPtr> void send_password_request(SocketPtr socket, const std::string& msg, const char* error_code = "", bool force_confirmation = false);
56  bool accepting_connections() const { return !graceful_restart; }
57 
58  template<class SocketPtr> void handle_player(boost::asio::yield_context yield, SocketPtr socket, player_iterator player);
66  void cleanup_game(game*); // deleter for shared_ptr
70 
71 public:
72  template<class SocketPtr> void send_server_message(SocketPtr socket, const std::string& message, const std::string& type);
73  void send_server_message(player_iterator player, const std::string& message, const std::string& type) {
74  utils::visit(
75  [this, &message, &type](auto&& socket) { send_server_message(socket, message, type); },
76  player->socket()
77  );
78  }
79  void send_to_lobby(simple_wml::document& data, utils::optional<player_iterator> exclude = {});
81  utils::visit(
82  [this, &data](auto&& socket) { async_send_doc_queued(socket, data); },
83  player->socket()
84  );
85  }
86  void send_server_message_to_lobby(const std::string& message, utils::optional<player_iterator> exclude = {});
87  void send_server_message_to_all(const std::string& message, utils::optional<player_iterator> exclude = {});
88 
90  return player->get_game() != nullptr;
91  }
92 
93 private:
95 
97  {
98  std::string nick, ip;
99  std::chrono::system_clock::time_point log_off;
100 
101  bool operator==(const connection_log& c) const
102  {
103  // log off time does not matter to find ip-nick pairs
104  return c.nick == nick && c.ip == ip;
105  }
106  };
107 
108  std::deque<connection_log> ip_log_;
109 
110  struct login_log
111  {
112  std::string ip;
113  int attempts;
114  std::chrono::steady_clock::time_point first_attempt;
115 
116  bool operator==(const login_log& l) const
117  {
118  // only the IP matters
119  return l.ip == ip;
120  }
121  };
122 
123  std::deque<login_log> failed_logins_;
124 
125  std::unique_ptr<user_handler> user_handler_;
126 
127  std::mt19937 die_;
128 
129 #ifndef _WIN32
130  /** server socket/fifo. */
131  std::string input_path_;
132 #endif
133 
134  std::string uuid_;
135 
136  const std::string config_file_;
138 
139  /** Read the server config from file 'config_file_'. */
140  config read_config() const;
141 
142  // settings from the server config
143  std::vector<std::string> accepted_versions_;
144  std::string recommended_version_;
145  std::map<std::string,config> redirected_versions_;
146  std::map<std::string,config> proxy_versions_;
147  std::vector<std::string> disallowed_names_;
148  std::string admin_passwd_;
149  std::string motd_;
150  std::string announcements_;
151  std::string server_id_;
152  std::string tournaments_;
153  std::string information_;
155  std::chrono::seconds default_time_period_;
158  std::chrono::seconds lan_server_;
159  std::string restart_command;
160  std::size_t max_ip_log_size_;
163  std::string replay_save_path_;
165  std::set<std::string> client_sources_;
166  std::vector<std::string> tor_ip_list_;
168  std::chrono::seconds failed_login_ban_;
169  std::deque<login_log>::size_type failed_login_buffer_size_;
170 
171  /** Parse the server config into local variables. */
172  void load_config();
173 
174  bool ip_exceeds_connection_limit(const std::string& ip) const;
175  std::string is_ip_banned(const std::string& ip);
176 
180 
182 
184 
185  std::deque<std::shared_ptr<game>> games() const
186  {
187  std::deque<std::shared_ptr<game>> result;
188 
189  for(const auto& iter : player_connections_.get<game_t>()) {
190  if(result.empty() || iter.get_game() != result.back()) {
191  result.push_back(iter.get_game());
192  }
193  }
194 
195  if(!result.empty() && result.front() == nullptr) {
196  result.pop_front();
197  }
198 
199  return result;
200  }
201 
202  boost::asio::steady_timer dump_stats_timer_;
203  void start_dump_stats();
204  void dump_stats(const boost::system::error_code& ec);
205 
206  boost::asio::steady_timer tournaments_timer_;
208  void refresh_tournaments(const boost::system::error_code& ec);
209 
210  /** Process commands from admins and users. */
211  std::string process_command(std::string cmd, std::string issuer_name);
212 
213  void delete_game(int, const std::string& reason="");
214 
215  void update_game_in_lobby(const game& g, utils::optional<player_iterator> exclude = {});
216 
217  void start_new_server();
218 
219  void setup_fifo();
220 #ifndef _WIN32
221  void handle_read_from_fifo(const boost::system::error_code& error, std::size_t bytes_transferred);
222 #endif
223  void setup_handlers();
224 
225  typedef std::function<void(const std::string&, const std::string&, std::string&, std::ostringstream *)> cmd_handler;
226  std::map<std::string, cmd_handler> cmd_handlers_;
227 
228  void shut_down_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
229  void restart_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
230  void sample_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
231  void help_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
232  void stats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
233  void metrics_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
234  void requests_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
235  void roll_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
236  void games_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
237  void wml_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
238  void adminmsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
239  void pm_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
240  void version_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
241  void msg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
242  void lobbymsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
243  void status_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
244  void clones_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
245  void bans_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
246  void ban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
247  void unban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
248  void ungban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
249  void kick_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
250  void kickban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
251  void gban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
252  void motd_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
253  void searchlog_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
254  void dul_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
255  void stopgame(const std::string &, const std::string &, std::string &, std::ostringstream *);
256 
257 #ifndef _WIN32
258  void handle_sighup(const boost::system::error_code& error, int signal_number);
259 #endif
260 
261  boost::asio::steady_timer timer_;
262  void handle_graceful_timeout(const boost::system::error_code& error);
263 
264  boost::asio::steady_timer lan_server_timer_;
265  void start_lan_server_timer();
266  void abort_lan_server_timer();
267  void handle_lan_server_shutdown(const boost::system::error_code& error);
268 
269  boost::asio::steady_timer dummy_player_timer_;
272  void dummy_player_updates(const boost::system::error_code& ec);
273 };
274 
275 }
double g
Definition: astarsearch.cpp:63
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Base class for implementing servers that use gzipped-WML network protocol.
Definition: server_base.hpp:80
void async_send_doc_queued(SocketPtr socket, simple_wml::document &doc)
High level wrapper for sending a WML document.
void send_to_lobby(simple_wml::document &data, utils::optional< player_iterator > exclude={})
Definition: server.cpp:2005
int failed_login_limit_
Definition: server.hpp:167
std::unique_ptr< user_handler > user_handler_
Definition: server.hpp:125
std::string uuid_
Definition: server.hpp:134
const std::string config_file_
Definition: server.hpp:136
boost::asio::steady_timer dummy_player_timer_
Definition: server.hpp:269
void handle_message(player_iterator player, simple_wml::node &message)
Definition: server.cpp:1322
void searchlog_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2847
std::string motd_
Definition: server.hpp:149
bool graceful_restart
Definition: server.hpp:157
std::mt19937 die_
Definition: server.hpp:127
std::vector< std::string > disallowed_names_
Definition: server.hpp:147
void clones_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2482
std::string information_
Definition: server.hpp:153
void setup_handlers()
Definition: server.cpp:364
void ban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2541
void start_dummy_player_updates()
Definition: server.cpp:608
std::string input_path_
server socket/fifo.
Definition: server.hpp:131
void unban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2745
std::map< std::string, config > proxy_versions_
Definition: server.hpp:146
void handle_new_client(tls_socket_ptr socket)
void handle_sighup(const boost::system::error_code &error, int signal_number)
Definition: server.cpp:273
int dummy_player_timer_interval_
Definition: server.hpp:270
void stats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2201
std::string server_id_
Definition: server.hpp:151
void delete_game(int, const std::string &reason="")
Definition: server.cpp:2942
void refresh_tournaments(const boost::system::error_code &ec)
Definition: server.cpp:657
std::string recommended_version_
Definition: server.hpp:144
void gban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2680
void pm_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2335
std::function< void(const std::string &, const std::string &, std::string &, std::ostringstream *)> cmd_handler
Definition: server.hpp:225
void adminmsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2290
bool save_replays_
Definition: server.hpp:162
void start_tournaments_timer()
Definition: server.cpp:651
std::string replay_save_path_
Definition: server.hpp:163
void handle_query(player_iterator player, simple_wml::node &query)
Definition: server.cpp:1219
void handle_player_in_game(player_iterator player, simple_wml::document &doc)
Definition: server.cpp:1505
std::string restart_command
Definition: server.hpp:159
void lobbymsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2399
bool ip_exceeds_connection_limit(const std::string &ip) const
Definition: server.cpp:563
std::chrono::seconds failed_login_ban_
Definition: server.hpp:168
void handle_graceful_timeout(const boost::system::error_code &error)
Definition: server.cpp:286
void cleanup_game(game *)
Definition: server.cpp:1393
void handle_whisper(player_iterator player, simple_wml::node &whisper)
Definition: server.cpp:1179
metrics metrics_
Definition: server.hpp:181
void start_dump_stats()
Definition: server.cpp:590
bool is_login_allowed(boost::asio::yield_context yield, SocketPtr socket, const simple_wml::node *const login, const std::string &username, bool &registered, bool &is_moderator)
Definition: server.cpp:823
void handle_new_client(socket_ptr socket)
Definition: server.cpp:669
void status_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2440
std::string announcements_
Definition: server.hpp:150
std::deque< connection_log > ip_log_
Definition: server.hpp:108
void bans_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2516
void kick_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2775
void dul_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2900
void handle_join_game(player_iterator player, simple_wml::node &join)
Definition: server.cpp:1427
std::deque< login_log >::size_type failed_login_buffer_size_
Definition: server.hpp:169
void wml_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2281
void send_server_message(SocketPtr socket, const std::string &message, const std::string &type)
Definition: server.cpp:1937
void handle_player(boost::asio::yield_context yield, SocketPtr socket, player_iterator player)
Definition: server.cpp:1071
player_connections player_connections_
Definition: server.hpp:183
simple_wml::document games_and_users_list_
Definition: server.hpp:179
void help_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2192
void requests_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2220
void dump_stats(const boost::system::error_code &ec)
Definition: server.cpp:596
std::string tournaments_
Definition: server.hpp:152
void start_new_server()
Definition: server.cpp:2034
void remove_player(player_iterator player)
Definition: server.cpp:1961
boost::asio::steady_timer timer_
Definition: server.hpp:261
void handle_read_from_fifo(const boost::system::error_code &error, std::size_t bytes_transferred)
Definition: server.cpp:335
server(int port, bool keep_alive, const std::string &config_file)
Definition: server.cpp:209
simple_wml::document login_response_
Definition: server.hpp:178
boost::asio::steady_timer tournaments_timer_
Definition: server.hpp:206
std::string is_ip_banned(const std::string &ip)
Definition: server.cpp:579
std::set< std::string > client_sources_
Definition: server.hpp:165
bool player_is_in_game(player_iterator player) const
Definition: server.hpp:89
void abort_lan_server_timer()
Definition: server.cpp:305
void send_server_message(player_iterator player, const std::string &message, const std::string &type)
Definition: server.hpp:73
void handle_lan_server_shutdown(const boost::system::error_code &error)
Definition: server.cpp:310
bool authenticate(SocketPtr socket, const std::string &username, const std::string &password, bool name_taken, bool &registered)
Definition: server.cpp:947
std::string admin_passwd_
Definition: server.hpp:148
void motd_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2826
void load_config()
Parse the server config into local variables.
Definition: server.cpp:428
bool deny_unregistered_login_
Definition: server.hpp:161
std::vector< std::string > accepted_versions_
Definition: server.hpp:143
std::chrono::seconds lan_server_
Definition: server.hpp:158
void dummy_player_updates(const boost::system::error_code &ec)
Definition: server.cpp:614
void handle_create_game(player_iterator player, simple_wml::node &create_game)
Definition: server.cpp:1349
void send_server_message_to_lobby(const std::string &message, utils::optional< player_iterator > exclude={})
Definition: server.cpp:2015
void kickban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2604
void send_server_message_to_all(const std::string &message, utils::optional< player_iterator > exclude={})
Definition: server.cpp:2025
void handle_nickserv(player_iterator player, simple_wml::node &nickserv)
Definition: server.cpp:1297
std::string process_command(std::string cmd, std::string issuer_name)
Process commands from admins and users.
Definition: server.cpp:2050
void login_client(boost::asio::yield_context yield, SocketPtr socket)
Definition: server.cpp:688
void stopgame(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2920
std::deque< login_log > failed_logins_
Definition: server.hpp:123
std::size_t default_max_messages_
Definition: server.hpp:154
std::size_t max_ip_log_size_
Definition: server.hpp:160
void restart_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2136
void msg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2377
boost::asio::steady_timer lan_server_timer_
Definition: server.hpp:264
void shut_down_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2105
std::map< std::string, config > redirected_versions_
Definition: server.hpp:145
void metrics_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2211
std::map< std::string, cmd_handler > cmd_handlers_
Definition: server.hpp:226
void setup_fifo()
Definition: server.cpp:318
void send_to_player(player_iterator player, simple_wml::document &data)
Definition: server.hpp:80
void update_game_in_lobby(const game &g, utils::optional< player_iterator > exclude={})
Definition: server.cpp:2986
wesnothd::ban_manager ban_manager_
Definition: server.hpp:94
config read_config() const
Read the server config from file 'config_file_'.
Definition: server.cpp:407
bool accepting_connections() const
Definition: server.hpp:56
boost::asio::steady_timer dump_stats_timer_
Definition: server.hpp:202
void handle_player_in_lobby(player_iterator player, simple_wml::document &doc)
Definition: server.cpp:1134
std::vector< std::string > tor_ip_list_
Definition: server.hpp:166
void roll_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2229
void games_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2272
std::deque< std::shared_ptr< game > > games() const
Definition: server.hpp:185
simple_wml::document version_query_response_
Definition: server.hpp:177
void version_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2420
std::size_t concurrent_connections_
Definition: server.hpp:156
void send_password_request(SocketPtr socket, const std::string &msg, const char *error_code="", bool force_confirmation=false)
Definition: server.cpp:1053
void sample_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2171
void start_lan_server_timer()
Definition: server.cpp:299
void disconnect_player(player_iterator player)
Definition: server.cpp:1948
bool allow_remote_shutdown_
Definition: server.hpp:164
void ungban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2760
std::chrono::seconds default_time_period_
Definition: server.hpp:155
Definitions for the interface to Wesnoth Markup Language (WML).
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
player_connections::const_iterator player_iterator
bmi::multi_index_container< player_record, bmi::indexed_by< bmi::ordered_unique< bmi::tag< socket_t >, bmi::const_mem_fun< player_record, const any_socket_ptr, &player_record::socket > >, bmi::hashed_unique< bmi::tag< name_t >, bmi::const_mem_fun< player_record, const std::string &, &player_record::name > >, bmi::ordered_non_unique< bmi::tag< game_t >, bmi::const_mem_fun< player_record, int, &player_record::game_id > > > > player_connections
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
std::string_view data
Definition: picture.cpp:178
Base class for servers using Wesnoth's WML over TCP protocol.
std::shared_ptr< boost::asio::ssl::stream< socket_ptr::element_type > > tls_socket_ptr
Definition: server_base.hpp:51
std::shared_ptr< boost::asio::ip::tcp::socket > socket_ptr
Definition: server_base.hpp:48
bool operator==(const connection_log &c) const
Definition: server.hpp:101
std::chrono::system_clock::time_point log_off
Definition: server.hpp:99
bool operator==(const login_log &l) const
Definition: server.hpp:116
std::chrono::steady_clock::time_point first_attempt
Definition: server.hpp:114
mock_char c