Xray with Reality protocol (#494)

* Xray with Reality for desktops
This commit is contained in:
Mykola Baibuz 2024-03-27 11:02:34 +00:00 committed by GitHub
parent f6acec53c0
commit ba4237f1dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
64 changed files with 1933 additions and 336 deletions

View file

@ -5,10 +5,14 @@
#include <utilities.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/route.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <paths.h>
#include <fcntl.h>
#include <errno.h>
@ -16,6 +20,8 @@
#include <unistd.h>
#include <QFileInfo>
#include <core/networkUtilities.h>
RouterLinux &RouterLinux::Instance()
{
static RouterLinux s;
@ -24,10 +30,10 @@ RouterLinux &RouterLinux::Instance()
bool RouterLinux::routeAdd(const QString &ipWithSubnet, const QString &gw, const int &sock)
{
QString ip = Utils::ipAddressFromIpWithSubnet(ipWithSubnet);
QString mask = Utils::netMaskFromIpWithSubnet(ipWithSubnet);
QString ip = NetworkUtilities::ipAddressFromIpWithSubnet(ipWithSubnet);
QString mask = NetworkUtilities::netMaskFromIpWithSubnet(ipWithSubnet);
if (!Utils::checkIPv4Format(ip) || !Utils::checkIPv4Format(gw)) {
if (!NetworkUtilities::checkIPv4Format(ip) || !NetworkUtilities::checkIPv4Format(gw)) {
qCritical().noquote() << "Critical, trying to add invalid route: " << ip << gw;
return false;
}
@ -91,18 +97,18 @@ bool RouterLinux::clearSavedRoutes()
bool RouterLinux::routeDelete(const QString &ipWithSubnet, const QString &gw, const int &sock)
{
#ifdef MZ_DEBUG
qDebug().noquote() << "RouterMac::routeDelete: " << ipWithSubnet << gw;
qDebug().noquote() << "RouterLinux::routeDelete: " << ipWithSubnet << gw;
#endif
QString ip = Utils::ipAddressFromIpWithSubnet(ipWithSubnet);
QString mask = Utils::netMaskFromIpWithSubnet(ipWithSubnet);
QString ip = NetworkUtilities::ipAddressFromIpWithSubnet(ipWithSubnet);
QString mask = NetworkUtilities::netMaskFromIpWithSubnet(ipWithSubnet);
if (!Utils::checkIPv4Format(ip) || !Utils::checkIPv4Format(gw)) {
if (!NetworkUtilities::checkIPv4Format(ip) || !NetworkUtilities::checkIPv4Format(gw)) {
qCritical().noquote() << "Critical, trying to remove invalid route: " << ip << gw;
return false;
}
if (ip == "0.0.0.0") {
if (ipWithSubnet == "0.0.0.0/0") {
qDebug().noquote() << "Warning, trying to remove default route, skipping: " << ip << gw;
return true;
}
@ -170,3 +176,169 @@ void RouterLinux::flushDns()
else
qDebug().noquote() << "OUTPUT systemctl restart nscd/systemd-resolved: " + output;
}
bool RouterLinux::createTun(const QString &dev, const QString &subnet) {
qDebug().noquote() << "createTun start";
QProcess process;
QStringList commands;
commands << "ip" << "tuntap" << "add" << "mode" << "tun" << "dev" << dev;
process.start("sudo", commands);
if (!process.waitForStarted(1000))
{
qDebug().noquote() << "Could not start adding tun device!\n";
return false;
}
else if (!process.waitForFinished(2000))
{
qDebug().noquote() << "Could not add tun device!\n";
return false;
}
commands.clear();
commands << "ip" << "addr" << "add" << QString("%1/24").arg(subnet) << "dev" << dev;
process.start("sudo", commands);
if (!process.waitForStarted(1000))
{
qDebug().noquote() << "Could not start adding a subnet for tun device!\n";
return false;
}
else if (!process.waitForFinished(2000))
{
qDebug().noquote() << "Could not add a subnet for tun device!\n";
return false;
}
commands.clear();
commands << "ip" << "link" << "set" << "dev" << dev << "up";
process.start("sudo", commands);
if (!process.waitForStarted(1000))
{
qDebug().noquote() << "Could not start link set for tun device!\n";
return false;
}
else if (!process.waitForFinished(2000))
{
qDebug().noquote() << "Could not link set for tun device!\n";
return false;
}
return true;
}
bool RouterLinux::deleteTun(const QString &dev)
{
struct {
struct nlmsghdr nh;
struct ifinfomsg ifm;
unsigned char data[64];
} req;
struct rtattr *rta;
int ret, rtnl;
rtnl = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
if (rtnl < 0) {
qDebug().noquote() << "can't open rtnl: " << errno;
return 1;
}
memset(&req, 0, sizeof(req));
req.nh.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(req.ifm)));
req.nh.nlmsg_flags = NLM_F_REQUEST;
req.nh.nlmsg_type = RTM_DELLINK;
req.ifm.ifi_family = AF_UNSPEC;
rta = (struct rtattr *)(((char *)&req) + NLMSG_ALIGN(req.nh.nlmsg_len));
rta->rta_type = IFLA_IFNAME;
rta->rta_len = RTA_LENGTH(IFNAMSIZ);
req.nh.nlmsg_len += rta->rta_len;
memcpy(RTA_DATA(rta), dev.toStdString().c_str(), IFNAMSIZ);
ret = send(rtnl, &req, req.nh.nlmsg_len, 0);
if (ret < 0)
qDebug().noquote() << "can't send: errno";
ret = (unsigned int)ret != req.nh.nlmsg_len;
close(rtnl);
qDebug().noquote() << "deleteTun ret" << ret;
return ret;
}
bool RouterLinux::updateResolvers(const QString& ifname, const QList<QHostAddress>& resolvers)
{
return m_dnsUtil->updateResolvers(ifname, resolvers);
}
void RouterLinux::StartRoutingIpv6()
{
QProcess process;
QStringList commands;
commands << "sysctl" << "-w" << "net.ipv6.conf.all.disable_ipv6=0";
process.start("sudo", commands);
if (!process.waitForStarted(1000))
{
qDebug().noquote() << "Could not start activate ipv6\n";
return;
}
else if (!process.waitForFinished(2000))
{
qDebug().noquote() << "Could not activate ipv6\n";
return;
}
commands.clear();
commands << "sysctl" << "-w" << "net.ipv6.conf.default.disable_ipv6=0";
process.start("sudo", commands);
if (!process.waitForStarted(1000))
{
qDebug().noquote() << "Could not start activate ipv6\n";
return;
}
else if (!process.waitForFinished(2000))
{
qDebug().noquote() << "Could not activate ipv6\n";
return;
}
commands.clear();
qDebug().noquote() << "StartRoutingIpv6 OK";
}
void RouterLinux::StopRoutingIpv6()
{
QProcess process;
QStringList commands;
commands << "sysctl" << "-w" << "net.ipv6.conf.all.disable_ipv6=1";
process.start("sudo", commands);
if (!process.waitForStarted(1000))
{
qDebug().noquote() << "Could not start disable ipv6\n";
return;
}
else if (!process.waitForFinished(2000))
{
qDebug().noquote() << "Could not disable ipv6\n";
return;
}
commands.clear();
commands << "sysctl" << "-w" << "net.ipv6.conf.default.disable_ipv6=1";
process.start("sudo", commands);
if (!process.waitForStarted(1000))
{
qDebug().noquote() << "Could not start disable ipv6\n";
return;
}
else if (!process.waitForFinished(2000))
{
qDebug().noquote() << "Could not disable ipv6\n";
return;
}
commands.clear();
qDebug().noquote() << "StopRoutingIpv6 OK";
}