Add installer

This commit is contained in:
driftingsun 2020-12-16 06:02:22 +03:00
parent c9bc8aa8c1
commit a2a5cafc5f
73 changed files with 4354 additions and 488 deletions

16
service/server/main.cpp Normal file
View file

@ -0,0 +1,16 @@
#include <QSettings>
#include <QDir>
#include "server.h"
int main(int argc, char **argv)
{
#if !defined(Q_OS_WIN)
// QtService stores service settings in SystemScope, which normally require root privileges.
// To allow testing this example as non-root, we change the directory of the SystemScope settings file.
QSettings::setPath(QSettings::NativeFormat, QSettings::SystemScope, QDir::tempPath());
qWarning("(Example uses dummy settings file: %s/QtSoftware.conf)", QDir::tempPath().toLatin1().constData());
#endif
HttpService service(argc, argv);
return service.exec();
}

112
service/server/server.cpp Normal file
View file

@ -0,0 +1,112 @@
#include <QDateTime>
#include <QDebug>
#include <QTcpSocket>
#include "server.h"
HttpDaemon::HttpDaemon(quint16 port, QObject* parent)
: QTcpServer(parent), disabled(false)
{
listen(QHostAddress::Any, port);
qDebug() << "Listen on port: " << port;
connect(this, &QTcpServer::newConnection, this, &HttpDaemon::sendFortune);
}
void HttpDaemon::sendFortune()
{
qDebug() << "New connection: ";
QTcpSocket *clientConnection = this->nextPendingConnection();
connect(clientConnection, &QAbstractSocket::disconnected,
clientConnection, &QObject::deleteLater);
connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readClient()));
connect(clientConnection, SIGNAL(disconnected()), this, SLOT(discardClient()));
//->setSocketDescriptor(socket);
}
void HttpDaemon::pause()
{
disabled = true;
}
void HttpDaemon::resume()
{
disabled = false;
}
void HttpDaemon::readClient()
{
qDebug() << "readClient";
// if (disabled)
// return;
//
// This slot is called when the client sent data to the server. The
// server looks if it was a get request and sends a very simple HTML
// document back.
QTcpSocket* socket = (QTcpSocket*)sender();
if (socket->canReadLine()) {
QStringList tokens = QString(socket->readLine()).split(QRegExp("[ \r\n][ \r\n]*"));
if (tokens[0] == "GET") {
QTextStream os(socket);
os.setAutoDetectUnicode(true);
os << "HTTP/1.0 200 Ok\r\n"
"Content-Type: text/html; charset=\"utf-8\"\r\n"
"\r\n"
"<h1>Nothing to see here</h1>\n"
<< QDateTime::currentDateTime().toString() << "\n";
socket->close();
QtServiceBase::instance()->logMessage("Wrote to client");
if (socket->state() == QTcpSocket::UnconnectedState) {
delete socket;
QtServiceBase::instance()->logMessage("Connection closed");
}
}
}
}
void HttpDaemon::discardClient()
{
QTcpSocket* socket = (QTcpSocket*)sender();
socket->deleteLater();
QtServiceBase::instance()->logMessage("Connection closed");
}
HttpService::HttpService(int argc, char **argv)
: QtService<QCoreApplication>(argc, argv, "Qt HTTP Daemon")
{
setServiceDescription("A dummy HTTP service implemented with Qt");
setServiceFlags(QtServiceBase::CanBeSuspended);
}
void HttpService::start()
{
QCoreApplication *app = application();
daemon = new HttpDaemon(8989, app);
if (!daemon->isListening()) {
logMessage(QString("Failed to bind to port %1").arg(daemon->serverPort()), QtServiceBase::Error);
app->quit();
}
}
void HttpService::pause()
{
daemon->pause();
}
void HttpService::resume()
{
daemon->resume();
}

43
service/server/server.h Normal file
View file

@ -0,0 +1,43 @@
#ifndef SERVER_H
#define SERVER_H
#include <QCoreApplication>
#include <QTcpServer>
#include "qtservice.h"
class HttpDaemon : public QTcpServer
{
Q_OBJECT
public:
HttpDaemon(quint16 port, QObject* parent = 0);
void sendFortune();
void pause();
void resume();
private slots:
void readClient();
void discardClient();
private:
bool disabled;
};
class HttpService : public QtService<QCoreApplication>
{
public:
HttpService(int argc, char **argv);
protected:
void pause();
void resume();
void start();
private:
HttpDaemon *daemon;
};
#endif // SERVER_H

19
service/server/server.pro Normal file
View file

@ -0,0 +1,19 @@
TARGET = AmneziaVPN-service
TEMPLATE = app
CONFIG += console qt
QT = core network
HEADERS = \
server.h
SOURCES = \
server.cpp \
main.cpp
include(../src/qtservice.pri)
CONFIG(release, debug|release) {
DESTDIR = $$PWD/../../../AmneziaVPN-build/server/release
MOC_DIR = $$DESTDIR
OBJECTS_DIR = $$DESTDIR
RCC_DIR = $$DESTDIR
}