2017-06-26 17:38:16 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-06-26 17:38:16 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-06-26 17:38:16 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2018-05-02 19:27:03 +00:00
|
|
|
#include "base/bytes.h"
|
|
|
|
|
2017-06-26 17:38:16 +00:00
|
|
|
namespace MTP {
|
|
|
|
|
2018-05-18 18:07:31 +00:00
|
|
|
struct ServiceWebRequest {
|
2019-07-10 15:03:48 +00:00
|
|
|
ServiceWebRequest(not_null<QNetworkReply*> reply);
|
|
|
|
ServiceWebRequest(ServiceWebRequest &&other);
|
|
|
|
ServiceWebRequest &operator=(ServiceWebRequest &&other);
|
|
|
|
~ServiceWebRequest();
|
2018-05-18 18:07:31 +00:00
|
|
|
|
2019-07-10 15:03:48 +00:00
|
|
|
void destroy();
|
2018-05-18 18:07:31 +00:00
|
|
|
|
2019-07-10 15:03:48 +00:00
|
|
|
QPointer<QNetworkReply> reply;
|
2018-05-18 18:07:31 +00:00
|
|
|
|
|
|
|
};
|
2018-05-17 19:58:00 +00:00
|
|
|
|
2017-06-26 17:38:16 +00:00
|
|
|
class SpecialConfigRequest : public QObject {
|
|
|
|
public:
|
2018-03-20 14:21:36 +00:00
|
|
|
SpecialConfigRequest(
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(
|
2018-03-20 14:21:36 +00:00
|
|
|
DcId dcId,
|
|
|
|
const std::string &ip,
|
2018-05-02 19:27:03 +00:00
|
|
|
int port,
|
|
|
|
bytes::const_span secret)> callback,
|
|
|
|
const QString &phone);
|
2019-07-10 17:28:33 +00:00
|
|
|
explicit SpecialConfigRequest(Fn<void()> timeDoneCallback);
|
2017-06-26 17:38:16 +00:00
|
|
|
|
|
|
|
private:
|
2018-05-03 08:11:07 +00:00
|
|
|
enum class Type {
|
2019-06-07 15:38:02 +00:00
|
|
|
//App,
|
2018-05-03 08:11:07 +00:00
|
|
|
Dns,
|
|
|
|
};
|
|
|
|
struct Attempt {
|
|
|
|
Type type;
|
|
|
|
QString domain;
|
|
|
|
};
|
|
|
|
|
2019-07-10 17:28:33 +00:00
|
|
|
SpecialConfigRequest(
|
|
|
|
Fn<void(
|
|
|
|
DcId dcId,
|
|
|
|
const std::string &ip,
|
|
|
|
int port,
|
|
|
|
bytes::const_span secret)> callback,
|
|
|
|
Fn<void()> timeDoneCallback,
|
|
|
|
const QString &phone);
|
|
|
|
|
2018-05-03 08:11:07 +00:00
|
|
|
void sendNextRequest();
|
|
|
|
void performRequest(const Attempt &attempt);
|
|
|
|
void requestFinished(Type type, not_null<QNetworkReply*> reply);
|
2019-07-10 15:03:48 +00:00
|
|
|
void handleHeaderUnixtime(not_null<QNetworkReply*> reply);
|
2018-05-03 08:11:07 +00:00
|
|
|
QByteArray finalizeRequest(not_null<QNetworkReply*> reply);
|
2017-06-26 17:38:16 +00:00
|
|
|
void handleResponse(const QByteArray &bytes);
|
|
|
|
bool decryptSimpleConfig(const QByteArray &bytes);
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(
|
2018-03-20 14:21:36 +00:00
|
|
|
DcId dcId,
|
|
|
|
const std::string &ip,
|
2018-05-02 19:27:03 +00:00
|
|
|
int port,
|
|
|
|
bytes::const_span secret)> _callback;
|
2019-07-10 17:28:33 +00:00
|
|
|
Fn<void()> _timeDoneCallback;
|
2018-05-02 19:27:03 +00:00
|
|
|
QString _phone;
|
2017-06-26 17:38:16 +00:00
|
|
|
MTPhelp_ConfigSimple _simpleConfig;
|
|
|
|
|
|
|
|
QNetworkAccessManager _manager;
|
2018-05-03 08:11:07 +00:00
|
|
|
std::vector<Attempt> _attempts;
|
2018-05-17 19:58:00 +00:00
|
|
|
std::vector<ServiceWebRequest> _requests;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class DomainResolver : public QObject {
|
|
|
|
public:
|
2018-06-04 15:35:11 +00:00
|
|
|
DomainResolver(Fn<void(
|
2018-05-17 19:58:00 +00:00
|
|
|
const QString &domain,
|
|
|
|
const QStringList &ips,
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time expireAt)> callback);
|
2018-05-17 19:58:00 +00:00
|
|
|
|
|
|
|
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;
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time expireAt = 0;
|
2018-05-17 19:58:00 +00:00
|
|
|
|
2019-03-20 09:23:14 +00:00
|
|
|
};
|
|
|
|
struct Attempts {
|
|
|
|
std::vector<QString> hosts;
|
|
|
|
base::has_weak_ptr guard;
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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<QNetworkReply*> reply);
|
|
|
|
QByteArray finalizeRequest(
|
|
|
|
const AttemptKey &key,
|
|
|
|
not_null<QNetworkReply*> reply);
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(
|
2018-05-17 19:58:00 +00:00
|
|
|
const QString &domain,
|
|
|
|
const QStringList &ips,
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time expireAt)> _callback;
|
2018-05-17 19:58:00 +00:00
|
|
|
|
|
|
|
QNetworkAccessManager _manager;
|
2019-03-20 09:23:14 +00:00
|
|
|
std::map<AttemptKey, Attempts> _attempts;
|
2018-05-17 19:58:00 +00:00
|
|
|
std::map<AttemptKey, std::vector<ServiceWebRequest>> _requests;
|
|
|
|
std::map<AttemptKey, CacheEntry> _cache;
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time _lastTimestamp = 0;
|
2017-06-26 17:38:16 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace MTP
|