Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 2 | """ |
Davide Pesavento | 48108e0 | 2024-05-13 19:04:25 -0400 | [diff] [blame] | 3 | Copyright (c) 2014-2024, Regents of the University of California, |
Junxiao Shi | e273333 | 2016-11-24 14:11:40 +0000 | [diff] [blame] | 4 | Arizona Board of Regents, |
| 5 | Colorado State University, |
| 6 | University Pierre & Marie Curie, Sorbonne University, |
| 7 | Washington University in St. Louis, |
| 8 | Beijing Institute of Technology, |
| 9 | The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | |
| 11 | This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | |
| 14 | NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | of the GNU General Public License as published by the Free Software Foundation, |
| 16 | either version 3 of the License, or (at your option) any later version. |
| 17 | |
| 18 | NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | PURPOSE. See the GNU General Public License for more details. |
| 21 | |
| 22 | You should have received a copy of the GNU General Public License along with |
| 23 | NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | """ |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 25 | |
Davide Pesavento | 48108e0 | 2024-05-13 19:04:25 -0400 | [diff] [blame] | 26 | import argparse |
| 27 | import ipaddress |
| 28 | import os |
| 29 | import socket |
| 30 | import subprocess |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 31 | from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer |
Davide Pesavento | b922c88 | 2018-02-24 23:55:57 -0500 | [diff] [blame] | 32 | from urllib.parse import urlsplit |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 33 | |
| 34 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 35 | class NfdStatusHandler(SimpleHTTPRequestHandler): |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 36 | """The handler class to handle HTTP requests.""" |
| 37 | |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 38 | def do_GET(self): |
Davide Pesavento | b922c88 | 2018-02-24 23:55:57 -0500 | [diff] [blame] | 39 | path = urlsplit(self.path).path |
| 40 | if path == "/": |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 41 | self.__serve_report() |
| 42 | elif path == "/robots.txt" and self.server.allow_robots: |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 43 | self.send_error(404) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 44 | else: |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 45 | super().do_GET() |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 46 | |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 47 | def __serve_report(self): |
| 48 | """Obtain XML-formatted NFD status report and send it back as response body.""" |
Junxiao Shi | 9137e9e | 2016-12-15 21:31:50 +0000 | [diff] [blame] | 49 | try: |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 50 | proc = subprocess.run( |
| 51 | ["nfdc", "status", "report", "xml"], capture_output=True, check=True, text=True, timeout=10 |
| 52 | ) |
| 53 | output = proc.stdout |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 54 | except OSError as err: |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 55 | super().log_message(f"error invoking nfdc: {err}") |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 56 | self.send_error(500) |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 57 | except subprocess.SubprocessError as err: |
| 58 | super().log_message(f"error invoking nfdc: {err}") |
| 59 | self.send_error(504, "Cannot connect to NFD") |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 60 | else: |
Junxiao Shi | 9137e9e | 2016-12-15 21:31:50 +0000 | [diff] [blame] | 61 | # add stylesheet processing instruction after the XML document type declaration |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 62 | # (yes, this is a ugly hack) |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 63 | if (pos := output.find(">") + 1) != 0: |
Davide Pesavento | 48108e0 | 2024-05-13 19:04:25 -0400 | [diff] [blame] | 64 | xml = output[:pos] + '<?xml-stylesheet type="text/xsl" href="nfd-status.xsl"?>' + output[pos:] |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 65 | self.send_response(200) |
| 66 | self.send_header("Content-Type", "text/xml; charset=UTF-8") |
| 67 | self.end_headers() |
| 68 | self.wfile.write(xml.encode()) |
| 69 | else: |
| 70 | super().log_message(f"malformed nfdc output: {output}") |
| 71 | self.send_error(500) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 72 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 73 | # override |
| 74 | def log_message(self, *args): |
| 75 | if self.server.verbose: |
| 76 | super().log_message(*args) |
| 77 | |
| 78 | |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 79 | class NfdStatusHttpServer(ThreadingHTTPServer): |
| 80 | def __init__(self, addr, port, *, robots=False, verbose=False): |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 81 | # socketserver.BaseServer defaults to AF_INET even if you provide an IPv6 address |
| 82 | # see https://bugs.python.org/issue20215 and https://bugs.python.org/issue24209 |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 83 | if addr.version == 6: |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 84 | self.address_family = socket.AF_INET6 |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 85 | self.allow_robots = robots |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 86 | self.verbose = verbose |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 87 | super().__init__((str(addr), port), NfdStatusHandler) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 88 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 89 | |
| 90 | def main(): |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 91 | def ip_address(arg, /): |
| 92 | """Validate IP address.""" |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 93 | try: |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 94 | value = ipaddress.ip_address(arg) |
| 95 | except ValueError: |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 96 | raise argparse.ArgumentTypeError(f"{arg!r} is not a valid IP address") |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 97 | return value |
| 98 | |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 99 | def port_number(arg, /): |
| 100 | """Validate port number.""" |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 101 | try: |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 102 | value = int(arg) |
| 103 | except ValueError: |
| 104 | value = -1 |
| 105 | if value < 0 or value > 65535: |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 106 | raise argparse.ArgumentTypeError(f"{arg!r} is not a valid port number") |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 107 | return value |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 108 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 109 | parser = argparse.ArgumentParser(description="Serves NFD status page via HTTP") |
| 110 | parser.add_argument("-V", "--version", action="version", version="@VERSION@") |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 111 | parser.add_argument("-a", "--address", default="127.0.0.1", type=ip_address, metavar="ADDR", |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 112 | help="bind to this IP address (default: %(default)s)") |
Davide Pesavento | 48108e0 | 2024-05-13 19:04:25 -0400 | [diff] [blame] | 113 | parser.add_argument("-p", "--port", default=6380, type=port_number, |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 114 | help="bind to this port number (default: %(default)s)") |
| 115 | parser.add_argument("-f", "--workdir", default="@DATAROOTDIR@/ndn", metavar="DIR", |
| 116 | help="server's working directory (default: %(default)s)") |
Davide Pesavento | 48108e0 | 2024-05-13 19:04:25 -0400 | [diff] [blame] | 117 | parser.add_argument("-r", "--robots", action="store_true", help="allow crawlers and other HTTP bots") |
| 118 | parser.add_argument("-v", "--verbose", action="store_true", help="turn on verbose logging") |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 119 | args = parser.parse_args() |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 120 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 121 | os.chdir(args.workdir) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 122 | |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 123 | with NfdStatusHttpServer(args.address, args.port, robots=args.robots, verbose=args.verbose) as httpd: |
| 124 | if httpd.address_family == socket.AF_INET6: |
| 125 | url = "http://[{}]:{}" |
| 126 | else: |
| 127 | url = "http://{}:{}" |
| 128 | print("Server started at", url.format(*httpd.server_address)) |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 129 | |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame] | 130 | try: |
| 131 | httpd.serve_forever() |
| 132 | except KeyboardInterrupt: |
| 133 | pass |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 134 | |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 135 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 136 | if __name__ == "__main__": |
| 137 | main() |