tdesktop/Telegram/SourceFiles/mtproto/dc_options.h

131 lines
2.9 KiB
C
Raw Normal View History

/*
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
2017-04-06 14:38:10 +00:00
#include "base/observer.h"
#include "base/bytes.h"
#include "mtproto/rsa_public_key.h"
#include <string>
#include <vector>
#include <map>
namespace MTP {
enum class DcType {
Regular,
2017-06-26 17:38:16 +00:00
Temporary,
MediaDownload,
Cdn,
};
class DcOptions {
public:
// construct methods don't notify "changed" subscribers.
void constructFromSerialized(const QByteArray &serialized);
void constructFromBuiltIn();
void constructAddOne(
int id,
MTPDdcOption::Flags flags,
const std::string &ip,
2018-04-25 09:24:48 +00:00
int port,
const bytes::vector &secret);
QByteArray serialize() const;
using Ids = std::vector<DcId>;
base::Observable<Ids> &changed() const {
return _changed;
}
void setFromList(const MTPVector<MTPDcOption> &options);
void addFromList(const MTPVector<MTPDcOption> &options);
void addFromOther(DcOptions &&options);
Ids configEnumDcIds() const;
struct Endpoint {
std::string ip;
int port = 0;
bytes::vector protocolSecret;
};
struct Variants {
enum Address {
IPv4 = 0,
IPv6 = 1,
AddressTypeCount = 2,
};
enum Protocol {
Tcp = 0,
Http = 1,
ProtocolCount = 2,
};
std::vector<Endpoint> data[AddressTypeCount][ProtocolCount];
};
Variants lookup(DcId dcId, DcType type, bool throughProxy) const;
DcType dcType(ShiftedDcId shiftedDcId) const;
void setCDNConfig(const MTPDcdnConfig &config);
bool hasCDNKeysForDc(DcId dcId) const;
bool getDcRSAKey(DcId dcId, const QVector<MTPlong> &fingerprints, internal::RSAPublicKey *result) const;
// Debug feature for now.
bool loadFromFile(const QString &path);
bool writeToFile(const QString &path) const;
private:
struct Option {
2018-04-25 09:24:48 +00:00
Option(
DcId id,
MTPDdcOption::Flags flags,
const std::string &ip,
int port,
const bytes::vector &secret)
: id(id)
, flags(flags)
, ip(ip)
, port(port)
, secret(secret) {
}
DcId id;
MTPDdcOption::Flags flags;
std::string ip;
int port;
2018-04-25 09:24:48 +00:00
bytes::vector secret;
};
2018-04-25 09:24:48 +00:00
bool applyOneGuarded(
DcId dcId,
MTPDdcOption::Flags flags,
const std::string &ip,
int port,
const bytes::vector &secret);
void processFromList(const QVector<MTPDcOption> &options, bool overwrite);
void computeCdnDcIds();
void readBuiltInPublicKeys();
class WriteLocker;
friend class WriteLocker;
class ReadLocker;
friend class ReadLocker;
std::map<ShiftedDcId, std::vector<Option>> _data;
std::set<DcId> _cdnDcIds;
std::map<uint64, internal::RSAPublicKey> _publicKeys;
std::map<DcId, std::map<uint64, internal::RSAPublicKey>> _cdnPublicKeys;
mutable QReadWriteLock _useThroughLockers;
mutable base::Observable<Ids> _changed;
// True when we have overriden options from a .tdesktop-endpoints file.
bool _immutable = false;
};
} // namespace MTP