mtproto_dc_options.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/bytes.h"
  9. #include <QtCore/QReadWriteLock>
  10. #include <string>
  11. #include <vector>
  12. #include <map>
  13. #include <set>
  14. namespace MTP {
  15. namespace details {
  16. class RSAPublicKey;
  17. } // namespace details
  18. enum class DcType {
  19. Regular,
  20. Temporary,
  21. MediaCluster,
  22. Cdn,
  23. };
  24. enum class Environment : uchar {
  25. Production,
  26. Test,
  27. };
  28. class DcOptions {
  29. public:
  30. using Flag = MTPDdcOption::Flag;
  31. using Flags = MTPDdcOption::Flags;
  32. struct Endpoint {
  33. Endpoint(
  34. DcId id,
  35. Flags flags,
  36. const std::string &ip,
  37. int port,
  38. const bytes::vector &secret)
  39. : id(id)
  40. , flags(flags)
  41. , ip(ip)
  42. , port(port)
  43. , secret(secret) {
  44. }
  45. DcId id;
  46. Flags flags;
  47. std::string ip;
  48. int port;
  49. bytes::vector secret;
  50. };
  51. explicit DcOptions(Environment environment);
  52. DcOptions(const DcOptions &other);
  53. ~DcOptions();
  54. [[nodiscard]] static bool ValidateSecret(bytes::const_span secret);
  55. [[nodiscard]] Environment environment() const;
  56. [[nodiscard]] bool isTestMode() const;
  57. // construct methods don't notify "changed" subscribers.
  58. bool constructFromSerialized(const QByteArray &serialized);
  59. void constructFromBuiltIn();
  60. void constructAddOne(
  61. int id,
  62. Flags flags,
  63. const std::string &ip,
  64. int port,
  65. const bytes::vector &secret);
  66. QByteArray serialize() const;
  67. [[nodiscard]] rpl::producer<DcId> changed() const;
  68. [[nodiscard]] rpl::producer<> cdnConfigChanged() const;
  69. void setFromList(const MTPVector<MTPDcOption> &options);
  70. void addFromList(const MTPVector<MTPDcOption> &options);
  71. void addFromOther(DcOptions &&options);
  72. [[nodiscard]] std::vector<DcId> configEnumDcIds() const;
  73. struct Variants {
  74. enum Address {
  75. IPv4 = 0,
  76. IPv6 = 1,
  77. AddressTypeCount = 2,
  78. };
  79. enum Protocol {
  80. Tcp = 0,
  81. Http = 1,
  82. ProtocolCount = 2,
  83. };
  84. std::vector<Endpoint> data[AddressTypeCount][ProtocolCount];
  85. };
  86. [[nodiscard]] Variants lookup(
  87. DcId dcId,
  88. DcType type,
  89. bool throughProxy) const;
  90. [[nodiscard]] DcType dcType(ShiftedDcId shiftedDcId) const;
  91. void setCDNConfig(const MTPDcdnConfig &config);
  92. [[nodiscard]] bool hasCDNKeysForDc(DcId dcId) const;
  93. [[nodiscard]] details::RSAPublicKey getDcRSAKey(
  94. DcId dcId,
  95. const QVector<MTPlong> &fingerprints) const;
  96. // Debug feature for now.
  97. bool loadFromFile(const QString &path);
  98. bool writeToFile(const QString &path) const;
  99. private:
  100. bool applyOneGuarded(
  101. DcId dcId,
  102. Flags flags,
  103. const std::string &ip,
  104. int port,
  105. const bytes::vector &secret);
  106. static bool ApplyOneOption(
  107. base::flat_map<DcId, std::vector<Endpoint>> &data,
  108. DcId dcId,
  109. Flags flags,
  110. const std::string &ip,
  111. int port,
  112. const bytes::vector &secret);
  113. static std::vector<DcId> CountOptionsDifference(
  114. const base::flat_map<DcId, std::vector<Endpoint>> &a,
  115. const base::flat_map<DcId, std::vector<Endpoint>> &b);
  116. static void FilterIfHasWithFlag(Variants &variants, Flag flag);
  117. [[nodiscard]] bool hasMediaOnlyOptionsFor(DcId dcId) const;
  118. void processFromList(const QVector<MTPDcOption> &options, bool overwrite);
  119. void computeCdnDcIds();
  120. void readBuiltInPublicKeys();
  121. class WriteLocker;
  122. friend class WriteLocker;
  123. class ReadLocker;
  124. friend class ReadLocker;
  125. const Environment _environment = Environment();
  126. base::flat_map<DcId, std::vector<Endpoint>> _data;
  127. base::flat_set<DcId> _cdnDcIds;
  128. base::flat_map<uint64, details::RSAPublicKey> _publicKeys;
  129. base::flat_map<
  130. DcId,
  131. base::flat_map<uint64, details::RSAPublicKey>> _cdnPublicKeys;
  132. mutable QReadWriteLock _useThroughLockers;
  133. rpl::event_stream<DcId> _changed;
  134. rpl::event_stream<> _cdnConfigChanged;
  135. // True when we have overriden options from a .tdesktop-endpoints file.
  136. bool _immutable = false;
  137. };
  138. } // namespace MTP