The Battle for Wesnoth  1.15.5+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/shared_array.hpp>
27 
28 #include <boost/asio/signal_set.hpp>
29 #include <boost/asio/steady_timer.hpp>
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 handle_version(socket_ptr socket);
43  void read_version(socket_ptr socket, std::shared_ptr<simple_wml::document> doc);
44 
45  void login(socket_ptr socket, std::string version, std::string source);
46  void handle_login(socket_ptr socket, std::shared_ptr<simple_wml::document> doc, std::string version, std::string source);
47  bool is_login_allowed(socket_ptr socket, const simple_wml::node* const login, const std::string& version, const std::string& source);
48  bool authenticate(socket_ptr socket, const std::string& username, const std::string& password, const std::string& version, const std::string& source, bool name_taken, bool& registered);
49  void send_password_request(socket_ptr socket, const std::string& msg,
50  const std::string& user, const std::string& version, const std::string& source, const char* error_code = "", bool force_confirmation = false);
51  bool accepting_connections() const { return !graceful_restart; }
52 
53  void add_player(socket_ptr socket, const wesnothd::player&);
54  void read_from_player(socket_ptr socket);
55  void handle_read_from_player(socket_ptr socket, std::shared_ptr<simple_wml::document> doc);
56  void handle_player_in_lobby(socket_ptr socket, std::shared_ptr<simple_wml::document> doc);
57  void handle_player_in_game(socket_ptr socket, std::shared_ptr<simple_wml::document> doc);
58  void handle_whisper(socket_ptr socket, simple_wml::node& whisper);
60  void handle_nickserv(socket_ptr socket, simple_wml::node& nickserv);
61  void handle_message(socket_ptr socket, simple_wml::node& message);
63  void create_game(player_record& host, simple_wml::node& create_game);
64  void cleanup_game(game*); // deleter for shared_ptr
66  void remove_player(socket_ptr socket);
67 
69  void send_server_message_to_lobby(const std::string& message, socket_ptr exclude = socket_ptr()) const;
70  void send_server_message_to_all(const std::string& message, socket_ptr exclude = socket_ptr()) const;
71  bool player_is_in_game(socket_ptr socket) const {
72  return bool(player_connections_.find(socket)->get_game());
73  }
74 
76 
77  struct connection_log {
78  connection_log(std::string _nick, std::string _ip, std::time_t _log_off) :
79  nick(_nick), ip(_ip), log_off(_log_off) {}
81  std::time_t log_off;
82 
83  bool operator==(const connection_log& c) const
84  {
85  // log off time does not matter to find ip-nick pairs
86  return c.nick == nick && c.ip == ip;
87  }
88  };
89 
90  std::deque<connection_log> ip_log_;
91 
92  struct login_log {
93  login_log(std::string _ip, int _attempts, std::time_t _first_attempt) :
94  ip(_ip), attempts(_attempts), first_attempt(_first_attempt) {}
96  int attempts;
97  std::time_t first_attempt;
98 
99  bool operator==(const login_log& l) const
100  {
101  // only the IP matters
102  return l.ip == ip;
103  }
104  };
105 
106  std::deque<login_log> failed_logins_;
107 
108  std::unique_ptr<user_handler> user_handler_;
109  std::map<socket_ptr::element_type*, std::string> seeds_;
110 
112  std::deque<std::shared_ptr<game>> games() {
113  std::deque<std::shared_ptr<game>> result;
114  for(const auto& iter : player_connections_.get<game_t>())
115  if(result.empty() || iter.get_game() != result.back())
116  result.push_back(iter.get_game());
117  if(!result.empty() && result.front() == 0)
118  result.pop_front();
119  return result;
120  }
121 
122 #ifndef _WIN32
123  /** server socket/fifo. */
125 #endif
126 
128 
131 
132  /** Read the server config from file 'config_file_'. */
133  config read_config() const;
134 
135  // settings from the server config
136  std::vector<std::string> accepted_versions_;
138  std::map<std::string,config> redirected_versions_;
139  std::map<std::string,config> proxy_versions_;
140  std::vector<std::string> disallowed_names_;
147  std::size_t default_time_period_;
150  std::time_t lan_server_;
151  std::time_t last_user_seen_time_;
153  std::size_t max_ip_log_size_;
158  std::set<std::string> client_sources_;
159  std::vector<std::string> tor_ip_list_;
161  std::time_t failed_login_ban_;
162  std::deque<login_log>::size_type failed_login_buffer_size_;
163 
164  /** Parse the server config into local variables. */
165  void load_config();
166 
167  bool ip_exceeds_connection_limit(const std::string& ip) const;
169 
174 
176 
177  boost::asio::steady_timer dump_stats_timer_;
178  void start_dump_stats();
179  void dump_stats(const boost::system::error_code& ec);
180 
181  boost::asio::steady_timer tournaments_timer_;
183  void refresh_tournaments(const boost::system::error_code& ec);
184 
185  /** Process commands from admins and users. */
187 
188  void delete_game(int, const std::string& reason="");
189 
190  void update_game_in_lobby(const wesnothd::game& g, const socket_ptr& exclude=socket_ptr());
191 
192  void start_new_server();
193 
194  void setup_fifo();
195 #ifndef _WIN32
196  void handle_read_from_fifo(const boost::system::error_code& error, std::size_t bytes_transferred);
197 #endif
198  void setup_handlers();
199 
200  typedef std::function<void(const std::string&, const std::string&, std::string&, std::ostringstream *)> cmd_handler;
201  std::map<std::string, cmd_handler> cmd_handlers_;
202 
203  void shut_down_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
204  void restart_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
205  void sample_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
206  void help_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
207  void stats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
208  void metrics_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
209  void requests_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
210  void games_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
211  void wml_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
212  void netstats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
213  void adminmsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
214  void pm_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
215  void version_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
216  void msg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
217  void lobbymsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
218  void status_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
219  void clones_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
220  void bans_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
221  void ban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
222  void unban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
223  void ungban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
224  void kick_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
225  void kickban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
226  void gban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
227  void motd_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
228  void searchlog_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
229  void dul_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
230  void stopgame(const std::string &, const std::string &, std::string &, std::ostringstream *);
231 
232 #ifndef _WIN32
233  void handle_sighup(const boost::system::error_code& error, int signal_number);
234 #endif
235 
236  boost::asio::deadline_timer timer_;
237  void handle_graceful_timeout(const boost::system::error_code& error);
238 
239  boost::asio::steady_timer lan_server_timer_;
240  void start_lan_server_timer();
241  void abort_lan_server_timer();
242  void handle_lan_server_shutdown(const boost::system::error_code& error);
243 };
244 
246 
247 void send_server_message(socket_ptr socket, const std::string& message, const std::string& type);
248 
249 }
std::string motd_
Definition: server.hpp:142
std::set< std::string > client_sources_
Definition: server.hpp:158
void handle_nickserv(socket_ptr socket, simple_wml::node &nickserv)
Definition: server.cpp:1228
void handle_whisper(socket_ptr socket, simple_wml::node &whisper)
Definition: server.cpp:1102
void shut_down_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2058
simple_wml::document games_and_users_list_
Definition: server.hpp:173
std::deque< login_log >::size_type failed_login_buffer_size_
Definition: server.hpp:162
std::string uuid_
Definition: server.hpp:127
void handle_version(socket_ptr socket)
Definition: server.cpp:613
void status_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2381
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:2183
void refresh_tournaments(const boost::system::error_code &ec)
Definition: server.cpp:596
std::string tournaments_
Definition: server.hpp:144
int failed_login_limit_
Definition: server.hpp:160
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:39
bool save_replays_
Definition: server.hpp:155
std::function< void(const std::string &, const std::string &, std::string &, std::ostringstream *)> cmd_handler
Definition: server.hpp:200
void kickban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2561
void start_dump_stats()
Definition: server.cpp:572
void handle_query(socket_ptr socket, simple_wml::node &query)
Definition: server.cpp:1142
void start_lan_server_timer()
Definition: server.cpp:309
std::string announcements_
Definition: server.hpp:143
std::unique_ptr< user_handler > user_handler_
Definition: server.hpp:108
bool accepting_connections() const
Definition: server.hpp:51
bool is_login_allowed(socket_ptr socket, const simple_wml::node *const login, const std::string &version, const std::string &source)
Definition: server.cpp:687
void help_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2145
void wml_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2192
void login(socket_ptr socket, std::string version, std::string source)
Definition: server.cpp:671
void stats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2154
std::deque< std::shared_ptr< game > > games()
Definition: server.hpp:112
login_log(std::string _ip, int _attempts, std::time_t _first_attempt)
Definition: server.hpp:93
void handle_read_from_fifo(const boost::system::error_code &error, std::size_t bytes_transferred)
Definition: server.cpp:345
void update_game_in_lobby(const wesnothd::game &g, const socket_ptr &exclude=socket_ptr())
Definition: server.cpp:2964
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:147
void requests_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2174
std::vector< std::string > accepted_versions_
Definition: server.hpp:136
std::size_t default_max_messages_
Definition: server.hpp:146
void send_server_message_to_lobby(const std::string &message, socket_ptr exclude=socket_ptr()) const
Definition: server.cpp:1969
void ban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2482
std::string information_
Definition: server.hpp:145
Definitions for the interface to Wesnoth Markup Language (WML).
void handle_sighup(const boost::system::error_code &error, int signal_number)
Definition: server.cpp:283
void handle_read_from_player(socket_ptr socket, std::shared_ptr< simple_wml::document > doc)
Definition: server.cpp:1052
void handle_create_game(socket_ptr socket, simple_wml::node &create_game)
Definition: server.cpp:1281
bool deny_unregistered_login_
Definition: server.hpp:154
void handle_graceful_timeout(const boost::system::error_code &error)
Definition: server.cpp:296
void gban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2652
bool operator==(const connection_log &c) const
Definition: server.hpp:83
std::string recommended_version_
Definition: server.hpp:137
bool player_is_in_game(socket_ptr socket) const
Definition: server.hpp:71
void start_tournaments_timer()
Definition: server.cpp:590
simple_wml::document login_response_
Definition: server.hpp:171
bool allow_remote_shutdown_
Definition: server.hpp:157
std::map< std::string, config > proxy_versions_
Definition: server.hpp:139
void msg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2316
void add_player(socket_ptr socket, const wesnothd::player &)
Definition: server.cpp:1013
std::map< socket_ptr::element_type *, std::string > seeds_
Definition: server.hpp:109
void remove_player(socket_ptr socket)
Definition: server.cpp:1906
void handle_lan_server_shutdown(const boost::system::error_code &error)
Definition: server.cpp:320
std::string restart_command
Definition: server.hpp:152
void handle_player_in_lobby(socket_ptr socket, std::shared_ptr< simple_wml::document > doc)
Definition: server.cpp:1084
void clones_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2423
bool ip_exceeds_connection_limit(const std::string &ip) const
Definition: server.cpp:545
std::string input_path_
server socket/fifo.
Definition: server.hpp:124
boost::asio::deadline_timer timer_
Definition: server.hpp:236
void motd_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2813
std::vector< std::string > tor_ip_list_
Definition: server.hpp:159
void bans_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2457
void searchlog_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2834
simple_wml::document version_query_response_
Definition: server.hpp:170
std::string replay_save_path_
Definition: server.hpp:156
void kick_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2762
void send_to_lobby(simple_wml::document &data, socket_ptr exclude=socket_ptr()) const
Definition: server.cpp:1960
void send_to_player(socket_ptr socket, simple_wml::document &doc)
Definition: server.cpp:1884
void netstats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2201
boost::asio::steady_timer dump_stats_timer_
Definition: server.hpp:177
void cleanup_game(game *)
Definition: server.cpp:1331
connection_log(std::string _nick, std::string _ip, std::time_t _log_off)
Definition: server.hpp:78
bool graceful_restart
Definition: server.hpp:149
void sample_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2124
bool authenticate(socket_ptr socket, const std::string &username, const std::string &password, const std::string &version, const std::string &source, bool name_taken, bool &registered)
Definition: server.cpp:859
void start_new_server()
Definition: server.cpp:1987
boost::asio::steady_timer lan_server_timer_
Definition: server.hpp:239
void send_server_message_to_all(const std::string &message, socket_ptr exclude=socket_ptr()) const
Definition: server.cpp:1978
void read_from_player(socket_ptr socket)
Definition: server.cpp:1044
std::size_t concurrent_connections_
Definition: server.hpp:148
void unban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2732
const std::string config_file_
Definition: server.hpp:129
void delete_game(int, const std::string &reason="")
Definition: server.cpp:2921
double g
Definition: astarsearch.cpp:64
std::time_t failed_login_ban_
Definition: server.hpp:161
std::string password(const std::string &server, const std::string &login)
std::string admin_passwd_
Definition: server.hpp:141
wesnothd::ban_manager ban_manager_
Definition: server.hpp:75
std::deque< login_log > failed_logins_
Definition: server.hpp:106
void adminmsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2228
boost::asio::steady_timer tournaments_timer_
Definition: server.hpp:181
void stopgame(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2899
metrics metrics_
Definition: server.hpp:175
std::time_t first_attempt
Definition: server.hpp:97
bool operator==(const login_log &l) const
Definition: server.hpp:99
void handle_player_in_game(socket_ptr socket, std::shared_ptr< simple_wml::document > doc)
Definition: server.cpp:1446
std::map< std::string, config > redirected_versions_
Definition: server.hpp:138
void dump_stats(const boost::system::error_code &ec)
Definition: server.cpp:578
void handle_message(socket_ptr socket, simple_wml::node &message)
Definition: server.cpp:1253
void restart_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2089
void version_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2361
std::string is_ip_banned(const std::string &ip)
Definition: server.cpp:561
std::time_t lan_server_
Definition: server.hpp:150
server(int port, bool keep_alive, const std::string &config_file, std::size_t, std::size_t)
Definition: server.cpp:219
void handle_join_game(socket_ptr socket, simple_wml::node &join)
Definition: server.cpp:1363
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:374
Definition: ban.cpp:28
std::map< std::string, cmd_handler > cmd_handlers_
Definition: server.hpp:201
void setup_fifo()
Definition: server.cpp:328
void dul_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2878
std::shared_ptr< boost::asio::ip::tcp::socket > socket_ptr
Definition: server_base.hpp:30
player_connections player_connections_
Definition: server.hpp:111
void send_server_message(socket_ptr socket, const std::string &message, const std::string &type)
Definition: server.cpp:1895
void load_config()
Parse the server config into local variables.
Definition: server.cpp:436
simple_wml::document join_lobby_response_
Definition: server.hpp:172
void read_version(socket_ptr socket, std::shared_ptr< simple_wml::document > doc)
Definition: server.cpp:618
std::deque< connection_log > ip_log_
Definition: server.hpp:90
void handle_new_client(socket_ptr socket)
Definition: server.cpp:608
void ungban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2747
std::string process_command(std::string cmd, std::string issuer_name)
Process commands from admins and users.
Definition: server.cpp:2003
config read_config() const
Read the server config from file &#39;config_file_&#39;.
Definition: server.cpp:417
void send_password_request(socket_ptr socket, const std::string &msg, const std::string &user, const std::string &version, const std::string &source, const char *error_code="", bool force_confirmation=false)
Definition: server.cpp:969
void metrics_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2165
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
void lobbymsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2339
void pm_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2274
mock_char c
void create_game(player_record &host, simple_wml::node &create_game)
Definition: server.cpp:1301
void abort_lan_server_timer()
Definition: server.cpp:315
std::size_t max_ip_log_size_
Definition: server.hpp:153
std::time_t last_user_seen_time_
Definition: server.hpp:151
std::vector< std::string > disallowed_names_
Definition: server.hpp:140
Base class for servers using Wesnoth&#39;s WML over TCP protocol.
void handle_login(socket_ptr socket, std::shared_ptr< simple_wml::document > doc, std::string version, std::string source)
Definition: server.cpp:676