9
3

net.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. */
  6. #include "../common/debug.h"
  7. #include <iostream>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <signal.h>
  11. #include <sstream>
  12. #include <string>
  13. #include <iostream>
  14. #include "../common/queue.h"
  15. #include "../common/timer.h"
  16. #include "../common/seperator.h"
  17. #include "net.h"
  18. #include "client.h"
  19. #include "LoginDatabase.h"
  20. #include "LWorld.h"
  21. #include "../common/packet_functions.h"
  22. #include "../common/EQStreamFactory.h"
  23. #include "../common/MiscFunctions.h"
  24. #include "../common/version.h"
  25. #include "../common/PacketStruct.h"
  26. #include "../common/DataBuffer.h"
  27. #include "../common/ConfigReader.h"
  28. #include "../common/Log.h"
  29. #include "../common/JsonParser.h"
  30. #include "../common/Common_Defines.h"
  31. #ifdef WIN32
  32. #define snprintf _snprintf
  33. #define vsnprintf _vsnprintf
  34. #define strncasecmp _strnicmp
  35. #define strcasecmp _stricmp
  36. #include <conio.h>
  37. #else
  38. #include <stdlib.h>
  39. #include "../common/unix.h"
  40. #endif
  41. EQStreamFactory eqsf(LoginStream);
  42. map<int16,OpcodeManager*>EQOpcodeManager;
  43. //TCPServer eqns(5999);
  44. NetConnection net;
  45. ClientList client_list;
  46. LWorldList world_list;
  47. LoginDatabase database;
  48. ConfigReader configReader;
  49. map<int16, int16> EQOpcodeVersions;
  50. Timer statTimer(60000);
  51. volatile bool RunLoops = true;
  52. bool ReadLoginConfig();
  53. #ifdef PUBLICLOGIN
  54. char version[200], consoletitle[200];
  55. #endif
  56. #include "../common/timer.h"
  57. #include "../common/CRC16.h"
  58. #include <fstream>
  59. int main(int argc, char** argv){
  60. #ifdef _DEBUG
  61. _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  62. #endif
  63. if (signal(SIGINT, CatchSignal) == SIG_ERR) {
  64. cerr << "Could not set signal handler" << endl;
  65. }
  66. LogStart();
  67. LogParseConfigs();
  68. net.WelcomeHeader();
  69. srand(time(NULL));
  70. if(!net.ReadLoginConfig())
  71. return 1;
  72. net.InitWebServer(net.GetWebLoginAddress(), net.GetWebLoginPort(), net.GetWebCertFile(), net.GetWebKeyFile(), net.GetWebKeyPassword(), net.GetWebHardcodeUser(), net.GetWebHardcodePassword());
  73. const char* structList[] = { "CommonStructs.xml", "LoginStructs.xml" };
  74. for (int s = 0; s < sizeof(structList) / sizeof(const char*); s++)
  75. {
  76. LogWrite(INIT__INFO, 0, "Init", "Loading Structs File %s..", structList[s]);
  77. if (configReader.processXML_Elements(structList[s]))
  78. LogWrite(INIT__INFO, 0, "Init", "Loading Structs File %s completed..", structList[s]);
  79. else
  80. {
  81. LogWrite(INIT__ERROR, 0, "Init", "Loading Structs File %s FAILED!", structList[s]);
  82. return 1;
  83. }
  84. }
  85. LogWrite(INIT__INFO, 0, "Init", "Initialize World List..");
  86. world_list.Init();
  87. if(eqsf.listen_ip_address)
  88. LogWrite(INIT__INFO, 0, "Init", "Login server listening on %s port %i", eqsf.listen_ip_address, net.GetPort());
  89. else
  90. LogWrite(INIT__INFO, 0, "Init", "Login server listening on port %i", net.GetPort());
  91. /*}
  92. else {
  93. cout << "EQNetworkServer.Open() error" << endl;
  94. return 1;
  95. }*/
  96. if (!eqsf.Open(net.GetPort())) {
  97. LogWrite(INIT__ERROR, 0, "Init", "Failed to open port %i.", net.GetPort());
  98. return 1;
  99. }
  100. net.login_running = true;
  101. net.login_uptime = getCurrentTimestamp();
  102. net.UpdateWindowTitle();
  103. EQStream* eqs;
  104. Timer* TimeoutTimer = new Timer(5000);
  105. TimeoutTimer->Start();
  106. while(RunLoops) {
  107. Timer::SetCurrentTime();
  108. while ((eqs = eqsf.Pop())) {
  109. struct in_addr in;
  110. in.s_addr = eqs->GetRemoteIP();
  111. LogWrite(LOGIN__INFO, 0, "Login", "New client from IP: %s on port %i", inet_ntoa(in), ntohs(eqs->GetRemotePort()));
  112. Client* client = new Client(eqs);
  113. eqs->SetClientVersion(0);
  114. client_list.Add(client);
  115. net.numclients++;
  116. net.UpdateWindowTitle();
  117. }
  118. if(TimeoutTimer->Check()){
  119. eqsf.CheckTimeout();
  120. }
  121. if(statTimer.Check()){
  122. world_list.UpdateWorldStats();
  123. database.RemoveOldWorldServerStats();
  124. database.FixBugReport();
  125. }
  126. client_list.Process();
  127. world_list.Process();
  128. #ifdef WIN32
  129. if(kbhit())
  130. {
  131. int hitkey = getch();
  132. net.HitKey(hitkey);
  133. }
  134. #endif
  135. Sleep(1);
  136. }
  137. //close
  138. //eqns.Close();
  139. eqsf.Close();
  140. world_list.Shutdown();
  141. return 0;
  142. }
  143. #ifdef WIN32
  144. void NetConnection::HitKey(int keyhit)
  145. {
  146. switch(keyhit)
  147. {
  148. case 'l':
  149. case 'L': {
  150. world_list.ListWorldsToConsole();
  151. break;
  152. }
  153. case 'v':
  154. case 'V':
  155. {
  156. printf("========Version Info=========\n");
  157. printf("%s %s\n", EQ2EMU_MODULE, CURRENT_VERSION);
  158. printf("Last Compiled on %s %s\n", COMPILE_DATE, COMPILE_TIME);
  159. printf("=============================\n\n");
  160. break;
  161. }
  162. case 'H':
  163. case 'h': {
  164. printf("===========Help=============\n");
  165. printf("Available Commands:\n");
  166. printf("l = Listing of World Servers\n");
  167. printf("v = Login Version\n");
  168. // printf("0 = Kick all connected world servers\n");
  169. printf("============================\n\n");
  170. break;
  171. }
  172. default:
  173. printf("Invalid Command.\n");
  174. break;
  175. }
  176. }
  177. #endif
  178. void CatchSignal(int sig_num) {
  179. cout << "Got signal " << sig_num << endl;
  180. RunLoops = false;
  181. }
  182. bool NetConnection::ReadLoginConfig() {
  183. JsonParser parser(MAIN_CONFIG_FILE);
  184. if(!parser.IsLoaded()) {
  185. LogWrite(INIT__ERROR, 0, "Init", "Failed to find %s in server directory..", MAIN_CONFIG_FILE);
  186. return false;
  187. }
  188. std::string serverport = parser.getValue("loginconfig.serverport");
  189. std::string serverip = parser.getValue("loginconfig.serverip");
  190. if (!parser.convertStringToUnsignedShort(serverport, port)) {
  191. LogWrite(INIT__ERROR, 0, "Init", "Failed to translate loginconfig.serverport..");
  192. return false;
  193. }
  194. if(serverip.size() > 0) {
  195. eqsf.listen_ip_address = new char[serverip.size() + 1];
  196. strcpy(eqsf.listen_ip_address, serverip.c_str());
  197. }
  198. else {
  199. safe_delete(eqsf.listen_ip_address);
  200. eqsf.listen_ip_address = nullptr;
  201. }
  202. std::string acctcreate_str = parser.getValue("loginconfig.accountcreation");
  203. int16 allow_acct = 0;
  204. parser.convertStringToUnsignedShort(acctcreate_str, allow_acct);
  205. allowAccountCreation = allow_acct > 0 ? true : false;
  206. std::string expflag_str = parser.getValue("loginconfig.expansionflag");
  207. parser.convertStringToUnsignedInt(expflag_str, expansionFlag);
  208. std::string citiesflag_str = parser.getValue("loginconfig.citiesflag");
  209. parser.convertStringToUnsignedChar(citiesflag_str, citiesFlag);
  210. std::string defaultsublevel_str = parser.getValue("loginconfig.defaultsubscriptionlevel");
  211. parser.convertStringToUnsignedInt(defaultsublevel_str, defaultSubscriptionLevel);
  212. std::string enableraces_str = parser.getValue("loginconfig.enabledraces");
  213. parser.convertStringToUnsignedInt(enableraces_str, enabledRaces);
  214. web_loginaddress = parser.getValue("loginconfig.webloginaddress");
  215. web_certfile = parser.getValue("loginconfig.webcertfile");
  216. web_keyfile = parser.getValue("loginconfig.webkeyfile");
  217. web_keypassword = parser.getValue("loginconfig.webkeypassword");
  218. web_hardcodeuser = parser.getValue("loginconfig.webhardcodeuser");
  219. web_hardcodepassword = parser.getValue("loginconfig.webhardcodepassword");
  220. std::string webloginport_str = parser.getValue("loginconfig.webloginport");
  221. parser.convertStringToUnsignedShort(webloginport_str, web_loginport);
  222. LogWrite(INIT__INFO, 0, "Init", "%s loaded..", MAIN_CONFIG_FILE);
  223. LogWrite(INIT__INFO, 0, "Init", "Database init begin..");
  224. //remove this when all database calls are using the new database class
  225. if (!database.Init()) {
  226. LogWrite(INIT__ERROR, 0, "Init", "Database init FAILED!");
  227. LogStop();
  228. return false;
  229. }
  230. LogWrite(INIT__INFO, 0, "Init", "Loading opcodes 2.0..");
  231. EQOpcodeVersions = database.GetVersions();
  232. map<int16,int16>::iterator version_itr2;
  233. int16 version1 = 0;
  234. for (version_itr2 = EQOpcodeVersions.begin(); version_itr2 != EQOpcodeVersions.end(); version_itr2++) {
  235. version1 = version_itr2->first;
  236. EQOpcodeManager[version1] = new RegularOpcodeManager();
  237. map<string, uint16> eq = database.GetOpcodes(version1);
  238. if(!EQOpcodeManager[version1]->LoadOpcodes(&eq)) {
  239. LogWrite(INIT__ERROR, 0, "Init", "Loading opcodes failed. Make sure you have sourced the opcodes.sql file!");
  240. return false;
  241. }
  242. }
  243. return true;
  244. }
  245. void NetConnection::UpdateWindowTitle(char* iNewTitle) {
  246. #ifdef WIN32
  247. char tmp[500];
  248. if (iNewTitle) {
  249. snprintf(tmp, sizeof(tmp), "Login: %s", iNewTitle);
  250. }
  251. else {
  252. snprintf(tmp, sizeof(tmp), "%s, Version: %s: %i Server(s), %i Client(s) Connected", EQ2EMU_MODULE, CURRENT_VERSION, net.numservers, net.numclients);
  253. }
  254. SetConsoleTitle(tmp);
  255. #endif
  256. }
  257. void NetConnection::WelcomeHeader()
  258. {
  259. #ifdef _WIN32
  260. HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  261. SetConsoleTextAttribute(console, FOREGROUND_WHITE_BOLD);
  262. #endif
  263. printf("Module: %s, Version: %s", EQ2EMU_MODULE, CURRENT_VERSION);
  264. #ifdef _WIN32
  265. SetConsoleTextAttribute(console, FOREGROUND_YELLOW_BOLD);
  266. #endif
  267. printf("\n\nCopyright (C) 2007-2021 EQ2Emulator. https://www.eq2emu.com \n\n");
  268. printf("EQ2Emulator is free software: you can redistribute it and/or modify\n");
  269. printf("it under the terms of the GNU General Public License as published by\n");
  270. printf("the Free Software Foundation, either version 3 of the License, or\n");
  271. printf("(at your option) any later version.\n\n");
  272. printf("EQ2Emulator is distributed in the hope that it will be useful,\n");
  273. printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
  274. printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
  275. printf("GNU General Public License for more details.\n\n");
  276. #ifdef _WIN32
  277. SetConsoleTextAttribute(console, FOREGROUND_GREEN_BOLD);
  278. #endif
  279. printf(" /$$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$$ \n");
  280. printf("| $$_____/ /$$__ $$ /$$__ $$| $$_____/ \n");
  281. printf("| $$ | $$ \\ $$|__/ \\ $$| $$ /$$$$$$/$$$$ /$$ /$$\n");
  282. printf("| $$$$$ | $$ | $$ /$$$$$$/| $$$$$ | $$_ $$_ $$| $$ | $$\n");
  283. printf("| $$__/ | $$ | $$ /$$____/ | $$__/ | $$ \\ $$ \\ $$| $$ | $$\n");
  284. printf("| $$ | $$/$$ $$| $$ | $$ | $$ | $$ | $$| $$ | $$\n");
  285. printf("| $$$$$$$$| $$$$$$/| $$$$$$$$| $$$$$$$$| $$ | $$ | $$| $$$$$$/\n");
  286. printf("|________/ \\____ $$$|________/|________/|__/ |__/ |__/ \\______/ \n");
  287. printf(" \\__/ \n\n");
  288. #ifdef _WIN32
  289. SetConsoleTextAttribute(console, FOREGROUND_MAGENTA_BOLD);
  290. #endif
  291. printf(" Website : https://eq2emu.com \n");
  292. printf(" Wiki : https://wiki.eq2emu.com \n");
  293. printf(" Git : https://git.eq2emu.com \n");
  294. printf(" Discord : https://discord.gg/5Cavm9NYQf \n\n");
  295. #ifdef _WIN32
  296. SetConsoleTextAttribute(console, FOREGROUND_WHITE_BOLD);
  297. #endif
  298. printf("For more detailed logging, modify 'Level' param the log_config.xml file.\n\n");
  299. #ifdef _WIN32
  300. SetConsoleTextAttribute(console, FOREGROUND_WHITE);
  301. #endif
  302. fflush(stdout);
  303. }
  304. void NetConnection::InitWebServer(std::string web_ipaddr, int16 web_port, std::string cert_file, std::string key_file, std::string key_password, std::string hardcode_user, std::string hardcode_password) {
  305. if(web_ipaddr.size() > 0 && web_port > 0) {
  306. try {
  307. login_webserver = new WebServer(web_ipaddr, web_port, cert_file, key_file, key_password, hardcode_user, hardcode_password);
  308. login_webserver->register_route("/status", NetConnection::Web_loginhandle_status);
  309. login_webserver->register_route("/worlds", NetConnection::Web_loginhandle_worlds);
  310. login_webserver->run();
  311. LogWrite(INIT__INFO, 0, "Init", "Login Web Server is listening on %s:%u..", web_ipaddr.c_str(), web_port);
  312. }
  313. catch (const std::exception& e) {
  314. LogWrite(INIT__ERROR, 0, "Init", "Login Web Server failed to listen on %s:%u due to reason %s", web_ipaddr.c_str(), web_port, e.what());
  315. }
  316. }
  317. }