Server.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #include "Server.h"
  2. #include <iostream>
  3. #include <Klient.h>
  4. #include <Globals.h>
  5. #include <HttpRequest.h>
  6. #include <JSON.h>
  7. #include <AsynchronCall.h>
  8. // Inhalt der LoginServer Klasse aus LoginServer.h
  9. // Konstruktor
  10. FactoryCraftServer::FactoryCraftServer(InitDatei* zIni)
  11. : ReferenceCounter()
  12. {
  13. Network::Start(100);
  14. runningThreads = 0;
  15. klients = new RCArray< FCKlient >();
  16. ini = dynamic_cast<InitDatei*>(zIni->getThis());
  17. id = *zIni->zWert("ServerId");
  18. sslServer = new SSLServer();
  19. sslServer->setPrivateKeyPassword(zIni->zWert("SSLPasswort")->getText());
  20. sslServer->setCertificateFile(zIni->zWert("SSLCert")->getText());
  21. std::cout << "using cert file " << zIni->zWert("SSLCert")->getText() << "\n";
  22. sslServer->setPrivateKeyFile(zIni->zWert("SSLKey")->getText());
  23. std::cout << "using private key " << zIni->zWert("SSLKey")->getText() << "\n";
  24. server = new Server();
  25. std::cout << "Server Port: " << ini->zWert("Port")->getText() << "\n";
  26. if (!server->verbinde((unsigned short)TextZuInt(ini->zWert("Port")->getText(), 10), 10))
  27. {
  28. std::cout << "Der Server konnte nicht gestartet werden.\n";
  29. exit(1);
  30. }
  31. std::cout << "SSL Server Port: " << ini->zWert("SSLPort")->getText() << "\n";
  32. if (!sslServer->verbinde((unsigned short)TextZuInt(ini->zWert("SSLPort")->getText(), 10), 10))
  33. {
  34. std::cout << "Der SSL Server konnte nicht gestartet werden.\n";
  35. exit(2);
  36. }
  37. Game::initialize(zIni->zWert("World")->getText(), zIni->zWert("SaveDir")->getText());
  38. new Framework::AsynchronCall("Server", [this]()
  39. {
  40. runningThreads++;
  41. while (server->isConnected())
  42. {
  43. SKlient* klient = server->getKlient();
  44. if (!klient)
  45. continue;
  46. unsigned short len;
  47. klient->setEmpfangTimeout(5000);
  48. klient->getNachricht((char*)&len, 2);
  49. char* key = new char[len];
  50. klient->getNachricht((char*)key, len);
  51. bool bg;
  52. klient->getNachricht((char*)&bg, 1);
  53. klient->setEmpfangTimeout(0);
  54. bool found = 0;
  55. EnterCriticalSection(&cs);
  56. for (FCKlient* client : *klients)
  57. {
  58. if (client->matchAuthKey(key, len))
  59. {
  60. if (bg)
  61. client->setBackgroundClient(klient);
  62. else
  63. client->setForegroundClient(klient);
  64. found = 1;
  65. break;
  66. }
  67. }
  68. LeaveCriticalSection(&cs);
  69. if (!found)
  70. klient->release();
  71. }
  72. runningThreads--;
  73. });
  74. InitializeCriticalSection(&cs);
  75. }
  76. // Destruktor
  77. FactoryCraftServer::~FactoryCraftServer()
  78. {
  79. sslServer->trenne();
  80. server->trenne();
  81. while (runningThreads > 0)
  82. Sleep(100);
  83. sslServer->release();
  84. server->release();
  85. if (klients)
  86. klients->release();
  87. ini->release();
  88. Game::INSTANCE->requestStop();
  89. Game::INSTANCE->release();
  90. DeleteCriticalSection(&cs);
  91. }
  92. // nicht constant
  93. void FactoryCraftServer::run()
  94. {
  95. runningThreads++;
  96. while (sslServer->isConnected())
  97. {
  98. SSLSKlient* klient = sslServer->getKlient();
  99. if (!klient)
  100. continue;
  101. Framework::getThreadRegister()->cleanUpClosedThreads();
  102. FCKlient* clHandle = new FCKlient(klient, dynamic_cast<FactoryCraftServer*>(getThis()));
  103. EnterCriticalSection(&cs);
  104. klients->add(clHandle);
  105. LeaveCriticalSection(&cs);
  106. clHandle->start();
  107. }
  108. runningThreads--;
  109. }
  110. void FactoryCraftServer::close()
  111. {
  112. sslServer->trenne();
  113. EnterCriticalSection(&cs);
  114. for (int i = 0; i < klients->getEintragAnzahl(); i++)
  115. klients->z(i)->absturz();
  116. klients = (RCArray< FCKlient > *)klients->release();
  117. Game::INSTANCE->save();
  118. LeaveCriticalSection(&cs);
  119. }
  120. bool FactoryCraftServer::absturzKlient(int accountId)
  121. {
  122. bool gefunden = 0;
  123. EnterCriticalSection(&cs);
  124. for (int i = 0; i < klients->getEintragAnzahl(); i++)
  125. {
  126. if (klients->z(i) && klients->z(i)->getAccountId() == accountId)
  127. {
  128. klients->z(i)->absturz();
  129. klients->remove(i);
  130. gefunden = 1;
  131. break;
  132. }
  133. }
  134. LeaveCriticalSection(&cs);
  135. return gefunden;
  136. }
  137. bool FactoryCraftServer::removeKlient(FCKlient* zKlient)
  138. {
  139. bool gefunden = 0;
  140. EnterCriticalSection(&cs);
  141. for (int i = 0; i < klients->getEintragAnzahl(); i++)
  142. {
  143. if (klients->z(i) == zKlient)
  144. {
  145. klients->remove(i);
  146. gefunden = 1;
  147. break;
  148. }
  149. }
  150. LeaveCriticalSection(&cs);
  151. return gefunden;
  152. }
  153. bool FactoryCraftServer::hatClients() const
  154. {
  155. return klients->hat(0);
  156. }
  157. int FactoryCraftServer::getUnencryptedPort() const
  158. {
  159. return server->getPort();
  160. }
  161. char* randomKey(int& len)
  162. {
  163. len = 1024 + (int)(((double)rand() / RAND_MAX - 0.5) * 512);
  164. char* key = new char[len];
  165. for (int i = 0; i < len; i++)
  166. key[i] = (char)(((double)rand() / RAND_MAX) * 256);
  167. return key;
  168. }
  169. // Inhalt der LSKlient aus LoginServer.h
  170. // Konstruktor
  171. FCKlient::FCKlient(SSLSKlient* klient, FactoryCraftServer* ls)
  172. : Thread()
  173. {
  174. this->klient = klient;
  175. background = 0;
  176. foreground = 0;
  177. accountId = 0;
  178. this->ls = ls;
  179. zGameClient = 0;
  180. backgroundReader = 0;
  181. foregroundReader = 0;
  182. backgroundWriter = 0;
  183. foregroundWriter = 0;
  184. authKey = randomKey(authKeyLen);
  185. }
  186. // Destruktor
  187. FCKlient::~FCKlient()
  188. {
  189. if (zGameClient)
  190. {
  191. zGameClient->logout();
  192. zGameClient = (GameClient*)zGameClient->release();
  193. }
  194. if (background)
  195. background->release();
  196. if (foreground)
  197. foreground->release();
  198. delete backgroundReader;
  199. delete foregroundReader;
  200. delete backgroundWriter;
  201. delete foregroundWriter;
  202. klient->release();
  203. ls->release();
  204. delete[] authKey;
  205. }
  206. // nicht constant
  207. void FCKlient::setForegroundClient(SKlient* foreground)
  208. {
  209. this->foreground = foreground;
  210. foregroundReader = new NetworkReader(foreground);
  211. foregroundWriter = new NetworkWriter(foreground);
  212. if (foreground && background)
  213. zGameClient = Game::INSTANCE->addPlayer(dynamic_cast<FCKlient*>(getThis()), Framework::Text((int)accountId));
  214. new AsynchronCall([this]()
  215. {
  216. while (this->foreground->waitForNextMessage())
  217. {
  218. if (zGameClient)
  219. zGameClient->addMessage(foregroundReader);
  220. if (!zGameClient)
  221. Sleep(100);
  222. }
  223. zGameClient->logout();
  224. ls->removeKlient(this);
  225. });
  226. }
  227. void FCKlient::setBackgroundClient(SKlient* background)
  228. {
  229. this->background = background;
  230. backgroundReader = new NetworkReader(background);
  231. backgroundWriter = new NetworkWriter(background);
  232. if (foreground && background)
  233. zGameClient = Game::INSTANCE->addPlayer(dynamic_cast<FCKlient*>(getThis()), Framework::Text((int)accountId));
  234. new AsynchronCall([this]()
  235. {
  236. while (this->background->waitForNextMessage())
  237. {
  238. if (zGameClient)
  239. zGameClient->addMessage(backgroundReader);
  240. if (!zGameClient)
  241. Sleep(100);
  242. }
  243. zGameClient->logout();
  244. ls->removeKlient(this);
  245. });
  246. }
  247. void FCKlient::absturz()
  248. {
  249. ende();
  250. klient->trenne();
  251. }
  252. void FCKlient::thread()
  253. {
  254. while (1)
  255. {
  256. char c = 0;
  257. if (!klient->getNachricht(&c, 1))
  258. break;
  259. else
  260. {
  261. bool br = 0;
  262. switch (c)
  263. {
  264. case 1: // Klient identifikation
  265. {
  266. int accountId = 0;
  267. klient->getNachricht((char*)&accountId, 4);
  268. unsigned char secretLength = 0;
  269. klient->getNachricht((char*)&secretLength, 1);
  270. char* secret = new char[secretLength + 1];
  271. klient->getNachricht(secret, (int)secretLength);
  272. secret[secretLength] = 0;
  273. Text data = "{\"account_id\":";
  274. data += accountId;
  275. data += ", \"secret\": \"";
  276. data += secret;
  277. data += "\"}";
  278. bool ok = false;
  279. HTTP::Answer* answer = HTTP::PostRequest("/game_client/api/verify_client.php", "koljastrohm-games.com", data, "application/json", 443, true).execute();
  280. if (answer->getStatusCode() == 200)
  281. {
  282. JSON::JSONObject obj(answer->getData());
  283. if (obj.hasValue("verified"))
  284. {
  285. JSON::JSONValue* value = obj.getValue("verified");
  286. if (value->getType() == JSON::JSONType::BOOLEAN)
  287. {
  288. if (((JSON::JSONBool*)value)->getBool())
  289. {
  290. this->accountId = accountId;
  291. if (zGameClient)
  292. {
  293. zGameClient->logout();
  294. zGameClient = (GameClient*)zGameClient->release();
  295. }
  296. klient->sende("\1", 1);
  297. klient->sende((char*)&authKeyLen, 4);
  298. klient->sende(authKey, authKeyLen);
  299. int port = ls->getUnencryptedPort();
  300. klient->sende((char*)&port, 4);
  301. ok = true;
  302. }
  303. }
  304. value->release();
  305. }
  306. }
  307. answer->release();
  308. delete[]secret;
  309. if (!ok)
  310. klient->sende("\0", 1);
  311. break;
  312. }
  313. case 2: // Verbindungsende
  314. br = 1;
  315. if (zGameClient)
  316. {
  317. zGameClient->logout();
  318. zGameClient = (GameClient*)zGameClient->release();
  319. }
  320. klient->sende("\1", 1);
  321. break;
  322. default:
  323. br = 1;
  324. break;
  325. }
  326. if (br)
  327. break;
  328. }
  329. }
  330. }
  331. int FCKlient::getAccountId() const // gibt die KlientId zurück
  332. {
  333. return accountId;
  334. }
  335. NetworkWriter* FCKlient::zBackgroundWriter() const
  336. {
  337. return backgroundWriter;
  338. }
  339. NetworkWriter* FCKlient::zForegroundWriter() const
  340. {
  341. return foregroundWriter;
  342. }
  343. bool FCKlient::matchAuthKey(char* key, int len) const
  344. {
  345. if (foreground && background)
  346. return 0;
  347. if (len != authKeyLen)
  348. return 0;
  349. for (int i = 0; i < len; i++)
  350. {
  351. if (key[i] != authKey[i])
  352. return 0;
  353. }
  354. return 1;
  355. }