ssl: Remove fallback code for missing SSL support
[quassel.git] / src / common / remotepeer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include <utility>
22
23 #include <QtEndian>
24
25 #include <QHostAddress>
26 #include <QSslSocket>
27 #include <QTimer>
28
29 #include "proxyline.h"
30 #include "remotepeer.h"
31 #include "util.h"
32
33 using namespace Protocol;
34
35 const quint32 maxMessageSize = 64 * 1024
36                                * 1024;  // This is uncompressed size. 64 MB should be enough for any sort of initData or backlog chunk
37
38 RemotePeer::RemotePeer(::AuthHandler* authHandler, QTcpSocket* socket, Compressor::CompressionLevel level, QObject* parent)
39     : Peer(authHandler, parent)
40     , _socket(socket)
41     , _compressor(new Compressor(socket, level, this))
42     , _signalProxy(nullptr)
43     , _proxyLine({})
44     , _useProxyLine(false)
45     , _heartBeatTimer(new QTimer(this))
46     , _heartBeatCount(0)
47     , _lag(0)
48     , _msgSize(0)
49 {
50     socket->setParent(this);
51     connect(socket, &QAbstractSocket::stateChanged, this, &RemotePeer::onSocketStateChanged);
52     connect(socket, selectOverload<QAbstractSocket::SocketError>(&QAbstractSocket::error), this, &RemotePeer::onSocketError);
53     connect(socket, &QAbstractSocket::disconnected, this, &Peer::disconnected);
54
55     auto* sslSocket = qobject_cast<QSslSocket*>(socket);
56     if (sslSocket) {
57         connect(sslSocket, &QSslSocket::encrypted, this, [this]() { emit secureStateChanged(true); });
58     }
59
60     connect(_compressor, &Compressor::readyRead, this, &RemotePeer::onReadyRead);
61     connect(_compressor, &Compressor::error, this, &RemotePeer::onCompressionError);
62
63     connect(_heartBeatTimer, &QTimer::timeout, this, &RemotePeer::sendHeartBeat);
64 }
65
66 void RemotePeer::onSocketStateChanged(QAbstractSocket::SocketState state)
67 {
68     if (state == QAbstractSocket::ClosingState) {
69         emit statusMessage(tr("Disconnecting..."));
70     }
71 }
72
73 void RemotePeer::onSocketError(QAbstractSocket::SocketError error)
74 {
75     emit socketError(error, socket()->errorString());
76 }
77
78 void RemotePeer::onCompressionError(Compressor::Error error)
79 {
80     close(QString("Compression error %1").arg(error));
81 }
82
83 QString RemotePeer::description() const
84 {
85     return address();
86 }
87
88 QHostAddress RemotePeer::hostAddress() const
89 {
90     if (_useProxyLine) {
91         return _proxyLine.sourceHost;
92     }
93     else if (socket()) {
94         return socket()->peerAddress();
95     }
96
97     return {};
98 }
99
100 QString RemotePeer::address() const
101 {
102     QHostAddress address = hostAddress();
103     if (address.isNull()) {
104         return {};
105     }
106     else {
107         return address.toString();
108     }
109 }
110
111 quint16 RemotePeer::port() const
112 {
113     if (_useProxyLine) {
114         return _proxyLine.sourcePort;
115     }
116     else if (socket()) {
117         return socket()->peerPort();
118     }
119
120     return 0;
121 }
122
123 ::SignalProxy* RemotePeer::signalProxy() const
124 {
125     return _signalProxy;
126 }
127
128 void RemotePeer::setSignalProxy(::SignalProxy* proxy)
129 {
130     if (proxy == _signalProxy)
131         return;
132
133     if (!proxy) {
134         _heartBeatTimer->stop();
135         disconnect(signalProxy(), nullptr, this, nullptr);
136         _signalProxy = nullptr;
137         if (isOpen())
138             close();
139     }
140     else {
141         if (signalProxy()) {
142             qWarning() << Q_FUNC_INFO << "Setting another SignalProxy not supported, ignoring!";
143             return;
144         }
145         _signalProxy = proxy;
146         connect(proxy, &SignalProxy::heartBeatIntervalChanged, this, &RemotePeer::changeHeartBeatInterval);
147         _heartBeatTimer->setInterval(proxy->heartBeatInterval() * 1000);
148         _heartBeatTimer->start();
149     }
150 }
151
152 void RemotePeer::changeHeartBeatInterval(int secs)
153 {
154     if (secs <= 0)
155         _heartBeatTimer->stop();
156     else {
157         _heartBeatTimer->setInterval(secs * 1000);
158         _heartBeatTimer->start();
159     }
160 }
161
162 int RemotePeer::lag() const
163 {
164     return _lag;
165 }
166
167 QTcpSocket* RemotePeer::socket() const
168 {
169     return _socket;
170 }
171
172 bool RemotePeer::isSecure() const
173 {
174     if (socket()) {
175         if (isLocal())
176             return true;
177         auto* sslSocket = qobject_cast<QSslSocket*>(socket());
178         if (sslSocket && sslSocket->isEncrypted())
179             return true;
180     }
181     return false;
182 }
183
184 bool RemotePeer::isLocal() const
185 {
186     return hostAddress() == QHostAddress::LocalHost ||
187            hostAddress() == QHostAddress::LocalHostIPv6;
188 }
189
190 bool RemotePeer::isOpen() const
191 {
192     return socket() && socket()->state() == QTcpSocket::ConnectedState;
193 }
194
195 void RemotePeer::close(const QString& reason)
196 {
197     if (!reason.isEmpty()) {
198         qWarning() << "Disconnecting:" << reason;
199     }
200
201     if (socket() && socket()->state() != QTcpSocket::UnconnectedState) {
202         socket()->disconnectFromHost();
203     }
204 }
205
206 void RemotePeer::onReadyRead()
207 {
208     QByteArray msg;
209     while (readMessage(msg)) {
210         if (SignalProxy::current())
211             SignalProxy::current()->setSourcePeer(this);
212
213         processMessage(msg);
214
215         if (SignalProxy::current())
216             SignalProxy::current()->setSourcePeer(nullptr);
217     }
218 }
219
220 bool RemotePeer::readMessage(QByteArray& msg)
221 {
222     if (_msgSize == 0) {
223         if (_compressor->bytesAvailable() < 4)
224             return false;
225         _compressor->read((char*) &_msgSize, 4);
226         _msgSize = qFromBigEndian<quint32>(_msgSize);
227
228         if (_msgSize > maxMessageSize) {
229             close("Peer tried to send package larger than max package size!");
230             return false;
231         }
232
233         if (_msgSize == 0) {
234             close("Peer tried to send an empty message!");
235             return false;
236         }
237     }
238
239     if (_compressor->bytesAvailable() < _msgSize) {
240         emit transferProgress(socket()->bytesAvailable(), _msgSize);
241         return false;
242     }
243
244     emit transferProgress(_msgSize, _msgSize);
245
246     msg.resize(_msgSize);
247     qint64 bytesRead = _compressor->read(msg.data(), _msgSize);
248     if (bytesRead != _msgSize) {
249         close("Premature end of data stream!");
250         return false;
251     }
252
253     _msgSize = 0;
254     return true;
255 }
256
257 void RemotePeer::writeMessage(const QByteArray& msg)
258 {
259     auto size = qToBigEndian<quint32>(msg.size());
260     _compressor->write((const char*)&size, 4, Compressor::NoFlush);
261     _compressor->write(msg.constData(), msg.size());
262 }
263
264 void RemotePeer::handle(const HeartBeat& heartBeat)
265 {
266     dispatch(HeartBeatReply(heartBeat.timestamp));
267 }
268
269 void RemotePeer::handle(const HeartBeatReply& heartBeatReply)
270 {
271     _heartBeatCount = 0;
272     emit lagUpdated(heartBeatReply.timestamp.msecsTo(QDateTime::currentDateTime().toUTC()) / 2);
273 }
274
275 void RemotePeer::sendHeartBeat()
276 {
277     if (signalProxy()->maxHeartBeatCount() > 0 && _heartBeatCount >= signalProxy()->maxHeartBeatCount()) {
278         qWarning() << "Disconnecting peer:" << description() << "(didn't receive a heartbeat for over"
279                    << _heartBeatCount * _heartBeatTimer->interval() / 1000 << "seconds)";
280         socket()->close();
281         _heartBeatTimer->stop();
282         return;
283     }
284
285     if (_heartBeatCount > 0) {
286         _lag = _heartBeatCount * _heartBeatTimer->interval();
287         emit lagUpdated(_lag);
288     }
289
290     dispatch(HeartBeat(QDateTime::currentDateTime().toUTC()));
291     ++_heartBeatCount;
292 }
293
294 void RemotePeer::setProxyLine(ProxyLine proxyLine)
295 {
296     _proxyLine = std::move(proxyLine);
297
298     if (socket()) {
299         if (_proxyLine.protocol != QAbstractSocket::UnknownNetworkLayerProtocol) {
300             QList<QString> subnets = Quassel::optionValue("proxy-cidr").split(",");
301             for (const QString& subnet : subnets) {
302                 if (socket()->peerAddress().isInSubnet(QHostAddress::parseSubnet(subnet))) {
303                     _useProxyLine = true;
304                     return;
305                 }
306             }
307         }
308     }
309     _useProxyLine = false;
310 }