2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 06:35:14 +00:00

Handle improved protocol secrets.

This commit is contained in:
John Preston
2018-06-25 20:22:03 +01:00
parent 8c2f11de7d
commit 22441ef80c
6 changed files with 50 additions and 50 deletions

View File

@@ -240,12 +240,16 @@ namespace {
bool ProxyData::valid() const {
if (type == Type::None || host.isEmpty() || !port) {
return false;
} else if (type == Type::Mtproto && !ValidSecret(password)) {
} else if (type == Type::Mtproto && !ValidMtprotoPassword(password)) {
return false;
}
return true;
}
int ProxyData::MaxMtprotoPasswordLength() {
return 34;
}
bool ProxyData::supportsCalls() const {
return (type == Type::Socks5);
}
@@ -258,6 +262,34 @@ bool ProxyData::tryCustomResolve() const {
).match(host).hasMatch();
}
bytes::vector ProxyData::secretFromMtprotoPassword() const {
Expects(type == Type::Mtproto);
Expects(password.size() % 2 == 0);
const auto length = password.size() / 2;
const auto fromHex = [](QChar ch) -> int {
const auto code = int(ch.unicode());
if (code >= '0' && code <= '9') {
return (code - '0');
} else if (code >= 'A' && code <= 'F') {
return 10 + (code - 'A');
} else if (ch >= 'a' && ch <= 'f') {
return 10 + (code - 'a');
}
return -1;
};
auto result = bytes::vector(length);
for (auto i = 0; i != length; ++i) {
const auto high = fromHex(password[2 * i]);
const auto low = fromHex(password[2 * i + 1]);
if (high < 0 || low < 0) {
return {};
}
result[i] = static_cast<gsl::byte>(high * 16 + low);
}
return result;
}
ProxyData::operator bool() const {
return valid();
}
@@ -277,8 +309,15 @@ bool ProxyData::operator!=(const ProxyData &other) const {
return !(*this == other);
}
bool ProxyData::ValidSecret(const QString &secret) {
return QRegularExpression("^[a-fA-F0-9]{32}$").match(secret).hasMatch();
bool ProxyData::ValidMtprotoPassword(const QString &secret) {
if (secret.size() == 32) {
static const auto check = QRegularExpression("^[a-fA-F0-9]{32}$");
return check.match(secret).hasMatch();
} else if (secret.size() == 34) {
static const auto check = QRegularExpression("^dd[a-fA-F0-9]{32}$");
return check.match(secret).hasMatch();
}
return false;
}
ProxyData ToDirectIpProxy(const ProxyData &proxy, int ipIndex) {

View File

@@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/flags.h"
#include "base/algorithm.h"
#include "base/assertion.h"
#include "base/bytes.h"
#include <QtCore/QReadWriteLock>
#include <QtCore/QRegularExpression>
@@ -400,11 +401,13 @@ struct ProxyData {
bool valid() const;
bool supportsCalls() const;
bool tryCustomResolve() const;
bytes::vector secretFromMtprotoPassword() const;
explicit operator bool() const;
bool operator==(const ProxyData &other) const;
bool operator!=(const ProxyData &other) const;
static bool ValidSecret(const QString &secret);
static bool ValidMtprotoPassword(const QString &secret);
static int MaxMtprotoPasswordLength();
};