MSClient.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #pragma once
  2. #include <Klient.h>
  3. #include <iostream>
  4. #include <Text.h>
  5. #include "Keys.h"
  6. #include "ESClient.h"
  7. #include "PSClient.h"
  8. #define MAIN_SERVER_PORT 5225
  9. #define MAIN_SERVER_IP "94.130.27.12"
  10. #define ACCOUNT_NAME "Test"
  11. #define ACCOUNT_PASSWORD "t"
  12. class MSClient
  13. {
  14. private:
  15. Network::Klient *k;
  16. char *key;
  17. unsigned char keyLen;
  18. int cId;
  19. unsigned short port;
  20. public:
  21. MSClient()
  22. {
  23. k = 0;
  24. key = 0;
  25. keyLen = 0;
  26. cId = 0;
  27. port = 0;
  28. }
  29. ~MSClient()
  30. {
  31. if( k )
  32. k->release();
  33. delete[] key;
  34. }
  35. bool registerSSL()
  36. {
  37. Network::SSLKlient *klientSSL = new Network::SSLKlient();
  38. int l = 0;
  39. if( !klientSSL->verbinde( MAIN_SERVER_PORT, MAIN_SERVER_IP ) )
  40. {
  41. std::cerr << "error while trying to connect to Main SSL Server\n";
  42. klientSSL->release();
  43. return 0;
  44. }
  45. klientSSL->sende( "\1", 5 );
  46. char byte = 0;
  47. klientSSL->getNachricht( &byte, 1 );
  48. if( byte == 3 )
  49. {
  50. klientSSL->getNachricht( &byte, 1 );
  51. char *message = new char[ byte + 1 ];
  52. message[ byte ] = 0;
  53. klientSSL->getNachricht( message, byte );
  54. std::cerr << "error while register new client server returned: " << message << "\n";
  55. delete[]message;
  56. klientSSL->sende( "\3", 1 );
  57. klientSSL->getNachricht( &byte, 1 );
  58. klientSSL->trenne();
  59. klientSSL->release();
  60. return 0;
  61. }
  62. if( byte == 1 )
  63. {
  64. klientSSL->getNachricht( (char*)&port, 4 );
  65. klientSSL->getNachricht( (char*)&cId, 4 );
  66. klientSSL->getNachricht( (char*)&keyLen, 1 );
  67. if( keyLen )
  68. {
  69. key = new char[ keyLen ];
  70. klientSSL->getNachricht( key, keyLen );
  71. }
  72. }
  73. klientSSL->sende( "\3", 1 );
  74. klientSSL->getNachricht( &byte, 1 );
  75. klientSSL->trenne();
  76. klientSSL->release();
  77. return 1;
  78. }
  79. bool connect()
  80. {
  81. if( k )
  82. return 1;
  83. k = new Network::Klient();
  84. int l = 0;
  85. char *key;
  86. Keys::getServerKey( &key, l, Keys::MAIN, Keys::SENDEN );
  87. k->setSendeKey( key, l );
  88. delete[] key;
  89. Keys::getServerKey( &key, l, Keys::MAIN, Keys::EMPFANGEN );
  90. k->setEmpfangKey( key, l );
  91. delete[] key;
  92. if( !k->verbinde( port, MAIN_SERVER_IP ) )
  93. {
  94. std::cerr << "error while trying to connect to Main Server\n";
  95. k = k->release();
  96. return 0;
  97. }
  98. k->sende( "\0", 1 ); // Verschlüsselung aktivieren
  99. k->sendeEncrypted( "\1", 1 );
  100. k->sendeEncrypted( (char*)&cId, 4 );
  101. char serverReturn = 0;
  102. k->getNachrichtEncrypted( &serverReturn, 1 );
  103. if( serverReturn == 3 )
  104. {
  105. k->getNachrichtEncrypted( &serverReturn, 1 );
  106. char *message = new char[ serverReturn + 1 ];
  107. message[ serverReturn ] = 0;
  108. k->getNachrichtEncrypted( message, serverReturn );
  109. std::cerr << "error while trying to identify registered client server returned: " << message << "\n";
  110. delete[] message;
  111. k->sendeEncrypted( "\3", 1 );
  112. k->getNachrichtEncrypted( &serverReturn, 1 );
  113. k->trenne();
  114. k = k->release();
  115. return 0;
  116. }
  117. else
  118. {
  119. k->setSendeKey( this->key, keyLen );
  120. k->setEmpfangKey( this->key, keyLen );
  121. return 1;
  122. }
  123. }
  124. ESClient *createErhaltungServerClient()
  125. {
  126. connect();
  127. if( !k )
  128. {
  129. std::cerr << "no connection to Main Server\n";
  130. return 0;
  131. }
  132. k->sendeEncrypted( "\6\x8", 2 );
  133. char byte = 0;
  134. k->getNachrichtEncrypted( &byte, 1 );
  135. if( byte == 2 )
  136. {
  137. unsigned char lsIp[ 4 ];
  138. k->getNachrichtEncrypted( (char *)lsIp, 4 );
  139. unsigned short lsPort = 0;
  140. k->getNachrichtEncrypted( (char*)&lsPort, 2 );
  141. Framework::Text ipT;
  142. ipT += (int)lsIp[ 0 ];
  143. ipT += ".";
  144. ipT += (int)lsIp[ 1 ];
  145. ipT += ".";
  146. ipT += (int)lsIp[ 2 ];
  147. ipT += ".";
  148. ipT += (int)lsIp[ 3 ];
  149. return new ESClient( cId, lsPort, ipT, key, keyLen );
  150. }
  151. else if( byte == 3 )
  152. {
  153. k->getNachrichtEncrypted( &byte, 1 );
  154. char *f = new char[ byte + 1 ];
  155. f[ byte ] = 0;
  156. k->getNachrichtEncrypted( f, byte );
  157. std::cerr << "error while requesting ErhaltungServer server returned: " << f << "\n";
  158. delete[]f;
  159. }
  160. return 0;
  161. }
  162. PSClient *createPatchServerClient()
  163. {
  164. connect();
  165. if( !k )
  166. {
  167. std::cerr << "no connection to Main Server\n";
  168. return 0;
  169. }
  170. k->sendeEncrypted( "\6\2", 2 );
  171. char byte = 0;
  172. k->getNachrichtEncrypted( &byte, 1 );
  173. if( byte == 2 )
  174. {
  175. unsigned char lsIp[ 4 ];
  176. k->getNachrichtEncrypted( (char *)lsIp, 4 );
  177. unsigned short lsPort = 0;
  178. k->getNachrichtEncrypted( (char*)&lsPort, 2 );
  179. Framework::Text ipT;
  180. ipT += (int)lsIp[ 0 ];
  181. ipT += ".";
  182. ipT += (int)lsIp[ 1 ];
  183. ipT += ".";
  184. ipT += (int)lsIp[ 2 ];
  185. ipT += ".";
  186. ipT += (int)lsIp[ 3 ];
  187. return new PSClient( cId, lsPort, ipT, key, keyLen );
  188. }
  189. else if( byte == 3 )
  190. {
  191. k->getNachrichtEncrypted( &byte, 1 );
  192. char *f = new char[ byte + 1 ];
  193. f[ byte ] = 0;
  194. k->getNachrichtEncrypted( f, byte );
  195. std::cerr << "error while requesting PatchServer server returned: " << f << "\n";
  196. delete[]f;
  197. }
  198. return 0;
  199. }
  200. bool unregister()
  201. {
  202. connect();
  203. if( !k )
  204. {
  205. std::cerr << "no connection to Main Server\n";
  206. return 0;
  207. }
  208. k->sendeEncrypted( "\7", 1 );
  209. char serverReturn = 0;
  210. k->getNachrichtEncrypted( &serverReturn, 1 );
  211. if( serverReturn == 3 )
  212. {
  213. k->getNachrichtEncrypted( &serverReturn, 1 );
  214. char *message = new char[ serverReturn + 1 ];
  215. message[ serverReturn ] = 0;
  216. k->getNachrichtEncrypted( message, serverReturn );
  217. std::cerr << "error while trying to unregistere client server returned: " << message << "\n";
  218. delete[] message;
  219. k->sendeEncrypted( "\3", 1 );
  220. k->getNachrichtEncrypted( &serverReturn, 1 );
  221. k->trenne();
  222. k = k->release();
  223. return 0;
  224. }
  225. k->sendeEncrypted( "\3", 1 );
  226. k->getNachrichtEncrypted( &serverReturn, 1 );
  227. k->trenne();
  228. k = k->release();
  229. cId = 0;
  230. delete key;
  231. key = 0;
  232. keyLen = 0;
  233. return 1;
  234. }
  235. };