Skip to content

Commit

Permalink
kiwix-serve displays both protocol attached ips
Browse files Browse the repository at this point in the history
  • Loading branch information
sgourdas committed Sep 23, 2024
1 parent c6b72e9 commit b5e180b
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/server/kiwix-serve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,17 +324,18 @@ 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;
kiwix::IpMode ipMode = (address == "all") ? kiwix::IpMode::all :
(address == "ipv6") ? kiwix::IpMode::ipv6 :
(address == "ipv4") ? kiwix::IpMode::ipv4 :
kiwix::IpMode::flex;

if(!address.empty()) { // -i has been provided
if (ipMode == kiwix::IpMode::flex) { // Ip has been provided
if (address.find(':') != std::string::npos) ipMode = kiwix::IpMode::ipv6;
else ipMode = kiwix::IpMode::ipv4;
} else {
address.clear(); // Clear to indicate that protocol has been provided.
}
}

#ifndef _WIN32
Expand Down Expand Up @@ -384,12 +385,14 @@ int main(int argc, char** argv)
exit(1);
}

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;
std::string prefix = "http://";
kiwix::IpAddress addresses = server.getAddress();
std::string suffix = ":" + std::to_string(server.getPort()) + normalizeRootUrl(rootLocation);
std::cout << "The Kiwix server is running and can be accessed in the local network at: " << std::endl;
bool validMode4 = (ipMode == kiwix::IpMode::ipv4 || ipMode == kiwix::IpMode::all); // Mode is valid to support ipv4
bool validMode6 = (ipMode == kiwix::IpMode::ipv6 || ipMode == kiwix::IpMode::all); // Mode is valid to support ipv6
if(!addresses.addr.empty() && validMode4) std::cout << " - " << prefix << addresses.addr << suffix << std::endl;
if(!addresses.addr6.empty() && validMode6) std::cout << " - " << prefix << "[" << addresses.addr6 << "]" << suffix << std::endl;

/* Run endless (until PPID dies) */
waiting = true;
Expand Down

0 comments on commit b5e180b

Please sign in to comment.