Merge branch 'dev' into wireguard_embedded
This commit is contained in:
commit
ba8755a6d4
150 changed files with 8320 additions and 8490 deletions
346
client/protocols/android_vpnprotocol.cpp
Normal file
346
client/protocols/android_vpnprotocol.cpp
Normal file
|
|
@ -0,0 +1,346 @@
|
|||
#include <QAndroidBinder>
|
||||
#include <QAndroidIntent>
|
||||
#include <QAndroidJniEnvironment>
|
||||
#include <QAndroidJniObject>
|
||||
#include <QAndroidParcel>
|
||||
#include <QAndroidServiceConnection>
|
||||
#include <QDebug>
|
||||
#include <QHostAddress>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QRandomGenerator>
|
||||
#include <QTextCodec>
|
||||
#include <QTimer>
|
||||
#include <QtAndroid>
|
||||
|
||||
#include "android_vpnprotocol.h"
|
||||
#include "core/errorstrings.h"
|
||||
|
||||
// Binder Codes for VPNServiceBinder
|
||||
// See also - VPNServiceBinder.kt
|
||||
// Actions that are Requestable
|
||||
const int ACTION_ACTIVATE = 1;
|
||||
const int ACTION_DEACTIVATE = 2;
|
||||
const int ACTION_REGISTERLISTENER = 3;
|
||||
const int ACTION_REQUEST_STATISTIC = 4;
|
||||
const int ACTION_REQUEST_GET_LOG = 5;
|
||||
const int ACTION_REQUEST_CLEANUP_LOG = 6;
|
||||
const int ACTION_RESUME_ACTIVATE = 7;
|
||||
const int ACTION_SET_NOTIFICATION_TEXT = 8;
|
||||
const int ACTION_SET_NOTIFICATION_FALLBACK = 9;
|
||||
|
||||
// Event Types that will be Dispatched after registration
|
||||
const int EVENT_INIT = 0;
|
||||
const int EVENT_CONNECTED = 1;
|
||||
const int EVENT_DISCONNECTED = 2;
|
||||
const int EVENT_STATISTIC_UPDATE = 3;
|
||||
const int EVENT_BACKEND_LOGS = 4;
|
||||
const int EVENT_ACTIVATION_ERROR = 5;
|
||||
|
||||
namespace {
|
||||
AndroidVpnProtocol* s_instance = nullptr;
|
||||
|
||||
constexpr auto PERMISSIONHELPER_CLASS =
|
||||
"org/amnezia/vpn/qt/VPNPermissionHelper";
|
||||
|
||||
} // namespace
|
||||
|
||||
AndroidVpnProtocol::AndroidVpnProtocol(Protocol protocol, const QJsonObject &configuration, QObject* parent)
|
||||
: VpnProtocol(configuration, parent),
|
||||
m_protocol(protocol),
|
||||
m_binder(this)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
AndroidVpnProtocol* AndroidVpnProtocol::instance() {
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
void AndroidVpnProtocol::initialize()
|
||||
{
|
||||
qDebug() << "Initializing";
|
||||
|
||||
// Hook in the native implementation for startActivityForResult into the JNI
|
||||
JNINativeMethod methods[]{{"startActivityForResult",
|
||||
"(Landroid/content/Intent;)V",
|
||||
reinterpret_cast<void*>(startActivityForResult)}};
|
||||
QAndroidJniObject javaClass(PERMISSIONHELPER_CLASS);
|
||||
QAndroidJniEnvironment env;
|
||||
jclass objectClass = env->GetObjectClass(javaClass.object<jobject>());
|
||||
env->RegisterNatives(objectClass, methods,
|
||||
sizeof(methods) / sizeof(methods[0]));
|
||||
env->DeleteLocalRef(objectClass);
|
||||
|
||||
auto appContext = QtAndroid::androidActivity().callObjectMethod(
|
||||
"getApplicationContext", "()Landroid/content/Context;");
|
||||
|
||||
QAndroidJniObject::callStaticMethod<void>(
|
||||
"org/amnezia/vpn/VPNService", "startService",
|
||||
"(Landroid/content/Context;)V", appContext.object());
|
||||
|
||||
// Start the VPN Service (if not yet) and Bind to it
|
||||
QtAndroid::bindService(
|
||||
QAndroidIntent(appContext.object(), "org.amnezia.vpn.VPNService"),
|
||||
*this, QtAndroid::BindFlag::AutoCreate);
|
||||
}
|
||||
|
||||
ErrorCode AndroidVpnProtocol::start()
|
||||
{
|
||||
|
||||
qDebug() << "Prompting for VPN permission";
|
||||
auto appContext = QtAndroid::androidActivity().callObjectMethod(
|
||||
"getApplicationContext", "()Landroid/content/Context;");
|
||||
QAndroidJniObject::callStaticMethod<void>(
|
||||
PERMISSIONHELPER_CLASS, "startService", "(Landroid/content/Context;)V",
|
||||
appContext.object());
|
||||
|
||||
|
||||
// QJsonObject jServer;
|
||||
// jServer["ipv4AddrIn"] = server.ipv4AddrIn();
|
||||
// jServer["ipv4Gateway"] = server.ipv4Gateway();
|
||||
// jServer["ipv6AddrIn"] = server.ipv6AddrIn();
|
||||
// jServer["ipv6Gateway"] = server.ipv6Gateway();
|
||||
// jServer["publicKey"] = server.publicKey();
|
||||
// jServer["port"] = (int)server.choosePort();
|
||||
|
||||
// QJsonArray allowedIPs;
|
||||
// foreach (auto item, allowedIPAddressRanges) {
|
||||
// QJsonValue val;
|
||||
// val = item.toString();
|
||||
// allowedIPs.append(val);
|
||||
// }
|
||||
|
||||
// QJsonArray excludedApps;
|
||||
// foreach (auto appID, vpnDisabledApps) {
|
||||
// excludedApps.append(QJsonValue(appID));
|
||||
// }
|
||||
|
||||
// QJsonObject args;
|
||||
// args["device"] = jDevice;
|
||||
// args["keys"] = jKeys;
|
||||
// args["server"] = jServer;
|
||||
// args["reason"] = (int)reason;
|
||||
// args["allowedIPs"] = allowedIPs;
|
||||
// args["excludedApps"] = excludedApps;
|
||||
// args["dns"] = dns.toString();
|
||||
|
||||
QAndroidParcel sendData;
|
||||
sendData.writeData(QJsonDocument(m_rawConfig).toJson());
|
||||
m_serviceBinder.transact(ACTION_ACTIVATE, sendData, nullptr);
|
||||
return NoError;
|
||||
}
|
||||
|
||||
// Activates the tunnel that is currently set
|
||||
// in the VPN Service
|
||||
void AndroidVpnProtocol::resume_start() {
|
||||
QAndroidParcel nullData;
|
||||
m_serviceBinder.transact(ACTION_RESUME_ACTIVATE, nullData, nullptr);
|
||||
}
|
||||
|
||||
void AndroidVpnProtocol::stop() {
|
||||
qDebug() << "deactivation";
|
||||
|
||||
// if (reason != ReasonNone) {
|
||||
// // Just show that we're disconnected
|
||||
// // we're doing the actual disconnect once
|
||||
// // the vpn-service has the new server ready in Action->Activate
|
||||
// emit disconnected();
|
||||
// logger.warning() << "deactivation skipped for Switching";
|
||||
// return;
|
||||
// }
|
||||
|
||||
QAndroidParcel nullData;
|
||||
m_serviceBinder.transact(ACTION_DEACTIVATE, nullData, nullptr);
|
||||
}
|
||||
|
||||
/*
|
||||
* Sets the current notification text that is shown
|
||||
*/
|
||||
void AndroidVpnProtocol::setNotificationText(const QString& title,
|
||||
const QString& message,
|
||||
int timerSec) {
|
||||
QJsonObject args;
|
||||
args["title"] = title;
|
||||
args["message"] = message;
|
||||
args["sec"] = timerSec;
|
||||
QJsonDocument doc(args);
|
||||
QAndroidParcel data;
|
||||
data.writeData(doc.toJson());
|
||||
m_serviceBinder.transact(ACTION_SET_NOTIFICATION_TEXT, data, nullptr);
|
||||
}
|
||||
|
||||
/*
|
||||
* Sets fallback Notification text that should be shown in case the VPN
|
||||
* switches into the Connected state without the app open
|
||||
* e.g via always-on vpn
|
||||
*/
|
||||
void AndroidVpnProtocol::setFallbackConnectedNotification() {
|
||||
QJsonObject args;
|
||||
args["title"] = qtTrId("vpn.main.productName");
|
||||
//% "Ready for you to connect"
|
||||
//: Refers to the app - which is currently running the background and waiting
|
||||
args["message"] = qtTrId("vpn.android.notification.isIDLE");
|
||||
QJsonDocument doc(args);
|
||||
QAndroidParcel data;
|
||||
data.writeData(doc.toJson());
|
||||
m_serviceBinder.transact(ACTION_SET_NOTIFICATION_FALLBACK, data, nullptr);
|
||||
}
|
||||
|
||||
void AndroidVpnProtocol::checkStatus() {
|
||||
qDebug() << "check status";
|
||||
|
||||
QAndroidParcel nullParcel;
|
||||
m_serviceBinder.transact(ACTION_REQUEST_STATISTIC, nullParcel, nullptr);
|
||||
}
|
||||
|
||||
void AndroidVpnProtocol::getBackendLogs(std::function<void(const QString&)>&& a_callback) {
|
||||
qDebug() << "get logs";
|
||||
|
||||
m_logCallback = std::move(a_callback);
|
||||
QAndroidParcel nullData, replyData;
|
||||
m_serviceBinder.transact(ACTION_REQUEST_GET_LOG, nullData, &replyData);
|
||||
}
|
||||
|
||||
void AndroidVpnProtocol::cleanupBackendLogs() {
|
||||
qDebug() << "cleanup logs";
|
||||
|
||||
QAndroidParcel nullParcel;
|
||||
m_serviceBinder.transact(ACTION_REQUEST_CLEANUP_LOG, nullParcel, nullptr);
|
||||
}
|
||||
|
||||
void AndroidVpnProtocol::onServiceConnected(
|
||||
const QString& name, const QAndroidBinder& serviceBinder) {
|
||||
qDebug() << "Server connected";
|
||||
|
||||
Q_UNUSED(name);
|
||||
|
||||
m_serviceBinder = serviceBinder;
|
||||
|
||||
// Send the Service our Binder to recive incoming Events
|
||||
QAndroidParcel binderParcel;
|
||||
binderParcel.writeBinder(m_binder);
|
||||
m_serviceBinder.transact(ACTION_REGISTERLISTENER, binderParcel, nullptr);
|
||||
}
|
||||
|
||||
void AndroidVpnProtocol::onServiceDisconnected(const QString& name) {
|
||||
qDebug() << "Server disconnected";
|
||||
m_serviceConnected = false;
|
||||
Q_UNUSED(name);
|
||||
// TODO: Maybe restart? Or crash?
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief AndroidController::VPNBinder::onTransact
|
||||
* @param code the Event-Type we get From the VPNService See
|
||||
* @param data - Might contain UTF-8 JSON in case the Event has a payload
|
||||
* @param reply - always null
|
||||
* @param flags - unused
|
||||
* @return Returns true is the code was a valid Event Code
|
||||
*/
|
||||
bool AndroidVpnProtocol::VPNBinder::onTransact(int code,
|
||||
const QAndroidParcel& data,
|
||||
const QAndroidParcel& reply,
|
||||
QAndroidBinder::CallType flags) {
|
||||
Q_UNUSED(data);
|
||||
Q_UNUSED(reply);
|
||||
Q_UNUSED(flags);
|
||||
|
||||
QJsonDocument doc;
|
||||
QString buffer;
|
||||
switch (code) {
|
||||
case EVENT_INIT:
|
||||
qDebug() << "Transact: init";
|
||||
doc = QJsonDocument::fromJson(data.readData());
|
||||
emit m_controller->initialized(
|
||||
true, doc.object()["connected"].toBool(),
|
||||
QDateTime::fromMSecsSinceEpoch(
|
||||
doc.object()["time"].toVariant().toLongLong()));
|
||||
// Pass a localised version of the Fallback string for the Notification
|
||||
m_controller->setFallbackConnectedNotification();
|
||||
|
||||
break;
|
||||
case EVENT_CONNECTED:
|
||||
qDebug() << "Transact: connected";
|
||||
m_controller->setConnectionState(Connected);
|
||||
break;
|
||||
case EVENT_DISCONNECTED:
|
||||
qDebug() << "Transact: disconnected";
|
||||
m_controller->setConnectionState(Disconnected);
|
||||
break;
|
||||
case EVENT_STATISTIC_UPDATE:
|
||||
qDebug() << "Transact:: update";
|
||||
|
||||
// Data is here a JSON String
|
||||
doc = QJsonDocument::fromJson(data.readData());
|
||||
// TODO update counters
|
||||
// emit m_controller->statusUpdated(doc.object()["endpoint"].toString(),
|
||||
// doc.object()["deviceIpv4"].toString(),
|
||||
// doc.object()["totalTX"].toInt(),
|
||||
// doc.object()["totalRX"].toInt());
|
||||
break;
|
||||
case EVENT_BACKEND_LOGS:
|
||||
qDebug() << "Transact: backend logs";
|
||||
|
||||
buffer = readUTF8Parcel(data);
|
||||
if (m_controller->m_logCallback) {
|
||||
m_controller->m_logCallback(buffer);
|
||||
}
|
||||
break;
|
||||
case EVENT_ACTIVATION_ERROR:
|
||||
m_controller->setConnectionState(Error);
|
||||
default:
|
||||
qWarning() << "Transact: Invalid!";
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QString AndroidVpnProtocol::VPNBinder::readUTF8Parcel(QAndroidParcel data) {
|
||||
// 106 is the Code for UTF-8
|
||||
return QTextCodec::codecForMib(106)->toUnicode(data.readData());
|
||||
}
|
||||
|
||||
const int ACTIVITY_RESULT_OK = 0xffffffff;
|
||||
/**
|
||||
* @brief Starts the Given intent in Context of the QTActivity
|
||||
* @param env
|
||||
* @param intent
|
||||
*/
|
||||
void AndroidVpnProtocol::startActivityForResult(JNIEnv *env, jobject, jobject intent)
|
||||
{
|
||||
qDebug() << "start activity";
|
||||
Q_UNUSED(env);
|
||||
QtAndroid::startActivity(intent, 1337,
|
||||
[](int receiverRequestCode, int resultCode,
|
||||
const QAndroidJniObject& data) {
|
||||
// Currently this function just used in
|
||||
// VPNService.kt::checkPersmissions. So the result
|
||||
// we're getting is if the User gave us the
|
||||
// Vpn.bind permission. In case of NO we should
|
||||
// abort.
|
||||
Q_UNUSED(receiverRequestCode);
|
||||
Q_UNUSED(data);
|
||||
|
||||
AndroidVpnProtocol* controller =
|
||||
AndroidVpnProtocol::instance();
|
||||
if (!controller) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (resultCode == ACTIVITY_RESULT_OK) {
|
||||
qDebug() << "VPN PROMPT RESULT - Accepted";
|
||||
controller->resume_start();
|
||||
return;
|
||||
}
|
||||
// If the request got rejected abort the current
|
||||
// connection.
|
||||
qWarning() << "VPN PROMPT RESULT - Rejected";
|
||||
controller->setConnectionState(Disconnected);
|
||||
|
||||
});
|
||||
return;
|
||||
}
|
||||
82
client/protocols/android_vpnprotocol.h
Normal file
82
client/protocols/android_vpnprotocol.h
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
#ifndef ANDROID_VPNPROTOCOL_H
|
||||
#define ANDROID_VPNPROTOCOL_H
|
||||
|
||||
#include <QAndroidBinder>
|
||||
#include <QAndroidServiceConnection>
|
||||
|
||||
#include "vpnprotocol.h"
|
||||
#include "protocols/protocols_defs.h"
|
||||
|
||||
using namespace amnezia;
|
||||
|
||||
|
||||
|
||||
class AndroidVpnProtocol : public VpnProtocol,
|
||||
public QAndroidServiceConnection
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AndroidVpnProtocol(Protocol protocol, const QJsonObject& configuration, QObject* parent = nullptr);
|
||||
static AndroidVpnProtocol* instance();
|
||||
|
||||
virtual ~AndroidVpnProtocol() override = default;
|
||||
|
||||
void initialize();
|
||||
|
||||
virtual ErrorCode start() override;
|
||||
virtual void stop() override;
|
||||
|
||||
void resume_start();
|
||||
|
||||
void checkStatus();
|
||||
|
||||
void setNotificationText(const QString& title, const QString& message,
|
||||
int timerSec);
|
||||
void setFallbackConnectedNotification();
|
||||
|
||||
void getBackendLogs(std::function<void(const QString&)>&& callback);
|
||||
|
||||
void cleanupBackendLogs();
|
||||
|
||||
// from QAndroidServiceConnection
|
||||
void onServiceConnected(const QString& name,
|
||||
const QAndroidBinder& serviceBinder) override;
|
||||
void onServiceDisconnected(const QString& name) override;
|
||||
|
||||
signals:
|
||||
|
||||
|
||||
protected slots:
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
private:
|
||||
Protocol m_protocol;
|
||||
|
||||
bool m_serviceConnected = false;
|
||||
std::function<void(const QString&)> m_logCallback;
|
||||
|
||||
QAndroidBinder m_serviceBinder;
|
||||
class VPNBinder : public QAndroidBinder {
|
||||
public:
|
||||
VPNBinder(AndroidVpnProtocol* controller) : m_controller(controller) {}
|
||||
|
||||
bool onTransact(int code, const QAndroidParcel& data,
|
||||
const QAndroidParcel& reply,
|
||||
QAndroidBinder::CallType flags) override;
|
||||
|
||||
QString readUTF8Parcel(QAndroidParcel data);
|
||||
|
||||
private:
|
||||
AndroidVpnProtocol* m_controller = nullptr;
|
||||
};
|
||||
|
||||
VPNBinder m_binder;
|
||||
|
||||
static void startActivityForResult(JNIEnv* env, jobject /*thiz*/,
|
||||
jobject intent);
|
||||
};
|
||||
|
||||
#endif // ANDROID_VPNPROTOCOL_H
|
||||
218
client/protocols/ikev2_vpn_protocol.cpp
Normal file
218
client/protocols/ikev2_vpn_protocol.cpp
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
#include <QCoreApplication>
|
||||
#include <QFileInfo>
|
||||
#include <QProcess>
|
||||
#include <QRegularExpression>
|
||||
#include <QTcpSocket>
|
||||
#include <QThread>
|
||||
|
||||
#include "debug.h"
|
||||
#include "ikev2_vpn_protocol.h"
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
Ikev2Protocol::Ikev2Protocol(const QJsonObject &configuration, QObject* parent) :
|
||||
VpnProtocol(configuration, parent)
|
||||
{
|
||||
//m_configFile.setFileTemplate(QDir::tempPath() + QDir::separator() + serviceName() + ".conf");
|
||||
readIkev2Configuration(configuration);
|
||||
}
|
||||
|
||||
Ikev2Protocol::~Ikev2Protocol()
|
||||
{
|
||||
qDebug() << "IpsecProtocol::~IpsecProtocol()";
|
||||
Ikev2Protocol::stop();
|
||||
QThread::msleep(200);
|
||||
}
|
||||
|
||||
void Ikev2Protocol::stop()
|
||||
{
|
||||
#ifdef Q_OS_WINDOWS
|
||||
{
|
||||
setConnectionState(Disconnecting);
|
||||
|
||||
auto disconnectProcess = new QProcess;
|
||||
|
||||
disconnectProcess->setProgram("rasdial");
|
||||
QString arguments = QString("\"%1\" /disconnect")
|
||||
.arg(tunnelName());
|
||||
disconnectProcess->setNativeArguments(arguments);
|
||||
|
||||
// connect(connectProcess, &QProcess::readyRead, [connectProcess]() {
|
||||
// qDebug().noquote() << "connectProcess readyRead" << connectProcess->readAll();
|
||||
// });
|
||||
|
||||
disconnectProcess->start();
|
||||
disconnectProcess->waitForFinished(5000);
|
||||
setConnectionState(Disconnected);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void Ikev2Protocol::readIkev2Configuration(const QJsonObject &configuration)
|
||||
{
|
||||
m_config = configuration.value(ProtocolProps::key_proto_config_data(Protocol::Ikev2)).toObject();
|
||||
}
|
||||
|
||||
ErrorCode Ikev2Protocol::start()
|
||||
{
|
||||
#ifdef Q_OS_WINDOWS
|
||||
QByteArray cert = QByteArray::fromBase64(m_config[config_key::cert].toString().toUtf8());
|
||||
setConnectionState(ConnectionState::Connecting);
|
||||
|
||||
QTemporaryFile certFile;
|
||||
certFile.setAutoRemove(false);
|
||||
certFile.open();
|
||||
certFile.write(cert);
|
||||
certFile.close();
|
||||
|
||||
|
||||
{
|
||||
auto certInstallProcess = IpcClient::CreatePrivilegedProcess();
|
||||
|
||||
if (!certInstallProcess) {
|
||||
setLastError(ErrorCode::AmneziaServiceConnectionFailed);
|
||||
return ErrorCode::AmneziaServiceConnectionFailed;
|
||||
}
|
||||
|
||||
certInstallProcess->waitForSource(1000);
|
||||
if (!certInstallProcess->isInitialized()) {
|
||||
qWarning() << "IpcProcess replica is not connected!";
|
||||
setLastError(ErrorCode::AmneziaServiceConnectionFailed);
|
||||
return ErrorCode::AmneziaServiceConnectionFailed;
|
||||
}
|
||||
certInstallProcess->setProgram("certutil");
|
||||
QStringList arguments({"-f" , "-importpfx",
|
||||
"-p", m_config[config_key::password].toString(),
|
||||
certFile.fileName(), "NoExport"
|
||||
});
|
||||
certInstallProcess->setArguments(arguments);
|
||||
|
||||
// qDebug() << arguments.join(" ");
|
||||
// connect(certInstallProcess.data(), &IpcProcessInterfaceReplica::errorOccurred, [certInstallProcess](QProcess::ProcessError error) {
|
||||
// qDebug() << "IpcProcessInterfaceReplica errorOccurred" << error;
|
||||
// });
|
||||
|
||||
// connect(certInstallProcess.data(), &IpcProcessInterfaceReplica::stateChanged, [certInstallProcess](QProcess::ProcessState newState) {
|
||||
// qDebug() << "IpcProcessInterfaceReplica stateChanged" << newState;
|
||||
// });
|
||||
|
||||
// connect(certInstallProcess.data(), &IpcProcessInterfaceReplica::readyRead, [certInstallProcess]() {
|
||||
// auto req = certInstallProcess->readAll();
|
||||
// req.waitForFinished();
|
||||
// qDebug() << "IpcProcessInterfaceReplica readyRead" << req.returnValue();
|
||||
// });
|
||||
|
||||
|
||||
certInstallProcess->start();
|
||||
}
|
||||
|
||||
{
|
||||
auto adapterRemoveProcess = new QProcess;
|
||||
|
||||
adapterRemoveProcess->setProgram("powershell");
|
||||
QString arguments = QString("-command \"Remove-VpnConnection -Name '%1' -Force\"").arg(tunnelName());
|
||||
adapterRemoveProcess->setNativeArguments(arguments);
|
||||
|
||||
adapterRemoveProcess->start();
|
||||
adapterRemoveProcess->waitForFinished(5000);
|
||||
}
|
||||
|
||||
{
|
||||
auto adapterInstallProcess = new QProcess;
|
||||
|
||||
adapterInstallProcess->setProgram("powershell");
|
||||
QString arguments = QString("-command \"Add-VpnConnection "
|
||||
"-ServerAddress '%1' "
|
||||
"-Name '%2' "
|
||||
"-TunnelType IKEv2 "
|
||||
"-AuthenticationMethod MachineCertificate "
|
||||
"-EncryptionLevel Required "
|
||||
"-PassThru\"")
|
||||
.arg(m_config[config_key::hostName].toString())
|
||||
.arg(tunnelName());
|
||||
adapterInstallProcess->setNativeArguments(arguments);
|
||||
// connect(adapterInstallProcess, &QProcess::readyRead, [adapterInstallProcess]() {
|
||||
// qDebug().noquote() << "adapterInstallProcess readyRead" << adapterInstallProcess->readAll();
|
||||
// });
|
||||
|
||||
adapterInstallProcess->start();
|
||||
adapterInstallProcess->waitForFinished(5000);
|
||||
}
|
||||
|
||||
{
|
||||
auto adapterConfigProcess = new QProcess;
|
||||
|
||||
adapterConfigProcess->setProgram("powershell");
|
||||
QString arguments = QString("-command \"Set-VpnConnectionIPsecConfiguration\ "
|
||||
"-ConnectionName '%1'\ "
|
||||
"-AuthenticationTransformConstants GCMAES128 "
|
||||
"-CipherTransformConstants GCMAES128 "
|
||||
"-EncryptionMethod AES256 "
|
||||
"-IntegrityCheckMethod SHA256 "
|
||||
"-PfsGroup None "
|
||||
"-DHGroup Group14 "
|
||||
"-PassThru -Force\"")
|
||||
.arg(tunnelName());
|
||||
adapterConfigProcess->setNativeArguments(arguments);
|
||||
|
||||
// connect(adapterConfigProcess, &QProcess::readyRead, [adapterConfigProcess]() {
|
||||
// qDebug().noquote() << "adapterConfigProcess readyRead" << adapterConfigProcess->readAll();
|
||||
// });
|
||||
|
||||
adapterConfigProcess->start();
|
||||
adapterConfigProcess->waitForFinished(5000);
|
||||
}
|
||||
|
||||
{
|
||||
// char buf[RASBUFFER]= {0};
|
||||
// DWORD err = 0;
|
||||
// RASDIALPARAMSA *param = (RASDIALPARAMSA *)buf;
|
||||
// param->dwSize = 1064;
|
||||
// strcpy(param->szEntryName, tunnelName().toStdString().c_str());
|
||||
// err = RasDialA(NULL, NULL, param, 0, (LPVOID)rasCallback, &g_h);
|
||||
// qDebug() << "Ikev2Protocol::start() ret" << err;
|
||||
|
||||
|
||||
auto connectProcess = new QProcess;
|
||||
|
||||
connectProcess->setProgram("rasdial");
|
||||
QString arguments = QString("\"%1\"")
|
||||
.arg(tunnelName());
|
||||
connectProcess->setNativeArguments(arguments);
|
||||
|
||||
connect(connectProcess, &QProcess::readyRead, [connectProcess]() {
|
||||
qDebug().noquote() << "connectProcess readyRead" << connectProcess->readAll();
|
||||
});
|
||||
|
||||
connectProcess->start();
|
||||
connectProcess->waitForFinished(5000);
|
||||
}
|
||||
|
||||
setConnectionState(Connected);
|
||||
return ErrorCode::NoError;
|
||||
|
||||
#endif
|
||||
|
||||
return ErrorCode::NoError;
|
||||
}
|
||||
|
||||
#ifdef Q_OS_WINDOWS
|
||||
DWORD CALLBACK rasCallback(UINT msg, RASCONNSTATE rascs, DWORD err)
|
||||
{
|
||||
if(err != 0) {
|
||||
printf("Error: %d\n", err);
|
||||
fflush(stdout);
|
||||
//g_done = 1;
|
||||
return 0; // stop the connection.
|
||||
} else {
|
||||
//printf("%s\n", rasState(rascs));
|
||||
fflush(stdout);
|
||||
if(rascs == RASCS_Connected) {
|
||||
printf("Success: Connected\n");
|
||||
fflush(stdout);
|
||||
//g_done = 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
60
client/protocols/ikev2_vpn_protocol.h
Normal file
60
client/protocols/ikev2_vpn_protocol.h
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
#ifndef IPSEC_PROTOCOL_H
|
||||
#define IPSEC_PROTOCOL_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QProcess>
|
||||
#include <QString>
|
||||
#include <QTemporaryFile>
|
||||
#include <QTimer>
|
||||
|
||||
#include "vpnprotocol.h"
|
||||
#include "core/ipcclient.h"
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <windows.h>
|
||||
#include <ras.h>
|
||||
#include <raserror.h>
|
||||
#include <shlwapi.h>
|
||||
|
||||
#pragma comment(lib, "shlwapi.lib")
|
||||
#pragma comment(lib, "rasapi32.lib")
|
||||
|
||||
#define RASBUFFER 0x1000
|
||||
#define RASMAXENUM 0x100
|
||||
#endif
|
||||
|
||||
class Ikev2Protocol : public VpnProtocol
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Ikev2Protocol(const QJsonObject& configuration, QObject* parent = nullptr);
|
||||
virtual ~Ikev2Protocol() override;
|
||||
|
||||
ErrorCode start() override;
|
||||
void stop() override;
|
||||
|
||||
static QString tunnelName() { return "AmneziaVPN IKEv2"; }
|
||||
|
||||
private:
|
||||
void readIkev2Configuration(const QJsonObject &configuration);
|
||||
|
||||
|
||||
private:
|
||||
QJsonObject m_config;
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
HRASCONN g_h;
|
||||
int g_done = 0;
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
DWORD CALLBACK rasCallback(UINT msg, RASCONNSTATE rascs, DWORD err);
|
||||
#endif
|
||||
|
||||
#endif // IPSEC_PROTOCOL_H
|
||||
|
|
@ -44,7 +44,8 @@ ErrorCode OpenVpnOverCloakProtocol::start()
|
|||
<< "-p" << m_cloakConfig.value(config_key::port).toString(amnezia::protocols::cloak::defaultPort)
|
||||
<< "-l" << amnezia::protocols::openvpn::defaultPort;
|
||||
|
||||
if (m_cloakConfig.value(config_key::transport_proto).toString() == protocols::UDP) {
|
||||
ProtocolEnumNS::TransportProto tp = ProtocolProps::transportProtoFromString(m_cloakConfig.value(config_key::transport_proto).toString());
|
||||
if (tp == ProtocolEnumNS::TransportProto::Udp) {
|
||||
args << "-u";
|
||||
}
|
||||
|
||||
|
|
@ -112,5 +113,5 @@ QString OpenVpnOverCloakProtocol::cloakExecPath()
|
|||
|
||||
void OpenVpnOverCloakProtocol::readCloakConfiguration(const QJsonObject &configuration)
|
||||
{
|
||||
m_cloakConfig = configuration.value(config::key_cloak_config_data).toObject();
|
||||
m_cloakConfig = configuration.value(ProtocolProps::key_proto_config_data(Protocol::Cloak)).toObject();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ void OpenVpnProtocol::stop()
|
|||
}
|
||||
}
|
||||
|
||||
ErrorCode OpenVpnProtocol::checkAndSetupTapDriver()
|
||||
ErrorCode OpenVpnProtocol::prepare()
|
||||
{
|
||||
if (!IpcClient::Interface()) {
|
||||
return ErrorCode::AmneziaServiceConnectionFailed;
|
||||
|
|
@ -86,24 +86,16 @@ void OpenVpnProtocol::killOpenVpnProcess()
|
|||
|
||||
void OpenVpnProtocol::readOpenVpnConfiguration(const QJsonObject &configuration)
|
||||
{
|
||||
if (configuration.contains(config::key_openvpn_config_data)) {
|
||||
if (configuration.contains(ProtocolProps::key_proto_config_data(Protocol::OpenVpn))) {
|
||||
QJsonObject jConfig = configuration.value(ProtocolProps::key_proto_config_data(Protocol::OpenVpn)).toObject();
|
||||
|
||||
m_configFile.open();
|
||||
m_configFile.write(configuration.value(config::key_openvpn_config_data).toString().toUtf8());
|
||||
m_configFile.write(jConfig.value(config_key::config).toString().toUtf8());
|
||||
m_configFile.close();
|
||||
m_configFileName = m_configFile.fileName();
|
||||
|
||||
qDebug().noquote() << QString("Set config data") << m_configFileName;
|
||||
}
|
||||
else if (configuration.contains(config::key_openvpn_config_path)) {
|
||||
m_configFileName = configuration.value(config::key_openvpn_config_path).toString();
|
||||
QFileInfo file(m_configFileName);
|
||||
|
||||
if (file.fileName().isEmpty()) {
|
||||
m_configFileName = defaultConfigFileName();
|
||||
}
|
||||
|
||||
qDebug().noquote() << QString("Set config file: '%1'").arg(configPath());
|
||||
}
|
||||
}
|
||||
|
||||
bool OpenVpnProtocol::openVpnProcessIsRunning() const
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ public:
|
|||
ErrorCode start() override;
|
||||
void stop() override;
|
||||
|
||||
ErrorCode checkAndSetupTapDriver();
|
||||
ErrorCode prepare() override;
|
||||
static QString defaultConfigFileName();
|
||||
static QString defaultConfigPath();
|
||||
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ TransportProto ProtocolProps::transportProtoFromString(QString p)
|
|||
QMetaEnum metaEnum = QMetaEnum::fromType<TransportProto>();
|
||||
for (int i = 0; i < metaEnum.keyCount(); ++i) {
|
||||
TransportProto tp = static_cast<TransportProto>(i);
|
||||
if (p.toLower() == transportProtoToString(tp)) return tp;
|
||||
if (p.toLower() == transportProtoToString(tp).toLower()) return tp;
|
||||
}
|
||||
return TransportProto::Udp;
|
||||
}
|
||||
|
|
@ -52,12 +52,17 @@ QString ProtocolProps::transportProtoToString(TransportProto proto, Protocol p)
|
|||
{
|
||||
QMetaEnum metaEnum = QMetaEnum::fromType<TransportProto>();
|
||||
QString protoKey = metaEnum.valueToKey(static_cast<int>(proto));
|
||||
if (p == Protocol::OpenVpn){
|
||||
return protoKey.toUpper();
|
||||
}
|
||||
else {
|
||||
return protoKey.toLower();
|
||||
}
|
||||
return protoKey.toLower();
|
||||
|
||||
// if (p == Protocol::OpenVpn){
|
||||
// return protoKey.toLower();
|
||||
// }
|
||||
// else if (p == Protocol::ShadowSocks) {
|
||||
// return protoKey.toUpper();
|
||||
// }
|
||||
// else {
|
||||
// return protoKey.toLower();
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -103,6 +108,9 @@ int ProtocolProps::defaultPort(Protocol p)
|
|||
case Protocol::Cloak : return 443;
|
||||
case Protocol::ShadowSocks : return 6789;
|
||||
case Protocol::WireGuard : return 51820;
|
||||
case Protocol::Ikev2 : return -1;
|
||||
case Protocol::L2tp : return -1;
|
||||
|
||||
case Protocol::TorWebSite : return -1;
|
||||
case Protocol::Dns : return 53;
|
||||
case Protocol::FileShare : return 139;
|
||||
|
|
@ -119,7 +127,11 @@ bool ProtocolProps::defaultPortChangeable(Protocol p)
|
|||
case Protocol::Cloak : return true;
|
||||
case Protocol::ShadowSocks : return true;
|
||||
case Protocol::WireGuard : return true;
|
||||
case Protocol::TorWebSite : return true;
|
||||
case Protocol::Ikev2 : return false;
|
||||
case Protocol::L2tp : return false;
|
||||
|
||||
|
||||
case Protocol::TorWebSite : return true;
|
||||
case Protocol::Dns : return false;
|
||||
case Protocol::FileShare : return false;
|
||||
default: return -1;
|
||||
|
|
@ -134,12 +146,13 @@ TransportProto ProtocolProps::defaultTransportProto(Protocol p)
|
|||
case Protocol::Cloak : return TransportProto::Tcp;
|
||||
case Protocol::ShadowSocks : return TransportProto::Tcp;
|
||||
case Protocol::WireGuard : return TransportProto::Udp;
|
||||
case Protocol::Ikev2 : return TransportProto::Udp;
|
||||
case Protocol::L2tp : return TransportProto::Udp;
|
||||
// non-vpn
|
||||
case Protocol::TorWebSite : return TransportProto::Tcp;
|
||||
case Protocol::Dns : return TransportProto::Udp;
|
||||
case Protocol::FileShare : return TransportProto::Udp;
|
||||
case Protocol::Sftp : return TransportProto::Tcp;
|
||||
default: return TransportProto::Udp;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -151,6 +164,8 @@ bool ProtocolProps::defaultTransportProtoChangeable(Protocol p)
|
|||
case Protocol::Cloak : return false;
|
||||
case Protocol::ShadowSocks : return false;
|
||||
case Protocol::WireGuard : return false;
|
||||
case Protocol::Ikev2 : return false;
|
||||
case Protocol::L2tp : return false;
|
||||
// non-vpn
|
||||
case Protocol::TorWebSite : return false;
|
||||
case Protocol::Dns : return false;
|
||||
|
|
@ -159,3 +174,13 @@ bool ProtocolProps::defaultTransportProtoChangeable(Protocol p)
|
|||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
QString ProtocolProps::key_proto_config_data(Protocol p)
|
||||
{
|
||||
return protoToString(p) + "_config_data";
|
||||
}
|
||||
|
||||
QString ProtocolProps::key_proto_config_path(Protocol p)
|
||||
{
|
||||
return protoToString(p) + "_config_path";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,14 +16,16 @@ constexpr char port[] = "port";
|
|||
constexpr char local_port[] = "local_port";
|
||||
|
||||
constexpr char description[] = "description";
|
||||
constexpr char cert[] = "cert";
|
||||
constexpr char config[] = "config";
|
||||
|
||||
|
||||
constexpr char containers[] = "containers";
|
||||
constexpr char container[] = "container";
|
||||
constexpr char defaultContainer[] = "defaultContainer";
|
||||
|
||||
constexpr char protocol[] = "protocol";
|
||||
constexpr char protocols[] = "protocols";
|
||||
//constexpr char protocol[] = "protocol";
|
||||
|
||||
constexpr char remote[] = "remote";
|
||||
constexpr char transport_proto[] = "transport_proto";
|
||||
|
|
@ -46,10 +48,6 @@ constexpr char last_config[] = "last_config";
|
|||
namespace protocols {
|
||||
|
||||
|
||||
|
||||
constexpr char UDP[] = "udp"; // case sens
|
||||
constexpr char TCP[] = "tcp";
|
||||
|
||||
namespace openvpn {
|
||||
constexpr char defaultSubnetAddress[] = "10.8.0.0";
|
||||
constexpr char defaultSubnetMask[] = "255.255.255.0";
|
||||
|
|
@ -122,6 +120,8 @@ enum Protocol {
|
|||
ShadowSocks,
|
||||
Cloak,
|
||||
WireGuard,
|
||||
Ikev2,
|
||||
L2tp,
|
||||
|
||||
// non-vpn
|
||||
TorWebSite,
|
||||
|
|
@ -166,6 +166,10 @@ public:
|
|||
Q_INVOKABLE static TransportProto defaultTransportProto(Protocol p);
|
||||
Q_INVOKABLE static bool defaultTransportProtoChangeable(Protocol p);
|
||||
|
||||
|
||||
Q_INVOKABLE static QString key_proto_config_data(Protocol p);
|
||||
Q_INVOKABLE static QString key_proto_config_path(Protocol p);
|
||||
|
||||
};
|
||||
|
||||
static void declareQmlProtocolEnum() {
|
||||
|
|
|
|||
|
|
@ -112,5 +112,5 @@ QString ShadowSocksVpnProtocol::shadowSocksExecPath()
|
|||
|
||||
void ShadowSocksVpnProtocol::readShadowSocksConfiguration(const QJsonObject &configuration)
|
||||
{
|
||||
m_shadowSocksConfig = configuration.value(config::key_shadowsocks_config_data).toObject();
|
||||
m_shadowSocksConfig = configuration.value(ProtocolProps::key_proto_config_data(Protocol::ShadowSocks)).toObject();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,13 @@
|
|||
|
||||
#include "vpnprotocol.h"
|
||||
#include "core/errorstrings.h"
|
||||
#include "containers/containers_defs.h"
|
||||
|
||||
#include "openvpnprotocol.h"
|
||||
#include "shadowsocksvpnprotocol.h"
|
||||
#include "openvpnovercloakprotocol.h"
|
||||
#include "wireguardprotocol.h"
|
||||
#include "ikev2_vpn_protocol.h"
|
||||
|
||||
|
||||
VpnProtocol::VpnProtocol(const QJsonObject &configuration, QObject* parent)
|
||||
: QObject(parent),
|
||||
|
|
@ -89,6 +95,19 @@ QString VpnProtocol::vpnGateway() const
|
|||
return m_vpnGateway;
|
||||
}
|
||||
|
||||
VpnProtocol *VpnProtocol::factory(DockerContainer container, const QJsonObject& configuration)
|
||||
{
|
||||
switch (container) {
|
||||
case DockerContainer::OpenVpn: return new OpenVpnProtocol(configuration);
|
||||
case DockerContainer::Cloak: return new OpenVpnOverCloakProtocol(configuration);
|
||||
case DockerContainer::ShadowSocks: return new ShadowSocksVpnProtocol(configuration);
|
||||
case DockerContainer::WireGuard: return new WireguardProtocol(configuration);
|
||||
case DockerContainer::Ipsec: return new Ikev2Protocol(configuration);
|
||||
|
||||
default: return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
QString VpnProtocol::routeGateway() const
|
||||
{
|
||||
return m_routeGateway;
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@
|
|||
#include <QJsonObject>
|
||||
|
||||
#include "core/defs.h"
|
||||
#include "containers/containers_defs.h"
|
||||
|
||||
using namespace amnezia;
|
||||
|
||||
class QTimer;
|
||||
|
|
@ -23,6 +25,7 @@ public:
|
|||
|
||||
static QString textConnectionState(ConnectionState connectionState);
|
||||
|
||||
virtual ErrorCode prepare() { return ErrorCode::NoError; }
|
||||
|
||||
virtual bool isConnected() const;
|
||||
virtual bool isDisconnected() const;
|
||||
|
|
@ -37,12 +40,21 @@ public:
|
|||
QString routeGateway() const;
|
||||
QString vpnGateway() const;
|
||||
|
||||
static VpnProtocol* factory(amnezia::DockerContainer container, const QJsonObject &configuration);
|
||||
|
||||
signals:
|
||||
void bytesChanged(quint64 receivedBytes, quint64 sentBytes);
|
||||
void connectionStateChanged(VpnProtocol::ConnectionState state);
|
||||
void timeoutTimerEvent();
|
||||
void protocolError(amnezia::ErrorCode e);
|
||||
|
||||
// This signal is emitted when the controller is initialized. Note that the
|
||||
// VPN tunnel can be already active. In this case, "connected" should be set
|
||||
// to true and the "connectionDate" should be set to the activation date if
|
||||
// known.
|
||||
// If "status" is set to false, the backend service is considered unavailable.
|
||||
void initialized(bool status, bool connected,
|
||||
const QDateTime& connectionDate);
|
||||
protected slots:
|
||||
virtual void onTimeout();
|
||||
|
||||
|
|
|
|||
|
|
@ -73,31 +73,22 @@ void WireguardProtocol::stop()
|
|||
|
||||
void WireguardProtocol::readWireguardConfiguration(const QJsonObject &configuration)
|
||||
{
|
||||
if (configuration.contains(config::key_wireguard_config_data)) {
|
||||
if (!m_configFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
qCritical() << "Failed to save wireguard config to" << m_configFile.fileName();
|
||||
return;
|
||||
}
|
||||
QJsonObject jConfig = configuration.value(ProtocolProps::key_proto_config_data(Protocol::WireGuard)).toObject();
|
||||
|
||||
m_isConfigLoaded = true;
|
||||
|
||||
m_configFile.write(configuration.value(config::key_wireguard_config_data).toString().toUtf8());
|
||||
m_configFile.close();
|
||||
m_configFileName = m_configFile.fileName();
|
||||
|
||||
qDebug().noquote() << QString("Set config data") << m_configFileName;
|
||||
qDebug().noquote() << QString("Set config data") << configuration.value(config::key_wireguard_config_data).toString().toUtf8();
|
||||
if (!m_configFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
qCritical() << "Failed to save wireguard config to" << m_configFile.fileName();
|
||||
return;
|
||||
}
|
||||
// else if (configuration.contains(config::key_wireguard_config_path)) {
|
||||
// m_configFileName = configuration.value(config::key_wireguard_config_path).toString();
|
||||
// QFileInfo file(m_configFileName);
|
||||
|
||||
// if (file.fileName().isEmpty()) {
|
||||
// m_configFileName = defaultConfigFileName();
|
||||
// }
|
||||
m_isConfigLoaded = true;
|
||||
|
||||
m_configFile.write(jConfig.value(config_key::config).toString().toUtf8());
|
||||
m_configFile.close();
|
||||
m_configFileName = m_configFile.fileName();
|
||||
|
||||
qDebug().noquote() << QString("Set config data") << m_configFileName;
|
||||
qDebug().noquote() << QString("Set config data") << configuration.value(ProtocolProps::key_proto_config_data(Protocol::WireGuard)).toString().toUtf8();
|
||||
|
||||
// qDebug().noquote() << QString("Set config file: '%1'").arg(configPath());
|
||||
// }
|
||||
}
|
||||
|
||||
//bool WireguardProtocol::openVpnProcessIsRunning() const
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue