Files
amnezia-client/client/core/protocols/protocolUtils.cpp
T

225 lines
6.7 KiB
C++
Raw Normal View History

#include "protocolUtils.h"
2021-05-10 02:33:31 +03:00
2023-10-20 02:25:40 +01:00
#include <QRandomGenerator>
#include <QJsonObject>
#include <QObject>
2023-10-20 02:25:40 +01:00
2021-09-20 21:51:28 +03:00
using namespace amnezia;
QList<Proto> ProtocolUtils::allProtocols()
2021-09-20 21:51:28 +03:00
{
QMetaEnum metaEnum = QMetaEnum::fromType<Proto>();
QList<Proto> all;
2021-09-20 21:51:28 +03:00
for (int i = 0; i < metaEnum.keyCount(); ++i) {
all.append(static_cast<Proto>(i));
2021-05-10 02:33:31 +03:00
}
2021-09-20 21:51:28 +03:00
return all;
2021-05-10 02:33:31 +03:00
}
TransportProto ProtocolUtils::transportProtoFromString(QString p)
2021-05-10 02:33:31 +03:00
{
2021-09-20 21:51:28 +03:00
QMetaEnum metaEnum = QMetaEnum::fromType<TransportProto>();
for (int i = 0; i < metaEnum.keyCount(); ++i) {
TransportProto tp = static_cast<TransportProto>(i);
if (p.toLower() == transportProtoToString(tp).toLower())
return tp;
2021-09-20 21:51:28 +03:00
}
return TransportProto::Udp;
}
QString ProtocolUtils::transportProtoToString(TransportProto proto, Proto p)
2021-09-20 21:51:28 +03:00
{
QMetaEnum metaEnum = QMetaEnum::fromType<TransportProto>();
QString protoKey = metaEnum.valueToKey(static_cast<int>(proto));
2021-10-02 21:56:47 +03:00
return protoKey.toLower();
2021-05-10 02:33:31 +03:00
}
Proto ProtocolUtils::protoFromString(QString proto)
{
QMetaEnum metaEnum = QMetaEnum::fromType<Proto>();
for (int i = 0; i < metaEnum.keyCount(); ++i) {
Proto p = static_cast<Proto>(i);
if (proto == protoToString(p))
return p;
}
return Proto::Unknown;
}
QString ProtocolUtils::protoToString(Proto p)
{
if (p == Proto::Unknown)
return "";
QMetaEnum metaEnum = QMetaEnum::fromType<Proto>();
QString protoKey = metaEnum.valueToKey(static_cast<int>(p));
return protoKey.toLower();
}
QMap<Proto, QString> ProtocolUtils::protocolHumanNames()
2021-09-10 22:19:00 +03:00
{
return { { Proto::OpenVpn, "OpenVPN" },
{ Proto::WireGuard, "WireGuard" },
{ Proto::Awg, "AmneziaWG" },
{ Proto::Ikev2, "IKEv2" },
2024-03-27 11:02:34 +00:00
{ Proto::Xray, "XRay" },
2024-07-27 20:42:11 +03:00
{ Proto::SSXray, "Shadowsocks"},
{ Proto::TorWebSite, "Website in Tor network" },
{ Proto::Dns, "DNS Service" },
2024-06-19 02:31:04 +03:00
{ Proto::Sftp, QObject::tr("SFTP service") },
2026-05-04 19:16:48 +03:00
{ Proto::Socks5Proxy, QObject::tr("SOCKS5 proxy server") },
{ Proto::MtProxy, QObject::tr("MTProxy (Telegram)") },
2026-05-04 22:58:26 +03:00
{ Proto::Telemt, QObject::tr("Telemt (Telegram)") },
2026-05-04 19:16:48 +03:00
};
2021-09-10 22:19:00 +03:00
}
QMap<Proto, QString> ProtocolUtils::protocolDescriptions()
2021-09-10 22:19:00 +03:00
{
return {};
}
ServiceType ProtocolUtils::protocolService(Proto p)
2021-09-20 21:51:28 +03:00
{
switch (p) {
case Proto::Unknown: return ServiceType::None;
case Proto::SSXray: return ServiceType::None;
case Proto::OpenVpn: return ServiceType::Vpn;
case Proto::WireGuard: return ServiceType::Vpn;
2023-10-06 17:19:44 +05:00
case Proto::Awg: return ServiceType::Vpn;
2023-10-15 02:30:42 +01:00
case Proto::Ikev2: return ServiceType::Vpn;
2024-03-27 11:02:34 +00:00
case Proto::Xray: return ServiceType::Vpn;
2023-10-15 02:30:42 +01:00
case Proto::TorWebSite: return ServiceType::Other;
case Proto::Dns: return ServiceType::Other;
2023-10-15 02:30:42 +01:00
case Proto::Sftp: return ServiceType::Other;
2024-06-10 18:35:24 +07:00
case Proto::Socks5Proxy: return ServiceType::Other;
2026-05-04 19:16:48 +03:00
case Proto::MtProxy: return ServiceType::Other;
2026-05-04 22:58:26 +03:00
case Proto::Telemt: return ServiceType::Other;
default: return ServiceType::Other;
2021-09-20 21:51:28 +03:00
}
}
int ProtocolUtils::getPortForInstall(Proto p)
2023-10-20 02:25:40 +01:00
{
switch (p) {
case Awg:
case WireGuard:
case OpenVpn:
2024-06-10 18:35:24 +07:00
case Socks5Proxy:
2023-10-20 02:25:40 +01:00
return QRandomGenerator::global()->bounded(30000, 50000);
2026-05-04 19:16:48 +03:00
case MtProxy:
2026-05-04 22:58:26 +03:00
case Telemt:
2023-10-20 02:25:40 +01:00
default:
return defaultPort(p);
}
}
int ProtocolUtils::defaultPort(Proto p)
2021-09-20 21:51:28 +03:00
{
switch (p) {
case Proto::Unknown: return -1;
2023-10-20 02:25:40 +01:00
case Proto::OpenVpn: return QString(protocols::openvpn::defaultPort).toInt();
case Proto::WireGuard: return QString(protocols::wireguard::defaultPort).toInt();
case Proto::Awg: return QString(protocols::awg::defaultPort).toInt();
2024-03-27 11:02:34 +00:00
case Proto::Xray: return QString(protocols::xray::defaultPort).toInt();
case Proto::Ikev2: return -1;
case Proto::TorWebSite: return -1;
case Proto::Dns: return 53;
case Proto::Sftp: return 222;
2024-06-10 18:35:24 +07:00
case Proto::Socks5Proxy: return 38080;
2026-05-04 19:16:48 +03:00
case Proto::MtProxy: return QString(protocols::mtProxy::defaultPort).toInt();
2026-05-04 22:58:26 +03:00
case Proto::Telemt: return QString(protocols::telemt::defaultPort).toInt();
default: return -1;
2021-09-20 21:51:28 +03:00
}
}
bool ProtocolUtils::defaultPortChangeable(Proto p)
2021-09-10 22:19:00 +03:00
{
switch (p) {
case Proto::Unknown: return false;
case Proto::OpenVpn: return true;
case Proto::WireGuard: return true;
2023-10-06 17:19:44 +05:00
case Proto::Awg: return true;
case Proto::Ikev2: return false;
2024-08-19 16:17:09 +07:00
case Proto::Xray: return true;
2023-10-15 02:30:42 +01:00
case Proto::TorWebSite: return false;
case Proto::Dns: return false;
2023-10-15 02:30:42 +01:00
case Proto::Sftp: return true;
2024-06-10 18:35:24 +07:00
case Proto::Socks5Proxy: return true;
2026-05-04 19:16:48 +03:00
case Proto::MtProxy: return true;
2026-05-04 22:58:26 +03:00
case Proto::Telemt: return true;
2023-10-15 02:30:42 +01:00
default: return false;
2021-09-20 21:51:28 +03:00
}
}
TransportProto ProtocolUtils::defaultTransportProto(Proto p)
2021-09-20 21:51:28 +03:00
{
switch (p) {
case Proto::Unknown: return TransportProto::Udp;
case Proto::OpenVpn: return TransportProto::Udp;
case Proto::WireGuard: return TransportProto::Udp;
2023-10-06 17:19:44 +05:00
case Proto::Awg: return TransportProto::Udp;
case Proto::Ikev2: return TransportProto::Udp;
2024-03-27 11:02:34 +00:00
case Proto::Xray: return TransportProto::Tcp;
case Proto::SSXray: return TransportProto::Tcp;
2024-03-27 11:02:34 +00:00
// non-vpn
case Proto::TorWebSite: return TransportProto::Tcp;
case Proto::Dns: return TransportProto::Udp;
case Proto::Sftp: return TransportProto::Tcp;
2024-06-10 18:35:24 +07:00
case Proto::Socks5Proxy: return TransportProto::Tcp;
2026-05-04 19:16:48 +03:00
case Proto::MtProxy: return TransportProto::Tcp;
2026-05-04 22:58:26 +03:00
case Proto::Telemt: return TransportProto::Tcp;
default: return TransportProto::Udp;
2021-09-20 21:51:28 +03:00
}
}
bool ProtocolUtils::defaultTransportProtoChangeable(Proto p)
2021-09-20 21:51:28 +03:00
{
switch (p) {
case Proto::Unknown: return false;
case Proto::OpenVpn: return true;
case Proto::WireGuard: return false;
2023-10-06 17:19:44 +05:00
case Proto::Awg: return false;
case Proto::Ikev2: return false;
2024-03-27 11:02:34 +00:00
case Proto::Xray: return false;
// non-vpn
case Proto::TorWebSite: return false;
case Proto::Dns: return false;
case Proto::Sftp: return false;
2024-06-10 18:35:24 +07:00
case Proto::Socks5Proxy: return false;
2026-05-04 19:16:48 +03:00
case Proto::MtProxy: return false;
2026-05-04 22:58:26 +03:00
case Proto::Telemt: return false;
default: return false;
2021-09-10 22:19:00 +03:00
}
}
2021-10-04 21:13:07 +03:00
QString ProtocolUtils::key_proto_config_data(Proto p)
2021-10-04 21:13:07 +03:00
{
return protoToString(p) + "_config_data";
}
QString ProtocolUtils::key_proto_config_path(Proto p)
2021-10-04 21:13:07 +03:00
{
return protoToString(p) + "_config_path";
}
QString ProtocolUtils::getProtocolVersion(const QJsonObject &protocolConfig)
{
return protocolConfig.value(configKey::protocolVersion).toString();
}
QString ProtocolUtils::getProtocolVersionString(const QJsonObject &protocolConfig)
{
auto version = getProtocolVersion(protocolConfig);
if (version == protocols::awg::awgV2) return QObject::tr(" (version 2)");
if (version == protocols::awg::awgV1_5) return QObject::tr(" (version 1.5)");
return "";
}