MSClient.h 5.8 KB

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