Guest User

Untitled

a guest
Apr 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. #include "VeyonCore.h"
  2.  
  3. #include <QCoreApplication>
  4. #include <QAbstractNativeEventFilter>
  5. #include <QWidget>
  6. #include <QMainWindow>
  7.  
  8. #include "WindowsService.h"
  9. #include "ComputerControlServer.h"
  10. #include "VeyonConfiguration.h"
  11.  
  12.  
  13. #ifdef VEYON_BUILD_WIN32
  14. static HANDLE hShutdownEvent = NULL;
  15.  
  16. // event filter which makes ICA recognize logoff events etc.
  17. class LogoffEventFilter : public QAbstractNativeEventFilter
  18. {
  19. public:
  20. virtual bool nativeEventFilter( const QByteArray& eventType, void *message, long *result)
  21. {
  22. Q_UNUSED(eventType);
  23. Q_UNUSED(result);
  24.  
  25. DWORD winMsg = ( ( MSG *) message )->message;
  26.  
  27. if( winMsg == WM_QUERYENDSESSION )
  28. {
  29. qInfo( "Got WM_QUERYENDSESSION - initiating server shutdown" );
  30.  
  31. // tell UltraVNC server to quit
  32. SetEvent( hShutdownEvent );
  33. }
  34.  
  35. return false;
  36. }
  37.  
  38. };
  39.  
  40. #endif
  41.  
  42.  
  43. int main( int argc, char **argv )
  44. {
  45.  
  46. // decide in what mode to run
  47. if( argc >= 2 )
  48. {
  49. #ifdef VEYON_BUILD_WIN32
  50. for( int i = 1; i < argc; ++i )
  51. {
  52. if( QString( argv[i] ).toLower().contains( "service" ) )
  53. {
  54. WindowsService winService( "VeyonService", "-service", "Veyon Service",
  55. QString(), argc, argv );
  56. if( winService.evalArgs( argc, argv ) )
  57. {
  58. return 0;
  59. }
  60. break;
  61. }
  62. }
  63. #endif
  64. }
  65.  
  66. QCoreApplication app( argc, argv );
  67.  
  68. VeyonCore core( &app, QStringLiteral("Service") );
  69.  
  70. // parse arguments
  71. QStringListIterator argIt( app.arguments() );
  72. argIt.next();
  73.  
  74. while( argc > 1 && argIt.hasNext() )
  75. {
  76. const QString a = argIt.next().toLower();
  77.  
  78. if( a == QStringLiteral("-session") && argIt.hasNext() )
  79. {
  80. int sessionId = argIt.next().toUInt();
  81. if( sessionId > 0 )
  82. {
  83. core.config().setPrimaryServicePort( core.config().primaryServicePort() + sessionId );
  84. core.config().setVncServerPort( core.config().vncServerPort() + sessionId );
  85. core.config().setFeatureWorkerManagerPort( core.config().featureWorkerManagerPort() + sessionId );
  86. }
  87. }
  88. }
  89.  
  90. #ifdef VEYON_BUILD_WIN32
  91. hShutdownEvent = OpenEvent( EVENT_ALL_ACCESS, false, L"Global\SessionEventUltra" );
  92. if( !hShutdownEvent )
  93. {
  94. // no global event available already as we're not running under the
  95. // control of the veyon service supervisor?
  96. if( GetLastError() == ERROR_FILE_NOT_FOUND )
  97. {
  98. qWarning( "Creating session event" );
  99. // then create our own event as otherwise the VNC server main loop
  100. // will eat 100% CPU due to failing WaitForSingleObject() calls
  101. hShutdownEvent = CreateEvent( NULL, false, false, L"Global\SessionEventUltra" );
  102. }
  103. else
  104. {
  105. qWarning( "Could not open or create session event" );
  106. }
  107. }
  108.  
  109. LogoffEventFilter eventFilter;
  110.  
  111. app.installNativeEventFilter( &eventFilter );
  112. #endif
  113.  
  114. auto server = new ComputerControlServer;
  115. server->start();
  116.  
  117. qInfo( "Exec" );
  118.  
  119. int ret = app.exec();
  120.  
  121. delete server;
  122.  
  123. qInfo( "Exec Done" );
  124.  
  125. #ifdef VEYON_BUILD_WIN32
  126. CloseHandle( hShutdownEvent );
  127. #endif
  128.  
  129. return ret;
  130. }
  131.  
  132. #include <QCoreApplication>
  133. #include <QHostInfo>
  134. #include <QWidget>
  135.  
  136. #include "AccessControlProvider.h"
  137. #include "ComputerControlServer.h"
  138. #include "ComputerControlClient.h"
  139. #include "FeatureMessage.h"
  140. #include "VeyonConfiguration.h"
  141. #include "LocalSystem.h"
  142. #include "SystemTrayIcon.h"
  143.  
  144.  
  145. ComputerControlServer::ComputerControlServer( QObject* parent ) :
  146. QObject( parent ),
  147. m_allowedIPs(),
  148. m_failedAuthHosts(),
  149. m_builtinFeatures(),
  150. m_featureManager(),
  151. m_featureWorkerManager( m_featureManager ),
  152. m_serverAuthenticationManager( this ),
  153. m_serverAccessControlManager( m_featureWorkerManager, m_builtinFeatures.desktopAccessDialog(), this ),
  154. m_vncServer(),
  155. m_vncProxyServer( VeyonCore::config().localConnectOnly() || AccessControlProvider().isAccessToLocalComputerDenied() ?
  156. QHostAddress::LocalHost : QHostAddress::Any,
  157. VeyonCore::config().primaryServicePort(),
  158. this,
  159. this )
  160. {
  161. m_builtinFeatures.systemTrayIcon().setToolTip(
  162. tr( "%1 Service %2 at %3:%4" ).arg( VeyonCore::applicationName(), QStringLiteral(VEYON_VERSION),
  163. QHostInfo::localHostName(),
  164. QString::number( VeyonCore::config().primaryServicePort() ) ),
  165. m_featureWorkerManager );
  166.  
  167. // make app terminate once the VNC server thread has finished
  168. connect( &m_vncServer, &VncServer::finished, QCoreApplication::instance(), &QCoreApplication::quit );
  169.  
  170. connect( &m_serverAuthenticationManager, &ServerAuthenticationManager::authenticationError,
  171. this, &ComputerControlServer::showAuthenticationErrorMessage );
  172. }
  173.  
  174.  
  175.  
  176. ComputerControlServer::~ComputerControlServer()
  177. {
  178. qDebug(Q_FUNC_INFO);
  179.  
  180. m_vncProxyServer.stop();
  181. }
  182.  
  183.  
  184.  
  185. void ComputerControlServer::start()
  186. {
  187. m_vncServer.start();
  188. m_vncProxyServer.start( m_vncServer.serverPort(), m_vncServer.password() );
  189.  
  190. QWidget *mw = new QWidget;
  191. mw->setWindowTitle("Main Window");
  192. mw->show();
  193. }
  194.  
  195.  
  196.  
  197. VncProxyConnection* ComputerControlServer::createVncProxyConnection( QTcpSocket* clientSocket,
  198. int vncServerPort,
  199. const QString& vncServerPassword,
  200. QObject* parent )
  201. {
  202. return new ComputerControlClient( this, clientSocket, vncServerPort, vncServerPassword, parent );
  203. }
  204.  
  205.  
  206.  
  207. bool ComputerControlServer::handleFeatureMessage( QTcpSocket* socket )
  208. {
  209. char messageType;
  210. if( socket->getChar( &messageType ) == false )
  211. {
  212. qWarning( "ComputerControlServer::handleFeatureMessage(): could not read feature message!" );
  213. return false;
  214. }
  215.  
  216. // receive message
  217. FeatureMessage featureMessage( socket );
  218. if( featureMessage.isReadyForReceive() == false )
  219. {
  220. socket->ungetChar( messageType );
  221. return false;
  222. }
  223.  
  224. featureMessage.receive();
  225.  
  226. return m_featureManager.handleServiceFeatureMessage( featureMessage, m_featureWorkerManager );
  227. }
  228.  
  229.  
  230.  
  231. void ComputerControlServer::showAuthenticationErrorMessage( const QString& host, const QString& user )
  232. {
  233. qWarning() << "ComputerControlServer: failed authenticating client" << host << user;
  234.  
  235. QMutexLocker l( &m_dataMutex );
  236.  
  237. if( m_failedAuthHosts.contains( host ) == false )
  238. {
  239. m_failedAuthHosts += host;
  240. m_builtinFeatures.systemTrayIcon().showMessage(
  241. tr( "Authentication error" ),
  242. tr( "User %1 (IP: %2) tried to access this computer "
  243. "but could not authenticate successfully!" ).arg( user, host ),
  244. m_featureWorkerManager );
  245. }
  246. }
Add Comment
Please, Sign In to add comment