Merge pull request #332 from amnezia-vpn/bugfix/wireguard-config-import
added parsing of wireguard config parameters when importing native configs
This commit is contained in:
commit
3414202b7b
1 changed files with 104 additions and 62 deletions
|
@ -1,72 +1,73 @@
|
|||
#include "StartPageLogic.h"
|
||||
#include "ViewConfigLogic.h"
|
||||
|
||||
#include "core/errorstrings.h"
|
||||
#include "../uilogic.h"
|
||||
#include "configurators/ssh_configurator.h"
|
||||
#include "configurators/vpn_configurator.h"
|
||||
#include "../uilogic.h"
|
||||
#include "utilities.h"
|
||||
#include "core/errorstrings.h"
|
||||
#include "core/servercontroller.h"
|
||||
#include "utilities.h"
|
||||
|
||||
#include <QEventLoop>
|
||||
#include <QFileDialog>
|
||||
#include <QStandardPaths>
|
||||
#include <QEventLoop>
|
||||
|
||||
#ifdef Q_OS_ANDROID
|
||||
#include <QJniObject>
|
||||
#include "../../platforms/android/androidutils.h"
|
||||
#include "../../platforms/android/android_controller.h"
|
||||
#include "../../platforms/android/android_controller.h"
|
||||
#include "../../platforms/android/androidutils.h"
|
||||
#include <QJniObject>
|
||||
#endif
|
||||
|
||||
#ifdef Q_OS_IOS
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
enum class ConfigTypes {
|
||||
Amnezia,
|
||||
OpenVpn,
|
||||
WireGuard
|
||||
};
|
||||
|
||||
ConfigTypes checkConfigFormat(const QString &config)
|
||||
namespace
|
||||
{
|
||||
const QString openVpnConfigPatternCli = "client";
|
||||
const QString openVpnConfigPatternProto1 = "proto tcp";
|
||||
const QString openVpnConfigPatternProto2 = "proto udp";
|
||||
const QString openVpnConfigPatternDriver1 = "dev tun";
|
||||
const QString openVpnConfigPatternDriver2 = "dev tap";
|
||||
enum class ConfigTypes {
|
||||
Amnezia,
|
||||
OpenVpn,
|
||||
WireGuard
|
||||
};
|
||||
|
||||
const QString wireguardConfigPatternSectionInterface = "[Interface]";
|
||||
const QString wireguardConfigPatternSectionPeer = "[Peer]";
|
||||
ConfigTypes checkConfigFormat(const QString &config)
|
||||
{
|
||||
const QString openVpnConfigPatternCli = "client";
|
||||
const QString openVpnConfigPatternProto1 = "proto tcp";
|
||||
const QString openVpnConfigPatternProto2 = "proto udp";
|
||||
const QString openVpnConfigPatternDriver1 = "dev tun";
|
||||
const QString openVpnConfigPatternDriver2 = "dev tap";
|
||||
|
||||
if (config.contains(openVpnConfigPatternCli) &&
|
||||
(config.contains(openVpnConfigPatternProto1) || config.contains(openVpnConfigPatternProto2)) &&
|
||||
(config.contains(openVpnConfigPatternDriver1) || config.contains(openVpnConfigPatternDriver2))) {
|
||||
return ConfigTypes::OpenVpn;
|
||||
} else if (config.contains(wireguardConfigPatternSectionInterface) &&
|
||||
config.contains(wireguardConfigPatternSectionPeer))
|
||||
return ConfigTypes::WireGuard;
|
||||
return ConfigTypes::Amnezia;
|
||||
}
|
||||
const QString wireguardConfigPatternSectionInterface = "[Interface]";
|
||||
const QString wireguardConfigPatternSectionPeer = "[Peer]";
|
||||
|
||||
if (config.contains(openVpnConfigPatternCli)
|
||||
&& (config.contains(openVpnConfigPatternProto1) || config.contains(openVpnConfigPatternProto2))
|
||||
&& (config.contains(openVpnConfigPatternDriver1) || config.contains(openVpnConfigPatternDriver2))) {
|
||||
return ConfigTypes::OpenVpn;
|
||||
} else if (config.contains(wireguardConfigPatternSectionInterface)
|
||||
&& config.contains(wireguardConfigPatternSectionPeer))
|
||||
return ConfigTypes::WireGuard;
|
||||
return ConfigTypes::Amnezia;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
StartPageLogic::StartPageLogic(UiLogic *logic, QObject *parent):
|
||||
PageLogicBase(logic, parent),
|
||||
m_pushButtonConnectEnabled{true},
|
||||
m_pushButtonConnectText{tr("Connect")},
|
||||
m_pushButtonConnectKeyChecked{false},
|
||||
m_labelWaitInfoVisible{true},
|
||||
m_pushButtonBackFromStartVisible{true},
|
||||
m_ipAddressPortRegex{Utils::ipAddressPortRegExp()}
|
||||
StartPageLogic::StartPageLogic(UiLogic *logic, QObject *parent)
|
||||
: PageLogicBase(logic, parent),
|
||||
m_pushButtonConnectEnabled { true },
|
||||
m_pushButtonConnectText { tr("Connect") },
|
||||
m_pushButtonConnectKeyChecked { false },
|
||||
m_labelWaitInfoVisible { true },
|
||||
m_pushButtonBackFromStartVisible { true },
|
||||
m_ipAddressPortRegex { Utils::ipAddressPortRegExp() }
|
||||
{
|
||||
#ifdef Q_OS_ANDROID
|
||||
// Set security screen for Android app
|
||||
AndroidUtils::runOnAndroidThreadSync([]() {
|
||||
QJniObject activity = AndroidUtils::getActivity();
|
||||
QJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
|
||||
if (window.isValid()){
|
||||
if (window.isValid()) {
|
||||
const int FLAG_SECURE = 8192;
|
||||
window.callMethod<void>("addFlags", "(I)V", FLAG_SECURE);
|
||||
}
|
||||
|
@ -93,17 +94,13 @@ void StartPageLogic::onUpdatePage()
|
|||
|
||||
void StartPageLogic::onPushButtonConnect()
|
||||
{
|
||||
if (pushButtonConnectKeyChecked()){
|
||||
if (lineEditIpText().isEmpty() ||
|
||||
lineEditLoginText().isEmpty() ||
|
||||
textEditSshKeyText().isEmpty() ) {
|
||||
if (pushButtonConnectKeyChecked()) {
|
||||
if (lineEditIpText().isEmpty() || lineEditLoginText().isEmpty() || textEditSshKeyText().isEmpty()) {
|
||||
set_labelWaitInfoText(tr("Please fill in all fields"));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (lineEditIpText().isEmpty() ||
|
||||
lineEditLoginText().isEmpty() ||
|
||||
lineEditPasswordText().isEmpty() ) {
|
||||
if (lineEditIpText().isEmpty() || lineEditLoginText().isEmpty() || lineEditPasswordText().isEmpty()) {
|
||||
set_labelWaitInfoText(tr("Please fill in all fields"));
|
||||
return;
|
||||
}
|
||||
|
@ -178,7 +175,8 @@ void StartPageLogic::onPushButtonConnect()
|
|||
set_pushButtonConnectText(tr("Connect"));
|
||||
|
||||
uiLogic()->m_installCredentials = serverCredentials;
|
||||
if (ok) emit uiLogic()->goToPage(Page::NewServer);
|
||||
if (ok)
|
||||
emit uiLogic()->goToPage(Page::NewServer);
|
||||
}
|
||||
|
||||
void StartPageLogic::onPushButtonImport()
|
||||
|
@ -189,22 +187,24 @@ void StartPageLogic::onPushButtonImport()
|
|||
void StartPageLogic::onPushButtonImportOpenFile()
|
||||
{
|
||||
QString fileName = UiLogic::getOpenFileName(Q_NULLPTR, tr("Open config file"),
|
||||
QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), "*.vpn *.ovpn *.conf");
|
||||
if (fileName.isEmpty()) return;
|
||||
QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),
|
||||
"*.vpn *.ovpn *.conf");
|
||||
if (fileName.isEmpty())
|
||||
return;
|
||||
|
||||
QFile file(fileName);
|
||||
|
||||
|
||||
#ifdef Q_OS_IOS
|
||||
CFURLRef url = CFURLCreateWithFileSystemPath(
|
||||
kCFAllocatorDefault, CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar *>(fileName.unicode()),
|
||||
fileName.length()),
|
||||
kCFURLPOSIXPathStyle, 0);
|
||||
|
||||
kCFAllocatorDefault,
|
||||
CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar *>(fileName.unicode()), fileName.length()),
|
||||
kCFURLPOSIXPathStyle, 0);
|
||||
|
||||
if (!CFURLStartAccessingSecurityScopedResource(url)) {
|
||||
qDebug() << "Could not access path " << QUrl::fromLocalFile(fileName).toString();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
file.open(QIODevice::ReadOnly);
|
||||
QByteArray data = file.readAll();
|
||||
|
||||
|
@ -242,8 +242,7 @@ bool StartPageLogic::importConnection(const QJsonObject &profile)
|
|||
// check config
|
||||
uiLogic()->pageLogic<ViewConfigLogic>()->set_configJson(profile);
|
||||
emit uiLogic()->goToPage(Page::ViewConfig);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
qDebug() << "Failed to import profile";
|
||||
qDebug().noquote() << QJsonDocument(profile).toJson();
|
||||
return false;
|
||||
|
@ -314,7 +313,6 @@ bool StartPageLogic::importConnectionFromOpenVpnConfig(const QString &config)
|
|||
o[config_key::defaultContainer] = "amnezia-openvpn";
|
||||
o[config_key::description] = m_settings->nextAvailableServerName();
|
||||
|
||||
|
||||
const static QRegularExpression dnsRegExp("dhcp-option DNS (\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b)");
|
||||
QRegularExpressionMatchIterator dnsMatch = dnsRegExp.globalMatch(config);
|
||||
if (dnsMatch.hasNext()) {
|
||||
|
@ -338,9 +336,51 @@ bool StartPageLogic::importConnectionFromWireguardConfig(const QString &config)
|
|||
QRegularExpressionMatch hostNameAndPortMatch = hostNameAndPortRegExp.match(config);
|
||||
QString hostName;
|
||||
QString port;
|
||||
if (hostNameAndPortMatch.hasMatch()) {
|
||||
if (hostNameAndPortMatch.hasCaptured(1)) {
|
||||
hostName = hostNameAndPortMatch.captured(1);
|
||||
} else {
|
||||
return importConnection(QJsonObject());
|
||||
}
|
||||
|
||||
if (hostNameAndPortMatch.hasCaptured(2)) {
|
||||
port = hostNameAndPortMatch.captured(2);
|
||||
} else {
|
||||
port = protocols::wireguard::defaultPort;
|
||||
}
|
||||
|
||||
lastConfig[config_key::hostName] = hostName;
|
||||
lastConfig[config_key::port] = port.toInt();
|
||||
|
||||
const static QRegularExpression clientPrivKeyRegExp("PrivateKey = (.*)");
|
||||
QRegularExpressionMatch clientPrivKeyMatch = clientPrivKeyRegExp.match(config);
|
||||
if (clientPrivKeyMatch.hasMatch()) {
|
||||
lastConfig[config_key::client_priv_key] = clientPrivKeyMatch.captured(1);
|
||||
} else {
|
||||
return importConnection(QJsonObject());
|
||||
}
|
||||
|
||||
const static QRegularExpression clientIpRegExp("Address = (\\d+\\.\\d+\\.\\d+\\.\\d+)");
|
||||
QRegularExpressionMatch clientIpMatch = clientIpRegExp.match(config);
|
||||
if (clientIpMatch.hasMatch()) {
|
||||
lastConfig[config_key::client_ip] = clientIpMatch.captured(1);
|
||||
} else {
|
||||
return importConnection(QJsonObject());
|
||||
}
|
||||
|
||||
const static QRegularExpression pskKeyRegExp("PresharedKey = (.*)");
|
||||
QRegularExpressionMatch pskKeyMatch = pskKeyRegExp.match(config);
|
||||
if (pskKeyMatch.hasMatch()) {
|
||||
lastConfig[config_key::psk_key] = pskKeyMatch.captured(1);
|
||||
} else {
|
||||
return importConnection(QJsonObject());
|
||||
}
|
||||
|
||||
const static QRegularExpression serverPubKeyRegExp("PublicKey = (.*)");
|
||||
QRegularExpressionMatch serverPubKeyMatch = serverPubKeyRegExp.match(config);
|
||||
if (serverPubKeyMatch.hasMatch()) {
|
||||
lastConfig[config_key::server_pub_key] = serverPubKeyMatch.captured(1);
|
||||
} else {
|
||||
return importConnection(QJsonObject());
|
||||
}
|
||||
|
||||
QJsonObject wireguardConfig;
|
||||
|
@ -361,7 +401,9 @@ bool StartPageLogic::importConnectionFromWireguardConfig(const QString &config)
|
|||
o[config_key::defaultContainer] = "amnezia-wireguard";
|
||||
o[config_key::description] = m_settings->nextAvailableServerName();
|
||||
|
||||
const static QRegularExpression dnsRegExp("DNS = (\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b).*(\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b)");
|
||||
const static QRegularExpression dnsRegExp(
|
||||
"DNS = "
|
||||
"(\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b).*(\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b)");
|
||||
QRegularExpressionMatch dnsMatch = dnsRegExp.match(config);
|
||||
if (dnsMatch.hasMatch()) {
|
||||
o[config_key::dns1] = dnsMatch.captured(1);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue