diff --git a/src/Test/main.cc b/src/Test/main.cc index 975329a9..bf2359f9 100644 --- a/src/Test/main.cc +++ b/src/Test/main.cc @@ -150,7 +150,25 @@ int main(int argc, char *argv[]) routeTableWithShutdownCommand->Add(shutdownCommandRoute); wd_server->SetRouteTable(routeTableWithShutdownCommand); - + // start VNC module + CommandLine cmdLine = webdriver::Server::GetInstance()->GetCommandLine(); + if (cmdLine.HasSwitch(webdriver::Switches::kVNCServer) || cmdLine.HasSwitch(webdriver::Switches::kVNCPort)) + { + QString address = "127.0.0.1"; + int port = 5900; + + if (cmdLine.HasSwitch(webdriver::Switches::kVNCServer)) + address = cmdLine.GetSwitchValueASCII(webdriver::Switches::kVNCServer).c_str(); + if (cmdLine.HasSwitch(webdriver::Switches::kVNCPort)) + port = QString(cmdLine.GetSwitchValueASCII(webdriver::Switches::kVNCPort).c_str()).toInt(); + + VNCClient *client = VNCClient::getInstance(); + if (!client->isReady()) + client->Init(address, port); + + WDEventDispatcher::getInstance()->add(new VNCEventDispatcher(client)); + } + setQtSettings(); wd_server->Start(); diff --git a/src/vnc/vncclient.cc b/src/vnc/vncclient.cc index ab45d113..fb9a7309 100644 --- a/src/vnc/vncclient.cc +++ b/src/vnc/vncclient.cc @@ -59,18 +59,21 @@ VNCClient::VNCClient() : _socket(NULL), _versionEstablished(false), _securityEstablished(false), + _autenticationPassed(false), _handshakeFinished(false), _communicationError(false), _isReady(false), _establishedVersion(38), _establishedSecurity(Invalid), - _serverParameters(NULL) + _serverParameters(NULL), + _password(NULL) { } VNCClient::~VNCClient() { delete _serverParameters; + delete _password; } VNCClient* VNCClient::getInstance() @@ -94,7 +97,30 @@ bool VNCClient::Init(QString remoteHost, quint16 port) addr.setAddress(remoteHost); } + // QObject::connect(_socket, SIGNAL(bytesWritten(qint64)), this, SLOT(readSocket(qint64))); + QObject::connect(_socket, SIGNAL(readyRead()), this, SLOT(readSocket())); + QObject::connect(_socket, SIGNAL(error(QAbstractSocket::SocketError)), + this, SLOT(onError(QAbstractSocket::SocketError))); + QObject::connect(_socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), + this, SLOT(onStateChanged(QAbstractSocket::SocketState))); + _socket->connectToHost(addr, port); + _socket->waitForConnected(); + + return _socket->isOpen(); +} + +bool VNCClient::Init(QString remoteHost, quint16 port, QString* password) +{ + _password = password; + _socket = new QTcpSocket(); + QHostAddress addr; + + if (!addr.setAddress(remoteHost)) + { + remoteHost.replace(QRegExp("http*://"), ""); + addr.setAddress(remoteHost); + } // QObject::connect(_socket, SIGNAL(bytesWritten(qint64)), this, SLOT(readSocket(qint64))); QObject::connect(_socket, SIGNAL(readyRead()), this, SLOT(readSocket())); @@ -103,6 +129,9 @@ bool VNCClient::Init(QString remoteHost, quint16 port) QObject::connect(_socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onStateChanged(QAbstractSocket::SocketState))); + _socket->connectToHost(addr, port); + _socket->waitForConnected(); + return _socket->isOpen(); } @@ -130,6 +159,12 @@ QByteArray VNCClient::readSocket() establishSecurity(data); return data; } + + // Go through autentication + if (!_autenticationPassed && VNCAuthentication == _establishedSecurity) + { + // todo + } if (!_handshakeFinished) { finishHandshaking(data); @@ -164,6 +199,12 @@ QByteArray VNCClient::readSocket(qint64 size) establishSecurity(data); return data; } + + // Go through autentication + if (!_autenticationPassed && VNCAuthentication == _establishedSecurity) + { + // todo + } if (!_handshakeFinished) { finishHandshaking(data); @@ -179,8 +220,17 @@ QByteArray VNCClient::readSocket(qint64 size) qint64 VNCClient::writeToSocket(QByteArray &data) { - int bytesNmb = _socket->write(data); - _socket->flush(); + int bytesNmb = 0; + + if (QAbstractSocket::ConnectedState == _socket->state()) + { + bytesNmb = _socket->write(data); + _socket->flush(); + } + else + { + std::cout << "#### Socket isn't in connected state. Couldn't write to socket" << std::endl; + } return bytesNmb; } @@ -304,7 +354,20 @@ bool VNCClient::establishSecurity(QByteArray& data) } case None: { - char one = 0x01; + if (NULL == _password) + { + char one = 0x01; + QByteArray response(1, one); + writeToSocket(response); + _securityEstablished = true; + _establishedSecurity = None; + return true; + } + break; + } + case VNCAuthentication: + { + char one = 0x02; QByteArray response(1, one); writeToSocket(response); _securityEstablished = true; @@ -312,7 +375,6 @@ bool VNCClient::establishSecurity(QByteArray& data) return true; break; } - case VNCAuthentication: break; case RA2: break; case RA2ne: break; case Tight: break; @@ -337,15 +399,31 @@ bool VNCClient::establishSecurity(QByteArray& data) break; } case None: + { + if (NULL == _password) + { + _securityEstablished = true; + _establishedSecurity = None; + return true; + } + break; + } + case VNCAuthentication: { _securityEstablished = true; - _establishedSecurity = None; + _establishedSecurity = VNCAuthentication; return true; - break; } - case VNCAuthentication: break; + break; } } + + return false; +} + +bool VNCClient::passAutentication(QByteArray &data) +{ + return false; } bool VNCClient::finishHandshaking(QByteArray &data) diff --git a/src/vnc/vncclient.h b/src/vnc/vncclient.h index c08741aa..aab3735c 100644 --- a/src/vnc/vncclient.h +++ b/src/vnc/vncclient.h @@ -17,6 +17,7 @@ class VNCClient : public QObject static VNCClient* getInstance(); bool Init(QString remoteHost, quint16 port); + bool Init(QString remoteHost, quint16 port, QString* password); void sendKeyEvent(QKeyEvent *key); void sendMouseEvent(QMouseEvent *mouse); bool isReady(); @@ -35,6 +36,7 @@ private slots: bool establishProtocolVersion(QByteArray& data); bool establishSecurity(QByteArray& data); + bool passAutentication(QByteArray& data); bool finishHandshaking(QByteArray& data); bool initServerParameters(QByteArray& data); void sendDoubleClick(QMouseEvent *event); @@ -75,12 +77,15 @@ private slots: QTcpSocket *_socket; bool _versionEstablished; bool _securityEstablished; + bool _autenticationPassed; bool _handshakeFinished; bool _communicationError; bool _isReady; int _establishedVersion; Encodings _establishedSecurity; ServerParameters* _serverParameters; + + QString *_password; }; #endif // VNCCLIENT_H diff --git a/src/webdriver/extension_qt/q_session_lifecycle_actions.cc b/src/webdriver/extension_qt/q_session_lifecycle_actions.cc index 449c4c99..1cbd6313 100644 --- a/src/webdriver/extension_qt/q_session_lifecycle_actions.cc +++ b/src/webdriver/extension_qt/q_session_lifecycle_actions.cc @@ -33,25 +33,6 @@ Error* QSessionLifeCycleActions::PostInit(const base::DictionaryValue* desired_c session_->logger().Log(kInfoLogLevel, "no proxy settings requsted."); } - // start VNC module - CommandLine cmdLine = webdriver::Server::GetInstance()->GetCommandLine(); - if (cmdLine.HasSwitch(webdriver::Switches::kVNCServer) || cmdLine.HasSwitch(webdriver::Switches::kVNCPort)) - { - QString address = "127.0.0.1"; - int port = 5900; - - if (cmdLine.HasSwitch(webdriver::Switches::kVNCServer)) - address = cmdLine.GetSwitchValueASCII(webdriver::Switches::kVNCServer).c_str(); - if (cmdLine.HasSwitch(webdriver::Switches::kVNCPort)) - port = QString(cmdLine.GetSwitchValueASCII(webdriver::Switches::kVNCPort).c_str()).toInt(); - - VNCClient *client = VNCClient::getInstance(); - if (!client->isReady()) - client->Init(address, port); - - WDEventDispatcher::getInstance()->add(new VNCEventDispatcher(client)); - } - return error; }