This repository has been archived on 2020-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
tdlib-fork/td/mtproto/ProxySecret.h
levlam 7958916080 Add strongly typed ProxySecret class.
GitOrigin-RevId: dbde277c6cce57fd6ff51b2e310dab95e60b38c1
2019-07-09 05:01:12 +03:00

66 lines
1.5 KiB
C++

//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include "td/utils/common.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
namespace td {
namespace mtproto {
class ProxySecret {
public:
static Result<ProxySecret> from_link(Slice encoded_secret);
static Result<ProxySecret> from_binary(Slice raw_unchecked_secret);
static ProxySecret from_raw(Slice raw_secret) {
ProxySecret result;
result.secret_ = raw_secret.str();
return result;
}
Slice get_raw_secret() const {
return secret_;
}
Slice get_proxy_secret() const {
auto proxy_secret = Slice(secret_).truncate(17);
if (proxy_secret.size() == 17) {
proxy_secret.remove_prefix(1);
}
return proxy_secret;
}
string get_encoded_secret() const;
bool use_random_padding() const {
return secret_.size() >= 17;
}
bool emulate_tls() const {
return secret_.size() >= 17 && static_cast<unsigned char>(secret_[0]) == 0xee;
}
string get_domain() const {
CHECK(emulate_tls());
return secret_.substr(17);
}
private:
friend bool operator==(const ProxySecret &lhs, const ProxySecret &rhs) {
return lhs.secret_ == rhs.secret_;
}
string secret_;
};
inline bool operator!=(const ProxySecret &lhs, const ProxySecret &rhs) {
return !(lhs == rhs);
}
} // namespace mtproto
} // namespace td