Fixed defined errors and refactor add_route for linux
This commit is contained in:
parent
148b1dacce
commit
f852ff6dff
7 changed files with 219 additions and 5 deletions
|
|
@ -43,7 +43,7 @@ SshKeyCreationDialog::SshKeyCreationDialog(QWidget *parent)
|
||||||
{
|
{
|
||||||
m_ui->setupUi(this);
|
m_ui->setupUi(this);
|
||||||
// Not using Utils::PathChooser::browseButtonLabel to avoid dependency
|
// Not using Utils::PathChooser::browseButtonLabel to avoid dependency
|
||||||
#ifdef Q_OS_MAC
|
#ifdef Q_OS_MAC || defined(Q_OS_LINUX)
|
||||||
m_ui->privateKeyFileButton->setText(tr("Choose..."));
|
m_ui->privateKeyFileButton->setText(tr("Choose..."));
|
||||||
#else
|
#else
|
||||||
m_ui->privateKeyFileButton->setText(tr("Browse..."));
|
m_ui->privateKeyFileButton->setText(tr("Browse..."));
|
||||||
|
|
|
||||||
|
|
@ -213,7 +213,7 @@ QString OpenVpnConfigurator::genOpenVpnConfig(const ServerCredentials &credentia
|
||||||
config.replace("</tls-auth>", "");
|
config.replace("</tls-auth>", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef Q_OS_MAC
|
#if defined Q_OS_MAC || defined(Q_OS_LINUX)
|
||||||
config.replace("block-outside-dns", "");
|
config.replace("block-outside-dns", "");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -236,7 +236,7 @@ QString OpenVpnConfigurator::processConfigWithLocalSettings(QString config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef Q_OS_MAC
|
#if defined Q_OS_MAC || defined(Q_OS_LINUX)
|
||||||
config.replace("block-outside-dns", "");
|
config.replace("block-outside-dns", "");
|
||||||
QString dnsConf = QString(
|
QString dnsConf = QString(
|
||||||
"\nscript-security 2\n"
|
"\nscript-security 2\n"
|
||||||
|
|
@ -259,7 +259,7 @@ QString OpenVpnConfigurator::processConfigWithExportSettings(QString config)
|
||||||
config.append("redirect-gateway def1 bypass-dhcp\n");
|
config.append("redirect-gateway def1 bypass-dhcp\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef Q_OS_MAC
|
#if defined Q_OS_MAC || defined(Q_OS_LINUX)
|
||||||
config.replace("block-outside-dns", "");
|
config.replace("block-outside-dns", "");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,8 +36,12 @@ ErrorCode ShadowSocksVpnProtocol::start()
|
||||||
m_shadowSocksCfgFile.write(QJsonDocument(m_shadowSocksConfig).toJson());
|
m_shadowSocksCfgFile.write(QJsonDocument(m_shadowSocksConfig).toJson());
|
||||||
m_shadowSocksCfgFile.close();
|
m_shadowSocksCfgFile.close();
|
||||||
|
|
||||||
|
#ifdef Q_OS_LINUX
|
||||||
|
QStringList args = QStringList() << "-c" << m_shadowSocksCfgFile.fileName();
|
||||||
|
#else
|
||||||
QStringList args = QStringList() << "-c" << m_shadowSocksCfgFile.fileName()
|
QStringList args = QStringList() << "-c" << m_shadowSocksCfgFile.fileName()
|
||||||
<< "--no-delay";
|
<< "--no-delay";
|
||||||
|
#endif
|
||||||
|
|
||||||
qDebug().noquote() << "ShadowSocksVpnProtocol::start()"
|
qDebug().noquote() << "ShadowSocksVpnProtocol::start()"
|
||||||
<< shadowSocksExecPath() << args.join(" ");
|
<< shadowSocksExecPath() << args.join(" ");
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@
|
||||||
#include "router_win.h"
|
#include "router_win.h"
|
||||||
#elif defined (Q_OS_MAC)
|
#elif defined (Q_OS_MAC)
|
||||||
#include "router_mac.h"
|
#include "router_mac.h"
|
||||||
|
#elif defined Q_OS_LINUX
|
||||||
|
#include "router_linux.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -13,6 +15,8 @@ int Router::routeAddList(const QString &gw, const QStringList &ips)
|
||||||
return RouterWin::Instance().routeAddList(gw, ips);
|
return RouterWin::Instance().routeAddList(gw, ips);
|
||||||
#elif defined (Q_OS_MAC)
|
#elif defined (Q_OS_MAC)
|
||||||
return RouterMac::Instance().routeAddList(gw, ips);
|
return RouterMac::Instance().routeAddList(gw, ips);
|
||||||
|
#elif defined Q_OS_LINUX
|
||||||
|
return RouterLinux::Instance().routeAddList(gw, ips);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -22,6 +26,8 @@ bool Router::clearSavedRoutes()
|
||||||
return RouterWin::Instance().clearSavedRoutes();
|
return RouterWin::Instance().clearSavedRoutes();
|
||||||
#elif defined (Q_OS_MAC)
|
#elif defined (Q_OS_MAC)
|
||||||
return RouterMac::Instance().clearSavedRoutes();
|
return RouterMac::Instance().clearSavedRoutes();
|
||||||
|
#elif defined Q_OS_LINUX
|
||||||
|
return RouterLinux::Instance().clearSavedRoutes();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -31,6 +37,8 @@ int Router::routeDeleteList(const QString &gw, const QStringList &ips)
|
||||||
return RouterWin::Instance().routeDeleteList(gw, ips);
|
return RouterWin::Instance().routeDeleteList(gw, ips);
|
||||||
#elif defined (Q_OS_MAC)
|
#elif defined (Q_OS_MAC)
|
||||||
return RouterMac::Instance().routeDeleteList(gw, ips);
|
return RouterMac::Instance().routeDeleteList(gw, ips);
|
||||||
|
#elif defined Q_OS_LINUX
|
||||||
|
return RouterLinux::Instance().routeDeleteList(gw, ips);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,6 +48,8 @@ void Router::flushDns()
|
||||||
RouterWin::Instance().flushDns();
|
RouterWin::Instance().flushDns();
|
||||||
#elif defined (Q_OS_MAC)
|
#elif defined (Q_OS_MAC)
|
||||||
RouterMac::Instance().flushDns();
|
RouterMac::Instance().flushDns();
|
||||||
|
#elif defined Q_OS_LINUX
|
||||||
|
RouterLinux::Instance().flushDns();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
154
service/server/router_linux.cpp
Normal file
154
service/server/router_linux.cpp
Normal file
|
|
@ -0,0 +1,154 @@
|
||||||
|
#include "router_linux.h"
|
||||||
|
|
||||||
|
#include <QProcess>
|
||||||
|
#include <QThread>
|
||||||
|
#include <utils.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <net/route.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <paths.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
RouterLinux &RouterLinux::Instance()
|
||||||
|
{
|
||||||
|
static RouterLinux s;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RouterLinux::routeAdd(const QString &ipWithSubnet, const QString &gw, const int &sock)
|
||||||
|
{
|
||||||
|
QString ip = Utils::ipAddressFromIpWithSubnet(ipWithSubnet);
|
||||||
|
QString mask = Utils::netMaskFromIpWithSubnet(ipWithSubnet);
|
||||||
|
|
||||||
|
if (!Utils::checkIPv4Format(ip) || !Utils::checkIPv4Format(gw)) {
|
||||||
|
qCritical().noquote() << "Critical, trying to add invalid route: " << ip << gw;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct rtentry route;
|
||||||
|
memset(&route, 0, sizeof( route ));
|
||||||
|
|
||||||
|
// set gateway
|
||||||
|
((struct sockaddr_in *)&route.rt_gateway)->sin_family = AF_INET;
|
||||||
|
((struct sockaddr_in *)&route.rt_gateway)->sin_addr.s_addr = inet_addr(gw.toStdString().c_str());
|
||||||
|
((struct sockaddr_in *)&route.rt_gateway)->sin_port = 0;
|
||||||
|
// set host rejecting
|
||||||
|
((struct sockaddr_in *)&route.rt_dst)->sin_family = AF_INET;
|
||||||
|
((struct sockaddr_in *)&route.rt_dst)->sin_addr.s_addr = inet_addr(ip.toStdString().c_str());
|
||||||
|
((struct sockaddr_in *)&route.rt_dst)->sin_port = 0;
|
||||||
|
// set mask
|
||||||
|
((struct sockaddr_in *)&route.rt_genmask)->sin_family = AF_INET;
|
||||||
|
((struct sockaddr_in *)&route.rt_genmask)->sin_addr.s_addr = inet_addr(mask.toStdString().c_str());
|
||||||
|
((struct sockaddr_in *)&route.rt_genmask)->sin_port = 0;
|
||||||
|
|
||||||
|
route.rt_flags = RTF_UP | RTF_GATEWAY;
|
||||||
|
route.rt_metric = 0;
|
||||||
|
//route.rt_dev = "ens33";
|
||||||
|
|
||||||
|
if (int err = ioctl(sock, SIOCADDRT, &route) < 0)
|
||||||
|
{
|
||||||
|
qDebug().noquote() << "route add error: gw "
|
||||||
|
<< ((struct sockaddr_in *)&route.rt_gateway)->sin_addr.s_addr
|
||||||
|
<< " ip " << ((struct sockaddr_in *)&route.rt_dst)->sin_addr.s_addr
|
||||||
|
<< " mask " << ((struct sockaddr_in *)&route.rt_genmask)->sin_addr.s_addr << " " << err;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int RouterLinux::routeAddList(const QString &gw, const QStringList &ips)
|
||||||
|
{
|
||||||
|
int temp_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
||||||
|
int cnt = 0;
|
||||||
|
for (const QString &ip: ips) {
|
||||||
|
if (routeAdd(ip, gw, temp_sock)) cnt++;
|
||||||
|
}
|
||||||
|
close(temp_sock);
|
||||||
|
return cnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RouterLinux::clearSavedRoutes()
|
||||||
|
{
|
||||||
|
// No need to delete routes after iface down
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// int cnt = 0;
|
||||||
|
// for (const QString &ip: m_addedRoutes) {
|
||||||
|
// if (routeDelete(ip)) cnt++;
|
||||||
|
// }
|
||||||
|
// return (cnt == m_addedRoutes.count());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RouterLinux::routeDelete(const QString &ipWithSubnet, const QString &gw, const int &sock)
|
||||||
|
{
|
||||||
|
QString ip = Utils::ipAddressFromIpWithSubnet(ipWithSubnet);
|
||||||
|
QString mask = Utils::netMaskFromIpWithSubnet(ipWithSubnet);
|
||||||
|
|
||||||
|
if (!Utils::checkIPv4Format(ip) || !Utils::checkIPv4Format(gw)) {
|
||||||
|
qCritical().noquote() << "Critical, trying to remove invalid route: " << ip << gw;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ip == "0.0.0.0") {
|
||||||
|
qDebug().noquote() << "Warning, trying to remove default route, skipping: " << ip << gw;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct rtentry route;
|
||||||
|
memset(&route, 0, sizeof( route ));
|
||||||
|
|
||||||
|
// set gateway
|
||||||
|
((struct sockaddr_in *)&route.rt_gateway)->sin_family = AF_INET;
|
||||||
|
((struct sockaddr_in *)&route.rt_gateway)->sin_addr.s_addr = inet_addr(gw.toStdString().c_str());
|
||||||
|
((struct sockaddr_in *)&route.rt_gateway)->sin_port = 0;
|
||||||
|
// set host rejecting
|
||||||
|
((struct sockaddr_in *)&route.rt_dst)->sin_family = AF_INET;
|
||||||
|
((struct sockaddr_in *)&route.rt_dst)->sin_addr.s_addr = inet_addr(ip.toStdString().c_str());
|
||||||
|
((struct sockaddr_in *)&route.rt_dst)->sin_port = 0;
|
||||||
|
// set mask
|
||||||
|
((struct sockaddr_in *)&route.rt_genmask)->sin_family = AF_INET;
|
||||||
|
((struct sockaddr_in *)&route.rt_genmask)->sin_addr.s_addr = inet_addr(mask.toStdString().c_str());
|
||||||
|
((struct sockaddr_in *)&route.rt_genmask)->sin_port = 0;
|
||||||
|
|
||||||
|
route.rt_flags = RTF_UP | RTF_GATEWAY;
|
||||||
|
route.rt_metric = 0;
|
||||||
|
//route.rt_dev = "ens33";
|
||||||
|
|
||||||
|
if (ioctl(sock, SIOCDELRT, &route) < 0)
|
||||||
|
{
|
||||||
|
qDebug().noquote() << "route delete error: gw " << gw << " ip " << ip << " mask " << mask;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RouterLinux::routeDeleteList(const QString &gw, const QStringList &ips)
|
||||||
|
{
|
||||||
|
int temp_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
||||||
|
int cnt = 0;
|
||||||
|
for (const QString &ip: ips) {
|
||||||
|
if (routeDelete(ip, gw, temp_sock)) cnt++;
|
||||||
|
}
|
||||||
|
close(temp_sock);
|
||||||
|
return cnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RouterLinux::flushDns()
|
||||||
|
{
|
||||||
|
QProcess p;
|
||||||
|
p.setProcessChannelMode(QProcess::MergedChannels);
|
||||||
|
|
||||||
|
p.start("systemctl restart nscd"); //running as root
|
||||||
|
p.waitForFinished();
|
||||||
|
QByteArray output(p.readAll());
|
||||||
|
if (output.isEmpty())
|
||||||
|
qDebug().noquote() << "Flush dns completed";
|
||||||
|
else
|
||||||
|
qDebug().noquote() << "OUTPUT sudo systemctl restart nscd: " + output;
|
||||||
|
}
|
||||||
38
service/server/router_linux.h
Normal file
38
service/server/router_linux.h
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
#ifndef ROUTERLINUX_H
|
||||||
|
#define ROUTERLINUX_H
|
||||||
|
|
||||||
|
#include <QTimer>
|
||||||
|
#include <QString>
|
||||||
|
#include <QSettings>
|
||||||
|
#include <QHash>
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The Router class - General class for handling ip routing
|
||||||
|
*/
|
||||||
|
class RouterLinux : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
static RouterLinux& Instance();
|
||||||
|
|
||||||
|
bool routeAdd(const QString &ip, const QString &gw, const int &sock);
|
||||||
|
int routeAddList(const QString &gw, const QStringList &ips);
|
||||||
|
bool clearSavedRoutes();
|
||||||
|
bool routeDelete(const QString &ip, const QString &gw, const int &sock);
|
||||||
|
bool routeDeleteList(const QString &gw, const QStringList &ips);
|
||||||
|
void flushDns();
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
|
||||||
|
private:
|
||||||
|
RouterLinux() {}
|
||||||
|
RouterLinux(RouterLinux const &) = delete;
|
||||||
|
RouterLinux& operator= (RouterLinux const&) = delete;
|
||||||
|
|
||||||
|
QList<QString> m_addedRoutes;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // ROUTERLINUX_H
|
||||||
|
|
||||||
|
|
@ -46,7 +46,7 @@ LIBS += \
|
||||||
|
|
||||||
macx {
|
macx {
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
router_mac.h
|
router_mac.h \
|
||||||
helper_route_mac.h
|
helper_route_mac.h
|
||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
|
|
@ -54,6 +54,14 @@ SOURCES += \
|
||||||
helper_route_mac.c
|
helper_route_mac.c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
linux {
|
||||||
|
HEADERS += \
|
||||||
|
router_linux.h
|
||||||
|
|
||||||
|
SOURCES += \
|
||||||
|
router_linux.cpp
|
||||||
|
}
|
||||||
|
|
||||||
include(../src/qtservice.pri)
|
include(../src/qtservice.pri)
|
||||||
|
|
||||||
#CONFIG(release, debug|release) {
|
#CONFIG(release, debug|release) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue