| 6 |
| * Copyright Nikolai Kudashov, 2015. |
| 7 | */ |
| 8 |
| 9 | #ifndef DATACENTER_H |
| 10 | #define DATACENTER_H |
| 11 |
| 12 | #include <stdint.h> |
| 13 | #include <vector> |
| 14 | #include <map> |
| 15 | #include <bits/unique_ptr.h> |
| 16 | #include "Defines.h" |
| 17 |
| 18 | class TL_future_salt; |
| 19 | class Connection; |
| 20 | class NativeByteBuffer; |
| 21 | class TL_future_salt; |
| 22 | class ByteArray; |
| 23 | class TLObject; |
| 24 | class Config; |
| 25 |
| 26 | class Datacenter { |
| 27 |
| 28 | public: |
| 29 | Datacenter(uint32_t id); |
| 30 | Datacenter(NativeByteBuffer *data); |
| 31 | void switchTo443Port(); |
| 32 | uint32_t getDatacenterId(); |
| 33 | std::string getCurrentAddress(uint32_t flags); |
| 34 | int32_t getCurrentPort(uint32_t flags); |
| 35 | void addAddressAndPort(std::string address, uint32_t port, uint32_t flags); |
| 36 | void nextAddressOrPort(uint32_t flags); |
| 37 | void storeCurrentAddressAndPortNum(); |
| 38 | void replaceAddressesAndPorts(std::vector<std::string> &newAddresses, std::map<std::string, uint32_t> &newPorts, uint32_t flags); |
| 39 | void serializeToStream(NativeByteBuffer *stream); |
| 40 | void clear(); |
| 41 | void clearServerSalts(); |
| 42 | int64_t getServerSalt(); |
| 43 | void mergeServerSalts(std::vector<std::unique_ptr<TL_future_salt>> &salts); |
| 44 | void addServerSalt(std::unique_ptr<TL_future_salt> &serverSalt); |
| 45 | bool containsServerSalt(int64_t value); |
| 46 | void suspendConnections(); |
| 47 | void getSessions(std::vector<int64_t> &sessions); |
| 48 | void recreateSessions(); |
| 49 | bool isHandshaking(); |
| 50 | bool hasAuthKey(); |
| 51 | bool isExportingAuthorization(); |
| 52 |
| 53 | Connection *getDownloadConnection(uint32_t num, bool create); |
| 54 | Connection *getUploadConnection(bool create); |
| 55 | Connection *getGenericConnection(bool create); |
| 56 | Connection *getPushConnection(bool create); |
| 57 | Connection *getConnectionByType(uint32_t connectionType, bool create); |
| 58 |
| 59 | private: |
| 60 | void onHandshakeConnectionClosed(Connection *connection); |
| 61 | void onHandshakeConnectionConnected(Connection *connection); |
| 62 | void processHandshakeResponse(TLObject *message, int64_t messageId); |
| 63 | void aesIgeEncryption(uint8_t *buffer, uint8_t *key, uint8_t *iv, bool encrypt, bool changeIv, uint32_t length); |
| 64 | NativeByteBuffer *createRequestsData(std::vector<std::unique_ptr<NetworkMessage>> &requests, int32_t *quickAckId, Connection *connection); |
| 65 | bool decryptServerResponse(int64_t keyId, uint8_t *key, uint8_t *data, uint32_t length); |
| 66 | TLObject *getCurrentHandshakeRequest(); |
| 67 |
| 68 | const int32_t *defaultPorts = new int32_t[11] {-1, 80, -1, 443, -1, 443, -1, 80, -1, 443, -1}; |
| 69 | const int32_t *defaultPorts8888 = new int32_t[11] {-1, 8888, -1, 443, -1, 8888, -1, 80, -1, 8888, -1}; |
| 70 |
| 71 | uint32_t datacenterId; |
| 72 | Connection *genericConnection = nullptr; |
| 73 | Connection *downloadConnections[DOWNLOAD_CONNECTIONS_COUNT]; |
| 74 | Connection *uploadConnection = nullptr; |
| 75 | Connection *pushConnection = nullptr; |
| 76 |
| 77 | uint32_t lastInitVersion = 0; |
| 78 | bool authorized = false; |
| 79 |
| 80 | std::vector<std::string> addressesIpv4; |
| 81 | std::vector<std::string> addressesIpv6; |
| 82 | std::vector<std::string> addressesIpv4Download; |
| 83 | std::vector<std::string> addressesIpv6Download; |
| 84 | std::map<std::string, uint32_t> ports; |
| 85 | std::vector<std::unique_ptr<TL_future_salt>> serverSalts; |
| 86 | uint32_t currentPortNumIpv4 = 0; |
| 87 | uint32_t currentAddressNumIpv4 = 0; |
| 88 | uint32_t currentPortNumIpv6 = 0; |
| 89 | uint32_t currentAddressNumIpv6 = 0; |
| 90 | uint32_t currentPortNumIpv4Download = 0; |
| 91 | uint32_t currentAddressNumIpv4Download = 0; |
| 92 | uint32_t currentPortNumIpv6Download = 0; |
| 93 | uint32_t currentAddressNumIpv6Download = 0; |
| 94 | ByteArray *authKey = nullptr; |
| 95 | int64_t authKeyId = 0; |
| 96 | int32_t overridePort = -1; |
| 97 | Config *config = nullptr; |
| 98 |
| 99 | const uint32_t configVersion = 5; |
| 100 | const uint32_t paramsConfigVersion = 1; |
| 101 |
| 102 | Connection *createDownloadConnection(uint32_t num); |
| 103 | Connection *createUploadConnection(); |
| 104 | Connection *createGenericConnection(); |
| 105 | Connection *createPushConnection(); |
| 106 | Connection *createConnectionByType(uint32_t connectionType); |
| 107 |
| 108 | uint8_t handshakeState = 0; |
| 109 | TLObject *handshakeRequest = nullptr; |
| 110 | ByteArray *authNonce = nullptr; |
| 111 | ByteArray *authServerNonce = nullptr; |
| 112 | ByteArray *authNewNonce = nullptr; |
| 113 | ByteArray *handshakeAuthKey = nullptr; |
| 114 | TL_future_salt *handshakeServerSalt = nullptr; |
| 115 | int32_t timeDifference = 0; |
| 116 | bool needResendData = false; |
| 117 | void beginHandshake(bool reconnect); |
| 118 | void sendRequestData(TLObject *object, bool important); |
| 119 | void cleanupHandshake(); |
| 120 | void sendAckRequest(int64_t messageId); |
| 121 | int32_t selectPublicKey(std::vector<int64_t> &fingerprints); |
| 122 |
| 123 | bool exportingAuthorization = false; |
| 124 | void exportAuthorization(); |
| 125 |
| 126 | friend class ConnectionsManager; |
| 127 | }; |
| 128 |
| 129 | #endif |
| 130 |