WIP: main
This commit is contained in:
parent
f25f34565a
commit
15730b470e
52 changed files with 2438 additions and 208 deletions
|
|
@ -7,21 +7,36 @@ TEMPLATE = app
|
|||
DEFINES += QT_DEPRECATED_WARNINGS
|
||||
|
||||
HEADERS += \
|
||||
communicator.h \
|
||||
core/router.h \
|
||||
debug.h \
|
||||
defines.h \
|
||||
localclient.h \
|
||||
managementserver.h \
|
||||
message.h \
|
||||
openvpnprotocol.h \
|
||||
runguard.h \
|
||||
ui/Controls/SlidingStackedWidget.h \
|
||||
ui/mainwindow.h \
|
||||
utils.h \
|
||||
vpnconnection.h \
|
||||
vpnprotocol.h \
|
||||
|
||||
SOURCES += \
|
||||
communicator.cpp \
|
||||
core/router.cpp \
|
||||
debug.cpp \
|
||||
localclient.cpp \
|
||||
main.cpp \
|
||||
managementserver.cpp \
|
||||
message.cpp \
|
||||
openvpnprotocol.cpp \
|
||||
runguard.cpp \
|
||||
ui/Controls/SlidingStackedWidget.cpp \
|
||||
ui/mainwindow.cpp \
|
||||
|
||||
utils.cpp \
|
||||
vpnconnection.cpp \
|
||||
vpnprotocol.cpp \
|
||||
|
||||
FORMS += ui/mainwindow.ui
|
||||
|
||||
|
|
@ -29,8 +44,7 @@ RESOURCES += \
|
|||
resources.qrc
|
||||
|
||||
TRANSLATIONS = \
|
||||
translations/amneziavpn.en.ts \
|
||||
translations/amneziavpn.ru.ts
|
||||
translations/amneziavpn_ru.ts
|
||||
|
||||
CONFIG(release, debug|release) {
|
||||
DESTDIR = $$PWD/../../AmneziaVPN-build/client/release
|
||||
|
|
@ -46,7 +60,7 @@ win32 {
|
|||
HEADERS +=
|
||||
SOURCES +=
|
||||
|
||||
VERSION = 1.1.1.1
|
||||
VERSION = 1.0.0.0
|
||||
QMAKE_TARGET_COMPANY = "AmneziaVPN"
|
||||
QMAKE_TARGET_PRODUCT = "AmneziaVPN"
|
||||
|
||||
|
|
|
|||
77
client/communicator.cpp
Normal file
77
client/communicator.cpp
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
#include "communicator.h"
|
||||
#include "defines.h"
|
||||
#include "localclient.h"
|
||||
#include "utils.h"
|
||||
|
||||
Communicator::Communicator(QObject* parent) : QObject(parent),
|
||||
m_localClient(nullptr)
|
||||
{
|
||||
connectToServer();
|
||||
}
|
||||
|
||||
Communicator::~Communicator()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Communicator::connectToServer()
|
||||
{
|
||||
if (m_localClient) {
|
||||
delete m_localClient;
|
||||
}
|
||||
|
||||
qDebug().noquote() << QString("Connect to local server '%1'").arg(SERVICE_NAME);
|
||||
|
||||
m_localClient = new LocalClient(this);
|
||||
connect(m_localClient, &LocalClient::connected, this, &Communicator::onConnected);
|
||||
connect(m_localClient, &LocalClient::lineAvailable, this, &Communicator::onLineAvailable);
|
||||
m_localClient->connectToServer(SERVICE_NAME);
|
||||
}
|
||||
|
||||
void Communicator::onConnected()
|
||||
{
|
||||
qDebug().noquote() << QString("Connected to local server '%1'").arg(m_localClient->serverName());
|
||||
Message message(Message::State::Initialize, QStringList({"Ping"}));
|
||||
sendMessage(message);
|
||||
}
|
||||
|
||||
void Communicator::onLineAvailable(const QString& line)
|
||||
{
|
||||
Message message(line);
|
||||
if (!message.isValid()) {
|
||||
qDebug() << "Message is not valid";
|
||||
return;
|
||||
}
|
||||
|
||||
emit messageReceived(message);
|
||||
}
|
||||
|
||||
bool Communicator::connected() const
|
||||
{
|
||||
if (!m_localClient) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return m_localClient->connectedState();
|
||||
}
|
||||
|
||||
QString Communicator::readData()
|
||||
{
|
||||
return QString();
|
||||
}
|
||||
|
||||
bool Communicator::writeData(const QString& data)
|
||||
{
|
||||
return m_localClient->write(data.toLocal8Bit());
|
||||
}
|
||||
|
||||
void Communicator::sendMessage(const Message& message)
|
||||
{
|
||||
if (!connected()) {
|
||||
return;
|
||||
}
|
||||
const QString data = message.toString();
|
||||
bool status = writeData(data + "\n");
|
||||
|
||||
qDebug().noquote() << QString("Send message '%1', status '%2'").arg(data).arg(Utils::toString(status));
|
||||
}
|
||||
41
client/communicator.h
Normal file
41
client/communicator.h
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#ifndef COMMUNICATOR_H
|
||||
#define COMMUNICATOR_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QStringList>
|
||||
|
||||
#include "message.h"
|
||||
|
||||
class LocalClient;
|
||||
|
||||
class Communicator : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Communicator(QObject* parent = nullptr);
|
||||
~Communicator();
|
||||
|
||||
void sendMessage(const Message& message);
|
||||
|
||||
signals:
|
||||
void messageReceived(const Message& message);
|
||||
|
||||
void comminicatorConnected();
|
||||
void comminicatorDisconnected();
|
||||
|
||||
protected slots:
|
||||
void onConnected();
|
||||
void onLineAvailable(const QString& line);
|
||||
|
||||
protected:
|
||||
QString readData();
|
||||
bool connected() const;
|
||||
bool writeData(const QString& data);
|
||||
void connectToServer();
|
||||
|
||||
LocalClient* m_localClient;
|
||||
};
|
||||
|
||||
|
||||
#endif // COMMUNICATOR_H
|
||||
|
|
@ -4,6 +4,8 @@
|
|||
#include <QStandardPaths>
|
||||
#include <QUrl>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "debug.h"
|
||||
#include "defines.h"
|
||||
|
||||
|
|
@ -23,6 +25,8 @@ void debugMessageHandler(QtMsgType type, const QMessageLogContext& context, cons
|
|||
}
|
||||
|
||||
Debug::m_textStream << qFormatLogMessage(type, context, msg) << endl << flush;
|
||||
|
||||
std::cout << qFormatLogMessage(type, context, msg).toStdString() << std::endl << std::flush;
|
||||
}
|
||||
|
||||
bool Debug::init()
|
||||
|
|
@ -35,7 +39,7 @@ bool Debug::init()
|
|||
|
||||
m_logFileName = QString("%1.log").arg(APPLICATION_NAME);
|
||||
|
||||
qSetMessagePattern("[%{time}|%{type}] %{message}");
|
||||
qSetMessagePattern("%{time yyyy-MM-dd hh:mm:ss} %{type} %{message}");
|
||||
|
||||
m_file.setFileName(appDir.filePath(m_logFileName));
|
||||
if (!m_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
|
|
@ -66,3 +70,8 @@ bool Debug::openLogsFolder()
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
QString Debug::appLogFileNamePath()
|
||||
{
|
||||
return m_file.fileName();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
#ifndef DEBUG_H
|
||||
#define DEBUG_H
|
||||
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
#include <QString>
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QString>
|
||||
#include <QTextStream>
|
||||
|
||||
class Debug
|
||||
{
|
||||
|
|
@ -12,6 +13,7 @@ public:
|
|||
static QString logsDir();
|
||||
static bool init();
|
||||
static bool openLogsFolder();
|
||||
static QString appLogFileNamePath();
|
||||
|
||||
private:
|
||||
static QFile m_file;
|
||||
|
|
|
|||
|
|
@ -4,5 +4,6 @@
|
|||
#define APPLICATION_NAME "AmneziaVPN"
|
||||
#define SERVICE_NAME "AmneziaVPN-service"
|
||||
#define ORGANIZATION_NAME "AmneziaVPN.ORG"
|
||||
#define APP_VERSION "1.0.0.0"
|
||||
|
||||
#endif // DEFINES_H
|
||||
|
|
|
|||
64
client/localclient.cpp
Normal file
64
client/localclient.cpp
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
#include <QDebug>
|
||||
#include <QtNetwork>
|
||||
|
||||
#include "localclient.h"
|
||||
|
||||
LocalClient::LocalClient(QObject *parent) : QObject(parent),
|
||||
m_socket(new QLocalSocket(this))
|
||||
{
|
||||
m_in.setDevice(m_socket);
|
||||
m_in.setVersion(QDataStream::Qt_5_10);
|
||||
|
||||
connect(m_socket, &QLocalSocket::readyRead, this, &LocalClient::onReadyRead);
|
||||
connect(m_socket, &QLocalSocket::connected, this, &LocalClient::onConnected);
|
||||
connect(m_socket, QOverload<QLocalSocket::LocalSocketError>::of(&QLocalSocket::error), this, &LocalClient::displayError);
|
||||
}
|
||||
|
||||
void LocalClient::connectToServer(const QString& name)
|
||||
{
|
||||
m_blockSize = 0;
|
||||
m_socket->abort();
|
||||
m_socket->connectToServer(name);
|
||||
}
|
||||
|
||||
QString LocalClient::serverName() const
|
||||
{
|
||||
return m_socket->serverName();
|
||||
}
|
||||
|
||||
void LocalClient::onConnected()
|
||||
{
|
||||
emit connected();
|
||||
}
|
||||
|
||||
bool LocalClient::connectedState() const
|
||||
{
|
||||
return (m_socket->state() == QLocalSocket::ConnectedState);
|
||||
}
|
||||
|
||||
quint64 LocalClient::write(const QByteArray& data)
|
||||
{
|
||||
return m_socket->write(data);
|
||||
}
|
||||
|
||||
void LocalClient::onReadyRead()
|
||||
{
|
||||
qDebug() << "On ready read";
|
||||
|
||||
if (m_socket->canReadLine()) {
|
||||
char buf[1024];
|
||||
qint64 lineLength = m_socket->readLine(buf, sizeof(buf));
|
||||
if (lineLength != -1) {
|
||||
QString line = buf;
|
||||
line = line.simplified();
|
||||
qDebug().noquote() << QString("Readed line: '%1'").arg(line);
|
||||
emit lineAvailable(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LocalClient::displayError(QLocalSocket::LocalSocketError socketError)
|
||||
{
|
||||
Q_UNUSED(socketError)
|
||||
qDebug() << QString("The following error occurred: %1.").arg(m_socket->errorString());
|
||||
}
|
||||
34
client/localclient.h
Normal file
34
client/localclient.h
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef LOCALCLIENT_H
|
||||
#define LOCALCLIENT_H
|
||||
|
||||
#include <QDataStream>
|
||||
#include <QLocalSocket>
|
||||
|
||||
class LocalClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit LocalClient(QObject *parent = nullptr);
|
||||
|
||||
QString serverName() const;
|
||||
bool connectedState() const;
|
||||
quint64 write(const QByteArray& data);
|
||||
void connectToServer(const QString& name);
|
||||
|
||||
signals:
|
||||
void connected();
|
||||
void lineAvailable(const QString& line);
|
||||
|
||||
private slots:
|
||||
void displayError(QLocalSocket::LocalSocketError socketError);
|
||||
void onConnected();
|
||||
void onReadyRead();
|
||||
|
||||
private:
|
||||
QLocalSocket* m_socket;
|
||||
QDataStream m_in;
|
||||
quint32 m_blockSize;
|
||||
};
|
||||
|
||||
#endif // LOCALCLIENT_H
|
||||
|
|
@ -10,6 +10,13 @@
|
|||
|
||||
#include "ui/mainwindow.h"
|
||||
|
||||
static void loadTranslator()
|
||||
{
|
||||
QTranslator* translator = new QTranslator;
|
||||
if (translator->load(QLocale(), QString("amneziavpn"), QLatin1String("_"), QLatin1String(":/translations"))) {
|
||||
qApp->installTranslator(translator);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
|
@ -17,6 +24,7 @@ int main(int argc, char *argv[])
|
|||
RunGuard::instance(APPLICATION_NAME).activate();
|
||||
|
||||
QApplication app(argc, argv);
|
||||
loadTranslator();
|
||||
|
||||
if (! RunGuard::instance().tryToRun()) {
|
||||
qDebug() << "Tried to run second instance. Exiting...";
|
||||
|
|
@ -35,32 +43,17 @@ int main(int argc, char *argv[])
|
|||
QFontDatabase::addApplicationFont(":/fonts/Lato-Thin.ttf");
|
||||
QFontDatabase::addApplicationFont(":/fonts/Lato-ThinItalic.ttf");
|
||||
|
||||
{
|
||||
QTranslator *translator = new QTranslator;
|
||||
QLocale ru(QLocale("ru_RU"));
|
||||
QLocale::setDefault(ru);
|
||||
if (translator->load(QLocale(), "amneziavpn", ".", QLatin1String(":/translations"))) {
|
||||
bool ok = qApp->installTranslator(translator);
|
||||
qDebug().noquote() << "Main: Installing translator for locale" << ru.name() << ok;
|
||||
}
|
||||
else {
|
||||
qDebug().noquote() << "Main: Failed to install translator for locale" << ru.name();
|
||||
}
|
||||
}
|
||||
|
||||
app.setApplicationName(APPLICATION_NAME);
|
||||
app.setOrganizationName(ORGANIZATION_NAME);
|
||||
app.setApplicationDisplayName(APPLICATION_NAME);
|
||||
|
||||
//app.setQuitOnLastWindowClosed(false);
|
||||
|
||||
QCommandLineParser parser;
|
||||
parser.setApplicationDescription(APPLICATION_NAME);
|
||||
parser.addHelpOption();
|
||||
parser.addVersionOption();
|
||||
|
||||
if (!Debug::init()) {
|
||||
qCritical() << "Initialization of debug subsystem failed";
|
||||
qWarning() << "Initialization of debug subsystem failed";
|
||||
}
|
||||
|
||||
QFont f("Lato Regular", 10);
|
||||
|
|
|
|||
111
client/managementserver.cpp
Normal file
111
client/managementserver.cpp
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
#include <QDebug>
|
||||
#include <QTcpServer>
|
||||
#include <QTcpSocket>
|
||||
|
||||
#include "managementserver.h"
|
||||
|
||||
ManagementServer::ManagementServer(QObject *parent) : QObject(parent),
|
||||
m_tcpServer(nullptr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ManagementServer::~ManagementServer()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool ManagementServer::isOpen() const
|
||||
{
|
||||
return (m_socket && m_socket->isOpen());
|
||||
}
|
||||
|
||||
void ManagementServer::stop()
|
||||
{
|
||||
m_tcpServer->close();
|
||||
}
|
||||
|
||||
void ManagementServer::onAcceptError(QAbstractSocket::SocketError socketError)
|
||||
{
|
||||
qDebug().noquote() << QString("Accept error: %1").arg(socketError);
|
||||
}
|
||||
|
||||
qint64 ManagementServer::writeCommand(const QString& message)
|
||||
{
|
||||
if (!isOpen()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const QString command = message + "\n";
|
||||
return m_socket->write(command.toStdString().c_str());
|
||||
}
|
||||
|
||||
void ManagementServer::onNewConnection()
|
||||
{
|
||||
qDebug() << "New incoming connection";
|
||||
|
||||
m_socket = m_tcpServer->nextPendingConnection();
|
||||
m_tcpServer->close();
|
||||
|
||||
QObject::connect(m_socket, SIGNAL(disconnected()), this, SLOT(onSocketDisconnected()));
|
||||
QObject::connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
|
||||
QObject::connect(m_socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
|
||||
}
|
||||
|
||||
void ManagementServer::onSocketError(QAbstractSocket::SocketError socketError)
|
||||
{
|
||||
Q_UNUSED(socketError);
|
||||
|
||||
qDebug().noquote() << QString("Mananement server error: %1").arg(m_socket->errorString());
|
||||
}
|
||||
|
||||
void ManagementServer::onSocketDisconnected()
|
||||
{
|
||||
m_socket->deleteLater();
|
||||
}
|
||||
|
||||
QTcpSocket* ManagementServer::socket() const
|
||||
{
|
||||
if (!isOpen()) {
|
||||
return nullptr;
|
||||
}
|
||||
return m_socket;
|
||||
}
|
||||
|
||||
void ManagementServer::onReadyRead()
|
||||
{
|
||||
emit readyRead();
|
||||
}
|
||||
|
||||
bool ManagementServer::start(const QString& host, unsigned int port)
|
||||
{
|
||||
if (m_tcpServer) {
|
||||
delete m_tcpServer;
|
||||
}
|
||||
|
||||
m_tcpServer = new QTcpServer(this);
|
||||
m_tcpServer->setMaxPendingConnections(1);
|
||||
|
||||
connect(m_tcpServer, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(onAcceptError(QAbstractSocket::SocketError)));
|
||||
connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
|
||||
|
||||
if (m_tcpServer->listen(QHostAddress(host), port)) {
|
||||
emit serverStarted();
|
||||
return true;
|
||||
}
|
||||
|
||||
qDebug().noquote() << QString("Can't start TCP server, %1,%2")
|
||||
.arg(m_tcpServer->serverError())
|
||||
.arg(m_tcpServer->errorString());
|
||||
return false;
|
||||
}
|
||||
|
||||
QString ManagementServer::readLine()
|
||||
{
|
||||
if (!isOpen()) {
|
||||
qDebug() << "Socket is not opened";
|
||||
return QString();
|
||||
}
|
||||
|
||||
return m_socket->readLine();
|
||||
}
|
||||
43
client/managementserver.h
Normal file
43
client/managementserver.h
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#ifndef MANAGEMENTSERVER_H
|
||||
#define MANAGEMENTSERVER_H
|
||||
|
||||
#include <QAbstractSocket>
|
||||
#include <QString>
|
||||
|
||||
class QTcpServer;
|
||||
class QTcpSocket;
|
||||
|
||||
class ManagementServer : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit ManagementServer(QObject *parent = nullptr);
|
||||
~ManagementServer();
|
||||
|
||||
bool start(const QString& host, unsigned int port);
|
||||
void stop();
|
||||
bool isOpen() const;
|
||||
|
||||
QString readLine();
|
||||
qint64 writeCommand(const QString& message);
|
||||
|
||||
QTcpSocket* socket() const;
|
||||
|
||||
signals:
|
||||
void readyRead();
|
||||
void serverStarted();
|
||||
|
||||
protected slots:
|
||||
void onAcceptError(QAbstractSocket::SocketError socketError);
|
||||
void onNewConnection();
|
||||
void onReadyRead();
|
||||
void onSocketDisconnected();
|
||||
void onSocketError(QAbstractSocket::SocketError socketError);
|
||||
|
||||
protected:
|
||||
QTcpServer* m_tcpServer;
|
||||
QTcpSocket* m_socket;
|
||||
};
|
||||
|
||||
#endif // MANAGEMENTSERVER_H
|
||||
95
client/message.cpp
Normal file
95
client/message.cpp
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
#include "message.h"
|
||||
|
||||
Message::Message(State state, const QStringList& args) :
|
||||
m_state(state),
|
||||
m_args(args),
|
||||
m_valid(true)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool Message::isValid() const
|
||||
{
|
||||
return m_valid;
|
||||
}
|
||||
|
||||
QString Message::textState() const
|
||||
{
|
||||
switch (m_state) {
|
||||
case State::Unknown: return "Unknown";
|
||||
case State::Initialize: return "Initialize";
|
||||
case State::StartRequest: return "StartRequest";
|
||||
case State::Started: return "Started";
|
||||
case State::FinishRequest: return "FinishRequest";
|
||||
case State::Finished: return "Finished";
|
||||
default:
|
||||
;
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
Message::State Message::state() const
|
||||
{
|
||||
return m_state;
|
||||
}
|
||||
|
||||
QString Message::toString() const
|
||||
{
|
||||
if (!isValid()) {
|
||||
return QString();
|
||||
}
|
||||
|
||||
return QString("%1%2%3")
|
||||
.arg(textState())
|
||||
.arg(m_dataSeparator)
|
||||
.arg(argsToString());
|
||||
}
|
||||
|
||||
QString Message::argAtIndex(int index) const
|
||||
{
|
||||
if ((index + 1) > args().size()) {
|
||||
return QString();
|
||||
}
|
||||
|
||||
return args().at(index);
|
||||
}
|
||||
|
||||
QStringList Message::args() const
|
||||
{
|
||||
return m_args;
|
||||
}
|
||||
|
||||
QString Message::argsToString() const
|
||||
{
|
||||
return m_args.join(m_argSeparator);
|
||||
}
|
||||
|
||||
Message::Message(const QString& data)
|
||||
{
|
||||
m_valid = false;
|
||||
if (data.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QStringList dataList = data.split(m_dataSeparator);
|
||||
if ((dataList.size() != 2)) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool stateFound = false;
|
||||
for (int i = static_cast<int>(State::Unknown); i <= static_cast<int>(State::Finished); i++ ) {
|
||||
m_state = static_cast<State>(i);
|
||||
if (textState() == dataList.at(0)) {
|
||||
stateFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!stateFound) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_args = dataList.at(1).split(m_argSeparator);
|
||||
m_valid = true;
|
||||
}
|
||||
|
||||
31
client/message.h
Normal file
31
client/message.h
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#ifndef MESSAGE_H
|
||||
#define MESSAGE_H
|
||||
|
||||
#include <QStringList>
|
||||
|
||||
class Message {
|
||||
|
||||
public:
|
||||
enum class State {Unknown, Initialize, StartRequest, Started, FinishRequest, Finished};
|
||||
Message(State state, const QStringList& args);
|
||||
Message(const QString& data);
|
||||
|
||||
QString argAtIndex(int index) const;
|
||||
QString argsToString() const;
|
||||
QString toString() const;
|
||||
QStringList args() const;
|
||||
State state() const;
|
||||
bool isValid() const;
|
||||
|
||||
protected:
|
||||
QString textState() const;
|
||||
|
||||
const QString m_argSeparator = ",";
|
||||
const QString m_dataSeparator = "|";
|
||||
|
||||
bool m_valid;
|
||||
State m_state;
|
||||
QStringList m_args;
|
||||
};
|
||||
|
||||
#endif // MESSAGE_H
|
||||
238
client/openvpnprotocol.cpp
Normal file
238
client/openvpnprotocol.cpp
Normal file
|
|
@ -0,0 +1,238 @@
|
|||
#include <QCoreApplication>
|
||||
#include <QFileInfo>
|
||||
|
||||
#include "communicator.h"
|
||||
#include "debug.h"
|
||||
#include "openvpnprotocol.h"
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
OpenVpnProtocol::OpenVpnProtocol(const QString& args, QObject* parent) :
|
||||
VpnProtocol(args, parent),
|
||||
m_requestFromUserToStop(false)
|
||||
{
|
||||
setConfigFile(args);
|
||||
connect(m_communicator, &Communicator::messageReceived, this, &OpenVpnProtocol::onMessageReceived);
|
||||
connect(&m_managementServer, &ManagementServer::readyRead, this, &OpenVpnProtocol::onReadyReadDataFromManagementServer);
|
||||
}
|
||||
|
||||
OpenVpnProtocol::~OpenVpnProtocol()
|
||||
{
|
||||
stop();
|
||||
}
|
||||
|
||||
void OpenVpnProtocol::onMessageReceived(const Message& message)
|
||||
{
|
||||
if (!message.isValid()) {
|
||||
qWarning().noquote() << QString("Message received: '%1', but it is not valid").arg(message.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
switch (message.state()) {
|
||||
case Message::State::Started:
|
||||
qDebug().noquote() << QString("OpenVPN process started");
|
||||
break;
|
||||
case Message::State::Finished:
|
||||
qDebug().noquote() << QString("OpenVPN process finished with status %1").arg(message.argAtIndex(1));
|
||||
onOpenVpnProcessFinished(message.argAtIndex(1).toInt());
|
||||
break;
|
||||
default:
|
||||
qDebug().noquote() << QString("Message received: '%1'").arg(message.toString());
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
void OpenVpnProtocol::stop()
|
||||
{
|
||||
if ((m_connectionState == VpnProtocol::ConnectionState::Preparing) ||
|
||||
(m_connectionState == VpnProtocol::ConnectionState::Connecting) ||
|
||||
(m_connectionState == VpnProtocol::ConnectionState::Connected)) {
|
||||
if (!sendTermSignal()) {
|
||||
killOpenVpnProcess();
|
||||
}
|
||||
setConnectionState(VpnProtocol::ConnectionState::Disconnecting);
|
||||
}
|
||||
}
|
||||
|
||||
void OpenVpnProtocol::killOpenVpnProcess()
|
||||
{
|
||||
// send command to kill openvpn process.
|
||||
}
|
||||
|
||||
bool OpenVpnProtocol::setConfigFile(const QString& configFileNamePath)
|
||||
{
|
||||
m_configFileName = configFileNamePath;
|
||||
QFileInfo file(m_configFileName);
|
||||
|
||||
if (file.fileName().isEmpty()) {
|
||||
m_configFileName = Utils::systemConfigPath() + "/" + QCoreApplication::applicationName() + ".ovpn";
|
||||
}
|
||||
|
||||
if (m_configFileName.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
qDebug() << "Set config file:" << configPath();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool OpenVpnProtocol::openVpnProcessIsRunning() const
|
||||
{
|
||||
return Utils::processIsRunning("openvpn");
|
||||
}
|
||||
|
||||
void OpenVpnProtocol::disconnectFromManagementServer()
|
||||
{
|
||||
m_managementServer.stop();
|
||||
}
|
||||
|
||||
QString OpenVpnProtocol::configPath() const
|
||||
{
|
||||
return m_configFileName;
|
||||
}
|
||||
|
||||
void OpenVpnProtocol::writeCommand(const QString& command)
|
||||
{
|
||||
QTextStream stream(reinterpret_cast<QIODevice*>(m_managementServer.socket()));
|
||||
stream << command << endl;
|
||||
}
|
||||
|
||||
QString OpenVpnProtocol::openVpnExecPath() const
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
return Utils::executable(QString("openvpn/%1/openvpn").arg(QSysInfo::buildCpuArchitecture()), true);
|
||||
#else
|
||||
return Utils::executable(QString("/openvpn"), true);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool OpenVpnProtocol::start()
|
||||
{
|
||||
qDebug() << "Start OpenVPN connection" << openVpnExecPath();
|
||||
|
||||
m_requestFromUserToStop = false;
|
||||
m_openVpnStateSigTermHandlerTimer.stop();
|
||||
stop();
|
||||
|
||||
if (!QFileInfo::exists(openVpnExecPath())) {
|
||||
qCritical() << "OpeVPN executable does not exist!";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!QFileInfo::exists(configPath())) {
|
||||
qCritical() << "OpeVPN config file does not exist!";
|
||||
return false;
|
||||
}
|
||||
|
||||
QString vpnLogFileNamePath = Utils::systemLogPath() + "/openvpn.log";
|
||||
Utils::createEmptyFile(vpnLogFileNamePath);
|
||||
|
||||
QStringList args({openVpnExecPath(),
|
||||
"--config" , configPath(),
|
||||
"--management", m_managementHost, QString::number(m_managementPort),
|
||||
"--management-client",
|
||||
"--log-append", vpnLogFileNamePath
|
||||
});
|
||||
|
||||
if (!m_managementServer.start(m_managementHost, m_managementPort)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
setConnectionState(ConnectionState::Connecting);
|
||||
|
||||
qDebug().noquote() << "Start OpenVPN process with args: " << args;
|
||||
m_communicator->sendMessage(Message(Message::State::StartRequest, args));
|
||||
|
||||
startTimeoutTimer();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void OpenVpnProtocol::openVpnStateSigTermHandlerTimerEvent()
|
||||
{
|
||||
bool processStatus = openVpnProcessIsRunning();
|
||||
if (processStatus) {
|
||||
killOpenVpnProcess();
|
||||
}
|
||||
onOpenVpnProcessFinished(0);
|
||||
}
|
||||
|
||||
void OpenVpnProtocol::openVpnStateSigTermHandler()
|
||||
{
|
||||
m_openVpnStateSigTermHandlerTimer.start(5000);
|
||||
}
|
||||
|
||||
bool OpenVpnProtocol::sendTermSignal()
|
||||
{
|
||||
return m_managementServer.writeCommand("signal SIGTERM");
|
||||
}
|
||||
|
||||
void OpenVpnProtocol::sendByteCount()
|
||||
{
|
||||
m_managementServer.writeCommand("bytecount 1");
|
||||
}
|
||||
|
||||
void OpenVpnProtocol::sendInitialData()
|
||||
{
|
||||
m_managementServer.writeCommand("state on");
|
||||
m_managementServer.writeCommand("log on");
|
||||
}
|
||||
|
||||
void OpenVpnProtocol::onReadyReadDataFromManagementServer()
|
||||
{
|
||||
for (;;) {
|
||||
QString line = m_managementServer.readLine().simplified();
|
||||
|
||||
if (line.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!line.contains(">BYTECOUNT")) {
|
||||
qDebug().noquote() << line;
|
||||
}
|
||||
|
||||
if (line.contains(">INFO:OpenVPN Management Interface")) {
|
||||
sendInitialData();
|
||||
} else if (line.startsWith(">STATE")) {
|
||||
if (line.contains("CONNECTED,SUCCESS")) {
|
||||
sendByteCount();
|
||||
stopTimeoutTimer();
|
||||
setConnectionState(VpnProtocol::ConnectionState::Connected);
|
||||
continue;
|
||||
} else if (line.contains("EXITING,SIGTER")) {
|
||||
openVpnStateSigTermHandler();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray data(line.toStdString().c_str());
|
||||
if (data.contains(">BYTECOUNT:")) {
|
||||
int beg = data.lastIndexOf(">BYTECOUNT:");
|
||||
int end = data.indexOf("\n", beg);
|
||||
|
||||
beg += sizeof(">BYTECOUNT:") - 1;
|
||||
QList<QByteArray> count = data.mid(beg, end - beg + 1).split(',');
|
||||
|
||||
quint64 r = static_cast<quint64>(count.at(0).trimmed().toULongLong());
|
||||
quint64 s = static_cast<quint64>(count.at(1).trimmed().toULongLong());
|
||||
|
||||
setBytesChanged(r, s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OpenVpnProtocol::onOpenVpnProcessFinished(int exitCode)
|
||||
{
|
||||
m_openVpnStateSigTermHandlerTimer.stop();
|
||||
if (m_connectionState == VpnProtocol::ConnectionState::Disconnected) {
|
||||
qDebug() << "Already in disconnected state";
|
||||
return;
|
||||
}
|
||||
|
||||
qDebug().noquote() << QString("Process finished with code: %1").arg(exitCode);
|
||||
|
||||
setConnectionState(VpnProtocol::ConnectionState::Disconnected);
|
||||
}
|
||||
|
||||
|
||||
51
client/openvpnprotocol.h
Normal file
51
client/openvpnprotocol.h
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#ifndef OPENVPNPROTOCOL_H
|
||||
#define OPENVPNPROTOCOL_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QTimer>
|
||||
|
||||
#include "managementserver.h"
|
||||
#include "message.h"
|
||||
#include "vpnprotocol.h"
|
||||
|
||||
class OpenVpnProtocol : public VpnProtocol
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit OpenVpnProtocol(const QString& args = QString(), QObject* parent = nullptr);
|
||||
~OpenVpnProtocol();
|
||||
|
||||
bool start() override;
|
||||
void stop() override;
|
||||
|
||||
protected slots:
|
||||
void onMessageReceived(const Message& message);
|
||||
void onOpenVpnProcessFinished(int exitCode);
|
||||
void onReadyReadDataFromManagementServer();
|
||||
|
||||
protected:
|
||||
QString configPath() const;
|
||||
QString openVpnExecPath() const;
|
||||
bool openVpnProcessIsRunning() const;
|
||||
bool sendTermSignal();
|
||||
bool setConfigFile(const QString& configFileNamePath);
|
||||
void disconnectFromManagementServer();
|
||||
void killOpenVpnProcess();
|
||||
void openVpnStateSigTermHandler();
|
||||
void openVpnStateSigTermHandlerTimerEvent();
|
||||
void sendByteCount();
|
||||
void sendInitialData();
|
||||
void writeCommand(const QString& command);
|
||||
|
||||
const QString m_managementHost = "127.0.0.1";
|
||||
const unsigned int m_managementPort = 57775;
|
||||
|
||||
ManagementServer m_managementServer;
|
||||
QString m_configFileName;
|
||||
QTimer m_openVpnStateSigTermHandlerTimer;
|
||||
bool m_requestFromUserToStop;
|
||||
};
|
||||
|
||||
#endif // OPENVPNPROTOCOL_H
|
||||
|
|
@ -1,4 +1,8 @@
|
|||
<RCC>
|
||||
<qresource prefix="/">
|
||||
<file>translations/amneziavpn_ru.qm</file>
|
||||
</qresource>
|
||||
|
||||
<qresource prefix="/">
|
||||
<file>images/close.png</file>
|
||||
<file>images/settings.png</file>
|
||||
|
|
|
|||
BIN
client/translations/amneziavpn_ru.qm
Normal file
BIN
client/translations/amneziavpn_ru.qm
Normal file
Binary file not shown.
197
client/translations/amneziavpn_ru.ts
Normal file
197
client/translations/amneziavpn_ru.ts
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!DOCTYPE TS>
|
||||
<TS version="2.1" language="ru_RU">
|
||||
<context>
|
||||
<name>MainWindow</name>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="294"/>
|
||||
<source>Подключиться к уже
|
||||
созданному серверу VPN</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="323"/>
|
||||
<source>Код для подключения</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="349"/>
|
||||
<location filename="../ui/mainwindow.ui" line="730"/>
|
||||
<source>Подключение...</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="421"/>
|
||||
<location filename="../ui/mainwindow.ui" line="676"/>
|
||||
<source>Подключиться</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="458"/>
|
||||
<source>Настроить собственный сервер</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="496"/>
|
||||
<source>Подключите ваш сервер,
|
||||
чтобы использовать VPN</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="525"/>
|
||||
<source>IP-адрес сервера</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="550"/>
|
||||
<source>Логин для подключения по SSH</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="575"/>
|
||||
<source>Пароль</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="596"/>
|
||||
<source>51.83.180.158</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="617"/>
|
||||
<source>root</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="638"/>
|
||||
<source>qazqazwsxwsx</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="704"/>
|
||||
<source>Где взять данные для подключения →</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="887"/>
|
||||
<location filename="../ui/mainwindow.ui" line="916"/>
|
||||
<source>0 Mbps</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="954"/>
|
||||
<source>Add site</source>
|
||||
<translation>Добавить сайт</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1024"/>
|
||||
<source>Connected</source>
|
||||
<translation>Подключено</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1067"/>
|
||||
<source>How to use VPN</source>
|
||||
<translation>Как использовать VPN</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1086"/>
|
||||
<source>For all connections</source>
|
||||
<translation>Для всех соединений</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1102"/>
|
||||
<source>For selected sites</source>
|
||||
<translation>Для выбранных сайтов</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1199"/>
|
||||
<source>List of most popular prohibited sites</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1233"/>
|
||||
<source>Эти сайты будут открываться через VPN</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1290"/>
|
||||
<source>Например, rutor.org или 17.21.111.8</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1315"/>
|
||||
<source>+</source>
|
||||
<translation>+</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1337"/>
|
||||
<source>Удалить выбранный</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1362"/>
|
||||
<source>Адрес сайта или ip-адрес</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1445"/>
|
||||
<source>Server settings</source>
|
||||
<translation>Настройки сервера</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1494"/>
|
||||
<source>Share connection</source>
|
||||
<translation>Поделиться подключением</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1572"/>
|
||||
<source>Connection string</source>
|
||||
<translation>Строка подключения</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1613"/>
|
||||
<source><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
|
||||
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
|
||||
p, li { white-space: pre-wrap; }
|
||||
</style></head><body style=" font-family:'Consolas'; font-size:22px; font-weight:600; font-style:normal;">
|
||||
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:20pt;">vpn:\\aosdiufhafafsuhgqejghuserhglaidhgauhgalgadg</span></p></body></html></source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1651"/>
|
||||
<source>Copy</source>
|
||||
<translation>Копировать</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.ui" line="1678"/>
|
||||
<source>Тот, кто зайдёт с этим кодом, будет иметь те же права на испольтование VPN, что и вы. Чтобы создать новый код смените логин и/или пароль для подлючения в настройках вашего сервера.</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../ui/mainwindow.cpp" line="78"/>
|
||||
<source>Cannot open logs folder!</source>
|
||||
<translation>Невозможно открыть папку с логами!</translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
<name>QObject</name>
|
||||
<message>
|
||||
<location filename="../main.cpp" line="31"/>
|
||||
<source>Notify</source>
|
||||
<translation>Уведомление</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../main.cpp" line="31"/>
|
||||
<source>AmneziaVPN is already running.</source>
|
||||
<translation>Приложение AmneziaVPN уже запущено.</translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
<name>VpnConnection</name>
|
||||
<message>
|
||||
<location filename="../vpnconnection.cpp" line="49"/>
|
||||
<source>Mbps</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
</context>
|
||||
</TS>
|
||||
|
|
@ -1,12 +1,20 @@
|
|||
#include <QKeyEvent>
|
||||
#include <QMessageBox>
|
||||
#include <QSysInfo>
|
||||
#include <QThread>
|
||||
|
||||
#include "communicator.h"
|
||||
#include "debug.h"
|
||||
#include "defines.h"
|
||||
#include "mainwindow.h"
|
||||
#include "ui_mainwindow.h"
|
||||
#include "utils.h"
|
||||
#include "vpnconnection.h"
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
|
||||
MainWindow::MainWindow(QWidget *parent) :
|
||||
QMainWindow(parent),
|
||||
ui(new Ui::MainWindow),
|
||||
m_vpnConnection(nullptr)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
|
|
@ -15,7 +23,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
|
|||
ui->stackedWidget_main->setCurrentIndex(2);
|
||||
|
||||
connect(ui->pushButton_blocked_list, SIGNAL(clicked(bool)), this, SLOT(onPushButtonBlockedListClicked(bool)));
|
||||
connect(ui->pushButton_connect, SIGNAL(clicked(bool)), this, SLOT(onPushButtonConnectClicked(bool)));
|
||||
connect(ui->pushButton_connect, SIGNAL(toggled(bool)), this, SLOT(onPushButtonConnectToggled(bool)));
|
||||
connect(ui->pushButton_settings, SIGNAL(clicked(bool)), this, SLOT(onPushButtonSettingsClicked(bool)));
|
||||
|
||||
connect(ui->pushButton_back_from_sites, SIGNAL(clicked(bool)), this, SLOT(onPushButtonBackFromSitesClicked(bool)));
|
||||
|
|
@ -23,14 +31,37 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
|
|||
|
||||
setFixedSize(width(),height());
|
||||
|
||||
qDebug() << APPLICATION_NAME;
|
||||
qDebug() << "Started";
|
||||
qInfo().noquote() << QString("Started %1 version %2").arg(APPLICATION_NAME).arg(APP_VERSION);
|
||||
qInfo().noquote() << QString("%1 (%2)").arg(QSysInfo::prettyProductName()).arg(QSysInfo::currentCpuArchitecture());
|
||||
|
||||
|
||||
QDir dir;
|
||||
QString configPath = Utils::systemConfigPath();
|
||||
if (!dir.mkpath(configPath)) {
|
||||
qWarning() << "Cannot initialize config path:" << configPath;
|
||||
}
|
||||
|
||||
m_vpnConnection = new VpnConnection;
|
||||
connect(m_vpnConnection, SIGNAL(bytesChanged(quint64, quint64)), this, SLOT(onBytesChanged(quint64, quint64)));
|
||||
connect(m_vpnConnection, SIGNAL(connectionStateChanged(VpnProtocol::ConnectionState)), this, SLOT(onConnectionStateChanged(VpnProtocol::ConnectionState)));
|
||||
|
||||
onConnectionStateChanged(VpnProtocol::ConnectionState::Disconnected);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
{
|
||||
delete ui;
|
||||
|
||||
hide();
|
||||
|
||||
m_vpnConnection->disconnectFromVpn();
|
||||
for (int i = 0; i < 50; i++) {
|
||||
qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
|
||||
QThread::msleep(100);
|
||||
if (m_vpnConnection->disconnected()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
qDebug() << "Closed";
|
||||
}
|
||||
|
||||
|
|
@ -42,16 +73,22 @@ void MainWindow::goToIndex(int index)
|
|||
void MainWindow::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
switch (event->key()) {
|
||||
case Qt::Key_L:
|
||||
if (!Debug::openLogsFolder()) {
|
||||
QMessageBox::warning(this, APPLICATION_NAME, tr("Cannot open logs folder!"));
|
||||
}
|
||||
case Qt::Key_L:
|
||||
if (!Debug::openLogsFolder()) {
|
||||
QMessageBox::warning(this, APPLICATION_NAME, tr("Cannot open logs folder!"));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
default:
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::onBytesChanged(quint64 receivedData, quint64 sentData)
|
||||
{
|
||||
ui->label_speed_received->setText(VpnConnection::bytesToText(receivedData));
|
||||
ui->label_speed_sent->setText(VpnConnection::bytesToText(sentData));
|
||||
}
|
||||
|
||||
void MainWindow::onPushButtonBackFromSettingsClicked(bool)
|
||||
{
|
||||
goToIndex(2);
|
||||
|
|
@ -72,9 +109,52 @@ void MainWindow::onPushButtonSettingsClicked(bool)
|
|||
goToIndex(4);
|
||||
}
|
||||
|
||||
void MainWindow::onPushButtonConnectClicked(bool)
|
||||
void MainWindow::onConnectionStateChanged(VpnProtocol::ConnectionState state)
|
||||
{
|
||||
qDebug() << "onPushButtonConnectClicked";
|
||||
bool pushButtonConnectEnabled = false;
|
||||
ui->label_state->setText(VpnProtocol::textConnectionState(state));
|
||||
|
||||
switch (state) {
|
||||
case VpnProtocol::ConnectionState::Disconnected:
|
||||
onBytesChanged(0,0);
|
||||
ui->pushButton_connect->setChecked(false);
|
||||
pushButtonConnectEnabled = true;
|
||||
break;
|
||||
case VpnProtocol::ConnectionState::Preparing:
|
||||
pushButtonConnectEnabled = false;
|
||||
break;
|
||||
case VpnProtocol::ConnectionState::Connecting:
|
||||
pushButtonConnectEnabled = false;
|
||||
break;
|
||||
case VpnProtocol::ConnectionState::Connected:
|
||||
pushButtonConnectEnabled = true;
|
||||
break;
|
||||
case VpnProtocol::ConnectionState::Disconnecting:
|
||||
pushButtonConnectEnabled = false;
|
||||
break;
|
||||
case VpnProtocol::ConnectionState::TunnelReconnecting:
|
||||
pushButtonConnectEnabled = false;
|
||||
break;
|
||||
case VpnProtocol::ConnectionState::Error:
|
||||
pushButtonConnectEnabled = true;
|
||||
break;
|
||||
case VpnProtocol::ConnectionState::Unknown:
|
||||
default:
|
||||
pushButtonConnectEnabled = true;
|
||||
;
|
||||
}
|
||||
|
||||
ui->pushButton_connect->setEnabled(pushButtonConnectEnabled);
|
||||
}
|
||||
|
||||
void MainWindow::onPushButtonConnectToggled(bool checked)
|
||||
{
|
||||
if (checked) {
|
||||
m_vpnConnection->connectToVpn();
|
||||
ui->pushButton_connect->setEnabled(false);
|
||||
} else {
|
||||
m_vpnConnection->disconnectFromVpn();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,10 @@
|
|||
|
||||
#include <QMainWindow>
|
||||
|
||||
#include "vpnprotocol.h"
|
||||
|
||||
class VpnConnection;
|
||||
|
||||
namespace Ui {
|
||||
class MainWindow;
|
||||
}
|
||||
|
|
@ -23,12 +27,15 @@ public slots:
|
|||
|
||||
private slots:
|
||||
void onPushButtonBlockedListClicked(bool clicked);
|
||||
void onPushButtonConnectClicked(bool clicked);
|
||||
void onPushButtonConnectToggled(bool checked);
|
||||
void onPushButtonSettingsClicked(bool clicked);
|
||||
|
||||
void onPushButtonBackFromSettingsClicked(bool clicked);
|
||||
void onPushButtonBackFromSitesClicked(bool clicked);
|
||||
|
||||
void onBytesChanged(quint64 receivedBytes, quint64 sentBytes);
|
||||
void onConnectionStateChanged(VpnProtocol::ConnectionState state);
|
||||
|
||||
protected:
|
||||
void keyPressEvent(QKeyEvent* event);
|
||||
|
||||
|
|
@ -36,6 +43,7 @@ private:
|
|||
void goToIndex(int index);
|
||||
|
||||
Ui::MainWindow *ui;
|
||||
VpnConnection* m_vpnConnection;
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
||||
|
|
|
|||
|
|
@ -861,7 +861,7 @@ QPushButton:hover {
|
|||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="label_speed_upload_2">
|
||||
<widget class="QLabel" name="label_speed_sent">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>260</x>
|
||||
|
|
@ -890,7 +890,7 @@ font: 63 12pt "Lato";</string>
|
|||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="label_speed_download_2">
|
||||
<widget class="QLabel" name="label_speed_received">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
|
|
@ -951,7 +951,7 @@ line-height: 21px;
|
|||
</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Добавить сайт</string>
|
||||
<string>Add site</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QPushButton" name="pushButton_settings">
|
||||
|
|
@ -1001,7 +1001,7 @@ image: url(:/images/connect_button_connected.png);
|
|||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="label_status_2">
|
||||
<widget class="QLabel" name="label_state">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
|
|
@ -1021,7 +1021,7 @@ color: #181922;
|
|||
</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Подключено</string>
|
||||
<string>Connected</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
|
|
@ -1043,7 +1043,7 @@ color: #181922;
|
|||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="label_status_3">
|
||||
<widget class="QLabel" name="label_description">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>20</x>
|
||||
|
|
@ -1064,7 +1064,7 @@ color: #181922;
|
|||
</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Как использовать VPN</string>
|
||||
<string>How to use VPN</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
|
||||
|
|
@ -1083,7 +1083,7 @@ color: #181922;
|
|||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Для всех соеднинений</string>
|
||||
<string>For all connections</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QRadioButton" name="radioButton_2">
|
||||
|
|
@ -1099,7 +1099,7 @@ color: #181922;
|
|||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Для определённых сайтов</string>
|
||||
<string>For selected sites</string>
|
||||
</property>
|
||||
<property name="checked">
|
||||
<bool>true</bool>
|
||||
|
|
|
|||
96
client/utils.cpp
Normal file
96
client/utils.cpp
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
#include <QCoreApplication>
|
||||
#include <QDir>
|
||||
#include <QProcess>
|
||||
#include <QStandardPaths>
|
||||
|
||||
#include "defines.h"
|
||||
#include "utils.h"
|
||||
|
||||
QString Utils::toString(bool value)
|
||||
{
|
||||
return value ? "true" : "false";
|
||||
}
|
||||
|
||||
QString Utils::systemLogPath()
|
||||
{
|
||||
return systemDataLocationPath() + "/log";
|
||||
}
|
||||
|
||||
QString Utils::systemConfigPath()
|
||||
{
|
||||
return systemDataLocationPath() + "/config";
|
||||
}
|
||||
|
||||
bool Utils::createEmptyFile(const QString& path)
|
||||
{
|
||||
QFile f(path);
|
||||
return f.open(QIODevice::WriteOnly | QIODevice::Truncate);
|
||||
}
|
||||
|
||||
QString Utils::systemDataLocationPath()
|
||||
{
|
||||
QStringList locationList = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
|
||||
QString primaryLocation;
|
||||
#ifdef Q_OS_WIN
|
||||
primaryLocation = "ProgramData";
|
||||
#elif defined Q_OS_MAC
|
||||
primaryLocation = "Users";
|
||||
#endif
|
||||
|
||||
foreach (const QString& location, locationList) {
|
||||
if (location.contains(primaryLocation)) {
|
||||
return QString("%1/%2").arg(location).arg(APPLICATION_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString Utils::executable(const QString& baseName, bool absPath)
|
||||
{
|
||||
QString ext;
|
||||
#ifdef Q_OS_WIN
|
||||
ext = ".exe";
|
||||
#endif
|
||||
const QString fileName = baseName + ext;
|
||||
if (!absPath) {
|
||||
return fileName;
|
||||
}
|
||||
return QCoreApplication::applicationDirPath() + "/" + fileName;
|
||||
}
|
||||
|
||||
bool Utils::processIsRunning(const QString& fileName)
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
QProcess process;
|
||||
process.setReadChannel(QProcess::StandardOutput);
|
||||
process.setReadChannelMode(QProcess::MergedChannels);
|
||||
process.start(QString("wmic.exe /OUTPUT:STDOUT PROCESS get %1").arg("Caption"));
|
||||
process.waitForStarted();
|
||||
process.waitForFinished();
|
||||
QString processData(process.readAll());
|
||||
QStringList processList = processData.split(QRegExp("[\r\n]"),QString::SkipEmptyParts);
|
||||
foreach (const QString& rawLine, processList) {
|
||||
const QString line = rawLine.simplified();
|
||||
if (line.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line == fileName) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
#else
|
||||
QProcess process;
|
||||
process.setProcessChannelMode(QProcess::MergedChannels);
|
||||
process.start("pgrep", QStringList({fileName}));
|
||||
process.waitForFinished();
|
||||
if (process.exitStatus() == QProcess::NormalExit) {
|
||||
return (process.readAll().toUInt() > 0);
|
||||
}
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
18
client/utils.h
Normal file
18
client/utils.h
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
class Utils {
|
||||
|
||||
public:
|
||||
static QString executable(const QString& baseName, bool absPath);
|
||||
static QString systemConfigPath();
|
||||
static QString systemDataLocationPath();
|
||||
static QString systemLogPath();
|
||||
static QString toString(bool value);
|
||||
static bool createEmptyFile(const QString& path);
|
||||
static bool processIsRunning(const QString& fileName);
|
||||
};
|
||||
|
||||
#endif // UTILS_H
|
||||
78
client/vpnconnection.cpp
Normal file
78
client/vpnconnection.cpp
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
#include <QDebug>
|
||||
|
||||
#include "openvpnprotocol.h"
|
||||
#include "vpnconnection.h"
|
||||
|
||||
VpnConnection::VpnConnection(QObject* parent) : QObject(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
VpnConnection::~VpnConnection()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void VpnConnection::onBytesChanged(quint64 receivedBytes, quint64 sentBytes)
|
||||
{
|
||||
emit bytesChanged(receivedBytes, sentBytes);
|
||||
}
|
||||
|
||||
void VpnConnection::onConnectionStateChanged(VpnProtocol::ConnectionState state)
|
||||
{
|
||||
emit connectionStateChanged(state);
|
||||
}
|
||||
|
||||
void VpnConnection::connectToVpn(Protocol protocol)
|
||||
{
|
||||
qDebug() << "Connect to VPN";
|
||||
|
||||
switch (protocol) {
|
||||
case Protocol::OpenVpn:
|
||||
m_vpnProtocol.reset(new OpenVpnProtocol());
|
||||
break;
|
||||
;
|
||||
default:
|
||||
// TODO, add later
|
||||
return;
|
||||
;
|
||||
}
|
||||
|
||||
connect(m_vpnProtocol.data(), SIGNAL(connectionStateChanged(VpnProtocol::ConnectionState)), this, SLOT(onConnectionStateChanged(VpnProtocol::ConnectionState)));
|
||||
connect(m_vpnProtocol.data(), SIGNAL(bytesChanged(quint64, quint64)), this, SLOT(onBytesChanged(quint64, quint64)));
|
||||
|
||||
m_vpnProtocol.data()->start();
|
||||
}
|
||||
|
||||
QString VpnConnection::bytesToText(quint64 bytes)
|
||||
{
|
||||
return QString("%1 %2").arg((bytes * 8) / 1024).arg(tr("Mbps"));
|
||||
}
|
||||
|
||||
void VpnConnection::disconnectFromVpn()
|
||||
{
|
||||
qDebug() << "Disconnect from VPN";
|
||||
|
||||
if (!m_vpnProtocol.data()) {
|
||||
return;
|
||||
}
|
||||
m_vpnProtocol.data()->stop();
|
||||
}
|
||||
|
||||
bool VpnConnection::connected() const
|
||||
{
|
||||
if (!m_vpnProtocol.data()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return m_vpnProtocol.data()->connected();
|
||||
}
|
||||
|
||||
bool VpnConnection::disconnected() const
|
||||
{
|
||||
if (!m_vpnProtocol.data()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return m_vpnProtocol.data()->disconnected();
|
||||
}
|
||||
40
client/vpnconnection.h
Normal file
40
client/vpnconnection.h
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#ifndef VPNCONNECTION_H
|
||||
#define VPNCONNECTION_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QScopedPointer>
|
||||
|
||||
#include "vpnprotocol.h"
|
||||
|
||||
class VpnConnection : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit VpnConnection(QObject* parent = nullptr);
|
||||
~VpnConnection();
|
||||
|
||||
enum class Protocol{OpenVpn};
|
||||
void connectToVpn(Protocol protocol = Protocol::OpenVpn);
|
||||
|
||||
bool connected() const;
|
||||
bool disconnected() const;
|
||||
void disconnectFromVpn();
|
||||
|
||||
static QString bytesToText(quint64 bytes);
|
||||
|
||||
signals:
|
||||
void bytesChanged(quint64 receivedBytes, quint64 sentBytes);
|
||||
void connectionStateChanged(VpnProtocol::ConnectionState state);
|
||||
|
||||
protected slots:
|
||||
void onBytesChanged(quint64 receivedBytes, quint64 sentBytes);
|
||||
void onConnectionStateChanged(VpnProtocol::ConnectionState state);
|
||||
|
||||
protected:
|
||||
|
||||
QScopedPointer<VpnProtocol> m_vpnProtocol;
|
||||
};
|
||||
|
||||
#endif // VPNCONNECTION_H
|
||||
93
client/vpnprotocol.cpp
Normal file
93
client/vpnprotocol.cpp
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
#include <QDebug>
|
||||
#include <QTimer>
|
||||
|
||||
#include "communicator.h"
|
||||
#include "vpnprotocol.h"
|
||||
|
||||
VpnProtocol::VpnProtocol(const QString& args, QObject* parent)
|
||||
: QObject(parent),
|
||||
m_connectionState(ConnectionState::Unknown),
|
||||
m_communicator(new Communicator),
|
||||
m_timeoutTimer(new QTimer(this))
|
||||
{
|
||||
m_timeoutTimer->setSingleShot(true);
|
||||
connect(m_timeoutTimer, &QTimer::timeout, this, &VpnProtocol::onTimeout);
|
||||
|
||||
Q_UNUSED(args)
|
||||
}
|
||||
|
||||
VpnProtocol::~VpnProtocol()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void VpnProtocol::onTimeout()
|
||||
{
|
||||
qDebug() << "Timeout";
|
||||
|
||||
emit timeoutTimerEvent();
|
||||
stop();
|
||||
}
|
||||
|
||||
void VpnProtocol::startTimeoutTimer()
|
||||
{
|
||||
m_timeoutTimer->start(30000);
|
||||
}
|
||||
|
||||
void VpnProtocol::stopTimeoutTimer()
|
||||
{
|
||||
m_timeoutTimer->stop();
|
||||
}
|
||||
|
||||
VpnProtocol::ConnectionState VpnProtocol::connectionState() const
|
||||
{
|
||||
return m_connectionState;
|
||||
}
|
||||
|
||||
void VpnProtocol::setBytesChanged(quint64 receivedBytes, quint64 sentBytes)
|
||||
{
|
||||
emit bytesChanged(receivedBytes, sentBytes);
|
||||
}
|
||||
|
||||
void VpnProtocol::setConnectionState(VpnProtocol::ConnectionState state)
|
||||
{
|
||||
if (m_connectionState == state) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_connectionState = state;
|
||||
emit connectionStateChanged(m_connectionState);
|
||||
}
|
||||
|
||||
QString VpnProtocol::textConnectionState(ConnectionState connectionState)
|
||||
{
|
||||
switch (connectionState) {
|
||||
case ConnectionState::Unknown: return "Unknown";
|
||||
case ConnectionState::Disconnected: return "Disconnected";
|
||||
case ConnectionState::Preparing: return "Preparing";
|
||||
case ConnectionState::Connecting: return "Connecting";
|
||||
case ConnectionState::Connected: return "Connected";
|
||||
case ConnectionState::Disconnecting: return "Disconnecting";
|
||||
case ConnectionState::TunnelReconnecting: return "TunnelReconnecting";
|
||||
case ConnectionState::Error: return "Error";
|
||||
default:
|
||||
;
|
||||
}
|
||||
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString VpnProtocol::textConnectionState() const
|
||||
{
|
||||
return textConnectionState(m_connectionState);
|
||||
}
|
||||
|
||||
bool VpnProtocol::connected() const
|
||||
{
|
||||
return m_connectionState == ConnectionState::Connected;
|
||||
}
|
||||
|
||||
bool VpnProtocol::disconnected() const
|
||||
{
|
||||
return m_connectionState == ConnectionState::Disconnected;
|
||||
}
|
||||
48
client/vpnprotocol.h
Normal file
48
client/vpnprotocol.h
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#ifndef VPNPROTOCOL_H
|
||||
#define VPNPROTOCOL_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
class QTimer;
|
||||
class Communicator;
|
||||
|
||||
class VpnProtocol : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit VpnProtocol(const QString& args = QString(), QObject* parent = nullptr);
|
||||
~VpnProtocol();
|
||||
|
||||
enum class ConnectionState {Unknown, Disconnected, Preparing, Connecting, Connected, Disconnecting, TunnelReconnecting, Error};
|
||||
static QString textConnectionState(ConnectionState connectionState);
|
||||
|
||||
ConnectionState connectionState() const;
|
||||
QString textConnectionState() const;
|
||||
virtual bool connected() const;
|
||||
virtual bool disconnected() const;
|
||||
virtual bool start() = 0;
|
||||
virtual void stop() = 0;
|
||||
|
||||
signals:
|
||||
void bytesChanged(quint64 receivedBytes, quint64 sentBytes);
|
||||
void connectionStateChanged(VpnProtocol::ConnectionState state);
|
||||
void timeoutTimerEvent();
|
||||
|
||||
protected slots:
|
||||
virtual void onTimeout();
|
||||
|
||||
protected:
|
||||
void startTimeoutTimer();
|
||||
void stopTimeoutTimer();
|
||||
|
||||
virtual void setBytesChanged(quint64 receivedBytes, quint64 sentBytes);
|
||||
virtual void setConnectionState(VpnProtocol::ConnectionState state);
|
||||
|
||||
Communicator* m_communicator;
|
||||
ConnectionState m_connectionState;
|
||||
QTimer* m_timeoutTimer;
|
||||
};
|
||||
|
||||
#endif // VPNPROTOCOL_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue