Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ipv6 support to local server #1221

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions resources/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
"start-kiwix-server":"Start Kiwix Server",
"stop-kiwix-server":"Stop Kiwix Server",
"all":"All",
"all_ips":"All - Dual Stack Mode",
"ipv4":"IPv4 Only Mode",
"ipv6":"IPv6 Only Mode",
"fulltext-search":"Fulltext search",
"pictures":"Pictures",
"videos":"Videos",
Expand Down
3 changes: 3 additions & 0 deletions resources/i18n/qqq.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
"start-kiwix-server": "Represents the action of starting the kiwix server.",
"stop-kiwix-server": "Represents the action of stopping the running kiwix server.",
"all": "{{Identical|All}}",
"all_ips": "Indicates using all ips - dual stack mode on local server.",
"ipv4": "Indicates IPv4 mode on local server.",
"ipv6": "Indicates IPv6 mode on local server",
"fulltext-search": "Represents the action of searching the text appearances in all the articles in a ZIM file.",
"pictures": "{{identical|Picture}}",
"videos": "{{identical|Video}}",
Expand Down
40 changes: 30 additions & 10 deletions src/localkiwixserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,21 @@ LocalKiwixServer::LocalKiwixServer(QWidget *parent) :
QVector<QString> interfaces;
interfaces.reserve(interfacesMap.size() + 1);
for (const auto &interfacePair : interfacesMap) {
QString ip = QString::fromStdString(interfacePair.second.addr);
interfaces.push_back(ip);
QString ipv4 = QString::fromStdString(interfacePair.second.addr);
if (!ipv4.isEmpty() && !ipv4.startsWith("169.254")) interfaces.push_back(ipv4);
QString ipv6 = QString::fromStdString(interfacePair.second.addr6);
if (!ipv6.isEmpty()) interfaces.push_back(ipv6);
}
std::sort(interfaces.begin(), interfaces.end());
interfaces.push_front(QString(gt("all")));
interfaces.push_front(QString(gt("ipv6")));
interfaces.push_front(QString(gt("ipv4")));
interfaces.push_front(QString(gt("all_ips")));
for (const auto &interface : interfaces) {
ui->IpChooser->addItem(interface);
}
ui->IpChooser->setCurrentText(KiwixApp::instance()->getSettingsManager()->getKiwixServerIpAddress());
QString ipAddress = KiwixApp::instance()->getSettingsManager()->getKiwixServerIpAddress();
if (ipAddress == "0.0.0.0") ipAddress = "ipv4";
ui->IpChooser->setCurrentText(ipAddress == "all_ips" || ipAddress == "ipv4" || ipAddress == "ipv6" ? gt(ipAddress) : ipAddress);
sgourdas marked this conversation as resolved.
Show resolved Hide resolved
ui->PortChooser->setText(QString::number(m_port));
ui->PortChooser->setValidator(new QIntValidator(1, 65535, this));
ui->KiwixServerButton->setStyleSheet("QPushButton {background-color: RoyalBlue;"
Expand Down Expand Up @@ -84,18 +90,32 @@ void LocalKiwixServer::runOrStopServer()
auto settingsManager = KiwixApp::instance()->getSettingsManager();
m_port = ui->PortChooser->text().toInt();
mp_server->setPort(m_port);
m_ipAddress = (ui->IpChooser->currentText() != gt("all")) ? ui->IpChooser->currentText() : "0.0.0.0";
settingsManager->setKiwixServerPort(m_port);
settingsManager->setKiwixServerIpAddress(m_ipAddress);
mp_server->setAddress(m_ipAddress.toStdString());
m_ipAddress = (m_ipAddress != "0.0.0.0") ? m_ipAddress : QString::fromStdString(kiwix::getBestPublicIp());
ui->IpAddress->setText("http://" + m_ipAddress + ":" + QString::number(m_port));
ui->IpAddress->setReadOnly(true);
mp_server->setIpMode(kiwix::IpMode::AUTO);
mp_server->setAddress("");
if (ui->IpChooser->currentText() == gt("all_ips")) {
mp_server->setIpMode(kiwix::IpMode::ALL);
settingsManager->setKiwixServerIpAddress("all_ips");
} else if (ui->IpChooser->currentText() == gt("ipv4")) {
mp_server->setIpMode(kiwix::IpMode::IPV4);
settingsManager->setKiwixServerIpAddress("ipv4");
} else if (ui->IpChooser->currentText() == gt("ipv6")) {
mp_server->setIpMode(kiwix::IpMode::IPV6);
settingsManager->setKiwixServerIpAddress("ipv6");
} else {
mp_server->setAddress(ui->IpChooser->currentText().toStdString());
settingsManager->setKiwixServerIpAddress(ui->IpChooser->currentText());
}
if (!mp_server->start()) {
QMessageBox messageBox;
messageBox.critical(0,gt("error-title"),gt("error-launch-server-message"));
return;
}
kiwix::IpAddress serverAddress = mp_server->getAddress();
m_ipAddress = QString::fromStdString(serverAddress.addr.empty() ? serverAddress.addr6 : serverAddress.addr);
if (m_ipAddress.contains(':')) m_ipAddress = "[" + m_ipAddress + "]";
ui->IpAddress->setText("http://" + m_ipAddress + ":" + QString::number(m_port));
ui->IpAddress->setReadOnly(true);
m_active = true;
} else {
mp_server->stop();
Expand Down
Loading