The Battle for Wesnoth  1.19.0-dev
Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
campaignd::server Class Reference

Legacy add-ons server. More...

#include <server.hpp>

Inheritance diagram for campaignd::server:

Classes

struct  request
 Client request information object. More...
 

Public Member Functions

 server (const std::string &cfg_file, unsigned short port=0)
 
 server (const config &server)=delete
 
 ~server ()
 
serveroperator= (const config &server)=delete
 
- Public Member Functions inherited from server_base
 server_base (unsigned short port, bool keep_alive)
 
virtual ~server_base ()
 
int run ()
 
template<class SocketPtr >
void coro_send_doc (SocketPtr socket, simple_wml::document &doc, boost::asio::yield_context yield)
 Send a WML document from within a coroutine. More...
 
void coro_send_file (socket_ptr socket, const std::string &filename, boost::asio::yield_context yield)
 Send contents of entire file directly to socket from within a coroutine. More...
 
void coro_send_file (tls_socket_ptr socket, const std::string &filename, boost::asio::yield_context yield)
 
template<class SocketPtr >
std::unique_ptr< simple_wml::documentcoro_receive_doc (SocketPtr socket, boost::asio::yield_context yield)
 Receive WML document from a coroutine. More...
 
template<class SocketPtr >
void async_send_doc_queued (SocketPtr socket, simple_wml::document &doc)
 High level wrapper for sending a WML document. More...
 
template<class SocketPtr >
void async_send_error (SocketPtr socket, const std::string &msg, const char *error_code="", const info_table &info={})
 
template<class SocketPtr >
void async_send_warning (SocketPtr socket, const std::string &msg, const char *warning_code="", const info_table &info={})
 
std::string create_unsecure_nonce (int length=8)
 Create the poor security nonce for use with passwords still hashed with MD5. More...
 
std::string create_secure_nonce ()
 Create a good security nonce for use with bcrypt/crypt_blowfish hashing. More...
 
std::string hash_password (const std::string &pw, const std::string &salt, const std::string &username)
 Handles hashing the password provided by the player before comparing it to the hashed password in the forum database. More...
 

Private Types

typedef std::function< void(server *, const request &req)> request_handler
 
typedef std::map< std::string, request_handlerrequest_handlers_table
 

Private Member Functions

void handle_new_client (socket_ptr socket)
 
void handle_new_client (tls_socket_ptr socket)
 
template<class Socket >
void serve_requests (Socket socket, boost::asio::yield_context yield)
 
void handle_read_from_fifo (const boost::system::error_code &error, std::size_t bytes_transferred)
 
void handle_sighup (const boost::system::error_code &error, int signal_number)
 
void flush_cfg ()
 Starts timer to write config to disk every ten minutes. More...
 
void handle_flush (const boost::system::error_code &error)
 
void load_config ()
 Reads the server configuration from WML. More...
 
void write_config ()
 Writes the server configuration WML back to disk. More...
 
void load_blacklist ()
 Reads the add-ons upload blacklist from WML. More...
 
void fire (const std::string &hook, const std::string &addon)
 Fires a hook script. More...
 
optional_config get_addon (const std::string &id)
 Retrieves an addon by id if found, or a null config otherwise. More...
 
void delete_addon (const std::string &id)
 
void mark_dirty (const std::string &addon)
 
ADDON_CHECK_STATUS validate_addon (const server::request &req, config *&existing_addon, std::string &error_data)
 Performs validation on an incoming add-on. More...
 
const configserver_info () const
 Retrieves the contents of the [server_info] WML node. More...
 
bool ignore_address_stats (const std::string &addr) const
 Checks if the specified address should never bump download counts. More...
 
void register_handlers ()
 Registers client request handlers. More...
 
void handle_server_id (const request &)
 
void handle_request_campaign_list (const request &)
 
void handle_request_campaign (const request &)
 
void handle_request_campaign_hash (const request &)
 
void handle_request_terms (const request &)
 
void handle_upload (const request &)
 
void handle_delete (const request &)
 
void handle_change_passphrase (const request &)
 
void send_message (const std::string &msg, const any_socket_ptr &sock)
 Send a client an informational message. More...
 
void send_error (const std::string &msg, const any_socket_ptr &sock)
 Send a client an error message. More...
 
