The Battle for Wesnoth  1.17.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 ()
 
void 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...
 
configget_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...
 
configserver_info ()
 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...
 

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 ()
 
void handle_termination (const boost::system::error_code &error, int signal_number)
 
- 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_
 
boost::asio::signal_set sigs_
 

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 315 of file server.cpp.

References write_config().

Member Function Documentation

◆ 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 770 of file server.cpp.

References ERR_CS, and hooks_.

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 711 of file server.cpp.

References flush_timer_, and handle_flush().

Referenced by handle_flush(), and server().

◆ get_addon()

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 864 of file server.cpp.

References addons_, and config::get_invalid().

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 717 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 487 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.

Definition at line 480 of file server.cpp.

References server_base::io_service_, and serve_requests().

◆ 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 698 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 810 of file server.cpp.

References stats_exempt_ips_, and utils::wildcard_string_match().

Referenced by handle_request_campaign(), and server_info().

◆ load_blacklist()

void campaignd::server::load_blacklist ( )
private

Reads the add-ons upload blacklist from WML.

Definition at line 727 of file server.cpp.

References blacklist_, blacklist_file_, campaignd::blacklist::clear(), ERR_CS, in, filesystem::istream_file(), LOG_CS, campaignd::blacklist::read(), and 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 validate_addon().

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 906 of file server.cpp.

References REGISTER_CAMPAIGND_HANDLER.

Referenced by server(), and server_info().

◆ 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 834 of file server.cpp.

References simple_wml::node::add_child(), server_base::async_send_doc_queued(), campaignd::client_address(), ERR_CS, 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(), serve_requests(), and server_info().

◆ 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 843 of file server.cpp.

References simple_wml::node::add_child(), server_base::async_send_doc_queued(), campaignd::client_address(), ERR_CS, 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 822 of file server.cpp.

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

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

◆ serve_requests()

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

◆ server_info() [1/2]

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 config::child().

Referenced by load_config().

◆ server_info() [2/2]

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

◆ 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 1246 of file server.cpp.

References addon_name_legal(), ADDON_UNKNOWN, addons_, campaignd::server::request::addr, BAD_FEEDBACK_TOPIC_ID, BAD_NAME, BAD_TYPE, blacklist_, c, campaignd::server::request::cfg, config::child(), config::clear(), 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_READ_ONLY, version_info::str(), strict_versions_, SUCCESS, TITLE_HAS_MARKUP, UNAUTHORIZED, UNEXPECTED_DELTA, user_handler_, and VERSION_NOT_INCREMENTED.

Referenced by handle_upload(), and mark_dirty().

◆ 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 474 of file server.cpp.

Referenced by campaignd::server::request::request().

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(), 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(), 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

Definition at line 128 of file server.hpp.

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

◆ 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

Definition at line 101 of file server.hpp.

Referenced by handle_upload(), load_config(), and validate_addon().

◆ 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: