Skip to content

Commit f48eae8

Browse files
Andrii MorozAndrii Moroz
authored andcommitted
Move VNC client init to app
Refactored init method Added stub for VNC athentication
1 parent a9a209d commit f48eae8

File tree

4 files changed

+110
-28
lines changed

4 files changed

+110
-28
lines changed

src/Test/main.cc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,25 @@ int main(int argc, char *argv[])
150150
routeTableWithShutdownCommand->Add<webdriver::ShutdownCommand>(shutdownCommandRoute);
151151
wd_server->SetRouteTable(routeTableWithShutdownCommand);
152152

153-
153+
// start VNC module
154+
CommandLine cmdLine = webdriver::Server::GetInstance()->GetCommandLine();
155+
if (cmdLine.HasSwitch(webdriver::Switches::kVNCServer) || cmdLine.HasSwitch(webdriver::Switches::kVNCPort))
156+
{
157+
QString address = "127.0.0.1";
158+
int port = 5900;
159+
160+
if (cmdLine.HasSwitch(webdriver::Switches::kVNCServer))
161+
address = cmdLine.GetSwitchValueASCII(webdriver::Switches::kVNCServer).c_str();
162+
if (cmdLine.HasSwitch(webdriver::Switches::kVNCPort))
163+
port = QString(cmdLine.GetSwitchValueASCII(webdriver::Switches::kVNCPort).c_str()).toInt();
164+
165+
VNCClient *client = VNCClient::getInstance();
166+
if (!client->isReady())
167+
client->Init(address, port);
168+
169+
WDEventDispatcher::getInstance()->add(new VNCEventDispatcher(client));
170+
}
171+
154172
setQtSettings();
155173
wd_server->Start();
156174

src/vnc/vncclient.cc

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,21 @@ VNCClient::VNCClient()
5959
: _socket(NULL),
6060
_versionEstablished(false),
6161
_securityEstablished(false),
62+
_autenticationPassed(false),
6263
_handshakeFinished(false),
6364
_communicationError(false),
6465
_isReady(false),
6566
_establishedVersion(38),
6667
_establishedSecurity(Invalid),
67-
_serverParameters(NULL)
68+
_serverParameters(NULL),
69+
_password(NULL)
6870
{
6971
}
7072

7173
VNCClient::~VNCClient()
7274
{
7375
delete _serverParameters;
76+
delete _password;
7477
}
7578

7679
VNCClient* VNCClient::getInstance()
@@ -94,7 +97,30 @@ bool VNCClient::Init(QString remoteHost, quint16 port)
9497
addr.setAddress(remoteHost);
9598
}
9699

100+
// QObject::connect(_socket, SIGNAL(bytesWritten(qint64)), this, SLOT(readSocket(qint64)));
101+
QObject::connect(_socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
102+
QObject::connect(_socket, SIGNAL(error(QAbstractSocket::SocketError)),
103+
this, SLOT(onError(QAbstractSocket::SocketError)));
104+
QObject::connect(_socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
105+
this, SLOT(onStateChanged(QAbstractSocket::SocketState)));
106+
97107
_socket->connectToHost(addr, port);
108+
_socket->waitForConnected();
109+
110+
return _socket->isOpen();
111+
}
112+
113+
bool VNCClient::Init(QString remoteHost, quint16 port, QString* password)
114+
{
115+
_password = password;
116+
_socket = new QTcpSocket();
117+
QHostAddress addr;
118+
119+
if (!addr.setAddress(remoteHost))
120+
{
121+
remoteHost.replace(QRegExp("http*://"), "");
122+
addr.setAddress(remoteHost);
123+
}
98124

99125
// QObject::connect(_socket, SIGNAL(bytesWritten(qint64)), this, SLOT(readSocket(qint64)));
100126
QObject::connect(_socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
@@ -103,6 +129,9 @@ bool VNCClient::Init(QString remoteHost, quint16 port)
103129
QObject::connect(_socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
104130
this, SLOT(onStateChanged(QAbstractSocket::SocketState)));
105131

132+
_socket->connectToHost(addr, port);
133+
_socket->waitForConnected();
134+
106135
return _socket->isOpen();
107136
}
108137

@@ -130,6 +159,12 @@ QByteArray VNCClient::readSocket()
130159
establishSecurity(data);
131160
return data;
132161
}
162+
163+
// Go through autentication
164+
if (!_autenticationPassed && VNCAuthentication == _establishedSecurity)
165+
{
166+
// todo
167+
}
133168
if (!_handshakeFinished)
134169
{
135170
finishHandshaking(data);
@@ -164,6 +199,12 @@ QByteArray VNCClient::readSocket(qint64 size)
164199
establishSecurity(data);
165200
return data;
166201
}
202+
203+
// Go through autentication
204+
if (!_autenticationPassed && VNCAuthentication == _establishedSecurity)
205+
{
206+
// todo
207+
}
167208
if (!_handshakeFinished)
168209
{
169210
finishHandshaking(data);
@@ -179,8 +220,17 @@ QByteArray VNCClient::readSocket(qint64 size)
179220

180221
qint64 VNCClient::writeToSocket(QByteArray &data)
181222
{
182-
int bytesNmb = _socket->write(data);
183-
_socket->flush();
223+
int bytesNmb = 0;
224+
225+
if (QAbstractSocket::ConnectedState == _socket->state())
226+
{
227+
bytesNmb = _socket->write(data);
228+
_socket->flush();
229+
}
230+
else
231+
{
232+
std::cout << "#### Socket isn't in connected state. Couldn't write to socket" << std::endl;
233+
}
184234

185235
return bytesNmb;
186236
}
@@ -304,15 +354,27 @@ bool VNCClient::establishSecurity(QByteArray& data)
304354
}
305355
case None:
306356
{
307-
char one = 0x01;
357+
if (NULL == _password)
358+
{
359+
char one = 0x01;
360+
QByteArray response(1, one);
361+
writeToSocket(response);
362+
_securityEstablished = true;
363+
_establishedSecurity = None;
364+
return true;
365+
}
366+
break;
367+
}
368+
case VNCAuthentication:
369+
{
370+
char one = 0x02;
308371
QByteArray response(1, one);
309372
writeToSocket(response);
310373
_securityEstablished = true;
311374
_establishedSecurity = None;
312375
return true;
313376
break;
314377
}
315-
case VNCAuthentication: break;
316378
case RA2: break;
317379
case RA2ne: break;
318380
case Tight: break;
@@ -337,15 +399,31 @@ bool VNCClient::establishSecurity(QByteArray& data)
337399
break;
338400
}
339401
case None:
402+
{
403+
if (NULL == _password)
404+
{
405+
_securityEstablished = true;
406+
_establishedSecurity = None;
407+
return true;
408+
}
409+
break;
410+
}
411+
case VNCAuthentication:
340412
{
341413
_securityEstablished = true;
342-
_establishedSecurity = None;
414+
_establishedSecurity = VNCAuthentication;
343415
return true;
344-
break;
345416
}
346-
case VNCAuthentication: break;
417+
break;
347418
}
348419
}
420+
421+
return false;
422+
}
423+
424+
bool VNCClient::passAutentication(QByteArray &data)
425+
{
426+
return false;
349427
}
350428

