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 "DeadlineTimer.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 "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. More...
 
void StopDB ()
 Close the connection to the database. More...
 
void SignalHandler (std::weak_ptr< Acore::Asio::IoContext > ioContextRef, boost::system::error_code const &error, int signalNumber)
 
void KeepDatabaseAliveHandler (std::weak_ptr< Acore::Asio::DeadlineTimer > dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const &error)
 
void BanExpiryHandler (std::weak_ptr< Acore::Asio::DeadlineTimer > 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. More...
 

Macro Definition Documentation

◆ _ACORE_REALM_CONFIG

#define _ACORE_REALM_CONFIG   "authserver.conf"

Function Documentation

◆ BanExpiryHandler()

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

References BanExpiryHandler(), 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 
)
272{
273 options_description all("Allowed options");
274 all.add_options()
275 ("help,h", "print usage message")
276 ("version,v", "print version build info")
277 ("dry-run,d", "Dry run")
278 ("config,c", value<fs::path>(&configFile)->default_value(fs::path(sConfigMgr->GetConfigPath() + std::string(_ACORE_REALM_CONFIG))), "use <arg> as configuration file");
279
280 variables_map variablesMap;
281
282 try
283 {
284 store(command_line_parser(argc, argv).options(all).allow_unregistered().run(), variablesMap);
285 notify(variablesMap);
286 }
287 catch (std::exception const& e)
288 {
289 std::cerr << e.what() << "\n";
290 }
291
292 if (variablesMap.count("help"))
293 {
294 std::cout << all << "\n";
295 }
296 else if (variablesMap.count("dry-run"))
297 {
298 sConfigMgr->setDryRun(true);
299 }
300
301 return variablesMap;
302}
#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< Acore::Asio::DeadlineTimer dbPingTimerRef,
int32  dbPingInterval,
boost::system::error_code const &  error 
)
242{
243 if (!error)
244 {
245 if (std::shared_ptr<Acore::Asio::DeadlineTimer> dbPingTimer = dbPingTimerRef.lock())
246 {
247 LOG_INFO("server.authserver", "Ping MySQL to keep connection alive");
248 LoginDatabase.KeepAlive();
249
250 dbPingTimer->expires_from_now(boost::posix_time::minutes(dbPingInterval));
251 dbPingTimer->async_wait(std::bind(&KeepDatabaseAliveHandler, dbPingTimerRef, dbPingInterval, std::placeholders::_1));
252 }
253 }
254}
#define LOG_INFO(filterType__,...)
Definition: Log.h:165
void KeepDatabaseAliveHandler(std::weak_ptr< Acore::Asio::DeadlineTimer > dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const &error)
Definition: Main.cpp:241

References KeepDatabaseAliveHandler(), LOG_INFO, 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<Acore::Asio::DeadlineTimer> dbPingTimer = std::make_shared<Acore::Asio::DeadlineTimer>(*ioContext);
182 dbPingTimer->expires_from_now(boost::posix_time::minutes(dbPingInterval));
183 dbPingTimer->async_wait(std::bind(&KeepDatabaseAliveHandler, std::weak_ptr<Acore::Asio::DeadlineTimer>(dbPingTimer), dbPingInterval, std::placeholders::_1));
184
185 int32 banExpiryCheckInterval = sConfigMgr->GetOption<int32>("BanExpiryCheckInterval", 60);
186 std::shared_ptr<Acore::Asio::DeadlineTimer> banExpiryCheckTimer = std::make_shared<Acore::Asio::DeadlineTimer>(*ioContext);
187 banExpiryCheckTimer->expires_from_now(boost::posix_time::seconds(banExpiryCheckInterval));
188 banExpiryCheckTimer->async_wait(std::bind(&BanExpiryHandler, std::weak_ptr<Acore::Asio::DeadlineTimer>(banExpiryCheckTimer), banExpiryCheckInterval, std::placeholders::_1));
189
190 // Start the io service worker loop
191 ioContext->run();
192
193 banExpiryCheckTimer->cancel();
194 dbPingTimer->cancel();
195
196 LOG_INFO("server.authserver", "Halting process...");
197
198 signals.cancel();
199
200 return 0;
201}
#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 sIPLocation
Definition: IPLocation.h:49
uint32 CreatePIDFile(std::string const &filename)
create PID file
Definition: Util.cpp:218
std::int32_t int32
Definition: Define.h:103
std::uint32_t uint32
Definition: Define.h:107
#define sAuthSocketMgr
Definition: AuthSocketMgr.h:63
#define sRealmList
Definition: RealmList.h:77
#define sSecretMgr
Definition: SecretMgr.h:72
@ SERVER_PROCESS_AUTHSERVER
Definition: SharedDefines.h:3738
AC_COMMON_API void AbortHandler(int sigval)
Definition: Errors.cpp:148
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 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
Definition: AppenderDB.h:25
static ServerProcessTypes _type
Definition: SharedDefines.h:3762
variables_map GetConsoleArguments(int argc, char **argv, fs::path &configFile)
Definition: Main.cpp:271
void SignalHandler(std::weak_ptr< Acore::Asio::IoContext > ioContextRef, boost::system::error_code const &error, int signalNumber)
Definition: Main.cpp:230
bool StartDB()
Initialize connection to the database.
Definition: Main.cpp:204
void StopDB()
Close the connection to the database.
Definition: Main.cpp:224

References _ACORE_REALM_CONFIG, Acore::Impl::CurrentServerProcessHolder::_type, Acore::AbortHandler(), BanExpiryHandler(), CONFIG_HIGH_PRIORITY, CONFIG_PROCESSOR_AFFINITY, CreatePIDFile(), GetConsoleArguments(), KeepDatabaseAliveHandler(), LOG_ERROR, LOG_INFO, 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 
)
231{
232 if (!error)
233 {
234 if (std::shared_ptr<Acore::Asio::IoContext> ioContext = ioContextRef.lock())
235 {
236 ioContext->stop();
237 }
238 }
239}

Referenced by main().

◆ StartDB()

bool StartDB ( )

Initialize connection to the database.

Initialize connection to the databases.

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

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.

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

References MySQL::Library_End(), and LoginDatabase.

Referenced by main().