void send_error (const std::string &msg, const std::string &extra_data, unsigned int status_code, const any_socket_ptr &sock)
 Send a client an error message. More...
 
bool authenticate_forum (const config &addon, const std::string &passphrase, bool is_delete)
 Check whether the provided passphrase matches the add-on and its author by checked against the forum database. More...
 

Private Attributes

std::unique_ptr< user_handleruser_handler_
 
std::set< std::string > capabilities_
 
std::unordered_map< std::string, configaddons_
 The hash map of addons metadata. More...
 
std::unordered_set< std::string > dirty_addons_
 The set of unique addon names with pending metadata updates. More...
 
config cfg_
 Server config. More...
 
const std::string cfg_file_
 
bool read_only_
 
int compress_level_
 Used for add-on archives. More...
 
time_t update_pack_lifespan_
 
bool strict_versions_
 
std::map< std::string, std::string > hooks_
 
request_handlers_table handlers_
 
std::string server_id_
 
std::string feedback_url_format_
 
std::string web_url_
 
std::string license_notice_
 
blacklist blacklist_
 
std::string blacklist_file_
 
std::vector< std::string > stats_exempt_ips_
 
boost::asio::basic_waitable_timer< std::chrono::steady_clock > flush_timer_
 

Static Private Attributes

static const std::size_t default_document_size_limit = 100 * 1024 * 1024
 Default upload size limit in bytes. More...
 

Friends

std::ostream & operator<< (std::ostream &o, const request &r)
 

Additional Inherited Members

- Public Types inherited from server_base
typedef std::map< std::string, std::string > info_table
 
- Protected Member Functions inherited from server_base
void load_tls_config (const config &cfg)
 
void start_server ()
 
void serve (boost::asio::yield_context yield, boost::asio::ip::tcp::acceptor &acceptor, boost::asio::ip::tcp::endpoint endpoint)
 
virtual bool accepting_connections () const
 
virtual std::string is_ip_banned (const std::string &)
 
virtual bool ip_exceeds_connection_limit (const std::string &) const
 
void read_from_fifo ()
 
- Protected Attributes inherited from server_base
unsigned short port_
 
bool keep_alive_
 
boost::asio::io_service io_service_
 
boost::asio::ssl::context tls_context_ { boost::asio::ssl::context::sslv23 }
 
bool tls_enabled_ { false }
 
boost::asio::ip::tcp::acceptor acceptor_v6_
 
boost::asio::ip::tcp::acceptor acceptor_v4_
 
uint32_t handshake_response_
 
boost::asio::posix::stream_descriptor input_
 
std::string fifo_path_
 
boost::asio::streambuf admin_cmd_
 
boost::asio::signal_set sighup_
 

Detailed Description

Legacy add-ons server.

Definition at line 39 of file server.hpp.

Member Typedef Documentation

◆ request_handler

typedef std::function<void (server*, const request& req)> campaignd::server::request_handler
private

Definition at line 102 of file server.hpp.

◆ request_handlers_table

typedef std::map<std::string, request_handler> campaignd::server::request_handlers_table
private

Definition at line 103 of file server.hpp.

Constructor & Destructor Documentation

◆ server() [1/2]

campaignd::server::server ( const std::string &  cfg_file,
unsigned short  port = 0 
)
explicit

◆ server() [2/2]

campaignd::server::server ( const config server)
delete

◆ ~server()

campaignd::server::~server ( )

Definition at line 324 of file server.cpp.

References write_config().

Member Function Documentation

◆ authenticate_forum()

bool campaignd::server::authenticate_forum ( const config addon,
const std::string &  passphrase,
bool  is_delete 
)
private

Check whether the provided passphrase matches the add-on and its author by checked against the forum database.

Parameters
addonThe add-on uploaded, which contains the username to use.
passphraseThe passphrase to use for authentication.
is_deleteWhether the authentication is being requested for an add-on upload or an add-on deletion.
Returns
Whether the provided information matches what's in the forum database.

Definition at line 1918 of file server.cpp.

References server_base::hash_password(), server_id_, and user_handler_.

Referenced by handle_delete(), and validate_addon().

◆ delete_addon()

void campaignd::server::delete_addon ( const std::string &  id)
private

◆ fire()

