Skip to content

Commit

Permalink
Merge pull request #673 from aryanA101a/main
Browse files Browse the repository at this point in the history
Add IPv6 support to Kiwix Server
  • Loading branch information
kelson42 authored Jun 1, 2024
2 parents 2cc1ca4 + 5f69a41 commit 353d20b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
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

0 comments on commit 353d20b

Please sign in to comment.