Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch for v20.04.2 #3

Open
wants to merge 6 commits into
base: origin-v20.04.2-1732769722
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions core/backends/lan/lanlinkprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@

#define MIN_VERSION_WITH_SSL_SUPPORT 6

static const int MAX_UNPAIRED_CONNECTIONS = 42;
static const int MAX_REMEMBERED_IDENTITY_PACKETS = 42;

LanLinkProvider::LanLinkProvider(
bool testMode,
quint16 udpBroadcastPort,
Expand Down Expand Up @@ -220,9 +223,20 @@ void LanLinkProvider::udpBroadcastReceived()
}

int tcpPort = receivedPacket->get<int>(QStringLiteral("tcpPort"));
if (tcpPort < MIN_TCP_PORT || tcpPort > MAX_TCP_PORT) {
qCDebug(KDECONNECT_CORE) << "TCP port outside of kdeconnect's range";
delete receivedPacket;
continue;
}

//qCDebug(KDECONNECT_CORE) << "Received Udp identity packet from" << sender << " asking for a tcp connection on port " << tcpPort;

if (m_receivedIdentityPackets.size() > MAX_REMEMBERED_IDENTITY_PACKETS) {
qCWarning(KDECONNECT_CORE) << "Too many remembered identities, ignoring" << receivedPacket->get<QString>(QStringLiteral("deviceId")) << "received via UDP";
delete receivedPacket;
continue;
}

QSslSocket* socket = new QSslSocket(this);
socket->setProxy(QNetworkProxy::NoProxy);
m_receivedIdentityPackets[socket].np = receivedPacket;
Expand Down Expand Up @@ -364,13 +378,31 @@ void LanLinkProvider::newConnection()
connect(socket, &QIODevice::readyRead,
this, &LanLinkProvider::dataReceived);

QTimer* timer = new QTimer(socket);
timer->setSingleShot(true);
timer->setInterval(1000);
connect(socket, &QSslSocket::encrypted,
timer, &QObject::deleteLater);
connect(timer, &QTimer::timeout, socket, [socket] {
qCWarning(KDECONNECT_CORE) << "LanLinkProvider/newConnection: Host timed out without sending any identity." << socket->peerAddress();
socket->disconnectFromHost();
});
timer->start();
}
}

//I'm the new device and this is the answer to my UDP identity packet (data received)
void LanLinkProvider::dataReceived()
{
QSslSocket* socket = qobject_cast<QSslSocket*>(sender());
//the size here is arbitrary and is now at 8192 bytes. It needs to be considerably long as it includes the capabilities but there needs to be a limit
//Tested between my systems and I get around 2000 per identity package.
if (socket->bytesAvailable() > 8192) {
qCWarning(KDECONNECT_CORE) << "LanLinkProvider/newConnection: Suspiciously long identity package received. Closing connection." << socket->peerAddress() << socket->bytesAvailable();
socket->disconnectFromHost();
return;
}

#if QT_VERSION < QT_VERSION_CHECK(5,7,0)
if (!socket->canReadLine())
return;
Expand Down Expand Up @@ -405,6 +437,12 @@ void LanLinkProvider::dataReceived()
return;
}

if (m_receivedIdentityPackets.size() > MAX_REMEMBERED_IDENTITY_PACKETS) {
qCWarning(KDECONNECT_CORE) << "Too many remembered identities, ignoring" << np->get<QString>(QStringLiteral("deviceId")) << "received via TCP";
delete np;
return;
}

// Needed in "encrypted" if ssl is used, similar to "tcpSocketConnected"
m_receivedIdentityPackets[socket].np = np;

Expand Down Expand Up @@ -527,6 +565,15 @@ void LanLinkProvider::addLink(const QString& deviceId, QSslSocket* socket, Netwo
deviceLink->reset(socket, connectionOrigin);
} else {
deviceLink = new LanDeviceLink(deviceId, this, socket, connectionOrigin);
// Socket disconnection will now be handled by LanDeviceLink
disconnect(socket, &QAbstractSocket::disconnected, socket, &QObject::deleteLater);
bool isDeviceTrusted = KdeConnectConfig::instance().trustedDevices().contains(deviceId);
if (!isDeviceTrusted && m_links.size() > MAX_UNPAIRED_CONNECTIONS) {
qCWarning(KDECONNECT_CORE) << "Too many unpaired devices to remember them all. Ignoring " << deviceId;
socket->disconnectFromHost();
socket->deleteLater();
return;
}
connect(deviceLink, &QObject::destroyed, this, &LanLinkProvider::deviceLinkDestroyed);
m_links[deviceId] = deviceLink;
if (m_pairingHandlers.contains(deviceId)) {
Expand Down
Loading