[WIP] OpenVPN tunnel implementation
This commit is contained in:
parent
4976dc3a4c
commit
eba71469a4
15 changed files with 845 additions and 230 deletions
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include "vpnprotocol.h"
|
||||
#include "protocols/protocols_defs.h"
|
||||
#include "json.h"
|
||||
|
||||
using namespace amnezia;
|
||||
|
||||
|
@ -45,6 +46,12 @@ private:
|
|||
bool m_serviceConnected = false;
|
||||
bool m_checkingStatus = false;
|
||||
std::function<void(const QString&)> m_logCallback;
|
||||
|
||||
void setupWireguardProtocol(const QtJson::JsonObject& result);
|
||||
void setupOpenVPNProtocol(const QtJson::JsonObject& result);
|
||||
|
||||
void launchWireguardTunnel(const QtJson::JsonObject &result);
|
||||
void launchOpenVPNTunnel(const QtJson::JsonObject &result);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -10,8 +10,6 @@
|
|||
|
||||
#include <QByteArray>
|
||||
|
||||
#include "json.h"
|
||||
|
||||
#include "ipaddressrange.h"
|
||||
#include "ios_vpnprotocol.h"
|
||||
#include "core/errorstrings.h"
|
||||
|
@ -35,10 +33,9 @@ bool IOSVpnProtocol::initialize()
|
|||
{
|
||||
qDebug() << "Initializing Swift Controller";
|
||||
|
||||
static bool creating = false;
|
||||
// No nested creation!
|
||||
Q_ASSERT(creating == false);
|
||||
creating = true;
|
||||
|
||||
// qDebug() << "config =>";
|
||||
// qDebug() << QJsonDocument(m_rawConfig).toJson();
|
||||
|
||||
if (!m_controller) {
|
||||
bool ok;
|
||||
|
@ -49,142 +46,41 @@ bool IOSVpnProtocol::initialize()
|
|||
return false;
|
||||
}
|
||||
|
||||
QString vpnProto = result["protocol"].toString();
|
||||
qDebug() << "protocol: " << vpnProto;
|
||||
qDebug() << "config data => ";
|
||||
QtJson::JsonObject config = result["wireguard_config_data"].toMap();
|
||||
|
||||
QString privateKey = config["client_priv_key"].toString();
|
||||
QByteArray key = QByteArray::fromBase64(privateKey.toLocal8Bit());
|
||||
|
||||
qDebug() << " - " << "client_priv_key: " << config["client_priv_key"].toString();
|
||||
qDebug() << " - " << "client_pub_key: " << config["client_pub_key"].toString();
|
||||
qDebug() << " - " << "interface config: " << config["config"].toString();
|
||||
|
||||
QString addr = config["config"].toString().split("\n").takeAt(1).split(" = ").takeLast();
|
||||
QString dns = config["config"].toString().split("\n").takeAt(2).split(" = ").takeLast();
|
||||
QString privkey = config["config"].toString().split("\n").takeAt(3).split(" = ").takeLast();
|
||||
QString pubkey = config["config"].toString().split("\n").takeAt(6).split(" = ").takeLast();
|
||||
QString presharedkey = config["config"].toString().split("\n").takeAt(7).split(" = ").takeLast();
|
||||
QString allowedips = config["config"].toString().split("\n").takeAt(8).split(" = ").takeLast();
|
||||
QString endpoint = config["config"].toString().split("\n").takeAt(9).split(" = ").takeLast();
|
||||
QString keepalive = config["config"].toString().split("\n").takeAt(10).split(" = ").takeLast();
|
||||
qDebug() << " - " << "[Interface] address: " << addr;
|
||||
qDebug() << " - " << "[Interface] dns: " << dns;
|
||||
qDebug() << " - " << "[Interface] private key: " << privkey;
|
||||
qDebug() << " - " << "[Peer] public key: " << pubkey;
|
||||
qDebug() << " - " << "[Peer] preshared key: " << presharedkey;
|
||||
qDebug() << " - " << "[Peer] allowed ips: " << allowedips;
|
||||
qDebug() << " - " << "[Peer] endpoint: " << endpoint;
|
||||
qDebug() << " - " << "[Peer] keepalive: " << keepalive;
|
||||
|
||||
qDebug() << " - " << "hostName: " << config["hostName"].toString();
|
||||
qDebug() << " - " << "psk_key: " << config["psk_key"].toString();
|
||||
qDebug() << " - " << "server_pub_key: " << config["server_pub_key"].toString();
|
||||
|
||||
|
||||
|
||||
m_controller = [[IOSVpnProtocolImpl alloc] initWithBundleID:@VPN_NE_BUNDLEID
|
||||
privateKey:key.toNSData()
|
||||
deviceIpv4Address:addr.toNSString()
|
||||
deviceIpv6Address:@"::/0"
|
||||
closure:^(ConnectionState state, NSDate* date) {
|
||||
qDebug() << "Creation completed with connection state:" << state;
|
||||
creating = false;
|
||||
|
||||
switch (state) {
|
||||
case ConnectionStateError: {
|
||||
[m_controller dealloc];
|
||||
m_controller = nullptr;
|
||||
emit connectionStateChanged(VpnConnectionState::Error);
|
||||
return;
|
||||
}
|
||||
case ConnectionStateConnected: {
|
||||
Q_ASSERT(date);
|
||||
QDateTime qtDate(QDateTime::fromNSDate(date));
|
||||
emit connectionStateChanged(VpnConnectionState::Connected);
|
||||
return;
|
||||
}
|
||||
case ConnectionStateDisconnected:
|
||||
// Just in case we are connecting, let's call disconnect.
|
||||
[m_controller disconnect];
|
||||
emit connectionStateChanged(VpnConnectionState::Disconnected);
|
||||
return;
|
||||
}
|
||||
QString protoName = result["protocol"].toString();
|
||||
qDebug() << "protocol: " << protoName;
|
||||
|
||||
if (protoName == "wireguard") {
|
||||
setupWireguardProtocol(result);
|
||||
} else if (protoName == "openvpn") {
|
||||
setupOpenVPNProtocol(result);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
callback:^(BOOL a_connected) {
|
||||
qDebug() << "State changed: " << a_connected;
|
||||
if (a_connected) {
|
||||
emit connectionStateChanged(Connected);
|
||||
return;
|
||||
}
|
||||
// emit connectionStateChanged(Disconnected);
|
||||
}];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
ErrorCode IOSVpnProtocol::start()
|
||||
{
|
||||
bool ok;
|
||||
QtJson::JsonObject result = QtJson::parse(QJsonDocument(m_rawConfig).toJson(), ok).toMap();
|
||||
qDebug() << "config: " << result;
|
||||
|
||||
if(!ok) {
|
||||
qDebug() << QString("An error occurred during config parsing");
|
||||
return InternalError;
|
||||
}
|
||||
QString protocol = result["protocol"].toString();
|
||||
QtJson::JsonObject config = result["wireguard_config_data"].toMap();
|
||||
|
||||
QString clientPrivateKey = config["client_priv_key"].toString();
|
||||
QByteArray key = QByteArray::fromBase64(clientPrivateKey.toLocal8Bit());
|
||||
QString clientPubKey = config["client_pub_key"].toString();
|
||||
|
||||
QString addr = config["config"].toString().split("\n").takeAt(1).split(" = ").takeLast();
|
||||
QStringList dnsServersList = config["config"].toString().split("\n").takeAt(2).split(" = ").takeLast().split(", ");
|
||||
QString privkey = config["config"].toString().split("\n").takeAt(3).split(" = ").takeLast();
|
||||
QString pubkey = config["config"].toString().split("\n").takeAt(6).split(" = ").takeLast();
|
||||
QString presharedkey = config["config"].toString().split("\n").takeAt(7).split(" = ").takeLast();
|
||||
QStringList allowedIPList = config["config"].toString().split("\n").takeAt(8).split(" = ").takeLast().split(", ");
|
||||
QString endpoint = config["config"].toString().split("\n").takeAt(9).split(" = ").takeLast();
|
||||
QString serverAddr = config["config"].toString().split("\n").takeAt(9).split(" = ").takeLast().split(":").takeFirst();
|
||||
QString port = config["config"].toString().split("\n").takeAt(9).split(" = ").takeLast().split(":").takeLast();
|
||||
QString keepalive = config["config"].toString().split("\n").takeAt(10).split(" = ").takeLast();
|
||||
|
||||
QString hostname = config["hostName"].toString();
|
||||
QString pskKey = config["psk_key"].toString();
|
||||
QString serverPubKey = config["server_pub_key"].toString();
|
||||
|
||||
qDebug() << "IOSVPNProtocol starts for" << hostname;
|
||||
qDebug() << "DNS:" << dnsServersList.takeFirst().toNSString();
|
||||
qDebug() << "serverPublicKey:" << serverPubKey.toNSString();
|
||||
qDebug() << "serverIpv4AddrIn:" << serverAddr.toNSString();
|
||||
qDebug() << "serverPort:" << (uint32_t)port.toInt();
|
||||
qDebug() << "allowed ip list" << allowedIPList;
|
||||
|
||||
NSMutableArray<VPNIPAddressRange*>* allowedIPAddressRangesNS =
|
||||
[NSMutableArray<VPNIPAddressRange*> arrayWithCapacity:allowedIPList.length()];
|
||||
for (const IPAddressRange item : allowedIPList) {
|
||||
VPNIPAddressRange* range =
|
||||
[[VPNIPAddressRange alloc] initWithAddress:item.ipAddress().toNSString()
|
||||
networkPrefixLength:item.range()
|
||||
isIpv6:item.type() == IPAddressRange::IPv6];
|
||||
[allowedIPAddressRangesNS addObject:[range autorelease]];
|
||||
if (protocol == "wireguard") {
|
||||
launchWireguardTunnel(result);
|
||||
} else if (protocol == "openvpn") {
|
||||
launchOpenVPNTunnel(result);
|
||||
} else {
|
||||
return InternalError;
|
||||
}
|
||||
|
||||
[m_controller connectWithDnsServer:dnsServersList.takeFirst().toNSString()
|
||||
serverIpv6Gateway:@"FE80::1"
|
||||
serverPublicKey:serverPubKey.toNSString()
|
||||
presharedKey:pskKey.toNSString()
|
||||
serverIpv4AddrIn:serverAddr.toNSString()
|
||||
serverPort:port.toInt()
|
||||
allowedIPAddressRanges:allowedIPAddressRangesNS
|
||||
ipv6Enabled:NO
|
||||
reason:0
|
||||
failureCallback:^() {
|
||||
qDebug() << "IOSVPNProtocol - connection failed";
|
||||
emit connectionStateChanged(Disconnected);
|
||||
}];
|
||||
return NoError;
|
||||
}
|
||||
|
||||
|
@ -296,3 +192,197 @@ void IOSVpnProtocol::cleanupBackendLogs()
|
|||
file.remove();
|
||||
}
|
||||
|
||||
void IOSVpnProtocol::setupWireguardProtocol(const QtJson::JsonObject &result)
|
||||
{
|
||||
static bool creating = false;
|
||||
// No nested creation!
|
||||
Q_ASSERT(creating == false);
|
||||
creating = true;
|
||||
|
||||
QtJson::JsonObject config = result["wireguard_config_data"].toMap();
|
||||
|
||||
QString privateKey = config["client_priv_key"].toString();
|
||||
QByteArray key = QByteArray::fromBase64(privateKey.toLocal8Bit());
|
||||
|
||||
qDebug() << " - " << "client_priv_key: " << config["client_priv_key"].toString();
|
||||
qDebug() << " - " << "client_pub_key: " << config["client_pub_key"].toString();
|
||||
qDebug() << " - " << "interface config: " << config["config"].toString();
|
||||
|
||||
QString addr = config["config"].toString().split("\n").takeAt(1).split(" = ").takeLast();
|
||||
QString dns = config["config"].toString().split("\n").takeAt(2).split(" = ").takeLast();
|
||||
QString privkey = config["config"].toString().split("\n").takeAt(3).split(" = ").takeLast();
|
||||
QString pubkey = config["config"].toString().split("\n").takeAt(6).split(" = ").takeLast();
|
||||
QString presharedkey = config["config"].toString().split("\n").takeAt(7).split(" = ").takeLast();
|
||||
QString allowedips = config["config"].toString().split("\n").takeAt(8).split(" = ").takeLast();
|
||||
QString endpoint = config["config"].toString().split("\n").takeAt(9).split(" = ").takeLast();
|
||||
QString keepalive = config["config"].toString().split("\n").takeAt(10).split(" = ").takeLast();
|
||||
qDebug() << " - " << "[Interface] address: " << addr;
|
||||
qDebug() << " - " << "[Interface] dns: " << dns;
|
||||
qDebug() << " - " << "[Interface] private key: " << privkey;
|
||||
qDebug() << " - " << "[Peer] public key: " << pubkey;
|
||||
qDebug() << " - " << "[Peer] preshared key: " << presharedkey;
|
||||
qDebug() << " - " << "[Peer] allowed ips: " << allowedips;
|
||||
qDebug() << " - " << "[Peer] endpoint: " << endpoint;
|
||||
qDebug() << " - " << "[Peer] keepalive: " << keepalive;
|
||||
|
||||
qDebug() << " - " << "hostName: " << config["hostName"].toString();
|
||||
qDebug() << " - " << "psk_key: " << config["psk_key"].toString();
|
||||
qDebug() << " - " << "server_pub_key: " << config["server_pub_key"].toString();
|
||||
|
||||
|
||||
|
||||
m_controller = [[IOSVpnProtocolImpl alloc] initWithBundleID:@VPN_NE_BUNDLEID
|
||||
privateKey:key.toNSData()
|
||||
deviceIpv4Address:addr.toNSString()
|
||||
deviceIpv6Address:@"::/0"
|
||||
closure:^(ConnectionState state, NSDate* date) {
|
||||
qDebug() << "Creation completed with connection state:" << state;
|
||||
creating = false;
|
||||
|
||||
switch (state) {
|
||||
case ConnectionStateError: {
|
||||
[m_controller dealloc];
|
||||
m_controller = nullptr;
|
||||
emit connectionStateChanged(VpnConnectionState::Error);
|
||||
return;
|
||||
}
|
||||
case ConnectionStateConnected: {
|
||||
Q_ASSERT(date);
|
||||
QDateTime qtDate(QDateTime::fromNSDate(date));
|
||||
emit connectionStateChanged(VpnConnectionState::Connected);
|
||||
return;
|
||||
}
|
||||
case ConnectionStateDisconnected:
|
||||
// Just in case we are connecting, let's call disconnect.
|
||||
[m_controller disconnect];
|
||||
emit connectionStateChanged(VpnConnectionState::Disconnected);
|
||||
return;
|
||||
}
|
||||
}
|
||||
callback:^(BOOL a_connected) {
|
||||
qDebug() << "State changed: " << a_connected;
|
||||
if (a_connected) {
|
||||
emit connectionStateChanged(Connected);
|
||||
return;
|
||||
}
|
||||
emit connectionStateChanged(Disconnected);
|
||||
}];
|
||||
}
|
||||
|
||||
void IOSVpnProtocol::setupOpenVPNProtocol(const QtJson::JsonObject &result)
|
||||
{
|
||||
static bool creating = false;
|
||||
// No nested creation!
|
||||
Q_ASSERT(creating == false);
|
||||
creating = true;
|
||||
|
||||
QtJson::JsonObject ovpn = result["openvpn_config_data"].toMap();
|
||||
QString ovpnConfig = ovpn["config"].toString();
|
||||
// qDebug() << ovpn;
|
||||
|
||||
m_controller = [[IOSVpnProtocolImpl alloc] initWithBundleID:@VPN_NE_BUNDLEID
|
||||
config:ovpnConfig.toNSString()
|
||||
closure:^(ConnectionState state, NSDate* date) {
|
||||
qDebug() << "OVPN Creation completed with connection state:" << state;
|
||||
creating = false;
|
||||
|
||||
switch (state) {
|
||||
case ConnectionStateError: {
|
||||
[m_controller dealloc];
|
||||
m_controller = nullptr;
|
||||
emit connectionStateChanged(VpnConnectionState::Error);
|
||||
return;
|
||||
}
|
||||
case ConnectionStateConnected: {
|
||||
Q_ASSERT(date);
|
||||
QDateTime qtDate(QDateTime::fromNSDate(date));
|
||||
emit connectionStateChanged(VpnConnectionState::Connected);
|
||||
return;
|
||||
}
|
||||
case ConnectionStateDisconnected:
|
||||
// Just in case we are connecting, let's call disconnect.
|
||||
[m_controller disconnect];
|
||||
emit connectionStateChanged(VpnConnectionState::Disconnected);
|
||||
return;
|
||||
}
|
||||
}
|
||||
callback:^(BOOL a_connected) {
|
||||
qDebug() << "OVPN State changed: " << a_connected;
|
||||
if (a_connected) {
|
||||
emit connectionStateChanged(Connected);
|
||||
return;
|
||||
}
|
||||
emit connectionStateChanged(Disconnected);
|
||||
}];
|
||||
}
|
||||
|
||||
void IOSVpnProtocol::launchWireguardTunnel(const QtJson::JsonObject &result)
|
||||
{
|
||||
QtJson::JsonObject config = result["wireguard_config_data"].toMap();
|
||||
|
||||
QString clientPrivateKey = config["client_priv_key"].toString();
|
||||
QByteArray key = QByteArray::fromBase64(clientPrivateKey.toLocal8Bit());
|
||||
QString clientPubKey = config["client_pub_key"].toString();
|
||||
|
||||
QString addr = config["config"].toString().split("\n").takeAt(1).split(" = ").takeLast();
|
||||
QStringList dnsServersList = config["config"].toString().split("\n").takeAt(2).split(" = ").takeLast().split(", ");
|
||||
QString privkey = config["config"].toString().split("\n").takeAt(3).split(" = ").takeLast();
|
||||
QString pubkey = config["config"].toString().split("\n").takeAt(6).split(" = ").takeLast();
|
||||
QString presharedkey = config["config"].toString().split("\n").takeAt(7).split(" = ").takeLast();
|
||||
QStringList allowedIPList = config["config"].toString().split("\n").takeAt(8).split(" = ").takeLast().split(", ");
|
||||
QString endpoint = config["config"].toString().split("\n").takeAt(9).split(" = ").takeLast();
|
||||
QString serverAddr = config["config"].toString().split("\n").takeAt(9).split(" = ").takeLast().split(":").takeFirst();
|
||||
QString port = config["config"].toString().split("\n").takeAt(9).split(" = ").takeLast().split(":").takeLast();
|
||||
QString keepalive = config["config"].toString().split("\n").takeAt(10).split(" = ").takeLast();
|
||||
|
||||
QString hostname = config["hostName"].toString();
|
||||
QString pskKey = config["psk_key"].toString();
|
||||
QString serverPubKey = config["server_pub_key"].toString();
|
||||
|
||||
qDebug() << "IOSVPNProtocol starts for" << hostname;
|
||||
qDebug() << "DNS:" << dnsServersList.takeFirst().toNSString();
|
||||
qDebug() << "serverPublicKey:" << serverPubKey.toNSString();
|
||||
qDebug() << "serverIpv4AddrIn:" << serverAddr.toNSString();
|
||||
qDebug() << "serverPort:" << (uint32_t)port.toInt();
|
||||
qDebug() << "allowed ip list" << allowedIPList;
|
||||
|
||||
NSMutableArray<VPNIPAddressRange*>* allowedIPAddressRangesNS =
|
||||
[NSMutableArray<VPNIPAddressRange*> arrayWithCapacity:allowedIPList.length()];
|
||||
for (const IPAddressRange item : allowedIPList) {
|
||||
VPNIPAddressRange* range =
|
||||
[[VPNIPAddressRange alloc] initWithAddress:item.ipAddress().toNSString()
|
||||
networkPrefixLength:item.range()
|
||||
isIpv6:item.type() == IPAddressRange::IPv6];
|
||||
[allowedIPAddressRangesNS addObject:[range autorelease]];
|
||||
}
|
||||
|
||||
[m_controller connectWithDnsServer:dnsServersList.takeFirst().toNSString()
|
||||
serverIpv6Gateway:@"FE80::1"
|
||||
serverPublicKey:serverPubKey.toNSString()
|
||||
presharedKey:pskKey.toNSString()
|
||||
serverIpv4AddrIn:serverAddr.toNSString()
|
||||
serverPort:port.toInt()
|
||||
allowedIPAddressRanges:allowedIPAddressRangesNS
|
||||
ipv6Enabled:NO
|
||||
reason:0
|
||||
failureCallback:^() {
|
||||
qDebug() << "Wireguard Protocol - connection failed";
|
||||
emit connectionStateChanged(Disconnected);
|
||||
}];
|
||||
}
|
||||
|
||||
void IOSVpnProtocol::launchOpenVPNTunnel(const QtJson::JsonObject &result)
|
||||
{
|
||||
QtJson::JsonObject ovpn = result["openvpn_config_data"].toMap();
|
||||
QString ovpnConfig = ovpn["config"].toString();
|
||||
|
||||
[m_controller connectWithOvpnConfig:ovpnConfig.toNSString()
|
||||
failureCallback:^{
|
||||
qDebug() << "IOSVPNProtocol - connection failed";
|
||||
emit connectionStateChanged(Disconnected);
|
||||
}];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue