1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
25 #include <QHostAddress>
29 # include <QSslSocket>
31 # include <QTcpSocket>
34 #include "proxyline.h"
35 #include "remotepeer.h"
38 using namespace Protocol;
40 const quint32 maxMessageSize = 64 * 1024
41 * 1024; // This is uncompressed size. 64 MB should be enough for any sort of initData or backlog chunk
43 RemotePeer::RemotePeer(::AuthHandler* authHandler, QTcpSocket* socket, Compressor::CompressionLevel level, QObject* parent)
44 : Peer(authHandler, parent)
46 , _compressor(new Compressor(socket, level, this))
47 , _signalProxy(nullptr)
49 , _useProxyLine(false)
50 , _heartBeatTimer(new QTimer(this))
55 socket->setParent(this);
56 connect(socket, &QAbstractSocket::stateChanged, this, &RemotePeer::onSocketStateChanged);
57 connect(socket, selectOverload<QAbstractSocket::SocketError>(&QAbstractSocket::error), this, &RemotePeer::onSocketError);
58 connect(socket, &QAbstractSocket::disconnected, this, &Peer::disconnected);
61 auto* sslSocket = qobject_cast<QSslSocket*>(socket);
63 connect(sslSocket, &QSslSocket::encrypted, this, [this]() { emit secureStateChanged(true); });
67 connect(_compressor, &Compressor::readyRead, this, &RemotePeer::onReadyRead);
68 connect(_compressor, &Compressor::error, this, &RemotePeer::onCompressionError);
70 connect(_heartBeatTimer, &QTimer::timeout, this, &RemotePeer::sendHeartBeat);
73 void RemotePeer::onSocketStateChanged(QAbstractSocket::SocketState state)
75 if (state == QAbstractSocket::ClosingState) {
76 emit statusMessage(tr("Disconnecting..."));
80 void RemotePeer::onSocketError(QAbstractSocket::SocketError error)
82 emit socketError(error, socket()->errorString());
85 void RemotePeer::onCompressionError(Compressor::Error error)
87 close(QString("Compression error %1").arg(error));
90 QString RemotePeer::description() const
95 QHostAddress RemotePeer::hostAddress() const
98 return _proxyLine.sourceHost;
101 return socket()->peerAddress();
107 QString RemotePeer::address() const
109 QHostAddress address = hostAddress();
110 if (address.isNull()) {
114 return address.toString();
118 quint16 RemotePeer::port() const
121 return _proxyLine.sourcePort;
124 return socket()->peerPort();
130 ::SignalProxy* RemotePeer::signalProxy() const
135 void RemotePeer::setSignalProxy(::SignalProxy* proxy)
137 if (proxy == _signalProxy)
141 _heartBeatTimer->stop();
142 disconnect(signalProxy(), nullptr, this, nullptr);
143 _signalProxy = nullptr;
149 qWarning() << Q_FUNC_INFO << "Setting another SignalProxy not supported, ignoring!";
152 _signalProxy = proxy;
153 connect(proxy, &SignalProxy::heartBeatIntervalChanged, this, &RemotePeer::changeHeartBeatInterval);
154 _heartBeatTimer->setInterval(proxy->heartBeatInterval() * 1000);
155 _heartBeatTimer->start();
159 void RemotePeer::changeHeartBeatInterval(int secs)
162 _heartBeatTimer->stop();
164 _heartBeatTimer->setInterval(secs * 1000);
165 _heartBeatTimer->start();
169 int RemotePeer::lag() const
174 QTcpSocket* RemotePeer::socket() const
179 bool RemotePeer::isSecure() const
185 auto* sslSocket = qobject_cast<QSslSocket*>(socket());
186 if (sslSocket && sslSocket->isEncrypted())
193 bool RemotePeer::isLocal() const
195 return hostAddress() == QHostAddress::LocalHost ||
196 hostAddress() == QHostAddress::LocalHostIPv6;
199 bool RemotePeer::isOpen() const
201 return socket() && socket()->state() == QTcpSocket::ConnectedState;
204 void RemotePeer::close(const QString& reason)
206 if (!reason.isEmpty()) {
207 qWarning() << "Disconnecting:" << reason;
210 if (socket() && socket()->state() != QTcpSocket::UnconnectedState) {
211 socket()->disconnectFromHost();
215 void RemotePeer::onReadyRead()
218 while (readMessage(msg)) {
219 if (SignalProxy::current())
220 SignalProxy::current()->setSourcePeer(this);
224 if (SignalProxy::current())
225 SignalProxy::current()->setSourcePeer(nullptr);
229 bool RemotePeer::readMessage(QByteArray& msg)
232 if (_compressor->bytesAvailable() < 4)
234 _compressor->read((char*) &_msgSize, 4);
235 _msgSize = qFromBigEndian<quint32>(_msgSize);
237 if (_msgSize > maxMessageSize) {
238 close("Peer tried to send package larger than max package size!");
243 close("Peer tried to send an empty message!");
248 if (_compressor->bytesAvailable() < _msgSize) {
249 emit transferProgress(socket()->bytesAvailable(), _msgSize);
253 emit transferProgress(_msgSize, _msgSize);
255 msg.resize(_msgSize);
256 qint64 bytesRead = _compressor->read(msg.data(), _msgSize);
257 if (bytesRead != _msgSize) {
258 close("Premature end of data stream!");
266 void RemotePeer::writeMessage(const QByteArray& msg)
268 auto size = qToBigEndian<quint32>(msg.size());
269 _compressor->write((const char*)&size, 4, Compressor::NoFlush);
270 _compressor->write(msg.constData(), msg.size());
273 void RemotePeer::handle(const HeartBeat& heartBeat)
275 dispatch(HeartBeatReply(heartBeat.timestamp));
278 void RemotePeer::handle(const HeartBeatReply& heartBeatReply)
281 emit lagUpdated(heartBeatReply.timestamp.msecsTo(QDateTime::currentDateTime().toUTC()) / 2);
284 void RemotePeer::sendHeartBeat()
286 if (signalProxy()->maxHeartBeatCount() > 0 && _heartBeatCount >= signalProxy()->maxHeartBeatCount()) {
287 qWarning() << "Disconnecting peer:" << description() << "(didn't receive a heartbeat for over"
288 << _heartBeatCount * _heartBeatTimer->interval() / 1000 << "seconds)";
290 _heartBeatTimer->stop();
294 if (_heartBeatCount > 0) {
295 _lag = _heartBeatCount * _heartBeatTimer->interval();
296 emit lagUpdated(_lag);
299 dispatch(HeartBeat(QDateTime::currentDateTime().toUTC()));
303 void RemotePeer::setProxyLine(ProxyLine proxyLine)
305 _proxyLine = std::move(proxyLine);
308 if (_proxyLine.protocol != QAbstractSocket::UnknownNetworkLayerProtocol) {
309 QList<QString> subnets = Quassel::optionValue("proxy-cidr").split(",");
310 for (const QString& subnet : subnets) {
311 if (socket()->peerAddress().isInSubnet(QHostAddress::parseSubnet(subnet))) {
312 _useProxyLine = true;
318 _useProxyLine = false;