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