351429
bool VNCClient::finishHandshaking(QByteArray &data)

src/vnc/vncclient.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class VNCClient : public QObject
1717
static VNCClient* getInstance();
1818

1919
bool Init(QString remoteHost, quint16 port);
20+
bool Init(QString remoteHost, quint16 port, QString* password);
2021
void sendKeyEvent(QKeyEvent *key);
2122
void sendMouseEvent(QMouseEvent *mouse);
2223
bool isReady();
@@ -35,6 +36,7 @@ private slots:
3536

3637
bool establishProtocolVersion(QByteArray& data);
3738
bool establishSecurity(QByteArray& data);
39+
bool passAutentication(QByteArray& data);
3840
bool finishHandshaking(QByteArray& data);
3941
bool initServerParameters(QByteArray& data);
4042
void sendDoubleClick(QMouseEvent *event);
@@ -75,12 +77,15 @@ private slots:
7577
QTcpSocket *_socket;
7678
bool _versionEstablished;
7779
bool _securityEstablished;
80+
bool _autenticationPassed;
7881
bool _handshakeFinished;
7982
bool _communicationError;
8083
bool _isReady;
8184
int _establishedVersion;
8285
Encodings _establishedSecurity;
8386
ServerParameters* _serverParameters;
87+
88+
QString *_password;
8489
};
8590

8691
#endif // VNCCLIENT_H

src/webdriver/extension_qt/q_session_lifecycle_actions.cc

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,6 @@ Error* QSessionLifeCycleActions::PostInit(const base::DictionaryValue* desired_c
3333
session_->logger().Log(kInfoLogLevel, "no proxy settings requsted.");
3434
}
3535

36-
// start VNC module
37-
CommandLine cmdLine = webdriver::Server::GetInstance()->GetCommandLine();
38-
if (cmdLine.HasSwitch(webdriver::Switches::kVNCServer) || cmdLine.HasSwitch(webdriver::Switches::kVNCPort))
39-
{
40-
QString address = "127.0.0.1";
41-
int port = 5900;
42-
43-
if (cmdLine.HasSwitch(webdriver::Switches::kVNCServer))
44-
address = cmdLine.GetSwitchValueASCII(webdriver::Switches::kVNCServer).c_str();
45-
if (cmdLine.HasSwitch(webdriver::Switches::kVNCPort))
46-
port = QString(cmdLine.GetSwitchValueASCII(webdriver::Switches::kVNCPort).c_str()).toInt();
47-
48-
VNCClient *client = VNCClient::getInstance();
49-
if (!client->isReady())
50-
client->Init(address, port);
51-
52-
WDEventDispatcher::getInstance()->add(new VNCEventDispatcher(client));
53-
}
54-
5536
return error;
5637
}
5738

0 commit comments

Comments
 (0)