The Battle for Wesnoth  1.15.9+dev
server.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License 2
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "config.hpp"
20 #include "server/wesnothd/ban.hpp"
25 
26 #include <boost/asio/steady_timer.hpp>
27 
28 #include <optional>
29 #include <random>
30 
31 namespace wesnothd
32 {
33 
34 class server : public server_base
35 {
36 public:
37  server(int port, bool keep_alive, const std::string& config_file, std::size_t, std::size_t);
38 
39 private:
40  void handle_new_client(socket_ptr socket);
41 
42  void login_client(boost::asio::yield_context yield, socket_ptr socket);
43  bool is_login_allowed(socket_ptr socket, const simple_wml::node* const login, const std::string& username, bool& registered, bool& is_moderator);
44  bool authenticate(socket_ptr socket, const std::string& username, const std::string& password, bool name_taken, bool& registered);
45  void send_password_request(socket_ptr socket, const std::string& msg,
46  const std::string& user, const char* error_code = "", bool force_confirmation = false);
47  bool accepting_connections() const { return !graceful_restart; }
48 
49  void handle_player(boost::asio::yield_context yield, socket_ptr socket, const player& player);
52  void handle_whisper(player_iterator player, simple_wml::node& whisper);
53  void handle_query(player_iterator player, simple_wml::node& query);
54  void handle_nickserv(player_iterator player, simple_wml::node& nickserv);
55  void handle_message(player_iterator player, simple_wml::node& message);
56  void handle_create_game(player_iterator player, simple_wml::node& create_game);
57  void cleanup_game(game*); // deleter for shared_ptr
60  void remove_player(player_iterator player);
61 
62  void send_server_message(socket_ptr socket, const std::string& message, const std::string& type);
63  void send_server_message(player_iterator player, const std::string& message, const std::string& type) {
64  send_server_message(player->socket(), message, type);
65  }
66  void send_to_lobby(simple_wml::document& data, std::optional<player_iterator> exclude = {});
67  void send_server_message_to_lobby(const std::string& message, std::optional<player_iterator> exclude = {});
68  void send_server_message_to_all(const std::string& message, std::optional<player_iterator> exclude = {});
69 
70  bool player_is_in_game(player_iterator player) const {
71  return player->get_game() != nullptr;
72  }
73 
75 
77  {
78  std::string nick, ip;
79  std::time_t log_off;
80 
81  bool operator==(const connection_log& c) const
82  {
83  // log off time does not matter to find ip-nick pairs
84  return c.nick == nick && c.ip == ip;
85  }
86  };
87 
88  std::deque<connection_log> ip_log_;
89 
90  struct login_log
91  {
92  std::string ip;
93  int attempts;
94  std::time_t first_attempt;
95 
96  bool operator==(const login_log& l) const
97  {
98  // only the IP matters
99  return l.ip == ip;
100  }
101  };
102 
103  std::deque<login_log> failed_logins_;
104 
105  std::unique_ptr<user_handler> user_handler_;
106  std::map<socket_ptr::element_type*, std::string> seeds_;
107 
108  std::mt19937 die_;
109 
111 
112  std::deque<std::shared_ptr<game>> games() const
113  {
114  std::deque<std::shared_ptr<game>> result;
115 
116  for(const auto& iter : player_connections_.get<game_t>()) {
117  if(result.empty() || iter.get_game() != result.back()) {
118  result.push_back(iter.get_game());
119  }
120  }
121 
122  if(!result.empty() && result.front() == nullptr) {
123  result.pop_front();
124  }
125 
126  return result;
127  }
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 tournaments_;
152  std::string information_;
154  std::size_t default_time_period_;
157  std::time_t lan_server_;
158  std::time_t last_user_seen_time_;
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::time_t 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 
183  boost::asio::steady_timer dump_stats_timer_;
184  void start_dump_stats();
185  void dump_stats(const boost::system::error_code& ec);
186 
187  boost::asio::steady_timer tournaments_timer_;
189  void refresh_tournaments(const boost::system::error_code& ec);
190 
191  /** Process commands from admins and users. */
192  std::string process_command(std::string cmd, std::string issuer_name);
193 
194  void delete_game(int, const std::string& reason="");
195 
196  void update_game_in_lobby(const game& g, std::optional<player_iterator> exclude = {});
197 
198  void start_new_server();
199 
200  void setup_fifo();
201 #ifndef _WIN32
202  void handle_read_from_fifo(const boost::system::error_code& error, std::size_t bytes_transferred);
203 #endif
204  void setup_handlers();
205 
206  typedef std::function<void(const std::string&, const std::string&, std::string&, std::ostringstream *)> cmd_handler;
207  std::map<std::string, cmd_handler> cmd_handlers_;
208 
209  void shut_down_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
210  void restart_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
211  void sample_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
212  void help_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
213  void stats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
214  void metrics_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
215  void requests_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
216  void roll_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
217  void games_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
218  void wml_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
219  void adminmsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
220  void pm_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
221  void version_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
222  void msg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
223  void lobbymsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
224  void status_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
225  void clones_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
226  void bans_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
227  void ban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
228  void unban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
229  void ungban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
230  void kick_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
231  void kickban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
232  void gban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
233  void motd_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
234  void searchlog_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
235  void dul_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
236  void stopgame(const std::string &, const std::string &, std::string &, std::ostringstream *);
237 
238 #ifndef _WIN32
239  void handle_sighup(const boost::system::error_code& error, int signal_number);
240 #endif
241 
242  boost::asio::steady_timer timer_;
243  void handle_graceful_timeout(const boost::system::error_code& error);
244 
245  boost::asio::steady_timer lan_server_timer_;
246  void start_lan_server_timer();
247  void abort_lan_server_timer();
248  void handle_lan_server_shutdown(const boost::system::error_code& error);
249 };
250 
251 }
std::string motd_
Definition: server.hpp:149
void handle_player(boost::asio::yield_context yield, socket_ptr socket, const player &player)
Definition: server.cpp:999
std::set< std::string > client_sources_
Definition: server.hpp:165
void handle_whisper(player_iterator player, simple_wml::node &whisper)
Definition: server.cpp:1080
void shut_down_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:1995
simple_wml::document games_and_users_list_
Definition: server.hpp:179
std::deque< login_log >::size_type failed_login_buffer_size_
Definition: server.hpp:169
std::string uuid_
Definition: server.hpp:134
void send_to_lobby(simple_wml::document &data, std::optional< player_iterator > exclude={})
Definition: server.cpp:1895
void status_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2334
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
void games_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2163
void refresh_tournaments(const boost::system::error_code &ec)
Definition: server.cpp:585
std::string tournaments_
Definition: server.hpp:151
void remove_player(player_iterator player)
Definition: server.cpp:1854
int failed_login_limit_
Definition: server.hpp:167
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:39
bool save_replays_
Definition: server.hpp:162
std::function< void(const std::string &, const std::string &, std::string &, std::ostringstream *)> cmd_handler
Definition: server.hpp:206
void kickban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2514
void start_dump_stats()
Definition: server.cpp:561
void start_lan_server_timer()
Definition: server.cpp:298
std::string announcements_
Definition: server.hpp:150
std::unique_ptr< user_handler > user_handler_
Definition: server.hpp:105
bool accepting_connections() const
Definition: server.hpp:47
void help_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2082
void send_server_message(player_iterator player, const std::string &message, const std::string &type)
Definition: server.hpp:63
void wml_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2172
void stats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2091
void handle_read_from_fifo(const boost::system::error_code &error, std::size_t bytes_transferred)
Definition: server.cpp:334
void handle_message(player_iterator player, simple_wml::node &message)
Definition: server.cpp:1225
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
std::size_t default_time_period_
Definition: server.hpp:154
void handle_join_game(player_iterator player, simple_wml::node &join)
Definition: server.cpp:1328
void requests_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2111
std::vector< std::string > accepted_versions_
Definition: server.hpp:143
std::size_t default_max_messages_
Definition: server.hpp:153
void ban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2435
std::string information_
Definition: server.hpp:152
Definitions for the interface to Wesnoth Markup Language (WML).
void handle_sighup(const boost::system::error_code &error, int signal_number)
Definition: server.cpp:272
player_connections::const_iterator player_iterator
bool deny_unregistered_login_
Definition: server.hpp:161
void handle_graceful_timeout(const boost::system::error_code &error)
Definition: server.cpp:285
void gban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2605
bool operator==(const connection_log &c) const
Definition: server.hpp:81
std::string recommended_version_
Definition: server.hpp:144
void start_tournaments_timer()
Definition: server.cpp:579
simple_wml::document login_response_
Definition: server.hpp:178
void login_client(boost::asio::yield_context yield, socket_ptr socket)
Definition: server.cpp:602
bool allow_remote_shutdown_
Definition: server.hpp:164
std::map< std::string, config > proxy_versions_
Definition: server.hpp:146
std::mt19937 die_
Definition: server.hpp:108
void msg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2269
std::map< socket_ptr::element_type *, std::string > seeds_
Definition: server.hpp:106
void handle_lan_server_shutdown(const boost::system::error_code &error)
Definition: server.cpp:309
bool is_login_allowed(socket_ptr socket, const simple_wml::node *const login, const std::string &username, bool &registered, bool &is_moderator)
Definition: server.cpp:731
std::string restart_command
Definition: server.hpp:159
void handle_player_in_game(player_iterator player, simple_wml::document &doc)
Definition: server.cpp:1406
bool player_is_in_game(player_iterator player) const
Definition: server.hpp:70
boost::asio::steady_timer timer_
Definition: server.hpp:242
void clones_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2376
bool ip_exceeds_connection_limit(const std::string &ip) const
Definition: server.cpp:534
std::string input_path_
server socket/fifo.
Definition: server.hpp:131
void send_server_message_to_all(const std::string &message, std::optional< player_iterator > exclude={})
Definition: server.cpp:1915
void motd_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2766
std::vector< std::string > tor_ip_list_
Definition: server.hpp:166
void bans_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2410
void searchlog_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2787
void send_password_request(socket_ptr socket, const std::string &msg, const std::string &user, const char *error_code="", bool force_confirmation=false)
Definition: server.cpp:958
simple_wml::document version_query_response_
Definition: server.hpp:177
std::string replay_save_path_
Definition: server.hpp:163
void kick_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2715
boost::asio::steady_timer dump_stats_timer_
Definition: server.hpp:183
std::string login()
void cleanup_game(game *)
Definition: server.cpp:1296
bool graceful_restart
Definition: server.hpp:156
void sample_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2061
void start_new_server()
Definition: server.cpp:1924
boost::asio::steady_timer lan_server_timer_
Definition: server.hpp:245
std::size_t concurrent_connections_
Definition: server.hpp:155
void unban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2685
const std::string config_file_
Definition: server.hpp:136
void roll_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2120
void delete_game(int, const std::string &reason="")
Definition: server.cpp:2874
double g
Definition: astarsearch.cpp:64
std::time_t failed_login_ban_
Definition: server.hpp:168
std::string password(const std::string &server, const std::string &login)
bool authenticate(socket_ptr socket, const std::string &username, const std::string &password, bool name_taken, bool &registered)
Definition: server.cpp:851
std::string admin_passwd_
Definition: server.hpp:148
wesnothd::ban_manager ban_manager_
Definition: server.hpp:74
std::deque< login_log > failed_logins_
Definition: server.hpp:103
void adminmsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2181
boost::asio::steady_timer tournaments_timer_
Definition: server.hpp:187
void stopgame(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2852
metrics metrics_
Definition: server.hpp:181
std::time_t first_attempt
Definition: server.hpp:94
bool operator==(const login_log &l) const
Definition: server.hpp:96
std::map< std::string, config > redirected_versions_
Definition: server.hpp:145
void handle_query(player_iterator player, simple_wml::node &query)
Definition: server.cpp:1120
void dump_stats(const boost::system::error_code &ec)
Definition: server.cpp:567
void restart_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2026
void send_server_message(socket_ptr socket, const std::string &message, const std::string &type)
Definition: server.cpp:1838
void version_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2314
std::string is_ip_banned(const std::string &ip)
Definition: server.cpp:550
std::time_t lan_server_
Definition: server.hpp:157
server(int port, bool keep_alive, const std::string &config_file, std::size_t, std::size_t)
Definition: server.cpp:208
void handle_create_game(player_iterator player, simple_wml::node &create_game)
Definition: server.cpp:1252
bmi::multi_index_container< player_record, bmi::indexed_by< bmi::ordered_unique< bmi::tag< socket_t >, bmi::const_mem_fun< player_record, const 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
void setup_handlers()
Definition: server.cpp:363
Definition: ban.cpp:28
std::map< std::string, cmd_handler > cmd_handlers_
Definition: server.hpp:207
void setup_fifo()
Definition: server.cpp:317
void dul_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2831
std::shared_ptr< boost::asio::ip::tcp::socket > socket_ptr
Definition: server_base.hpp:43
player_connections player_connections_
Definition: server.hpp:110
void load_config()
Parse the server config into local variables.
Definition: server.cpp:425
std::deque< std::shared_ptr< game > > games() const
Definition: server.hpp:112
void update_game_in_lobby(const game &g, std::optional< player_iterator > exclude={})
Definition: server.cpp:2917
std::deque< connection_log > ip_log_
Definition: server.hpp:88
void handle_new_client(socket_ptr socket)
Definition: server.cpp:597
void ungban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2700
std::string process_command(std::string cmd, std::string issuer_name)
Process commands from admins and users.
Definition: server.cpp:1940
config read_config() const
Read the server config from file &#39;config_file_&#39;.
Definition: server.cpp:406
void metrics_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2102
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
void lobbymsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2292
void pm_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2227
mock_char c
void handle_player_in_lobby(player_iterator player, simple_wml::document &doc)
Definition: server.cpp:1062
void abort_lan_server_timer()
Definition: server.cpp:304
std::size_t max_ip_log_size_
Definition: server.hpp:160
void disconnect_player(player_iterator player)
Definition: server.cpp:1849
void handle_nickserv(player_iterator player, simple_wml::node &nickserv)
Definition: server.cpp:1200
void send_server_message_to_lobby(const std::string &message, std::optional< player_iterator > exclude={})
Definition: server.cpp:1905
std::time_t last_user_seen_time_
Definition: server.hpp:158
std::vector< std::string > disallowed_names_
Definition: server.hpp:147
Base class for servers using Wesnoth&#39;s WML over TCP protocol.