#include "server.h" #include "connection.h" #include "../alpm/config.h" #include #include #include #include #include using namespace std; namespace RepoIndex { Server::Server(RepoIndex::Manager &alpmManager, const RepoIndex::Config &config, QObject *parent) : QObject(parent), m_server(new QWebSocketServer(QStringLiteral("Repository index Web Socket server"), config.serverInsecure() ? QWebSocketServer::NonSecureMode : QWebSocketServer::SecureMode, this)), m_alpmManager(alpmManager) { if(!config.serverInsecure()) { // load SSL configuration QSslConfiguration sslConfiguration; QFile certFile(config.serverCertFile().toLocal8Bit().data()); QFile keyFile(config.serverKeyFile().toLocal8Bit().data()); if(!certFile.open(QIODevice::ReadOnly)) { throw runtime_error("Unable to open SSL certificate file."); } if(!keyFile.open(QIODevice::ReadOnly)) { throw runtime_error("Unable to open private SSL key file."); } QSslCertificate certificate(&certFile, QSsl::Pem); if(certificate.isNull()) { throw runtime_error("Unable to parse specified SSL certificate."); } QSslKey sslKey(&keyFile, QSsl::Rsa, QSsl::Pem); if(sslKey.isNull()) { throw runtime_error("Unable to parse specified SSL key."); } certFile.close(); keyFile.close(); sslConfiguration.setPeerVerifyMode(QSslSocket::VerifyNone); sslConfiguration.setLocalCertificate(certificate); sslConfiguration.setPrivateKey(sslKey); sslConfiguration.setProtocol(QSsl::TlsV1SslV3); m_server->setSslConfiguration(sslConfiguration); } else { cerr << shchar << "Warning: The server is running in insecure mode." << endl; } // start listening if(m_server->listen(config.websocketServerListeningAddr(), config.websocketServerListeningPort())) { if(useShSyntax) { cout << "export REPOINDEX_SERVER_NAME='" << m_server->serverName().toLocal8Bit().data() << '\'' << endl; cout << "export REPOINDEX_SERVER_PORT='" << m_server->serverPort() << '\'' << endl; } else { cout << m_server->serverName().toLocal8Bit().data() << " is listening on port " << m_server->serverPort() << endl; } connect(m_server, &QWebSocketServer::newConnection, this, &Server::incomingConnection); connect(m_server, &QWebSocketServer::closed, this, &Server::closed); } else { throw runtime_error("Failed to start web socket server"); } } Server::~Server() { m_server->close(); } void Server::incomingConnection() { emit connectionEstablished(new Connection(m_alpmManager, m_server->nextPendingConnection(), this)); } }