mtproto_domain_resolver.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "base/weak_ptr.h"
  9. #include <QtCore/QPointer>
  10. #include <QtNetwork/QNetworkReply>
  11. #include <optional>
  12. namespace MTP::details {
  13. [[nodiscard]] const std::vector<QString> &DnsDomains();
  14. [[nodiscard]] QString GenerateDnsRandomPadding();
  15. [[nodiscard]] QByteArray DnsUserAgent();
  16. struct DnsEntry {
  17. QString data;
  18. crl::time TTL = 0;
  19. };
  20. [[nodiscard]] std::vector<DnsEntry> ParseDnsResponse(
  21. const QByteArray &bytes,
  22. std::optional<int> typeRestriction = std::nullopt);
  23. struct ServiceWebRequest {
  24. ServiceWebRequest(not_null<QNetworkReply*> reply);
  25. ServiceWebRequest(ServiceWebRequest &&other);
  26. ServiceWebRequest &operator=(ServiceWebRequest &&other);
  27. ~ServiceWebRequest();
  28. void destroy();
  29. QPointer<QNetworkReply> reply;
  30. };
  31. class DomainResolver : public QObject {
  32. public:
  33. DomainResolver(Fn<void(
  34. const QString &domain,
  35. const QStringList &ips,
  36. crl::time expireAt)> callback);
  37. void resolve(const QString &domain);
  38. private:
  39. enum class Type {
  40. Mozilla,
  41. Google,
  42. };
  43. struct Attempt {
  44. Type type;
  45. QString data;
  46. QString host;
  47. };
  48. struct AttemptKey {
  49. QString domain;
  50. bool ipv6 = false;
  51. inline bool operator<(const AttemptKey &other) const {
  52. return (domain < other.domain)
  53. || (domain == other.domain && !ipv6 && other.ipv6);
  54. }
  55. inline bool operator==(const AttemptKey &other) const {
  56. return (domain == other.domain) && (ipv6 == other.ipv6);
  57. }
  58. };
  59. struct CacheEntry {
  60. QStringList ips;
  61. crl::time expireAt = 0;
  62. };
  63. struct Attempts {
  64. std::vector<Attempt> list;
  65. base::has_weak_ptr guard;
  66. };
  67. void resolve(const AttemptKey &key);
  68. void sendNextRequest(const AttemptKey &key);
  69. void performRequest(const AttemptKey &key, const Attempt &attempt);
  70. void checkExpireAndPushResult(const QString &domain);
  71. void requestFinished(
  72. const AttemptKey &key,
  73. not_null<QNetworkReply*> reply);
  74. QByteArray finalizeRequest(
  75. const AttemptKey &key,
  76. not_null<QNetworkReply*> reply);
  77. Fn<void(
  78. const QString &domain,
  79. const QStringList &ips,
  80. crl::time expireAt)> _callback;
  81. QNetworkAccessManager _manager;
  82. std::map<AttemptKey, Attempts> _attempts;
  83. std::map<AttemptKey, std::vector<ServiceWebRequest>> _requests;
  84. std::map<AttemptKey, CacheEntry> _cache;
  85. crl::time _lastTimestamp = 0;
  86. };
  87. } // namespace MTP::details