Skip to content

Commit

Permalink
refactor #2
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanA101a committed May 8, 2024
1 parent 4a41b29 commit e473abc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
9 changes: 4 additions & 5 deletions docs/kiwix-serve.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +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:


.. option:: -6, --ipv6

Enable IPV6 support. Both IPV4 and IPV6 clients are compatible.
all : Accept connections from all IP addresses (IPv4 and IPv6).
ipv4 : Accept connections from all IPv4 addresses.
ipv6 : Accept connections from all IPv6 addresses.


.. option:: -p PORT, --port=PORT
Expand Down
10 changes: 6 additions & 4 deletions src/man/kiwix-serve.1
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +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:

.TP
\fB-6, --ipv6\fR
Enable IPV6 support. Both IPV4 and IPV6 clients are compatible.
all : Accept connections from all IP addresses (IPv4 and IPv6).
.br
ipv4 : Accept connections from all IPv4 addresses.
.br
ipv6 : Accept connections from all IPv6 addresses.

.TP
\fB-p PORT, --port=PORT\fR
Expand Down
31 changes: 20 additions & 11 deletions src/server/kiwix-serve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +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-6, --ipv6\t\tEnable IPV6 support" << std::endl
<< "\t-i, --address\t\tListen only on this ip address, all available ones otherwise, or specify special values to accept connections from ipv4/ipv6/all devices" << 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 @@ -215,7 +214,6 @@ int main(int argc, char** argv)
bool blockExternalLinks = false;
bool isVerboseFlag = false;
bool monitorLibrary = false;
bool ipv6Flag = false;
unsigned int PPID = 0;
int ipConnectionLimit = 0;
int searchLimit = 0;
Expand All @@ -230,7 +228,6 @@ int main(int argc, char** argv)
{"nodatealiases", no_argument, 0, 'z'},
{"nosearchbar", no_argument, 0, 'n'},
{"blockexternallinks", no_argument, 0, 'b'},
{"ipv6", no_argument, 0, 'v'},
{"attachToProcess", required_argument, 0, 'a'},
{"port", required_argument, 0, 'p'},
{"address", required_argument, 0, 'i'},
Expand All @@ -247,7 +244,7 @@ int main(int argc, char** argv)
while (true) {
int option_index = 0;
int c
= getopt_long(argc, argv, "hzmnbdvVl6a:p:f:t:r:i:c:ML:s:", long_options, &option_index);
= getopt_long(argc, argv, "hzmnbdvVla:p:f:t:r:i:c:ML:s:", long_options, &option_index);

if (c != -1) {
auto insertRes = usedOptions.insert(c);
Expand Down Expand Up @@ -310,9 +307,6 @@ int main(int argc, char** argv)
case 's':
searchLimit = atoi(optarg);
break;
case '6':
ipv6Flag = true;
break;
case '?':
usage();
return 2;
Expand Down Expand Up @@ -363,6 +357,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 @@ -404,14 +411,16 @@ int main(int argc, char** argv)
server.setIndexTemplateString(indexTemplateString);
server.setIpConnectionLimit(ipConnectionLimit);
server.setMultiZimSearchLimit(searchLimit);
server.setIPv6(ipv6Flag);
server.setIpMode(ipMode);

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

std::string url = "http://" + (server.isAddressIPv6()? "[" + server.getAddress() + "]" : 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 e473abc

Please sign in to comment.