Datacenter.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * This is the source code of tgnet library v. 1.1
  3. * It is licensed under GNU GPL v. 2 or later.
  4. * You should have received a copy of the license in this archive (see LICENSE).
  5. *
  6. * Copyright Nikolai Kudashov, 2015-2018.
  7. */
  8. #ifndef DATACENTER_H
  9. #define DATACENTER_H
  10. #include <stdint.h>
  11. #include <vector>
  12. #include <map>
  13. #include "Defines.h"
  14. class TL_future_salt;
  15. class Connection;
  16. class NativeByteBuffer;
  17. class TL_future_salt;
  18. class TL_future_salts;
  19. class TL_help_configSimple;
  20. class ByteArray;
  21. class TLObject;
  22. class Config;
  23. class Handshake;
  24. class Datacenter : public HandshakeDelegate {
  25. public:
  26. Datacenter(int32_t instance, uint32_t id);
  27. Datacenter(int32_t instance, NativeByteBuffer *data);
  28. uint32_t getDatacenterId();
  29. TcpAddress *getCurrentAddress(uint32_t flags);
  30. int32_t getCurrentPort(uint32_t flags);
  31. void addAddressAndPort(std::string address, uint32_t port, uint32_t flags, std::string secret);
  32. void nextAddressOrPort(uint32_t flags);
  33. bool isCustomPort(uint32_t flags);
  34. void storeCurrentAddressAndPortNum();
  35. void replaceAddresses(std::vector<TcpAddress> &newAddresses, uint32_t flags);
  36. void serializeToStream(NativeByteBuffer *stream);
  37. void clearAuthKey(HandshakeType type);
  38. void clearServerSalts(bool media);
  39. int64_t getServerSalt(bool media);
  40. void mergeServerSalts(TL_future_salts *newSalts, bool media);
  41. void addServerSalt(std::unique_ptr<TL_future_salt> &serverSalt, bool media);
  42. bool containsServerSalt(int64_t value, bool media);
  43. void suspendConnections(bool suspendPush);
  44. void getSessions(std::vector<int64_t> &sessions);
  45. void recreateSessions(HandshakeType type);
  46. void resetAddressAndPortNum();
  47. bool isHandshakingAny();
  48. bool isHandshaking(bool media);
  49. bool isHandshaking(HandshakeType type);
  50. bool hasAuthKey(ConnectionType connectionTyoe, int32_t allowPendingKey);
  51. bool hasPermanentAuthKey();
  52. int64_t getPermanentAuthKeyId();
  53. bool isExportingAuthorization();
  54. bool hasMediaAddress();
  55. void resetInitVersion();
  56. bool isRepeatCheckingAddresses();
  57. Connection *getDownloadConnection(uint8_t num, bool create);
  58. Connection *getProxyConnection(uint8_t num, bool create, bool connect);
  59. Connection *getUploadConnection(uint8_t num, bool create);
  60. Connection *getGenericConnection(bool create, int32_t allowPendingKey);
  61. Connection *getGenericMediaConnection(bool create, int32_t allowPendingKey);
  62. Connection *getPushConnection(bool create);
  63. Connection *getTempConnection(bool create);
  64. Connection *getConnectionByType(uint32_t connectionType, bool create, int32_t allowPendingKey);
  65. static void aesIgeEncryption(uint8_t *buffer, uint8_t *key, uint8_t *iv, bool encrypt, bool changeIv, uint32_t length);
  66. private:
  67. void onHandshakeConnectionClosed(Connection *connection);
  68. void onHandshakeConnectionConnected(Connection *connection);
  69. void onHandshakeComplete(Handshake *handshake, int64_t keyId, ByteArray *authKey, int32_t timeDifference);
  70. void processHandshakeResponse(bool media, TLObject *message, int64_t messageId);
  71. NativeByteBuffer *createRequestsData(std::vector<std::unique_ptr<NetworkMessage>> &requests, int32_t *quickAckId, Connection *connection, bool pfsInit);
  72. bool decryptServerResponse(int64_t keyId, uint8_t *key, uint8_t *data, uint32_t length, Connection *connection);
  73. TLObject *getCurrentHandshakeRequest(bool media);
  74. ByteArray *getAuthKey(ConnectionType connectionType, bool perm, int64_t *authKeyId, int32_t allowPendingKey);
  75. const int32_t *defaultPorts = new int32_t[4] {-1, 443, 5222, -1};
  76. int32_t instanceNum;
  77. uint32_t datacenterId;
  78. Connection *genericConnection = nullptr;
  79. Connection *genericMediaConnection = nullptr;
  80. Connection *tempConnection = nullptr;
  81. Connection *proxyConnection[PROXY_CONNECTIONS_COUNT];
  82. Connection *downloadConnection[DOWNLOAD_CONNECTIONS_COUNT];
  83. Connection *uploadConnection[UPLOAD_CONNECTIONS_COUNT];
  84. Connection *pushConnection = nullptr;
  85. uint32_t lastInitVersion = 0;
  86. uint32_t lastInitMediaVersion = 0;
  87. bool authorized = false;
  88. std::vector<TcpAddress> addressesIpv4;
  89. std::vector<TcpAddress> addressesIpv6;
  90. std::vector<TcpAddress> addressesIpv4Download;
  91. std::vector<TcpAddress> addressesIpv6Download;
  92. std::vector<TcpAddress> addressesIpv4Temp;
  93. std::vector<std::unique_ptr<TL_future_salt>> serverSalts;
  94. std::vector<std::unique_ptr<TL_future_salt>> mediaServerSalts;
  95. uint32_t currentPortNumIpv4 = 0;
  96. uint32_t currentAddressNumIpv4 = 0;
  97. uint32_t currentPortNumIpv4Temp = 0;
  98. uint32_t currentAddressNumIpv4Temp = 0;
  99. uint32_t currentPortNumIpv6 = 0;
  100. uint32_t currentAddressNumIpv6 = 0;
  101. uint32_t currentPortNumIpv4Download = 0;
  102. uint32_t currentAddressNumIpv4Download = 0;
  103. uint32_t currentPortNumIpv6Download = 0;
  104. uint32_t currentAddressNumIpv6Download = 0;
  105. ByteArray *authKeyPerm = nullptr;
  106. int64_t authKeyPermId = 0;
  107. ByteArray *authKeyTemp = nullptr;
  108. int64_t authKeyTempId = 0;
  109. ByteArray *authKeyMediaTemp = nullptr;
  110. int64_t authKeyMediaTempId = 0;
  111. Config *config = nullptr;
  112. bool isCdnDatacenter = false;
  113. bool repeatCheckingAddresses = false;
  114. std::vector<std::unique_ptr<Handshake>> handshakes;
  115. const uint32_t configVersion = 13;
  116. const uint32_t paramsConfigVersion = 1;
  117. Connection *createProxyConnection(uint8_t num);
  118. Connection *createDownloadConnection(uint8_t num);
  119. Connection *createUploadConnection(uint8_t num);
  120. Connection *createGenericConnection();
  121. Connection *createGenericMediaConnection();
  122. Connection *createTempConnection();
  123. Connection *createPushConnection();
  124. Connection *createConnectionByType(uint32_t connectionType);
  125. void beginHandshake(HandshakeType handshakeType, bool reconnect);
  126. bool exportingAuthorization = false;
  127. void exportAuthorization();
  128. static TL_help_configSimple *decodeSimpleConfig(NativeByteBuffer *buffer);
  129. friend class ConnectionsManager;
  130. friend class Connection;
  131. friend class Handshake;
  132. friend class Request;
  133. };
  134. #endif