2016-03-24 12:57:10 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-03-24 12:57:10 +00:00
|
|
|
*/
|
|
|
|
#include "mtproto/connection_http.h"
|
|
|
|
|
2021-09-15 10:21:45 +00:00
|
|
|
#include "base/random.h"
|
2018-04-24 19:09:20 +00:00
|
|
|
#include "base/qthelp_url.h"
|
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
namespace MTP {
|
2019-12-02 13:10:19 +00:00
|
|
|
namespace details {
|
2018-04-24 19:09:20 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kForceHttpPort = 80;
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kFullConnectionTimeout = crl::time(8000);
|
2018-04-24 19:09:20 +00:00
|
|
|
|
|
|
|
} // namespace
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
HttpConnection::HttpConnection(QThread *thread, const ProxyData &proxy)
|
|
|
|
: AbstractConnection(thread, proxy)
|
2021-09-15 10:21:45 +00:00
|
|
|
, _checkNonce(base::RandomValue<MTPint128>()) {
|
2018-05-17 10:27:27 +00:00
|
|
|
_manager.moveToThread(thread);
|
2018-05-17 19:58:00 +00:00
|
|
|
_manager.setProxy(ToNetworkProxy(proxy));
|
2018-05-17 10:27:27 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
ConnectionPointer HttpConnection::clone(const ProxyData &proxy) {
|
|
|
|
return ConnectionPointer::New<HttpConnection>(thread(), proxy);
|
2018-05-17 10:27:27 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 18:55:27 +00:00
|
|
|
void HttpConnection::sendData(mtpBuffer &&buffer) {
|
|
|
|
Expects(buffer.size() > 2);
|
2018-05-17 10:27:27 +00:00
|
|
|
|
2018-06-25 18:55:27 +00:00
|
|
|
if (_status == Status::Finished) {
|
2018-05-17 10:27:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-25 18:55:27 +00:00
|
|
|
int32 requestSize = (buffer.size() - 2) * sizeof(mtpPrime);
|
2018-05-17 10:27:27 +00:00
|
|
|
|
|
|
|
QNetworkRequest request(url());
|
2022-11-29 21:46:36 +00:00
|
|
|
request.setHeader(
|
|
|
|
QNetworkRequest::ContentLengthHeader,
|
|
|
|
QVariant(requestSize));
|
|
|
|
request.setHeader(
|
|
|
|
QNetworkRequest::ContentTypeHeader,
|
|
|
|
QVariant(u"application/x-www-form-urlencoded"_q));
|
2018-05-17 10:27:27 +00:00
|
|
|
|
2022-02-07 11:24:02 +00:00
|
|
|
CONNECTION_LOG_INFO(u"Sending %1 len request."_q.arg(requestSize));
|
2018-05-17 10:27:27 +00:00
|
|
|
_requests.insert(_manager.post(request, QByteArray((const char*)(&buffer[2]), requestSize)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpConnection::disconnectFromServer() {
|
|
|
|
if (_status == Status::Finished) return;
|
|
|
|
_status = Status::Finished;
|
|
|
|
|
|
|
|
for (const auto request : base::take(_requests)) {
|
|
|
|
request->abort();
|
|
|
|
request->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect(
|
|
|
|
&_manager,
|
|
|
|
&QNetworkAccessManager::finished,
|
|
|
|
this,
|
|
|
|
&HttpConnection::requestFinished);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpConnection::connectToServer(
|
|
|
|
const QString &address,
|
|
|
|
int port,
|
|
|
|
const bytes::vector &protocolSecret,
|
2021-06-15 12:45:48 +00:00
|
|
|
int16 protocolDcId,
|
|
|
|
bool protocolForFiles) {
|
2018-05-17 10:27:27 +00:00
|
|
|
_address = address;
|
|
|
|
connect(
|
|
|
|
&_manager,
|
|
|
|
&QNetworkAccessManager::finished,
|
|
|
|
this,
|
|
|
|
&HttpConnection::requestFinished);
|
|
|
|
|
2018-06-25 18:55:27 +00:00
|
|
|
auto buffer = preparePQFake(_checkNonce);
|
2018-05-17 10:27:27 +00:00
|
|
|
|
2022-02-07 11:24:02 +00:00
|
|
|
if (Logs::DebugEnabled()) {
|
|
|
|
_debugId = u"%1(dc:%2,%3)"_q
|
|
|
|
.arg(_debugId.toInt())
|
2023-12-21 23:50:42 +00:00
|
|
|
.arg(ProtocolDcDebugId(protocolDcId), url().toDisplayString());
|
2022-02-07 11:24:02 +00:00
|
|
|
}
|
2018-05-17 10:27:27 +00:00
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
_pingTime = crl::now();
|
2018-06-25 18:55:27 +00:00
|
|
|
sendData(std::move(buffer));
|
2018-05-17 10:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mtpBuffer HttpConnection::handleResponse(QNetworkReply *reply) {
|
2016-03-24 12:57:10 +00:00
|
|
|
QByteArray response = reply->readAll();
|
2022-02-07 11:24:02 +00:00
|
|
|
CONNECTION_LOG_INFO(u"Read %1 bytes."_q.arg(response.size()));
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
if (response.isEmpty()) return mtpBuffer();
|
|
|
|
|
|
|
|
if (response.size() & 0x03 || response.size() < 8) {
|
2022-02-07 11:24:02 +00:00
|
|
|
CONNECTION_LOG_ERROR(u"Bad response size %1."_q.arg(response.size()));
|
2016-03-24 12:57:10 +00:00
|
|
|
return mtpBuffer(1, -500);
|
|
|
|
}
|
|
|
|
|
|
|
|
mtpBuffer data(response.size() >> 2);
|
|
|
|
memcpy(data.data(), response.constData(), response.size());
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2022-02-07 11:24:02 +00:00
|
|
|
// Returns "maybe bad key".
|
|
|
|
qint32 HttpConnection::handleError(QNetworkReply *reply) {
|
2017-02-25 16:44:02 +00:00
|
|
|
auto result = qint32(kErrorCodeOther);
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2022-02-07 11:24:02 +00:00
|
|
|
QVariant statusCode = reply->attribute(
|
|
|
|
QNetworkRequest::HttpStatusCodeAttribute);
|
2016-03-24 12:57:10 +00:00
|
|
|
if (statusCode.isValid()) {
|
|
|
|
int status = statusCode.toInt();
|
2017-02-25 16:44:02 +00:00
|
|
|
result = -status;
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (reply->error()) {
|
2022-02-07 11:24:02 +00:00
|
|
|
case QNetworkReply::ConnectionRefusedError:
|
|
|
|
CONNECTION_LOG_ERROR(u"Connection refused - %1."_q
|
|
|
|
.arg(reply->errorString()));
|
|
|
|
break;
|
|
|
|
case QNetworkReply::RemoteHostClosedError:
|
|
|
|
CONNECTION_LOG_ERROR(u"Remote host closed - %1."_q
|
|
|
|
.arg(reply->errorString()));
|
|
|
|
break;
|
|
|
|
case QNetworkReply::HostNotFoundError:
|
|
|
|
CONNECTION_LOG_ERROR(u"Host not found - %1."_q
|
|
|
|
.arg(reply->errorString()));
|
|
|
|
break;
|
|
|
|
case QNetworkReply::TimeoutError:
|
|
|
|
CONNECTION_LOG_ERROR(u"Timeout - %1."_q
|
|
|
|
.arg(reply->errorString()));
|
|
|
|
break;
|
|
|
|
case QNetworkReply::OperationCanceledError:
|
|
|
|
CONNECTION_LOG_ERROR(u"Cancelled - %1."_q
|
|
|
|
.arg(reply->errorString()));
|
|
|
|
break;
|
2016-03-24 12:57:10 +00:00
|
|
|
case QNetworkReply::SslHandshakeFailedError:
|
|
|
|
case QNetworkReply::TemporaryNetworkFailureError:
|
|
|
|
case QNetworkReply::NetworkSessionFailedError:
|
|
|
|
case QNetworkReply::BackgroundRequestNotAllowedError:
|
2021-05-28 11:21:15 +00:00
|
|
|
case QNetworkReply::UnknownNetworkError:
|
2022-02-07 11:24:02 +00:00
|
|
|
CONNECTION_LOG_ERROR(u"Network error %1 - %2."_q
|
|
|
|
.arg(reply->error())
|
|
|
|
.arg(reply->errorString()));
|
2021-05-28 11:21:15 +00:00
|
|
|
break;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
// proxy errors (101-199):
|
2016-03-24 12:57:10 +00:00
|
|
|
case QNetworkReply::ProxyConnectionRefusedError:
|
|
|
|
case QNetworkReply::ProxyConnectionClosedError:
|
|
|
|
case QNetworkReply::ProxyNotFoundError:
|
|
|
|
case QNetworkReply::ProxyTimeoutError:
|
|
|
|
case QNetworkReply::ProxyAuthenticationRequiredError:
|
2022-02-07 11:24:02 +00:00
|
|
|
case QNetworkReply::UnknownProxyError:
|
|
|
|
CONNECTION_LOG_ERROR(u"Proxy error %1 - %2."_q
|
|
|
|
.arg(reply->error())
|
|
|
|
.arg(reply->errorString()));
|
|
|
|
break;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
// content errors (201-299):
|
2016-03-24 12:57:10 +00:00
|
|
|
case QNetworkReply::ContentAccessDenied:
|
|
|
|
case QNetworkReply::ContentOperationNotPermittedError:
|
|
|
|
case QNetworkReply::ContentNotFoundError:
|
|
|
|
case QNetworkReply::AuthenticationRequiredError:
|
|
|
|
case QNetworkReply::ContentReSendError:
|
2022-02-07 11:24:02 +00:00
|
|
|
case QNetworkReply::UnknownContentError:
|
|
|
|
CONNECTION_LOG_ERROR(u"Content error %1 - %2."_q
|
|
|
|
.arg(reply->error())
|
|
|
|
.arg(reply->errorString()));
|
|
|
|
break;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
// protocol errors
|
2016-03-24 12:57:10 +00:00
|
|
|
case QNetworkReply::ProtocolUnknownError:
|
|
|
|
case QNetworkReply::ProtocolInvalidOperationError:
|
2022-02-07 11:24:02 +00:00
|
|
|
case QNetworkReply::ProtocolFailure:
|
|
|
|
CONNECTION_LOG_ERROR(u"Protocol error %1 - %2."_q
|
|
|
|
.arg(reply->error())
|
|
|
|
.arg(reply->errorString()));
|
|
|
|
break;
|
2016-03-24 12:57:10 +00:00
|
|
|
};
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
return result;
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 10:27:27 +00:00
|
|
|
bool HttpConnection::isConnected() const {
|
|
|
|
return (_status == Status::Ready);
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 10:27:27 +00:00
|
|
|
void HttpConnection::requestFinished(QNetworkReply *reply) {
|
|
|
|
if (_status == Status::Finished) return;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
reply->deleteLater();
|
|
|
|
if (reply->error() == QNetworkReply::NoError) {
|
2018-05-17 10:27:27 +00:00
|
|
|
_requests.remove(reply);
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
mtpBuffer data = handleResponse(reply);
|
|
|
|
if (data.size() == 1) {
|
2021-03-03 18:22:42 +00:00
|
|
|
error(data[0]);
|
2016-03-24 12:57:10 +00:00
|
|
|
} else if (!data.isEmpty()) {
|
2018-05-17 10:27:27 +00:00
|
|
|
if (_status == Status::Ready) {
|
2017-02-27 09:51:03 +00:00
|
|
|
_receivedQueue.push_back(data);
|
2021-03-03 18:22:42 +00:00
|
|
|
receivedData();
|
2019-07-18 14:06:38 +00:00
|
|
|
} else if (const auto res_pq = readPQFakeReply(data)) {
|
|
|
|
const auto &data = res_pq->c_resPQ();
|
|
|
|
if (data.vnonce() == _checkNonce) {
|
2022-02-07 11:24:02 +00:00
|
|
|
CONNECTION_LOG_INFO(
|
|
|
|
"HTTP-transport connected by pq-response.");
|
2019-07-18 14:06:38 +00:00
|
|
|
_status = Status::Ready;
|
|
|
|
_pingTime = crl::now() - _pingTime;
|
2021-03-03 18:22:42 +00:00
|
|
|
connected();
|
2019-07-18 14:06:38 +00:00
|
|
|
} else {
|
2022-02-07 11:24:02 +00:00
|
|
|
CONNECTION_LOG_ERROR(
|
|
|
|
"Wrong nonce in HTTP fake pq-response.");
|
2021-03-03 18:22:42 +00:00
|
|
|
error(kErrorCodeOther);
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
2019-07-18 14:06:38 +00:00
|
|
|
} else {
|
2022-02-07 11:24:02 +00:00
|
|
|
CONNECTION_LOG_ERROR(
|
|
|
|
"Could not parse HTTP fake pq-response.");
|
2021-03-03 18:22:42 +00:00
|
|
|
error(kErrorCodeOther);
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-17 10:27:27 +00:00
|
|
|
if (!_requests.remove(reply)) {
|
2016-03-24 12:57:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-03 18:22:42 +00:00
|
|
|
error(handleError(reply));
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time HttpConnection::pingTime() const {
|
|
|
|
return isConnected() ? _pingTime : crl::time(0);
|
2018-04-30 15:49:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time HttpConnection::fullConnectTimeout() const {
|
2018-05-17 19:58:00 +00:00
|
|
|
return kFullConnectionTimeout;
|
|
|
|
}
|
|
|
|
|
2018-05-17 10:27:27 +00:00
|
|
|
bool HttpConnection::usingHttpWait() {
|
2016-03-24 12:57:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-17 10:27:27 +00:00
|
|
|
bool HttpConnection::needHttpWait() {
|
|
|
|
return _requests.isEmpty();
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 10:27:27 +00:00
|
|
|
int32 HttpConnection::debugState() const {
|
2016-03-24 12:57:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-05-17 10:27:27 +00:00
|
|
|
QString HttpConnection::transport() const {
|
2018-04-24 19:09:20 +00:00
|
|
|
if (!isConnected()) {
|
2016-03-24 12:57:10 +00:00
|
|
|
return QString();
|
|
|
|
}
|
2022-11-29 21:46:36 +00:00
|
|
|
auto result = u"HTTP"_q;
|
2018-04-24 19:09:20 +00:00
|
|
|
if (qthelp::is_ipv6(_address)) {
|
2022-11-29 21:46:36 +00:00
|
|
|
result += u"/IPv6"_q;
|
2018-04-24 19:09:20 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-17 10:27:27 +00:00
|
|
|
QString HttpConnection::tag() const {
|
2022-11-29 21:46:36 +00:00
|
|
|
auto result = u"HTTP"_q;
|
2018-04-24 19:09:20 +00:00
|
|
|
if (qthelp::is_ipv6(_address)) {
|
2022-11-29 21:46:36 +00:00
|
|
|
result += u"/IPv6"_q;
|
2018-04-24 19:09:20 +00:00
|
|
|
} else {
|
2022-11-29 21:46:36 +00:00
|
|
|
result += u"/IPv4"_q;
|
2018-04-24 19:09:20 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-17 10:27:27 +00:00
|
|
|
QUrl HttpConnection::url() const {
|
2018-04-24 19:09:20 +00:00
|
|
|
const auto pattern = qthelp::is_ipv6(_address)
|
2022-11-29 21:46:36 +00:00
|
|
|
? u"http://[%1]:%2/api"_q
|
|
|
|
: u"http://%1:%2/api"_q;
|
2018-04-24 19:09:20 +00:00
|
|
|
|
|
|
|
// Not endpoint.port - always 80 port for http transport.
|
|
|
|
return QUrl(pattern.arg(_address).arg(kForceHttpPort));
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
} // namespace details
|
2016-03-24 12:57:10 +00:00
|
|
|
} // namespace MTP
|