void campaignd::server::fire ( const std::string &  hook,
const std::string &  addon 
)
private

Fires a hook script.

Definition at line 789 of file server.cpp.

References ERR_CS, hooks_, and PLAIN_LOG.

Referenced by delete_addon(), and handle_upload().

◆ flush_cfg()

void campaignd::server::flush_cfg ( )
private

Starts timer to write config to disk every ten minutes.

Definition at line 730 of file server.cpp.

References flush_timer_, and handle_flush().

Referenced by handle_flush(), and server().

◆ get_addon()

optional_config campaignd::server::get_addon ( const std::string &  id)
private

Retrieves an addon by id if found, or a null config otherwise.

Definition at line 882 of file server.cpp.

References addons_.

Referenced by delete_addon(), handle_change_passphrase(), handle_delete(), handle_read_from_fifo(), handle_request_campaign(), handle_request_campaign_hash(), load_config(), and write_config().

◆ handle_change_passphrase()

void campaignd::server::handle_change_passphrase ( const request req)
private

◆ handle_delete()

void campaignd::server::handle_delete ( const request req)
private

◆ handle_flush()

void campaignd::server::handle_flush ( const boost::system::error_code &  error)
private

Definition at line 736 of file server.cpp.

References ERR_CS, flush_cfg(), and write_config().

Referenced by flush_cfg().

◆ handle_new_client() [1/2]

void campaignd::server::handle_new_client ( socket_ptr  socket)
privatevirtual

Implements server_base.

Definition at line 489 of file server.cpp.

References server_base::io_service_, and serve_requests().

◆ handle_new_client() [2/2]

void campaignd::server::handle_new_client ( tls_socket_ptr  socket)
privatevirtual

Implements server_base.

◆ handle_read_from_fifo()

void campaignd::server::handle_read_from_fifo ( const boost::system::error_code &  error,
std::size_t  bytes_transferred 
)
privatevirtual

◆ handle_request_campaign()

void campaignd::server::handle_request_campaign ( const request req)
private

◆ handle_request_campaign_hash()

void campaignd::server::handle_request_campaign_hash ( const request req)
private

◆ handle_request_campaign_list()

void campaignd::server::handle_request_campaign_list ( const request req)
private

◆ handle_request_terms()

void campaignd::server::handle_request_terms ( const request req)
private

◆ handle_server_id()

void campaignd::server::handle_server_id ( const request req)
private

◆ handle_sighup()

void campaignd::server::handle_sighup ( const boost::system::error_code &  error,
int  signal_number 
)
privatevirtual

Implements server_base.

Definition at line 717 of file server.cpp.

References load_config(), LOG_CS, and server_base::sighup_.

◆ handle_upload()

void campaignd::server::handle_upload ( const request req)
private

◆ ignore_address_stats()

bool campaignd::server::ignore_address_stats ( const std::string &  addr) const
private

Checks if the specified address should never bump download counts.

Definition at line 828 of file server.cpp.

References stats_exempt_ips_, and utils::wildcard_string_match().

Referenced by handle_request_campaign().

◆ load_blacklist()

void campaignd::server::load_blacklist ( )
private

Reads the add-ons upload blacklist from WML.

Definition at line 746 of file server.cpp.

References blacklist_, blacklist_file_, campaignd::blacklist::clear(), ERR_CS, in, filesystem::istream_file(), LOG_CS, read(), and campaignd::blacklist::read().

Referenced by handle_read_from_fifo(), and load_config().

◆ load_config()

void campaignd::server::load_config ( )
private

◆ mark_dirty()

void campaignd::server::mark_dirty ( const std::string &  addon)
inlineprivate

Definition at line 185 of file server.hpp.

References dirty_addons_.

Referenced by handle_read_from_fifo(), handle_request_campaign(), handle_upload(), and load_config().

◆ operator=()

server& campaignd::server::operator= ( const config server)
delete

◆ register_handlers()

void campaignd::server::register_handlers ( )
private

Registers client request handlers.

This is called by the class constructor. Individual handlers must be methods of this class that take a single parameter of type request and they are registered using the register_handler method.

When adding new handlers, make sure to update the implementation of this method accordingly so they are recognized and invoked at runtime.

Definition at line 928 of file server.cpp.

References REGISTER_CAMPAIGND_HANDLER.

