diff --git a/client/client.pro b/client/client.pro index bf9db7ed..032fe5d3 100644 --- a/client/client.pro +++ b/client/client.pro @@ -74,6 +74,7 @@ HEADERS += \ ui/pages_logic/protocols/OtherProtocolsLogic.h \ ui/pages_logic/protocols/PageProtocolLogicBase.h \ ui/pages_logic/protocols/ShadowSocksLogic.h \ + ui/pages_logic/protocols/WireGuardLogic.h \ ui/property_helper.h \ ui/models/servers_model.h \ ui/uilogic.h \ @@ -136,6 +137,7 @@ SOURCES += \ ui/pages_logic/protocols/PageProtocolLogicBase.cpp \ ui/pages_logic/protocols/ShadowSocksLogic.cpp \ ui/models/servers_model.cpp \ + ui/pages_logic/protocols/WireGuardLogic.cpp \ ui/uilogic.cpp \ ui/qautostart.cpp \ ui/models/sites_model.cpp \ diff --git a/client/protocols/protocols_defs.h b/client/protocols/protocols_defs.h index 462c7c51..c5f15d5b 100644 --- a/client/protocols/protocols_defs.h +++ b/client/protocols/protocols_defs.h @@ -59,7 +59,7 @@ constexpr char additional_server_config[] = "additional_server_config"; // proto config keys constexpr char last_config[] = "last_config"; -constexpr char is_third_party_config[] = "is_third_party_config"; +constexpr char isThirdPartyConfig[] = "isThirdPartyConfig"; constexpr char openvpn[] = "openvpn"; constexpr char wireguard[] = "wireguard"; diff --git a/client/resources.qrc b/client/resources.qrc index a7e67e39..47d10f7c 100644 --- a/client/resources.qrc +++ b/client/resources.qrc @@ -160,5 +160,6 @@ ui/qml/Pages/PageQrDecoderIos.qml server_scripts/website_tor/Dockerfile ui/qml/Pages/PageViewConfig.qml + ui/qml/Pages/Protocols/PageProtoWireGuard.qml diff --git a/client/settings.cpp b/client/settings.cpp index 8202eb27..fc3a5825 100644 --- a/client/settings.cpp +++ b/client/settings.cpp @@ -317,5 +317,5 @@ bool Settings::isThirdPartyConfig(int serverIndex) const { if (serverIndex < 0) return false; const QJsonObject &s = server(serverIndex); - return s.value(config_key::is_third_party_config).toBool(); + return s.value(config_key::isThirdPartyConfig).toBool(); } diff --git a/client/ui/pages_logic/StartPageLogic.cpp b/client/ui/pages_logic/StartPageLogic.cpp index 47b893bc..37b557e0 100644 --- a/client/ui/pages_logic/StartPageLogic.cpp +++ b/client/ui/pages_logic/StartPageLogic.cpp @@ -267,22 +267,22 @@ bool StartPageLogic::importConnectionFromOpenVpnConfig(const QString &config) o[config_key::description] = "OpenVpn server"; - const static QRegularExpression dnsRegExp("dhcp-option DNS \\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b"); + 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()) { - o[config_key::dns1] = dnsMatch.next().captured(0).split(" ").at(2); + o[config_key::dns1] = dnsMatch.next().captured(1); } if (dnsMatch.hasNext()) { - o[config_key::dns2] = dnsMatch.next().captured(0).split(" ").at(2); + o[config_key::dns2] = dnsMatch.next().captured(1); } - const static QRegularExpression hostNameRegExp("remote \\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b"); + const static QRegularExpression hostNameRegExp("remote (\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b)"); QRegularExpressionMatch hostNameMatch = hostNameRegExp.match(config); if (hostNameMatch.hasMatch()) { - o[config_key::hostName] = hostNameMatch.captured(0).split(" ").at(1); + o[config_key::hostName] = hostNameMatch.captured(1); } - o[config_key::is_third_party_config] = true; + o[config_key::isThirdPartyConfig] = true; return importConnection(o); } @@ -301,9 +301,8 @@ bool StartPageLogic::importConnectionFromWireguardConfig(const QString &config) port = hostNameAndPortMatch.captured(2); } - QJsonObject wireguardConfig; - wireguardConfig[config_key::last_config] = lastConfig; + wireguardConfig[config_key::last_config] = QString(QJsonDocument(lastConfig).toJson());; wireguardConfig[config_key::port] = port; wireguardConfig[config_key::transport_proto] = "udp"; @@ -318,7 +317,16 @@ bool StartPageLogic::importConnectionFromWireguardConfig(const QString &config) o[config_key::containers] = arr; o[config_key::defaultContainer] = "amnezia-wireguard"; o[config_key::description] = "Wireguard server"; + + 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); + o[config_key::dns2] = dnsMatch.captured(2); + } + o[config_key::hostName] = hostName; + o[config_key::isThirdPartyConfig] = true; return importConnection(o); } diff --git a/client/ui/pages_logic/protocols/OpenVpnLogic.cpp b/client/ui/pages_logic/protocols/OpenVpnLogic.cpp index 7d726670..fbbf1c86 100644 --- a/client/ui/pages_logic/protocols/OpenVpnLogic.cpp +++ b/client/ui/pages_logic/protocols/OpenVpnLogic.cpp @@ -91,11 +91,12 @@ void OpenVpnLogic::updateProtocolPage(const QJsonObject &openvpnConfig, DockerCo auto lastConfig = openvpnConfig.value(config_key::last_config).toString(); auto lastConfigJson = QJsonDocument::fromJson(lastConfig.toUtf8()).object(); QStringList lines = lastConfigJson.value(config_key::config).toString().replace("\r", "").split("\n"); + QString openVpnLastConfigText; for (const QString &l: lines) { - m_openVpnLastConfigText.append(l + "\n"); + openVpnLastConfigText.append(l + "\n"); } - emit openVpnLastConfigTextChanged(m_openVpnLastConfigText); + set_openVpnLastConfigText(m_openVpnLastConfigText); set_isThirdPartyConfig(isThirdPartyConfig); } diff --git a/client/ui/pages_logic/protocols/WireGuardLogic.cpp b/client/ui/pages_logic/protocols/WireGuardLogic.cpp new file mode 100644 index 00000000..d4c18df6 --- /dev/null +++ b/client/ui/pages_logic/protocols/WireGuardLogic.cpp @@ -0,0 +1,30 @@ +#include "WireGuardLogic.h" +#include "core/servercontroller.h" +#include +#include "../../uilogic.h" + +using namespace amnezia; +using namespace PageEnumNS; + +WireGuardLogic::WireGuardLogic(UiLogic *logic, QObject *parent): + PageProtocolLogicBase(logic, parent) +{ + +} + +void WireGuardLogic::updateProtocolPage(const QJsonObject &wireGuardConfig, DockerContainer container, bool haveAuthData, bool isThirdPartyConfig) +{ + qDebug() << "WireGuardLogic::updateProtocolPage"; + + auto lastConfigJsonDoc = QJsonDocument::fromJson(wireGuardConfig.value(config_key::last_config).toString().toUtf8()); + auto lastConfigJson = lastConfigJsonDoc.object(); + + QString wireGuardLastConfigText; + QStringList lines = lastConfigJson.value(config_key::config).toString().replace("\r", "").split("\n"); + for (const QString &l: lines) { + wireGuardLastConfigText.append(l + "\n"); + } + + set_wireGuardLastConfigText(wireGuardLastConfigText); + set_isThirdPartyConfig(isThirdPartyConfig); +} diff --git a/client/ui/pages_logic/protocols/WireGuardLogic.h b/client/ui/pages_logic/protocols/WireGuardLogic.h new file mode 100644 index 00000000..19e9f90d --- /dev/null +++ b/client/ui/pages_logic/protocols/WireGuardLogic.h @@ -0,0 +1,26 @@ +#ifndef WIREGUARDLOGIC_H +#define WIREGUARDLOGIC_H + +#include "PageProtocolLogicBase.h" + +class UiLogic; + +class WireGuardLogic : public PageProtocolLogicBase +{ + Q_OBJECT + + AUTO_PROPERTY(QString, wireGuardLastConfigText) + AUTO_PROPERTY(bool, isThirdPartyConfig) + +public: + explicit WireGuardLogic(UiLogic *uiLogic, QObject *parent = nullptr); + ~WireGuardLogic() = default; + + void updateProtocolPage(const QJsonObject &wireGuardConfig, DockerContainer container, bool haveAuthData, bool isThirdPartyConfig) override; + +private: + UiLogic *m_uiLogic; + +}; + +#endif // WIREGUARDLOGIC_H diff --git a/client/ui/qml/Pages/Protocols/PageProtoWireGuard.qml b/client/ui/qml/Pages/Protocols/PageProtoWireGuard.qml new file mode 100644 index 00000000..e4dde50e --- /dev/null +++ b/client/ui/qml/Pages/Protocols/PageProtoWireGuard.qml @@ -0,0 +1,60 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.12 +import QtQuick.Layouts 1.15 +import ProtocolEnum 1.0 +import "../" +import "../../Controls" +import "../../Config" + +PageProtocolBase { + id: root + protocol: ProtocolEnum.WireGuard + logic: UiLogic.protocolLogic(protocol) + + BackButton { + id: back + } + Caption { + id: caption + text: qsTr("WireGuard Settings") + } + + Flickable { + id: fl + width: root.width + anchors.top: caption.bottom + anchors.topMargin: 20 + anchors.bottom: parent.bottom + anchors.bottomMargin: 20 + anchors.left: root.left + anchors.leftMargin: 30 + anchors.right: root.right + anchors.rightMargin: 30 + + contentHeight: content.height + clip: true + + ColumnLayout { + id: content + anchors.top: parent.top + anchors.left: parent.left + anchors.right: parent.right + + TextAreaType { + id: ta_config + + Layout.topMargin: 5 + Layout.bottomMargin: 20 + Layout.fillWidth: true + Layout.leftMargin: 1 + Layout.rightMargin: 1 + Layout.preferredHeight: fl.height - 70 + flickableDirection: Flickable.AutoFlickIfNeeded + + textArea.readOnly: true + textArea.text: logic.wireGuardLastConfigText + } + } + } + +} diff --git a/client/ui/uilogic.cpp b/client/ui/uilogic.cpp index e1468da7..81492023 100644 --- a/client/ui/uilogic.cpp +++ b/client/ui/uilogic.cpp @@ -71,7 +71,7 @@ #include "pages_logic/protocols/OpenVpnLogic.h" #include "pages_logic/protocols/ShadowSocksLogic.h" #include "pages_logic/protocols/OtherProtocolsLogic.h" - +#include "pages_logic/protocols/WireGuardLogic.h" using namespace amnezia; using namespace PageEnumNS; @@ -94,7 +94,7 @@ UiLogic::UiLogic(std::shared_ptr settings, std::shared_ptrinsert(Proto::WireGuard, new WireguardLogic(this)); + m_protocolLogicMap.insert(Proto::WireGuard, new WireGuardLogic(this)); m_protocolLogicMap.insert(Proto::Dns, new OtherProtocolsLogic(this)); m_protocolLogicMap.insert(Proto::Sftp, new OtherProtocolsLogic(this));