AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
Main.cpp File Reference
#include "AppenderDB.h"
#include "AuthSocketMgr.h"
#include "Banner.h"
#include "Config.h"
#include "DatabaseEnv.h"
#include "DatabaseLoader.h"
#include "IPLocation.h"
#include "IoContext.h"
#include "Log.h"
#include "MySQLThreading.h"
#include "OpenSSLCrypto.h"
#include "ProcessPriority.h"
#include "RealmList.h"
#include "SecretMgr.h"
#include "SharedDefines.h"
#include "SteadyTimer.h"
#include "Util.h"
#include <boost/asio/signal_set.hpp>
#include <boost/program_options.hpp>
#include <boost/version.hpp>
#include <csignal>
#include <filesystem>
#include <iostream>
#include <openssl/crypto.h>
#include <openssl/opensslv.h>

Go to the source code of this file.

Macros

#define _ACORE_REALM_CONFIG   "authserver.conf"
 

Functions

bool StartDB ()
 Initialize connection to the database.
 
void StopDB ()
 Close the connection to the database.
 
void SignalHandler (std::weak_ptr< Acore::Asio::IoContext > ioContextRef, boost::system::error_code const &error, int signalNumber)
 
void KeepDatabaseAliveHandler (std::weak_ptr< boost::asio::steady_timer > dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const &error)
 
void BanExpiryHandler (std::weak_ptr< boost::asio::steady_timer > banExpiryCheckTimerRef, int32 banExpiryCheckInterval, boost::system::error_code const &error)
 
variables_map GetConsoleArguments (int argc, char **argv, fs::path &configFile)
 
int main (int argc, char **argv)
 Launch the auth server.
 

Macro Definition Documentation

◆ _ACORE_REALM_CONFIG

#define _ACORE_REALM_CONFIG   "authserver.conf"

Function Documentation

◆ BanExpiryHandler()

void BanExpiryHandler ( std::weak_ptr< boost::asio::steady_timer >  banExpiryCheckTimerRef,
int32  banExpiryCheckInterval,
boost::system::error_code const &  error 
)
259{
260 if (!error)
261 {
262 if (std::shared_ptr<boost::asio::steady_timer> banExpiryCheckTimer = banExpiryCheckTimerRef.lock())
263 {
264 LoginDatabase.Execute(LoginDatabase.GetPreparedStatement(LOGIN_DEL_EXPIRED_IP_BANS));
265 LoginDatabase.Execute(LoginDatabase.GetPreparedStatement(LOGIN_UPD_EXPIRED_ACCOUNT_BANS));
266
267 banExpiryCheckTimer->expires_at(Acore::Asio::SteadyTimer::GetExpirationTime(banExpiryCheckInterval));
268 banExpiryCheckTimer->async_wait(std::bind(&BanExpiryHandler, banExpiryCheckTimerRef, banExpiryCheckInterval, std::placeholders::_1));
269 }
270 }
271}
DatabaseWorkerPool< LoginDatabaseConnection > LoginDatabase
Accessor to the realm/login database.
Definition DatabaseEnv.cpp:22
@ LOGIN_DEL_EXPIRED_IP_BANS
Definition LoginDatabase.h:32
@ LOGIN_UPD_EXPIRED_ACCOUNT_BANS
Definition LoginDatabase.h:33
auto GetExpirationTime(int32 seconds)
Definition SteadyTimer.h:25
void BanExpiryHandler(std::weak_ptr< boost::asio::steady_timer > banExpiryCheckTimerRef, int32 banExpiryCheckInterval, boost::system::error_code const &error)
Definition Main.cpp:258

References BanExpiryHandler(), Acore::Asio::SteadyTimer::GetExpirationTime(), LOGIN_DEL_EXPIRED_IP_BANS, LOGIN_UPD_EXPIRED_ACCOUNT_BANS, and LoginDatabase.

Referenced by BanExpiryHandler(), and main().

◆ GetConsoleArguments()

variables_map GetConsoleArguments ( int  argc,
char **  argv,
fs::path &  configFile 
)
274{
275 options_description all("Allowed options");
276 all.add_options()
277 ("help,h", "print usage message")
278 ("version,v", "print version build info")
279 ("dry-run,d", "Dry run")
280 ("config,c", value<fs::path>(&configFile)->default_value(fs::path(sConfigMgr->GetConfigPath() + std::string(_ACORE_REALM_CONFIG))), "use <arg> as configuration file");
281
282 variables_map variablesMap;
283
284 try
285 {
286 store(command_line_parser(argc, argv).options(all).allow_unregistered().run(), variablesMap);
287 notify(variablesMap);
288 }
289 catch (std::exception const& e)
290 {
291 std::cerr << e.what() << "\n";
292 }
293
294 if (variablesMap.count("help"))
295 {
296 std::cout << all << "\n";
297 }
298 else if (variablesMap.count("dry-run"))
299 {
300 sConfigMgr->setDryRun(true);
301 }
302
303 return variablesMap;
304}
#define sConfigMgr
Definition Config.h:74
#define _ACORE_REALM_CONFIG
Definition Main.cpp:53

References _ACORE_REALM_CONFIG, and sConfigMgr.

Referenced by main().

◆ KeepDatabaseAliveHandler()

void KeepDatabaseAliveHandler ( std::weak_ptr< boost::asio::steady_timer >  dbPingTimerRef,
int32  dbPingInterval,
boost::system::error_code const &  error 
)
244{
245 if (!error)
246 {
247 if (std::shared_ptr<boost::asio::steady_timer> dbPingTimer = dbPingTimerRef.lock())
248 {
249 LOG_DEBUG("sql.driver", "Ping MySQL to keep connection alive");
250 LoginDatabase.KeepAlive();
251
252 dbPingTimer->expires_at(Acore::Asio::SteadyTimer::GetExpirationTime(dbPingInterval));
253 dbPingTimer->async_wait(std::bind(&KeepDatabaseAliveHandler, dbPingTimerRef, dbPingInterval, std::placeholders::_1));
254 }
255 }
256}
#define LOG_DEBUG(filterType__,...)
Definition Log.h:169
void KeepDatabaseAliveHandler(std::weak_ptr< boost::asio::steady_timer > dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const &error)
Definition Main.cpp:243

References Acore::Asio::SteadyTimer::GetExpirationTime(), KeepDatabaseAliveHandler(), LOG_DEBUG, and LoginDatabase.

Referenced by KeepDatabaseAliveHandler(), and main().

◆ main()

int main ( int  argc,
char **  argv 
)

Launch the auth server.