Referenced by server().

◆ send_error() [1/2]

void campaignd::server::send_error ( const std::string &  msg,
const any_socket_ptr sock 
)
private

Send a client an error message.

The WML sent consists of a document containing a single [error] child with a message attribute holding the value of msg. In addition to sending the error to the client, a line with the client IP and message is recorded to the server log.

Definition at line 852 of file server.cpp.

References simple_wml::node::add_child(), server_base::async_send_doc_queued(), campaignd::client_address(), ERR_CS, wfl::msg(), simple_wml::document::root(), and simple_wml::node::set_attr_dup().

Referenced by handle_change_passphrase(), handle_delete(), handle_request_campaign(), handle_request_campaign_hash(), handle_request_terms(), handle_upload(), and serve_requests().

◆ send_error() [2/2]

void campaignd::server::send_error ( const std::string &  msg,
const std::string &  extra_data,
unsigned int  status_code,
const any_socket_ptr sock 
)
private

Send a client an error message.

The WML sent consists of a document containing a single [error] child with a message attribute holding the value of msg, an extra_data attribute holding the value of extra_data, and a status_code attribute holding the value of status_code. In addition to sending the error to the client, a line with the client IP and message is recorded to the server log.

Definition at line 861 of file server.cpp.

References simple_wml::node::add_child(), server_base::async_send_doc_queued(), campaignd::client_address(), ERR_CS, wfl::msg(), simple_wml::document::root(), and simple_wml::node::set_attr_dup().

◆ send_message()

void campaignd::server::send_message ( const std::string &  msg,
const any_socket_ptr sock 
)
private

Send a client an informational message.

The WML sent consists of a document containing a single [message] child with a message attribute holding the value of msg.

Definition at line 840 of file server.cpp.

References simple_wml::node::add_child(), server_base::async_send_doc_queued(), wfl::msg(), simple_wml::document::root(), and simple_wml::node::set_attr_dup().

Referenced by handle_change_passphrase(), handle_delete(), handle_request_terms(), and handle_upload().

◆ serve_requests()

template<class Socket >
void campaignd::server::serve_requests ( Socket  socket,
boost::asio::yield_context  yield 
)
private

Definition at line 504 of file server.cpp.

References c, server_base::coro_receive_doc(), data, handlers_, i, read(), and send_error().

Referenced by handle_new_client().

◆ server_info()

const config& campaignd::server::server_info ( ) const
inlineprivate

Retrieves the contents of the [server_info] WML node.

Definition at line 210 of file server.hpp.

References cfg_, and config::child_or_empty().

Referenced by load_config().

◆ validate_addon()

ADDON_CHECK_STATUS campaignd::server::validate_addon ( const server::request req,
config *&  existing_addon,
std::string &  error_data 
)
private

Performs validation on an incoming add-on.

Parameters
reqServer request info, containing either a full WML pack, or a delta update pack.
existing_addonUsed to store a pointer to the existing add-on WML on the server if the add-on already exists. nullptr is stored otherwise.
error_dataUsed to store extra error data for status codes other than ADDON_CHECK_STATUS::SUCCESS. Cleared otherwise.
Returns
ADDON_CHECK_STATUS::SUCCESS if the validation checks pass, a more relevant value otherwise.

Definition at line 1265 of file server.cpp.

References addon_name_legal(), ADDON_UNKNOWN, addons_, campaignd::server::request::addr, AUTH_TYPE_MISMATCH, authenticate_forum(), BAD_FEEDBACK_TOPIC_ID, BAD_NAME, BAD_TYPE, blacklist_, c, campaignd::server::request::cfg, config::clear(), data, DENIED, EMPTY_PACK, ERR_CS, FEEDBACK_TOPIC_ID_NOT_FOUND, FILENAME_CASE_CONFLICT, get_addon_type(), ILLEGAL_FILENAME, INVALID_UTF8_ATTRIBUTE, INVALID_UTF8_NAME, campaignd::blacklist::is_blacklisted(), campaignd::is_text_markup_char(), utils::join(), LOG_CS, utf8::lowercase(), NAME_HAS_MARKUP, NO_AUTHOR, NO_DESCRIPTION, NO_EMAIL, NO_PASSPHRASE, NO_TITLE, NO_VERSION, config::optional_child(), read_only_, SERVER_ADDONS_LIST, SERVER_FORUM_AUTH_DISABLED, SERVER_READ_ONLY, utils::split(), version_info::str(), strict_versions_, SUCCESS, TITLE_HAS_MARKUP, UNAUTHORIZED, UNEXPECTED_DELTA, USER_DOES_NOT_EXIST, user_handler_, and VERSION_NOT_INCREMENTED.

