Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "VeyonCore.h"
- #include <QCoreApplication>
- #include <QAbstractNativeEventFilter>
- #include <QWidget>
- #include <QMainWindow>
- #include "WindowsService.h"
- #include "ComputerControlServer.h"
- #include "VeyonConfiguration.h"
- #ifdef VEYON_BUILD_WIN32
- static HANDLE hShutdownEvent = NULL;
- // event filter which makes ICA recognize logoff events etc.
- class LogoffEventFilter : public QAbstractNativeEventFilter
- {
- public:
- virtual bool nativeEventFilter( const QByteArray& eventType, void *message, long *result)
- {
- Q_UNUSED(eventType);
- Q_UNUSED(result);
- DWORD winMsg = ( ( MSG *) message )->message;
- if( winMsg == WM_QUERYENDSESSION )
- {
- qInfo( "Got WM_QUERYENDSESSION - initiating server shutdown" );
- // tell UltraVNC server to quit
- SetEvent( hShutdownEvent );
- }
- return false;
- }
- };
- #endif
- int main( int argc, char **argv )
- {
- // decide in what mode to run
- if( argc >= 2 )
- {
- #ifdef VEYON_BUILD_WIN32
- for( int i = 1; i < argc; ++i )
- {
- if( QString( argv[i] ).toLower().contains( "service" ) )
- {
- WindowsService winService( "VeyonService", "-service", "Veyon Service",
- QString(), argc, argv );
- if( winService.evalArgs( argc, argv ) )
- {
- return 0;
- }
- break;
- }
- }
- #endif
- }
- QCoreApplication app( argc, argv );
- VeyonCore core( &app, QStringLiteral("Service") );
- // parse arguments
- QStringListIterator argIt( app.arguments() );
- argIt.next();
- while( argc > 1 && argIt.hasNext() )
- {
- const QString a = argIt.next().toLower();
- if( a == QStringLiteral("-session") && argIt.hasNext() )
- {
- int sessionId = argIt.next().toUInt();
- if( sessionId > 0 )
- {
- core.config().setPrimaryServicePort( core.config().primaryServicePort() + sessionId );
- core.config().setVncServerPort( core.config().vncServerPort() + sessionId );
- core.config().setFeatureWorkerManagerPort( core.config().featureWorkerManagerPort() + sessionId );
- }
- }
- }
- #ifdef VEYON_BUILD_WIN32
- hShutdownEvent = OpenEvent( EVENT_ALL_ACCESS, false, L"Global\SessionEventUltra" );
- if( !hShutdownEvent )
- {
- // no global event available already as we're not running under the
- // control of the veyon service supervisor?
- if( GetLastError() == ERROR_FILE_NOT_FOUND )
- {
- qWarning( "Creating session event" );
- // then create our own event as otherwise the VNC server main loop
- // will eat 100% CPU due to failing WaitForSingleObject() calls
- hShutdownEvent = CreateEvent( NULL, false, false, L"Global\SessionEventUltra" );
- }
- else
- {
- qWarning( "Could not open or create session event" );
- }
- }
- LogoffEventFilter eventFilter;
- app.installNativeEventFilter( &eventFilter );
- #endif
- auto server = new ComputerControlServer;
- server->start();
- qInfo( "Exec" );
- int ret = app.exec();
- delete server;
- qInfo( "Exec Done" );
- #ifdef VEYON_BUILD_WIN32
- CloseHandle( hShutdownEvent );
- #endif
- return ret;
- }
- #include <QCoreApplication>
- #include <QHostInfo>
- #include <QWidget>
- #include "AccessControlProvider.h"
- #include "ComputerControlServer.h"
- #include "ComputerControlClient.h"
- #include "FeatureMessage.h"
- #include "VeyonConfiguration.h"
- #include "LocalSystem.h"
- #include "SystemTrayIcon.h"
- ComputerControlServer::ComputerControlServer( QObject* parent ) :
- QObject( parent ),
- m_allowedIPs(),
- m_failedAuthHosts(),
- m_builtinFeatures(),
- m_featureManager(),
- m_featureWorkerManager( m_featureManager ),
- m_serverAuthenticationManager( this ),
- m_serverAccessControlManager( m_featureWorkerManager, m_builtinFeatures.desktopAccessDialog(), this ),
- m_vncServer(),
- m_vncProxyServer( VeyonCore::config().localConnectOnly() || AccessControlProvider().isAccessToLocalComputerDenied() ?
- QHostAddress::LocalHost : QHostAddress::Any,
- VeyonCore::config().primaryServicePort(),
- this,
- this )
- {
- m_builtinFeatures.systemTrayIcon().setToolTip(
- tr( "%1 Service %2 at %3:%4" ).arg( VeyonCore::applicationName(), QStringLiteral(VEYON_VERSION),
- QHostInfo::localHostName(),
- QString::number( VeyonCore::config().primaryServicePort() ) ),
- m_featureWorkerManager );
- // make app terminate once the VNC server thread has finished
- connect( &m_vncServer, &VncServer::finished, QCoreApplication::instance(), &QCoreApplication::quit );
- connect( &m_serverAuthenticationManager, &ServerAuthenticationManager::authenticationError,
- this, &ComputerControlServer::showAuthenticationErrorMessage );
- }
- ComputerControlServer::~ComputerControlServer()
- {
- qDebug(Q_FUNC_INFO);
- m_vncProxyServer.stop();
- }
- void ComputerControlServer::start()
- {
- m_vncServer.start();
- m_vncProxyServer.start( m_vncServer.serverPort(), m_vncServer.password() );
- QWidget *mw = new QWidget;
- mw->setWindowTitle("Main Window");
- mw->show();
- }
- VncProxyConnection* ComputerControlServer::createVncProxyConnection( QTcpSocket* clientSocket,
- int vncServerPort,
- const QString& vncServerPassword,
- QObject* parent )
- {
- return new ComputerControlClient( this, clientSocket, vncServerPort, vncServerPassword, parent );
- }
- bool ComputerControlServer::handleFeatureMessage( QTcpSocket* socket )
- {
- char messageType;
- if( socket->getChar( &messageType ) == false )
- {
- qWarning( "ComputerControlServer::handleFeatureMessage(): could not read feature message!" );
- return false;
- }
- // receive message
- FeatureMessage featureMessage( socket );
- if( featureMessage.isReadyForReceive() == false )
- {
- socket->ungetChar( messageType );
- return false;
- }
- featureMessage.receive();
- return m_featureManager.handleServiceFeatureMessage( featureMessage, m_featureWorkerManager );
- }
- void ComputerControlServer::showAuthenticationErrorMessage( const QString& host, const QString& user )
- {
- qWarning() << "ComputerControlServer: failed authenticating client" << host << user;
- QMutexLocker l( &m_dataMutex );
- if( m_failedAuthHosts.contains( host ) == false )
- {
- m_failedAuthHosts += host;
- m_builtinFeatures.systemTrayIcon().showMessage(
- tr( "Authentication error" ),
- tr( "User %1 (IP: %2) tried to access this computer "
- "but could not authenticate successfully!" ).arg( user, host ),
- m_featureWorkerManager );
- }
- }
Add Comment
Please, Sign In to add comment