mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-12-02 21:28:21 +09:00
125 lines
4.1 KiB
C++
125 lines
4.1 KiB
C++
#include "deviceTransferCode.h"
|
|
|
|
#include <QNetworkInterface>
|
|
#include <QSysInfo>
|
|
#include <QUrl>
|
|
#include <QUrlQuery>
|
|
|
|
#ifdef Q_OS_MACOS
|
|
#include "platforms/macos/macosutils.h"
|
|
#endif
|
|
|
|
namespace
|
|
{
|
|
constexpr QLatin1String scheme("amnezia");
|
|
constexpr QLatin1String host("receive");
|
|
constexpr QLatin1String versionKey("v");
|
|
constexpr QLatin1String nameKey("n");
|
|
constexpr QLatin1String addressesKey("a");
|
|
constexpr QLatin1String portKey("p");
|
|
constexpr QLatin1String publicKeyKey("k");
|
|
|
|
constexpr auto base64Options = QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals;
|
|
}
|
|
|
|
QString deviceTransfer::TransferCode::toLink() const
|
|
{
|
|
QUrlQuery query;
|
|
query.addQueryItem(versionKey, QString::number(protocolVersion));
|
|
query.addQueryItem(nameKey, deviceName);
|
|
query.addQueryItem(addressesKey, addresses.join(','));
|
|
query.addQueryItem(portKey, QString::number(port));
|
|
query.addQueryItem(publicKeyKey, QString::fromLatin1(publicKey.toBase64(base64Options)));
|
|
|
|
QUrl url;
|
|
url.setScheme(scheme);
|
|
url.setHost(host);
|
|
url.setQuery(query);
|
|
return url.toString(QUrl::FullyEncoded);
|
|
}
|
|
|
|
std::optional<deviceTransfer::TransferCode> deviceTransfer::TransferCode::parse(const QString &text)
|
|
{
|
|
const QUrl url(text.trimmed());
|
|
if (!url.isValid() || url.scheme() != scheme || url.host() != host) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
const QUrlQuery query(url);
|
|
if (query.queryItemValue(versionKey) != QString::number(protocolVersion)) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
bool isPortValid = false;
|
|
const uint port = query.queryItemValue(portKey).toUInt(&isPortValid);
|
|
if (!isPortValid || port == 0 || port > 65535) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
QStringList addresses;
|
|
for (const QString &candidate : query.queryItemValue(addressesKey).split(',', Qt::SkipEmptyParts)) {
|
|
const QHostAddress address(candidate);
|
|
if (address.protocol() == QAbstractSocket::IPv4Protocol && isLanAddress(address)) {
|
|
addresses.append(address.toString());
|
|
}
|
|
}
|
|
if (addresses.isEmpty()) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
const auto decoded = QByteArray::fromBase64Encoding(query.queryItemValue(publicKeyKey).toLatin1(), base64Options);
|
|
if (!decoded || decoded.decoded.size() != publicKeySize) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
TransferCode code;
|
|
code.deviceName = query.queryItemValue(nameKey, QUrl::FullyDecoded);
|
|
code.addresses = addresses;
|
|
code.port = static_cast<quint16>(port);
|
|
code.publicKey = decoded.decoded;
|
|
return code;
|
|
}
|
|
|
|
bool deviceTransfer::isLanAddress(const QHostAddress &address)
|
|
{
|
|
return address.isInSubnet(QHostAddress("10.0.0.0"), 8) || address.isInSubnet(QHostAddress("172.16.0.0"), 12)
|
|
|| address.isInSubnet(QHostAddress("192.168.0.0"), 16);
|
|
}
|
|
|
|
QStringList deviceTransfer::localLanAddresses()
|
|
{
|
|
QStringList addresses;
|
|
const auto interfaces = QNetworkInterface::allInterfaces();
|
|
for (const QNetworkInterface &networkInterface : interfaces) {
|
|
const auto flags = networkInterface.flags();
|
|
if (!flags.testFlag(QNetworkInterface::IsUp) || !flags.testFlag(QNetworkInterface::IsRunning)
|
|
|| flags.testFlag(QNetworkInterface::IsLoopBack) || flags.testFlag(QNetworkInterface::IsPointToPoint)
|
|
|| networkInterface.type() == QNetworkInterface::Virtual) {
|
|
continue;
|
|
}
|
|
for (const QNetworkAddressEntry &entry : networkInterface.addressEntries()) {
|
|
const QHostAddress address = entry.ip();
|
|
if (address.protocol() == QAbstractSocket::IPv4Protocol && isLanAddress(address)
|
|
&& !addresses.contains(address.toString())) {
|
|
addresses.append(address.toString());
|
|
}
|
|
}
|
|
}
|
|
return addresses;
|
|
}
|
|
|
|
QString deviceTransfer::localDeviceName()
|
|
{
|
|
#ifdef Q_OS_MACOS
|
|
const QString computerName = MacOSUtils::computerName();
|
|
if (!computerName.isEmpty()) {
|
|
return computerName;
|
|
}
|
|
#endif
|
|
const QString hostName = QSysInfo::machineHostName();
|
|
if (!hostName.isEmpty() && hostName != QLatin1String("localhost")) {
|
|
return hostName;
|
|
}
|
|
return QSysInfo::prettyProductName();
|
|
}
|