mirror of
https://github.com/YACReader/yacreader
synced 2025-05-25 18:00:46 -04:00
Server: Port IP detection and filtering to QNetworkInterface
This commit is contained in:
parent
1a6e36d7d2
commit
9f67bb5e53
@ -12,11 +12,6 @@ DEFINES += YACREADER
|
||||
include (../config.pri)
|
||||
include (../dependencies/pdf_backend.pri)
|
||||
|
||||
unix:haiku {
|
||||
DEFINES += _BSD_SOURCE
|
||||
LIBS += -lnetwork -lbsd
|
||||
}
|
||||
|
||||
CONFIG(force_angle) {
|
||||
contains(QMAKE_TARGET.arch, x86_64) {
|
||||
Release:DESTDIR = ../release64_angle
|
||||
|
@ -18,11 +18,6 @@ DEFINES += SERVER_RELEASE YACREADER_LIBRARY
|
||||
include (../config.pri)
|
||||
include (../dependencies/pdf_backend.pri)
|
||||
|
||||
unix:haiku {
|
||||
DEFINES += _BSD_SOURCE
|
||||
LIBS += -lnetwork -lbsd
|
||||
}
|
||||
|
||||
INCLUDEPATH += ../common/gl
|
||||
|
||||
# there are two builds for Windows, Desktop OpenGL based and ANGLE OpenGL ES based
|
||||
|
@ -1,25 +1,18 @@
|
||||
#include "server_config_dialog.h"
|
||||
#include <QCoreApplication>
|
||||
#include <QGridLayout>
|
||||
#include <QNetworkInterface>
|
||||
#include <QHostInfo>
|
||||
#include <QHostAddress>
|
||||
#include <QSettings>
|
||||
#include <QPalette>
|
||||
#include <QIntValidator>
|
||||
#include <QFormLayout>
|
||||
#include <QBitmap>
|
||||
#include <QPainter>
|
||||
#include <QPixmap>
|
||||
|
||||
#include "server_config_dialog.h"
|
||||
#include "yacreader_http_server.h"
|
||||
#include "yacreader_global_gui.h"
|
||||
|
||||
#include "qnaturalsorting.h"
|
||||
#include "qrcodegen.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
// 192.168 (most comon local subnet for ips are always put first)
|
||||
// IPs are sorted using natoral sorting
|
||||
bool ipComparator(const QString &ip1, const QString &ip2)
|
||||
@ -36,50 +29,6 @@ bool ipComparator(const QString &ip1, const QString &ip2)
|
||||
return naturalSortLessThanCI(ip1, ip2);
|
||||
}
|
||||
|
||||
#ifndef Q_OS_WIN32
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
QList<QString> addresses()
|
||||
{
|
||||
struct ifaddrs *ifAddrStruct = NULL;
|
||||
struct ifaddrs *ifa = NULL;
|
||||
void *tmpAddrPtr = NULL;
|
||||
|
||||
QList<QString> localAddreses;
|
||||
|
||||
getifaddrs(&ifAddrStruct);
|
||||
|
||||
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
|
||||
if (ifa->ifa_addr) {
|
||||
if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4
|
||||
// is a valid IP4 Address
|
||||
tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
|
||||
char addressBuffer[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
|
||||
localAddreses.push_back(QString(addressBuffer));
|
||||
// printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
|
||||
} else if (ifa->ifa_addr->sa_family == AF_INET6) { // check it is IP6
|
||||
// is a valid IP6 Address
|
||||
tmpAddrPtr = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
|
||||
char addressBuffer[INET6_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
|
||||
// printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ifAddrStruct != NULL)
|
||||
freeifaddrs(ifAddrStruct);
|
||||
return localAddreses;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
extern YACReaderHttpServer *httpServer;
|
||||
|
||||
ServerConfigDialog::ServerConfigDialog(QWidget *parent)
|
||||
@ -224,47 +173,20 @@ void ServerConfigDialog::enableperformanceWorkaround(int status)
|
||||
void ServerConfigDialog::generateQR()
|
||||
{
|
||||
ip->clear();
|
||||
QString dir;
|
||||
|
||||
#ifdef Q_OS_WIN32
|
||||
QList<QHostAddress> list = QHostInfo::fromName(QHostInfo::localHostName()).addresses();
|
||||
|
||||
QList<QString> otherAddresses;
|
||||
foreach (QHostAddress add, list) {
|
||||
QString tmp = add.toString();
|
||||
if (tmp.contains(".") && !tmp.startsWith("127")) {
|
||||
otherAddresses.push_back(tmp);
|
||||
QList<QString> addresses;
|
||||
for (auto add : QNetworkInterface::allAddresses()) {
|
||||
// Exclude loopback, local, multicast
|
||||
if (add.isGlobal()) {
|
||||
addresses.push_back(add.toString());
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
QList<QString> list = addresses();
|
||||
std::sort(addresses.begin(), addresses.end(), ipComparator);
|
||||
|
||||
QList<QString> otherAddresses;
|
||||
foreach (QString add, list) {
|
||||
QString tmp = add;
|
||||
if (tmp.contains(".") && !tmp.startsWith("127")) {
|
||||
otherAddresses.push_back(tmp);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
std::sort(otherAddresses.begin(), otherAddresses.end(), ipComparator);
|
||||
|
||||
if (!otherAddresses.isEmpty()) {
|
||||
dir = otherAddresses.first();
|
||||
otherAddresses.pop_front();
|
||||
}
|
||||
|
||||
if (otherAddresses.length() > 0 || !dir.isEmpty()) {
|
||||
if (!dir.isEmpty()) {
|
||||
generateQR(dir + ":" + httpServer->getPort());
|
||||
|
||||
ip->addItem(dir);
|
||||
} else {
|
||||
generateQR(otherAddresses.first() + ":" + httpServer->getPort());
|
||||
}
|
||||
ip->addItems(otherAddresses);
|
||||
if (addresses.length() > 0) {
|
||||
generateQR(addresses.first() + ":" + httpServer->getPort());
|
||||
ip->addItems(addresses);
|
||||
port->setText(httpServer->getPort());
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ DEFINES += SERVER_RELEASE YACREADER_LIBRARY
|
||||
# do a basic dependency check
|
||||
include(headless_config.pri)
|
||||
include(../dependencies/pdf_backend.pri)
|
||||
include(../third_party/QrCode/QrCode.pri)
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat
|
||||
|
||||
@ -32,11 +33,6 @@ macx {
|
||||
CONFIG += objective_c
|
||||
}
|
||||
|
||||
unix:haiku {
|
||||
DEFINES += _BSD_SOURCE
|
||||
LIBS += -lnetwork -lbsd
|
||||
}
|
||||
|
||||
#CONFIG += release
|
||||
CONFIG -= flat
|
||||
QT += core sql network
|
||||
|
Loading…
Reference in New Issue
Block a user