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 Kiwix Server #673

Merged
merged 1 commit into from
Jun 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/kiwix-serve.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ Options
.. option:: -i ADDR, --address=ADDR

Listen only on this IP address. By default the server listens on all
available IP addresses.
available IP addresses. Alternatively, you can use special values to define which types of connections to accept:

- all : Listen for connections on all IP addresses (IPv4 and IPv6).
- ipv4 : Listen for connections on all IPv4 addresses.
- ipv6 : Listen for connections on all IPv6 addresses.


.. option:: -p PORT, --port=PORT
Expand Down
8 changes: 7 additions & 1 deletion src/man/kiwix-serve.1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ By default, kiwix-serve expects a list of ZIM files as command line arguments. P

.TP
\fB-i ADDR, --address=ADDR\fR
Listen only on this IP address. By default, the server listens on all available IP addresses.
Listen only on this IP address. By default, the server listens on all available IP addresses. Alternatively, you can use special values to define which types of connections to accept:

all : Listen for connections on all IP addresses (IPv4 and IPv6).
.br
ipv4 : Listen for connections on all IPv4 addresses.
.br
ipv6 : Listen for connections on all IPv6 addresses.

.TP
\fB-p PORT, --port=PORT\fR
Expand Down
21 changes: 19 additions & 2 deletions src/server/kiwix-serve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void usage()
<< "\t-h, --help\t\tPrint this help" << std::endl << std::endl
<< "\t-a, --attachToProcess\tExit if given process id is not running anymore" << std::endl
<< "\t-d, --daemon\t\tDetach the HTTP server daemon from the main process" << std::endl
<< "\t-i, --address\t\tListen only on this ip address, all available ones otherwise" << std::endl
<< "\t-i, --address\t\tListen only on the specified IP address. Specify 'ipv4', 'ipv6' or 'all' to listen on all IPv4, IPv6 or both types of addresses, respectively (default: all)." << std::endl
<< "\t-M, --monitorLibrary\tMonitor the XML library file and reload it automatically" << std::endl
<< "\t-m, --nolibrarybutton\tDon't print the builtin home button in the builtin top bar overlay" << std::endl
<< "\t-n, --nosearchbar\tDon't print the builtin bar overlay on the top of each served page" << std::endl
Expand Down Expand Up @@ -367,6 +367,19 @@ int main(int argc, char** argv)
auto libraryFileTimestamp = newestFileTimestamp(libraryPaths);
auto curLibraryFileTimestamp = libraryFileTimestamp;

/* Infer ipMode from address */
kiwix::IpMode ipMode = kiwix::IpMode::ipv4;

if (address == "all"){
address = "";
ipMode = kiwix::IpMode::all;
} else if (address == "ipv4"){
address = "";
} else if (address == "ipv6"){
address = "";
ipMode = kiwix::IpMode::ipv6;
}

#ifndef _WIN32
/* Fork if necessary */
if (daemonFlag) {
Expand Down Expand Up @@ -408,12 +421,16 @@ int main(int argc, char** argv)
server.setIndexTemplateString(indexTemplateString);
server.setIpConnectionLimit(ipConnectionLimit);
server.setMultiZimSearchLimit(searchLimit);
server.setIpMode(ipMode);

if (! server.start()) {
exit(1);
}

std::string url = "http://" + server.getAddress() + ":" + std::to_string(server.getPort()) + normalizeRootUrl(rootLocation);
std::string host = (server.getIpMode()==kiwix::IpMode::all || server.getIpMode()==kiwix::IpMode::ipv6)
? "[" + server.getAddress() + "]" : server.getAddress();

std::string url = "http://" + host + ":" + std::to_string(server.getPort()) + normalizeRootUrl(rootLocation);
std::cout << "The Kiwix server is running and can be accessed in the local network at: "
<< url << std::endl;

Expand Down