2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2004 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "server/ClientProxyUnknown.h"
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "base/IEventQueue.h"
|
|
|
|
|
#include "base/Log.h"
|
|
|
|
|
#include "base/TMethodEventJob.h"
|
2024-09-17 19:00:25 +00:00
|
|
|
#include "deskflow/AppUtil.h"
|
2025-03-19 21:28:51 +00:00
|
|
|
#include "deskflow/ProtocolTypes.h"
|
2024-09-17 19:00:25 +00:00
|
|
|
#include "deskflow/ProtocolUtil.h"
|
|
|
|
|
#include "deskflow/XDeskflow.h"
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "io/IStream.h"
|
|
|
|
|
#include "io/XIO.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "server/ClientProxy1_0.h"
|
|
|
|
|
#include "server/ClientProxy1_1.h"
|
|
|
|
|
#include "server/ClientProxy1_2.h"
|
|
|
|
|
#include "server/ClientProxy1_3.h"
|
|
|
|
|
#include "server/ClientProxy1_4.h"
|
|
|
|
|
#include "server/ClientProxy1_5.h"
|
2015-05-19 16:30:35 +00:00
|
|
|
#include "server/ClientProxy1_6.h"
|
2021-06-29 15:45:15 +00:00
|
|
|
#include "server/ClientProxy1_7.h"
|
2021-10-25 15:55:09 +00:00
|
|
|
#include "server/ClientProxy1_8.h"
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "server/Server.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2021-10-04 15:51:12 +00:00
|
|
|
#include <iterator>
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <sstream>
|
2021-10-04 15:51:12 +00:00
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// ClientProxyUnknown
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
ClientProxyUnknown::ClientProxyUnknown(deskflow::IStream *stream, double timeout, Server *server, IEventQueue *events)
|
2024-07-16 13:36:44 +00:00
|
|
|
: m_stream(stream),
|
2025-04-27 19:39:23 +00:00
|
|
|
m_proxy(nullptr),
|
2024-07-16 13:36:44 +00:00
|
|
|
m_ready(false),
|
|
|
|
|
m_server(server),
|
2024-10-17 18:04:35 +00:00
|
|
|
m_events(events)
|
|
|
|
|
{
|
2025-04-27 19:39:23 +00:00
|
|
|
assert(m_server != nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-07-16 13:36:44 +00:00
|
|
|
m_events->adoptHandler(
|
2025-05-02 01:50:53 +00:00
|
|
|
EventTypes::Timer, this,
|
|
|
|
|
new TMethodEventJob<ClientProxyUnknown>(this, &ClientProxyUnknown::handleTimeout, nullptr)
|
2024-10-17 18:04:35 +00:00
|
|
|
);
|
2024-07-02 19:07:06 +00:00
|
|
|
m_timer = m_events->newOneShotTimer(timeout, this);
|
|
|
|
|
addStreamHandlers();
|
|
|
|
|
|
2024-10-18 15:13:13 +00:00
|
|
|
const auto protocol = m_server->protocolString();
|
|
|
|
|
const auto helloMessage = protocol + kMsgHelloArgs;
|
2024-10-16 15:12:17 +00:00
|
|
|
|
2024-10-18 15:13:13 +00:00
|
|
|
LOG_DEBUG("saying hello as %s, protocol v%d.%d", protocol.c_str(), kProtocolMajorVersion, kProtocolMinorVersion);
|
2024-10-16 15:12:17 +00:00
|
|
|
ProtocolUtil::writef(m_stream, helloMessage.c_str(), kProtocolMajorVersion, kProtocolMinorVersion);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
ClientProxyUnknown::~ClientProxyUnknown()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
removeHandlers();
|
|
|
|
|
removeTimer();
|
|
|
|
|
delete m_stream;
|
|
|
|
|
delete m_proxy;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
ClientProxy *ClientProxyUnknown::orphanClientProxy()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
if (m_ready) {
|
|
|
|
|
removeHandlers();
|
|
|
|
|
ClientProxy *proxy = m_proxy;
|
2025-04-27 19:39:23 +00:00
|
|
|
m_proxy = nullptr;
|
2024-07-02 19:07:06 +00:00
|
|
|
return proxy;
|
|
|
|
|
} else {
|
2025-04-27 19:39:23 +00:00
|
|
|
return nullptr;
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ClientProxyUnknown::sendSuccess()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
m_ready = true;
|
|
|
|
|
removeTimer();
|
2025-05-02 01:50:53 +00:00
|
|
|
m_events->addEvent(Event(EventTypes::ClientProxyUnknownSuccess, this));
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ClientProxyUnknown::sendFailure()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
delete m_proxy;
|
2025-04-27 19:39:23 +00:00
|
|
|
m_proxy = nullptr;
|
2024-07-02 19:07:06 +00:00
|
|
|
m_ready = false;
|
|
|
|
|
removeHandlers();
|
|
|
|
|
removeTimer();
|
2025-05-02 01:50:53 +00:00
|
|
|
m_events->addEvent(Event(EventTypes::ClientProxyUnknownFailure, this));
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ClientProxyUnknown::addStreamHandlers()
|
|
|
|
|
{
|
2025-04-27 19:39:23 +00:00
|
|
|
assert(m_stream != nullptr);
|
2024-07-16 13:36:44 +00:00
|
|
|
m_events->adoptHandler(
|
2025-05-02 01:50:53 +00:00
|
|
|
EventTypes::StreamInputReady, m_stream->getEventTarget(),
|
2024-10-17 18:04:35 +00:00
|
|
|
new TMethodEventJob<ClientProxyUnknown>(this, &ClientProxyUnknown::handleData)
|
|
|
|
|
);
|
2024-07-16 13:36:44 +00:00
|
|
|
m_events->adoptHandler(
|
2025-05-02 01:50:53 +00:00
|
|
|
EventTypes::StreamOutputError, m_stream->getEventTarget(),
|
2024-10-17 18:04:35 +00:00
|
|
|
new TMethodEventJob<ClientProxyUnknown>(this, &ClientProxyUnknown::handleWriteError)
|
|
|
|
|
);
|
2024-07-16 13:36:44 +00:00
|
|
|
m_events->adoptHandler(
|
2025-05-02 01:50:53 +00:00
|
|
|
EventTypes::StreamInputShutdown, m_stream->getEventTarget(),
|
2024-10-17 18:04:35 +00:00
|
|
|
new TMethodEventJob<ClientProxyUnknown>(this, &ClientProxyUnknown::handleDisconnect)
|
|
|
|
|
);
|
2024-12-17 13:53:22 +00:00
|
|
|
m_events->adoptHandler(
|
2025-05-02 01:50:53 +00:00
|
|
|
EventTypes::StreamInputFormatError, m_stream->getEventTarget(),
|
2024-12-17 13:53:22 +00:00
|
|
|
new TMethodEventJob<ClientProxyUnknown>(this, &ClientProxyUnknown::handleDisconnect)
|
|
|
|
|
);
|
2024-07-16 13:36:44 +00:00
|
|
|
m_events->adoptHandler(
|
2025-05-02 01:50:53 +00:00
|
|
|
EventTypes::StreamOutputShutdown, m_stream->getEventTarget(),
|
2024-10-17 18:04:35 +00:00
|
|
|
new TMethodEventJob<ClientProxyUnknown>(this, &ClientProxyUnknown::handleWriteError)
|
|
|
|
|
);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ClientProxyUnknown::addProxyHandlers()
|
|
|
|
|
{
|
2025-04-27 19:39:23 +00:00
|
|
|
assert(m_proxy != nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-07-16 13:36:44 +00:00
|
|
|
m_events->adoptHandler(
|
2025-05-02 01:50:53 +00:00
|
|
|
EventTypes::ClientProxyReady, m_proxy,
|
2024-10-17 18:04:35 +00:00
|
|
|
new TMethodEventJob<ClientProxyUnknown>(this, &ClientProxyUnknown::handleReady)
|
|
|
|
|
);
|
2024-07-16 13:36:44 +00:00
|
|
|
m_events->adoptHandler(
|
2025-05-02 01:50:53 +00:00
|
|
|
EventTypes::ClientProxyDisconnected, m_proxy,
|
2024-10-17 18:04:35 +00:00
|
|
|
new TMethodEventJob<ClientProxyUnknown>(this, &ClientProxyUnknown::handleDisconnect)
|
|
|
|
|
);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ClientProxyUnknown::removeHandlers()
|
|
|
|
|
{
|
2025-04-27 19:39:23 +00:00
|
|
|
if (m_stream != nullptr) {
|
2025-05-02 01:50:53 +00:00
|
|
|
m_events->removeHandler(EventTypes::StreamInputReady, m_stream->getEventTarget());
|
|
|
|
|
m_events->removeHandler(EventTypes::StreamOutputError, m_stream->getEventTarget());
|
|
|
|
|
m_events->removeHandler(EventTypes::StreamInputShutdown, m_stream->getEventTarget());
|
|
|
|
|
m_events->removeHandler(EventTypes::StreamInputFormatError, m_stream->getEventTarget());
|
|
|
|
|
m_events->removeHandler(EventTypes::StreamOutputShutdown, m_stream->getEventTarget());
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2025-04-27 19:39:23 +00:00
|
|
|
if (m_proxy != nullptr) {
|
2025-05-02 01:50:53 +00:00
|
|
|
m_events->removeHandler(EventTypes::ClientProxyReady, m_proxy);
|
|
|
|
|
m_events->removeHandler(EventTypes::ClientProxyDisconnected, m_proxy);
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ClientProxyUnknown::removeTimer()
|
|
|
|
|
{
|
2025-04-27 19:39:23 +00:00
|
|
|
if (m_timer != nullptr) {
|
2024-07-02 19:07:06 +00:00
|
|
|
m_events->deleteTimer(m_timer);
|
2025-05-02 01:50:53 +00:00
|
|
|
m_events->removeHandler(EventTypes::Timer, this);
|
2025-04-27 19:39:23 +00:00
|
|
|
m_timer = nullptr;
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-06 00:55:07 +00:00
|
|
|
void ClientProxyUnknown::initProxy(const std::string &name, int major, int minor)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
if (major == 1) {
|
|
|
|
|
switch (minor) {
|
|
|
|
|
case 0:
|
|
|
|
|
m_proxy = new ClientProxy1_0(name, m_stream, m_events);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
|
m_proxy = new ClientProxy1_1(name, m_stream, m_events);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
|
m_proxy = new ClientProxy1_2(name, m_stream, m_events);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
|
m_proxy = new ClientProxy1_3(name, m_stream, m_events);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
|
m_proxy = new ClientProxy1_4(name, m_stream, m_server, m_events);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
|
m_proxy = new ClientProxy1_5(name, m_stream, m_server, m_events);
|
|
|
|
|
break;
|
2021-10-25 15:55:09 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
case 6:
|
|
|
|
|
m_proxy = new ClientProxy1_6(name, m_stream, m_server, m_events);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
|
m_proxy = new ClientProxy1_7(name, m_stream, m_server, m_events);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
|
m_proxy = new ClientProxy1_8(name, m_stream, m_server, m_events);
|
|
|
|
|
break;
|
2021-10-25 15:55:09 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// hangup (with error) if version isn't supported
|
2025-04-27 19:39:23 +00:00
|
|
|
if (m_proxy == nullptr) {
|
2024-07-02 19:07:06 +00:00
|
|
|
throw XIncompatibleClient(major, minor);
|
|
|
|
|
}
|
2021-10-25 15:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ClientProxyUnknown::handleData(const Event &, void *)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
LOG((CLOG_DEBUG1 "parsing hello reply"));
|
|
|
|
|
|
2025-01-06 00:55:07 +00:00
|
|
|
std::string name("<unknown>");
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// limit the maximum length of the hello
|
2025-01-11 02:13:19 +00:00
|
|
|
uint32_t n = m_stream->getSize();
|
2024-07-02 19:07:06 +00:00
|
|
|
if (n > kMaxHelloLength) {
|
|
|
|
|
LOG((CLOG_DEBUG1 "hello reply too long"));
|
|
|
|
|
throw XBadClient();
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
// parse the reply to hello
|
2025-01-11 00:55:14 +00:00
|
|
|
int16_t major, minor;
|
2024-10-14 17:13:49 +00:00
|
|
|
std::string protocolName;
|
2024-10-16 15:12:17 +00:00
|
|
|
if (!ProtocolUtil::readf(m_stream, kMsgHelloBack, &protocolName, &major, &minor, &name)) {
|
2024-07-02 19:07:06 +00:00
|
|
|
throw XBadClient();
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
// disallow invalid version numbers
|
|
|
|
|
if (major <= 0 || minor < 0) {
|
|
|
|
|
throw XIncompatibleClient(major, minor);
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// remove stream event handlers. the proxy we're about to create
|
|
|
|
|
// may install its own handlers and we don't want to accidentally
|
|
|
|
|
// remove those later.
|
|
|
|
|
removeHandlers();
|
|
|
|
|
|
|
|
|
|
// create client proxy for highest version supported by the client
|
|
|
|
|
initProxy(name, major, minor);
|
|
|
|
|
|
|
|
|
|
// the proxy is created and now proxy now owns the stream
|
2024-10-17 18:04:35 +00:00
|
|
|
LOG((CLOG_DEBUG1 "created proxy for client \"%s\" version %d.%d", name.c_str(), major, minor));
|
2025-04-27 19:39:23 +00:00
|
|
|
m_stream = nullptr;
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
// wait until the proxy signals that it's ready or has disconnected
|
|
|
|
|
addProxyHandlers();
|
|
|
|
|
return;
|
|
|
|
|
} catch (XIncompatibleClient &e) {
|
|
|
|
|
// client is incompatible
|
2024-10-17 18:04:35 +00:00
|
|
|
LOG((CLOG_WARN "client \"%s\" has incompatible version %d.%d)", name.c_str(), e.getMajor(), e.getMinor()));
|
|
|
|
|
ProtocolUtil::writef(m_stream, kMsgEIncompatible, kProtocolMajorVersion, kProtocolMinorVersion);
|
2024-07-02 19:07:06 +00:00
|
|
|
} catch (XBadClient &) {
|
|
|
|
|
// client not behaving
|
|
|
|
|
LOG((CLOG_WARN "protocol error from client \"%s\"", name.c_str()));
|
|
|
|
|
ProtocolUtil::writef(m_stream, kMsgEBad);
|
|
|
|
|
} catch (XBase &e) {
|
|
|
|
|
// misc error
|
2024-10-17 18:04:35 +00:00
|
|
|
LOG((CLOG_WARN "error communicating with client \"%s\": %s", name.c_str(), e.what()));
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
sendFailure();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ClientProxyUnknown::handleWriteError(const Event &, void *)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
LOG((CLOG_NOTE "error communicating with new client"));
|
|
|
|
|
sendFailure();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ClientProxyUnknown::handleTimeout(const Event &, void *)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
LOG((CLOG_NOTE "new client is unresponsive"));
|
|
|
|
|
sendFailure();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ClientProxyUnknown::handleDisconnect(const Event &, void *)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
LOG((CLOG_NOTE "new client disconnected"));
|
|
|
|
|
sendFailure();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ClientProxyUnknown::handleReady(const Event &, void *)
|
|
|
|
|
{
|
|
|
|
|
sendSuccess();
|
|
|
|
|
}
|