Refactoring
This commit is contained in:
parent
13f9764853
commit
5eede71667
21 changed files with 566 additions and 220 deletions
|
@ -1,6 +1,11 @@
|
|||
#include <QDebug>
|
||||
#include <QFile>
|
||||
|
||||
#include "openvpnprotocol.h"
|
||||
#include <core/openvpnconfigurator.h>
|
||||
#include <core/servercontroller.h>
|
||||
|
||||
#include "protocols/openvpnprotocol.h"
|
||||
#include "utils.h"
|
||||
#include "vpnconnection.h"
|
||||
|
||||
VpnConnection::VpnConnection(QObject* parent) : QObject(parent)
|
||||
|
@ -8,11 +13,6 @@ VpnConnection::VpnConnection(QObject* parent) : QObject(parent)
|
|||
VpnProtocol::initializeCommunicator(parent);
|
||||
}
|
||||
|
||||
VpnConnection::~VpnConnection()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void VpnConnection::onBytesChanged(quint64 receivedBytes, quint64 sentBytes)
|
||||
{
|
||||
emit bytesChanged(receivedBytes, sentBytes);
|
||||
|
@ -23,28 +23,56 @@ void VpnConnection::onConnectionStateChanged(VpnProtocol::ConnectionState state)
|
|||
emit connectionStateChanged(state);
|
||||
}
|
||||
|
||||
QString VpnConnection::lastError() const
|
||||
ErrorCode VpnConnection::lastError() const
|
||||
{
|
||||
if (!m_vpnProtocol.data()) {
|
||||
return "Unnown protocol";
|
||||
return ErrorCode::InternalError;
|
||||
}
|
||||
|
||||
return m_vpnProtocol.data()->lastError();
|
||||
}
|
||||
|
||||
bool VpnConnection::connectToVpn(Protocol protocol)
|
||||
ErrorCode VpnConnection::requestVpnConfig(const ServerCredentials &credentials, Protocol protocol)
|
||||
{
|
||||
ErrorCode errorCode = ErrorCode::NoError;
|
||||
if (protocol == Protocol::OpenVpn) {
|
||||
QString configData = OpenVpnConfigurator::genOpenVpnConfig(credentials, &errorCode);
|
||||
if (errorCode) {
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
QFile file(Utils::defaultVpnConfigFileName());
|
||||
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)){
|
||||
QTextStream stream(&file);
|
||||
stream << configData << endl;
|
||||
return ErrorCode::NoError;
|
||||
}
|
||||
|
||||
return ErrorCode::FailedToSaveConfigData;
|
||||
}
|
||||
else if (protocol == Protocol::ShadowSocks) {
|
||||
// Request OpenVPN config and ShadowSocks
|
||||
return ErrorCode::NotImplementedError;
|
||||
}
|
||||
return ErrorCode::NotImplementedError;
|
||||
|
||||
}
|
||||
|
||||
ErrorCode VpnConnection::connectToVpn(const ServerCredentials &credentials, Protocol protocol)
|
||||
{
|
||||
// TODO: Try protocols one by one in case of Protocol::Any
|
||||
// TODO: Implement some behavior in case if connection not stable
|
||||
qDebug() << "Connect to VPN";
|
||||
|
||||
switch (protocol) {
|
||||
case Protocol::OpenVpn:
|
||||
if (protocol == Protocol::Any || protocol == Protocol::OpenVpn) {
|
||||
ErrorCode e = requestVpnConfig(credentials, Protocol::OpenVpn);
|
||||
if (e) {
|
||||
return e;
|
||||
}
|
||||
m_vpnProtocol.reset(new OpenVpnProtocol());
|
||||
break;
|
||||
;
|
||||
default:
|
||||
// TODO, add later
|
||||
return false;
|
||||
;
|
||||
}
|
||||
else if (protocol == Protocol::ShadowSocks) {
|
||||
return ErrorCode::NotImplementedError;
|
||||
}
|
||||
|
||||
connect(m_vpnProtocol.data(), SIGNAL(connectionStateChanged(VpnProtocol::ConnectionState)), this, SLOT(onConnectionStateChanged(VpnProtocol::ConnectionState)));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue