/* This file is part of Telegram Desktop, the official desktop application for the Telegram messaging service. For license and copyright information please follow this link: https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once #include "base/bytes.h" namespace MTP { struct ServiceWebRequest { ServiceWebRequest(not_null reply); ServiceWebRequest(ServiceWebRequest &&other); ServiceWebRequest &operator=(ServiceWebRequest &&other); ~ServiceWebRequest(); void destroy(); QPointer reply; }; class SpecialConfigRequest : public QObject { public: SpecialConfigRequest( Fn callback, const QString &phone); private: enum class Type { App, Dns, }; struct Attempt { Type type; QString domain; }; void sendNextRequest(); void performRequest(const Attempt &attempt); void requestFinished(Type type, not_null reply); QByteArray finalizeRequest(not_null reply); void handleResponse(const QByteArray &bytes); bool decryptSimpleConfig(const QByteArray &bytes); Fn _callback; QString _phone; MTPhelp_ConfigSimple _simpleConfig; QNetworkAccessManager _manager; std::vector _attempts; std::vector _requests; }; class DomainResolver : public QObject { public: DomainResolver(Fn callback); void resolve(const QString &domain); private: struct AttemptKey { QString domain; bool ipv6 = false; inline bool operator<(const AttemptKey &other) const { return (domain < other.domain) || (domain == other.domain && !ipv6 && other.ipv6); } inline bool operator==(const AttemptKey &other) const { return (domain == other.domain) && (ipv6 == other.ipv6); } }; struct CacheEntry { QStringList ips; crl::time expireAt = 0; }; void resolve(const AttemptKey &key); void sendNextRequest(const AttemptKey &key); void performRequest(const AttemptKey &key, const QString &host); void checkExpireAndPushResult(const QString &domain); void requestFinished( const AttemptKey &key, not_null reply); QByteArray finalizeRequest( const AttemptKey &key, not_null reply); Fn _callback; QNetworkAccessManager _manager; std::map> _attempts; std::map> _requests; std::map _cache; crl::time _lastTimestamp = 0; }; } // namespace MTP