3 #include "../vnc/vncserver.h"
4 #include "../../shared/util.h"
5 #include "../../shared/settings.h"
6 #include "../util/platform/blankscreen.h"
7 #include "../clientapp/clientapp.h"
10 #include <QGuiApplication>
14 #include <sys/types.h>
18 #define CHALLENGE_LEN 20
21 QObject(nullptr), _timerDelete(0), _jpegQuality(80), _authed(0), _autoConnect(autoConnect), _isLocalConnection(-1), _sessionName(sessionName), _certHash(certHash)
28 connect(
_socket, QOverload<QAbstractSocket::SocketError>::of(&QSslSocket::errorOccurred),
30 connect(
_socket, QOverload<
const QList<QSslError> &>::of(&QSslSocket::sslErrors),
33 qDebug(
"Connecting to %s on port %d", host.toUtf8().data(), int(port));
35 _socket->connectToHostEncrypted(host, port);
47 qCritical(
"**** SOCKET DELETE IN DESTRUCTOR");
50 qDebug(
"*** Server connection destroyed.");
60 if (
_socket ==
nullptr ||
_socket->state() != QAbstractSocket::ConnectedState)
64 qCritical() <<
"SendMessage to server failed!";
88 qDebug(
"Closing connection to server");
112 if (
id == _CHALLENGE) {
125 qDebug(
"Received challenge, replying, sending own challenge step <- 1");
132 if (
id == _CHALLENGE) {
133 qDebug(
"Received challenge reply");
142 qDebug(
"invalid. STOP.");
148 const char *user = getpwuid(getuid())->pw_name;
149 if (user ==
nullptr || *user ==
'\0')
150 user = getenv(
"USER");
151 if (user ==
nullptr || *user ==
'\0')
152 user = getenv(
"USERNAME");
153 if (user ==
nullptr || *user ==
'\0')
159 qDebug() <<
"logging into manager, exam mode is " <<
clientApp->isExamMode();
162 qDebug(
"Sending login request!");
165 qDebug(
"valid, step <- 2");
175 qDebug(
"login accepted, step <- 3");
185 QByteArray emptyArray;
194 QRect primaryRect = QGuiApplication::primaryScreen()->geometry();
198 }
else if (x > primaryRect.width() / 8) {
199 x = primaryRect.width() / 8;
203 }
else if (y > primaryRect.height() / 8) {
204 y = primaryRect.height() / 8;
208 QGuiApplication::primaryScreen()->grabWindow(0,
209 primaryRect.x(), primaryRect.y(), primaryRect.width(), primaryRect.height()
210 ).scaled(x, y, Qt::KeepAspectRatio, Qt::SmoothTransformation
213 QBuffer jpgBuffer(&bytes);
214 jpgBuffer.open(QIODevice::WriteOnly);
224 QBuffer pngBuffer(&bytes);
225 pngBuffer.open(QIODevice::WriteOnly);
226 if (!desktop.save(&pngBuffer,
"PNG")) {
227 qDebug(
"Could not convert screenshot to PNG nor JPG");
236 else if (
id == _VNCSERVER) {
238 qDebug() <<
"denied request for vnc server (exam mode)";
248 }
else if (
id == _VNCCLIENT) {
250 qDebug() <<
"denied request for vnc projection (exam mode)";
255 if (host.isEmpty() || port <= 0) {
260 }
else if (
id == _LOCK) {
262 if (enable && !
clientApp->isConnectedToLocalManager()) {
267 }
else if (
id == _ATTENTION) {
291 if (
_lastData < QDateTime::currentMSecsSinceEpoch()) {
292 qDebug() <<
"Ping timeout";
296 }
else if (event->timerId() ==
_timerId) {
297 qDebug() <<
"Connect timeout";
302 if (
_socket ==
nullptr ||
_socket->state() == QAbstractSocket::UnconnectedState) {
311 qDebug(
"A socket is still pending...");
313 killTimer(event->timerId());
362 for (
const auto &err : errors) {
363 qDebug(
"Connect SSL: %s", qPrintable(err.errorString()));
364 if (err.error() == QSslError::HostNameMismatch)
366 if (err.error() == QSslError::SelfSignedCertificate)
368 if (err.error() == QSslError::CertificateNotYetValid || err.error() == QSslError::CertificateExpired)
371 qDebug() <<
"FATAL!";
372 _socket->ignoreSslErrors(QList<QSslError>());
379 if (
_socket ==
nullptr ||
_socket->state() != QAbstractSocket::ConnectedState) {
380 qDebug(
"dataArrival called in bad state");
384 while (
_socket->bytesAvailable() > 0) {
405 qDebug(
"Socket was closed... oh well..");
411 qDebug(
"Connection error: %d",
int(errcode));
417 qDebug() <<
"Connection to server established and encrypted";
418 QByteArray cert(
_socket->peerCertificate().digest(QCryptographicHash::Sha1));
static const QByteArray __TRUE("1")
static const QByteArray __FALSE("0")
void checkLocalConnection()
void stop()
VncServer::stop.
void started(int port, const QString &ropass, const QString &rwpass)
#define NM_READ_INCOMPLETE
bool writeMessage(QAbstractSocket *socket)
QString getFieldString(const QByteArray &key) const
~ServerConnection() override
bool readComplete() const
void stateChange(ConnectWindow::ConnectionState state)
void sendMessage(NetworkMessage &message)
Send the given message to the server.
QByteArray genSha1(const QByteArray *a, const QByteArray *b, const QByteArray *c, const QByteArray *d, const QByteArray *e)
int _timerConnectionCheck
void disconnectFromServer()
Disconnect from current server.
void attentionChanged(bool state)
NetworkMessage _fromServer
void disconnected(ServerConnection *connection)
void onVncServerStartStop(int port, const QString &ropass, const QString &rwpass)
This slot is triggered by the vnc server runner once the external VNC server was succesfully started...
void timerEvent(QTimerEvent *event) override
void start()
VncServer::start.
bool lock(const QString &message)
void sock_error(QAbstractSocket::SocketError errcode)
int readMessage(QAbstractSocket *socket)
void sslErrors(const QList< QSslError > &errors)
An ssl error happened.
bool writeComplete() const
ServerConnection(const QString &host, quint16 port, const QByteArray &sessionName, const QByteArray &certHash, bool autoConnect)
void openVnc(const QString &host, int port, const QString &passwd, bool ro, bool fullscreen, const QString &caption, const int clientId, const QByteArray &rawThumb)
static const QByteArray & _BOOL(bool val)
void handleMsg()
Handles an incoming message by the server.
QByteArray getFieldBytes(const QByteArray &key) const
void setField(const QByteArray &key, const QByteArray &value)
void onVncViewerStartStop(bool started, int clientId)
This slot is triggered once the internal VNC viewer has started or stopped displaying a VNC stream...
static VncServer * instance()
VncServer::instance.
void sendAttention(bool on)