69{
71 signal(SIGABRT, &Acore::AbortHandler);
72
73 // Command line parsing
74 auto configFile = fs::path(sConfigMgr->GetConfigPath() + std::string(_ACORE_REALM_CONFIG));
75 auto vm = GetConsoleArguments(argc, argv, configFile);
76
77 // exit if help or version is enabled
78 if (vm.count("help"))
79 return 0;
80
81 // Add file and args in config
82 sConfigMgr->Configure(configFile.generic_string(), std::vector<std::string>(argv, argv + argc));
83
84 if (!sConfigMgr->LoadAppConfigs())
85 return 1;
86
87 // Init logging
88 sLog->RegisterAppender<AppenderDB>();
89 sLog->Initialize(nullptr);
90
91 Acore::Banner::Show("authserver",
92 [](std::string_view text)
93 {
94 LOG_INFO("server.authserver", text);
95 },
96 []()
97 {
98 LOG_INFO("server.authserver", "> Using configuration file {}", sConfigMgr->GetFilename());
99 LOG_INFO("server.authserver", "> Using SSL version: {} (library: {})", OPENSSL_VERSION_TEXT, OpenSSL_version(OPENSSL_VERSION));
100 LOG_INFO("server.authserver", "> Using Boost version: {}.{}.{}", BOOST_VERSION / 100000, BOOST_VERSION / 100 % 1000, BOOST_VERSION % 100);
101 });
102
104
105 std::shared_ptr<void> opensslHandle(nullptr, [](void*) { OpenSSLCrypto::threadsCleanup(); });
106
107 // authserver PID file creation
108 std::string pidFile = sConfigMgr->GetOption<std::string>("PidFile", "");
109 if (!pidFile.empty())
110 {
111 if (uint32 pid = CreatePIDFile(pidFile))
112 LOG_INFO("server.authserver", "Daemon PID: {}\n", pid); // outError for red color in console
113 else
114 {
115 LOG_ERROR("server.authserver", "Cannot create PID file {} (possible error: permission)\n", pidFile);
116 return 1;
117 }
118 }
119
120 // Initialize the database connection
121 if (!StartDB())
122 return 1;
123
124 sSecretMgr->Initialize();
125
126 // Load IP Location Database
127 sIPLocation->Load();
128
129 std::shared_ptr<void> dbHandle(nullptr, [](void*) { StopDB(); });
130
131 std::shared_ptr<Acore::Asio::IoContext> ioContext = std::make_shared<Acore::Asio::IoContext>();
132
133 // Get the list of realms for the server
134 sRealmList->Initialize(*ioContext, sConfigMgr->GetOption<int32>("RealmsStateUpdateDelay", 20));
135
136 std::shared_ptr<void> sRealmListHandle(nullptr, [](void*) { sRealmList->Close(); });
137
138 if (sRealmList->GetRealms().empty())
139 {
140 LOG_ERROR("server.authserver", "No valid realms specified.");
141 return 1;
142 }
143
144 // Stop auth server if dry run
145 if (sConfigMgr->isDryRun())
146 {
147 LOG_INFO("server.authserver", "Dry run completed, terminating.");
148 return 0;
149 }
150
151 // Start the listening port (acceptor) for auth connections
152 int32 port = sConfigMgr->GetOption<int32>("RealmServerPort", 3724);
153 if (port < 0 || port > 0xFFFF)
154 {
155 LOG_ERROR("server.authserver", "Specified port out of allowed range (1-65535)");
156 return 1;
157 }
158
159 std::string bindIp = sConfigMgr->GetOption<std::string>("BindIP", "0.0.0.0");
160
161 if (!sAuthSocketMgr.StartNetwork(*ioContext, bindIp, port))
162 {
163 LOG_ERROR("server.authserver", "Failed to initialize network");
164 return 1;
165 }
166
167 std::shared_ptr<void> sAuthSocketMgrHandle(nullptr, [](void*) { sAuthSocketMgr.StopNetwork(); });
168
169 // Set signal handlers
170 boost::asio::signal_set signals(*ioContext, SIGINT, SIGTERM);
171#if AC_PLATFORM == AC_PLATFORM_WINDOWS
172 signals.add(SIGBREAK);
173#endif
174 signals.async_wait(std::bind(&SignalHandler, std::weak_ptr<Acore::Asio::IoContext>(ioContext), std::placeholders::_1, std::placeholders::_2));
175
176 // Set process priority according to configuration settings
177 SetProcessPriority("server.authserver", sConfigMgr->GetOption<int32>(CONFIG_PROCESSOR_AFFINITY, 0), sConfigMgr->GetOption<bool>(CONFIG_HIGH_PRIORITY, false));
178
179 // Enabled a timed callback for handling the database keep alive ping
180 int32 dbPingInterval = sConfigMgr->GetOption<int32>("MaxPingTime", 30);
181 std::shared_ptr<boost::asio::steady_timer> dbPingTimer = std::make_shared<boost::asio::steady_timer>(*ioContext);
182
183 dbPingTimer->expires_at(Acore::Asio::SteadyTimer::GetExpirationTime(dbPingInterval * MINUTE));
184 dbPingTimer->async_wait(std::bind(&KeepDatabaseAliveHandler, std::weak_ptr<boost::asio::steady_timer>(dbPingTimer), dbPingInterval, std::placeholders::_1));
185
186 int32 banExpiryCheckInterval = sConfigMgr->GetOption<int32>("BanExpiryCheckInterval", 60);
187 std::shared_ptr<boost::asio::steady_timer> banExpiryCheckTimer = std::make_shared<boost::asio::steady_timer>(*ioContext);
188
189 banExpiryCheckTimer->expires_at(Acore::Asio::SteadyTimer::GetExpirationTime(banExpiryCheckInterval));
190 banExpiryCheckTimer->async_wait(std::bind(&BanExpiryHandler, std::weak_ptr<boost::asio::steady_timer>(banExpiryCheckTimer), banExpiryCheckInterval, std::placeholders::_1));
191
192 // Start the io service worker loop
193 ioContext->run();
194
195 banExpiryCheckTimer->cancel();
196 dbPingTimer->cancel();
197
198 LOG_INFO("server.authserver", "Halting process...");
199
200 signals.cancel();
201
202 return 0;
203}
#define sAuthSocketMgr
Definition AuthSocketMgr.h:63
constexpr auto MINUTE
Definition Common.h:47
std::int32_t int32
Definition Define.h:103
std::uint32_t uint32
Definition Define.h:107
#define sIPLocation
Definition IPLocation.h:49
#define LOG_INFO(filterType__,...)
Definition Log.h:165
#define LOG_ERROR(filterType__,...)
Definition Log.h:157
#define sLog
Definition Log.h:126
void SetProcessPriority(std::string const &logChannel, uint32 affinity, bool highPriority)
Definition ProcessPriority.cpp:29
#define CONFIG_HIGH_PRIORITY
Definition ProcessPriority.h:25
#define CONFIG_PROCESSOR_AFFINITY
Definition ProcessPriority.h:24
#define sRealmList
Definition RealmList.h:83
#define sSecretMgr
Definition SecretMgr.h:72
@ SERVER_PROCESS_AUTHSERVER
Definition SharedDefines.h:3738
uint32 CreatePIDFile(std::string const &filename)
create PID file
Definition Util.cpp:218
Definition AppenderDB.h:25
AC_COMMON_API void Show(std::string_view applicationName, void(*log)(std::string_view text), void(*logExtraInfo)())
Definition Banner.cpp:22
AC_COMMON_API void AbortHandler(int sigval)
Definition Errors.cpp:148
AC_COMMON_API void threadsSetup()
Needs to be called before threads using openssl are spawned.
Definition OpenSSLCrypto.cpp:41
AC_COMMON_API void threadsCleanup()
Needs to be called after threads using openssl are despawned.
Definition OpenSSLCrypto.cpp:50
bool StartDB()
Initialize connection to the database.
Definition Main.cpp:206
variables_map GetConsoleArguments(int argc, char **argv, fs::path &configFile)
Definition Main.cpp:273
void StopDB()
Close the connection to the database.
Definition Main.cpp:226
void SignalHandler(std::weak_ptr< Acore::Asio::IoContext > ioContextRef, boost::system::error_code const &error, int signalNumber)
Definition Main.cpp:232
static ServerProcessTypes _type
Definition SharedDefines.h:3762

