forked from zerochat2/td
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add strongly typed ProxySecret class.
GitOrigin-RevId: dbde277c6cce57fd6ff51b2e310dab95e60b38c1
- Loading branch information
Showing
13 changed files
with
194 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// Copyright Aliaksei Levin ([email protected]), Arseny Smirnov ([email protected]) 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) | ||
// | ||
#include "td/mtproto/ProxySecret.h" | ||
|
||
#include "td/utils/base64.h" | ||
#include "td/utils/misc.h" | ||
|
||
namespace td { | ||
namespace mtproto { | ||
|
||
Result<ProxySecret> ProxySecret::from_link(Slice encoded_secret) { | ||
auto r_decoded = hex_decode(encoded_secret); | ||
if (r_decoded.is_error()) { | ||
r_decoded = base64url_decode(encoded_secret); | ||
} | ||
if (r_decoded.is_error()) { | ||
return Status::Error(400, "Wrong proxy secret"); | ||
} | ||
return from_binary(r_decoded.ok()); | ||
} | ||
|
||
Result<ProxySecret> ProxySecret::from_binary(Slice raw_unchecked_secret) { | ||
if (raw_unchecked_secret.size() == 16 || | ||
(raw_unchecked_secret.size() == 17 && static_cast<unsigned char>(raw_unchecked_secret[0]) == 0xdd) || | ||
(raw_unchecked_secret.size() >= 17 && static_cast<unsigned char>(raw_unchecked_secret[0]) == 0xee)) { | ||
return from_raw(raw_unchecked_secret); | ||
} | ||
if (raw_unchecked_secret.size() < 16) { | ||
return Status::Error(400, "Wrong proxy secret"); | ||
} | ||
return Status::Error(400, "Unsupported proxy secret"); | ||
} | ||
|
||
string ProxySecret::get_encoded_secret() const { | ||
if (emulate_tls()) { | ||
return base64url_encode(secret_); | ||
} | ||
return buffer_to_hex(secret_); | ||
} | ||
|
||
} // namespace mtproto | ||
} // namespace td |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// Copyright Aliaksei Levin ([email protected]), Arseny Smirnov ([email protected]) 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.