The Battle for Wesnoth  1.15.2+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"
18 #include "server/user_handler.hpp"
19 #include "server/metrics.hpp"
20 #include "server/ban.hpp"
21 #include "server/player.hpp"
22 #include "server/simple_wml.hpp"
23 #include "server/server_base.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);
59  void handle_query(socket_ptr socket, simple_wml::node& query);
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 
68  void send_to_lobby(simple_wml::document& data, socket_ptr exclude = socket_ptr()) const;
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) {}
80  std::string nick, ip;
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) {}
95  std::string ip;
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. */
124  std::string input_path_;
125 #endif
126 
127  std::string uuid_;
128 
129  const std::string config_file_;
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_;
137  std::string recommended_version_;
138  std::map<std::string,config> redirected_versions_;
139  std::map<std::string,config> proxy_versions_;
140  std::vector<std::string> disallowed_names_;
141  std::string admin_passwd_;
142  std::string motd_;
144  std::size_t default_time_period_;
147  std::time_t lan_server_;
148  std::time_t last_user_seen_time_;
149  std::string restart_command;
150  std::size_t max_ip_log_size_;
153  std::string replay_save_path_;
155  std::vector<std::string> tor_ip_list_;
157  std::time_t failed_login_ban_;
158  std::deque<login_log>::size_type failed_login_buffer_size_;
159 
160  /** Parse the server config into local variables. */
161  void load_config();
162 
163  bool ip_exceeds_connection_limit(const std::string& ip) const;
164  std::string is_ip_banned(const std::string& ip);
165 
170 
172 
173  boost::asio::steady_timer dump_stats_timer_;
174  void start_dump_stats();
175  void dump_stats(const boost::system::error_code& ec);
176 
177  /** Process commands from admins and users. */
178  std::string process_command(std::string cmd, std::string issuer_name);
179 
180  void delete_game(int, const std::string& reason="");
181 
182  void update_game_in_lobby(const wesnothd::game& g, const socket_ptr& exclude=socket_ptr());
183 
184  void start_new_server();
185 
186  void setup_fifo();
187 #ifndef _WIN32
188  void handle_read_from_fifo(const boost::system::error_code& error, std::size_t bytes_transferred);
189 #endif
190  void setup_handlers();
191 
192  typedef std::function<void(const std::string&, const std::string&, std::string&, std::ostringstream *)> cmd_handler;
193  std::map<std::string, cmd_handler> cmd_handlers_;
194 
195  void shut_down_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
196  void restart_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
197  void sample_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
198  void help_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
199  void stats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
200  void metrics_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
201  void requests_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
202  void games_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
203  void wml_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
204  void netstats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
205  void adminmsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
206  void pm_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
207  void version_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
208  void msg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
209  void lobbymsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
210  void status_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
211  void clones_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
212  void bans_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
213  void ban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
214  void unban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
215  void ungban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
216  void kick_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
217  void kickban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
218  void gban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
219  void motd_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
220  void searchlog_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
221  void dul_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
222  void stopgame(const std::string &, const std::string &, std::string &, std::ostringstream *);
223 
224 #ifndef _WIN32
225  void handle_sighup(const boost::system::error_code& error, int signal_number);
226 #endif
227 
228  boost::asio::deadline_timer timer_;
229  void handle_graceful_timeout(const boost::system::error_code& error);
230 
231  boost::asio::steady_timer lan_server_timer_;
232  void start_lan_server_timer();
233  void abort_lan_server_timer();
234  void handle_lan_server_shutdown(const boost::system::error_code& error);
235 };
236 
238 
239 void send_server_message(socket_ptr socket, const std::string& message);
240 
241 }
std::string motd_
Definition: server.hpp:142
void handle_nickserv(socket_ptr socket, simple_wml::node &nickserv)
Definition: server.cpp:1195
void handle_whisper(socket_ptr socket, simple_wml::node &whisper)
Definition: server.cpp:1069
void shut_down_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2000
simple_wml::document games_and_users_list_
Definition: server.hpp:169
std::deque< login_log >::size_type failed_login_buffer_size_
Definition: server.hpp:158
std::string uuid_
Definition: server.hpp:127
void handle_version(socket_ptr socket)
Definition: server.cpp:582
void status_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2323
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:2125
int failed_login_limit_
Definition: server.hpp:156
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:39
bool save_replays_
Definition: server.hpp:152
std::function< void(const std::string &, const std::string &, std::string &, std::ostringstream *)> cmd_handler
Definition: server.hpp:192
void kickban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2503
void start_dump_stats()
Definition: server.cpp:559
void handle_query(socket_ptr socket, simple_wml::node &query)
Definition: server.cpp:1109
void start_lan_server_timer()
Definition: server.cpp:303
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:656
void help_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2087
void wml_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2134
void login(socket_ptr socket, std::string version, std::string source)
Definition: server.cpp:640
void stats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2096
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:339
void update_game_in_lobby(const wesnothd::game &g, const socket_ptr &exclude=socket_ptr())
Definition: server.cpp:2906
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:144
void requests_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2116
std::vector< std::string > accepted_versions_
Definition: server.hpp:136
std::size_t default_max_messages_
Definition: server.hpp:143
void send_server_message_to_lobby(const std::string &message, socket_ptr exclude=socket_ptr()) const
Definition: server.cpp:1911
void ban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2424
Definitions for the interface to Wesnoth Markup Language (WML).
void handle_sighup(const boost::system::error_code &error, int signal_number)
Definition: server.cpp:277
void handle_read_from_player(socket_ptr socket, std::shared_ptr< simple_wml::document > doc)
Definition: server.cpp:1019
void handle_create_game(socket_ptr socket, simple_wml::node &create_game)
Definition: server.cpp:1248
bool deny_unregistered_login_
Definition: server.hpp:151
void handle_graceful_timeout(const boost::system::error_code &error)
Definition: server.cpp:290
void gban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2594
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
simple_wml::document login_response_
Definition: server.hpp:167
bool allow_remote_shutdown_
Definition: server.hpp:154
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:2258
void add_player(socket_ptr socket, const wesnothd::player &)
Definition: server.cpp:982
std::map< socket_ptr::element_type *, std::string > seeds_
Definition: server.hpp:109
void remove_player(socket_ptr socket)
Definition: server.cpp:1848
void handle_lan_server_shutdown(const boost::system::error_code &error)
Definition: server.cpp:314
std::string restart_command
Definition: server.hpp:149
void handle_player_in_lobby(socket_ptr socket, std::shared_ptr< simple_wml::document > doc)
Definition: server.cpp:1051
void clones_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2365
bool ip_exceeds_connection_limit(const std::string &ip) const
Definition: server.cpp:532
std::string input_path_
server socket/fifo.
Definition: server.hpp:124
boost::asio::deadline_timer timer_
Definition: server.hpp:228
void motd_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2755
std::vector< std::string > tor_ip_list_
Definition: server.hpp:155
void bans_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2399
void searchlog_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2776
simple_wml::document version_query_response_
Definition: server.hpp:166
std::string replay_save_path_
Definition: server.hpp:153
void kick_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2704
void send_to_lobby(simple_wml::document &data, socket_ptr exclude=socket_ptr()) const
Definition: server.cpp:1902
void send_to_player(socket_ptr socket, simple_wml::document &doc)
Definition: server.cpp:1827
void netstats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2143
boost::asio::steady_timer dump_stats_timer_
Definition: server.hpp:173
void cleanup_game(game *)
Definition: server.cpp:1302
connection_log(std::string _nick, std::string _ip, std::time_t _log_off)
Definition: server.hpp:78
bool graceful_restart
Definition: server.hpp:146
void sample_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2066
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:828
void start_new_server()
Definition: server.cpp:1929
boost::asio::steady_timer lan_server_timer_
Definition: server.hpp:231
void send_server_message_to_all(const std::string &message, socket_ptr exclude=socket_ptr()) const
Definition: server.cpp:1920
void read_from_player(socket_ptr socket)
Definition: server.cpp:1011
std::size_t concurrent_connections_
Definition: server.hpp:145
void unban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2674
const std::string config_file_
Definition: server.hpp:129
void delete_game(int, const std::string &reason="")
Definition: server.cpp:2863
double g
Definition: astarsearch.cpp:64
std::time_t failed_login_ban_
Definition: server.hpp:157
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:2170
void stopgame(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2841
metrics metrics_
Definition: server.hpp:171
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:1417
std::map< std::string, config > redirected_versions_
Definition: server.hpp:138
void dump_stats(const boost::system::error_code &ec)
Definition: server.cpp:565
void handle_message(socket_ptr socket, simple_wml::node &message)
Definition: server.cpp:1220
void restart_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2031
void version_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2303
std::string is_ip_banned(const std::string &ip)
Definition: server.cpp:548
std::time_t lan_server_
Definition: server.hpp:147
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:1334
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:368
Definition: ban.cpp:28
std::map< std::string, cmd_handler > cmd_handlers_
Definition: server.hpp:193
void setup_fifo()
Definition: server.cpp:322
void dul_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2820
std::shared_ptr< boost::asio::ip::tcp::socket > socket_ptr
Definition: server_base.hpp:30
player_connections player_connections_
Definition: server.hpp:111
void load_config()
Parse the server config into local variables.
Definition: server.cpp:430
simple_wml::document join_lobby_response_
Definition: server.hpp:168
void read_version(socket_ptr socket, std::shared_ptr< simple_wml::document > doc)
Definition: server.cpp:587
std::deque< connection_log > ip_log_
Definition: server.hpp:90
void handle_new_client(socket_ptr socket)
Definition: server.cpp:577
void ungban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2689
std::string process_command(std::string cmd, std::string issuer_name)
Process commands from admins and users.
Definition: server.cpp:1945
config read_config() const
Read the server config from file &#39;config_file_&#39;.
Definition: server.cpp:411
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:938
void metrics_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2107
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:2281
void pm_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2216
mock_char c
void create_game(player_record &host, simple_wml::node &create_game)
Definition: server.cpp:1268
void abort_lan_server_timer()
Definition: server.cpp:309
std::size_t max_ip_log_size_
Definition: server.hpp:150
void send_server_message(socket_ptr socket, const std::string &message)
Definition: server.cpp:1838
std::time_t last_user_seen_time_
Definition: server.hpp:148
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:645