References _ACORE_REALM_CONFIG, Acore::Impl::CurrentServerProcessHolder::_type, Acore::AbortHandler(), BanExpiryHandler(), CONFIG_HIGH_PRIORITY, CONFIG_PROCESSOR_AFFINITY, CreatePIDFile(), GetConsoleArguments(), Acore::Asio::SteadyTimer::GetExpirationTime(), KeepDatabaseAliveHandler(), LOG_ERROR, LOG_INFO, MINUTE, sAuthSocketMgr, sConfigMgr, SERVER_PROCESS_AUTHSERVER, SetProcessPriority(), Acore::Banner::Show(), SignalHandler(), sIPLocation, sLog, sRealmList, sSecretMgr, StartDB(), StopDB(), OpenSSLCrypto::threadsCleanup(), and OpenSSLCrypto::threadsSetup().

◆ SignalHandler()

void SignalHandler ( std::weak_ptr< Acore::Asio::IoContext ioContextRef,
boost::system::error_code const &  error,
int  signalNumber 
)
233{
234 if (!error)
235 {
236 if (std::shared_ptr<Acore::Asio::IoContext> ioContext = ioContextRef.lock())
237 {
238 ioContext->stop();
239 }
240 }
241}

Referenced by main().

◆ StartDB()

bool StartDB ( )

Initialize connection to the database.

207{
209
210 // Load databases
211 // NOTE: While authserver is singlethreaded you should keep synch_threads == 1.
212 // Increasing it is just silly since only 1 will be used ever.
213 DatabaseLoader loader("server.authserver");
214 loader
215 .AddDatabase(LoginDatabase, "Login");
216
217 if (!loader.Load())
218 return false;
219
220 LOG_INFO("server.authserver", "Started auth database connection pool.");
221 sLog->SetRealmId(0); // Enables DB appenders when realm is set.
222 return true;
223}
Definition DatabaseLoader.h:33
AC_DATABASE_API void Library_Init()
Definition MySQLThreading.cpp:21

References DatabaseLoader::AddDatabase(), MySQL::Library_Init(), DatabaseLoader::Load(), LOG_INFO, LoginDatabase, and sLog.

Referenced by main().

◆ StopDB()

void StopDB ( )

Close the connection to the database.

227{
228 LoginDatabase.Close();
230}
AC_DATABASE_API void Library_End()
Definition MySQLThreading.cpp:26

References MySQL::Library_End(), and LoginDatabase.

Referenced by main().