Referenced by handle_upload().

◆ write_config()

void campaignd::server::write_config ( )
private

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  o,
const request r 
)
friend

Definition at line 483 of file server.cpp.

Member Data Documentation

◆ addons_

std::unordered_map<std::string, config> campaignd::server::addons_
private

The hash map of addons metadata.

Definition at line 108 of file server.hpp.

Referenced by delete_addon(), get_addon(), handle_request_campaign_list(), handle_upload(), load_config(), server(), and validate_addon().

◆ blacklist_

blacklist campaignd::server::blacklist_
private

Definition at line 135 of file server.hpp.

Referenced by load_blacklist(), and validate_addon().

◆ blacklist_file_

std::string campaignd::server::blacklist_file_
private

Definition at line 136 of file server.hpp.

Referenced by load_blacklist(), and load_config().

◆ capabilities_

std::set<std::string> campaignd::server::capabilities_
private

Definition at line 105 of file server.hpp.

Referenced by handle_server_id().

◆ cfg_

config campaignd::server::cfg_
private

Server config.

Definition at line 113 of file server.hpp.

Referenced by handle_read_from_fifo(), load_config(), server_info(), and write_config().

◆ cfg_file_

const std::string campaignd::server::cfg_file_
private

Definition at line 114 of file server.hpp.

Referenced by load_config(), and write_config().

◆ compress_level_

int campaignd::server::compress_level_
private

Used for add-on archives.

Definition at line 117 of file server.hpp.

Referenced by handle_upload(), and load_config().

◆ default_document_size_limit

const std::size_t campaignd::server::default_document_size_limit = 100 * 1024 * 1024
staticprivate

Default upload size limit in bytes.

Definition at line 123 of file server.hpp.

Referenced by load_config().

◆ dirty_addons_

std::unordered_set<std::string> campaignd::server::dirty_addons_
private

The set of unique addon names with pending metadata updates.

Definition at line 110 of file server.hpp.

Referenced by handle_change_passphrase(), mark_dirty(), and write_config().

◆ feedback_url_format_

std::string campaignd::server::feedback_url_format_
private

Definition at line 130 of file server.hpp.

Referenced by handle_request_campaign_list(), and load_config().

◆ flush_timer_

boost::asio::basic_waitable_timer<std::chrono::steady_clock> campaignd::server::flush_timer_
private

Definition at line 140 of file server.hpp.

Referenced by flush_cfg().

◆ handlers_

request_handlers_table campaignd::server::handlers_
private

Definition at line 126 of file server.hpp.

Referenced by serve_requests().

◆ hooks_

std::map<std::string, std::string> campaignd::server::hooks_
private

Definition at line 125 of file server.hpp.

Referenced by fire(), and load_config().

◆ license_notice_

std::string campaignd::server::license_notice_
private

Definition at line 133 of file server.hpp.

Referenced by handle_request_terms(), handle_server_id(), and load_config().

◆ read_only_

bool campaignd::server::read_only_
private

◆ server_id_

std::string campaignd::server::server_id_
private

◆ stats_exempt_ips_

std::vector<std::string> campaignd::server::stats_exempt_ips_
private

Definition at line 138 of file server.hpp.

Referenced by ignore_address_stats(), and load_config().

◆ strict_versions_

bool campaignd::server::strict_versions_
private

Definition at line 120 of file server.hpp.

Referenced by load_config(), and validate_addon().

◆ update_pack_lifespan_

time_t campaignd::server::update_pack_lifespan_
private

Definition at line 118 of file server.hpp.

Referenced by handle_upload(), and load_config().

◆ user_handler_

std::unique_ptr<user_handler> campaignd::server::user_handler_
private

◆ web_url_

std::string campaignd::server::web_url_
private

Definition at line 132 of file server.hpp.

Referenced by handle_server_id(), and load_config().


The documentation for this class was generated from the following files: