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 | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame^] | 3 | Copyright (c) 2014-2023, 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 | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame^] | 26 | from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer |
Davide Pesavento | b922c88 | 2018-02-24 23:55:57 -0500 | [diff] [blame] | 27 | from urllib.parse import urlsplit |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 28 | import argparse, ipaddress, os, socket, subprocess |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 29 | |
| 30 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 31 | class NfdStatusHandler(SimpleHTTPRequestHandler): |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame^] | 32 | """The handler class to handle HTTP requests.""" |
| 33 | |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 34 | def do_GET(self): |
Davide Pesavento | b922c88 | 2018-02-24 23:55:57 -0500 | [diff] [blame] | 35 | path = urlsplit(self.path).path |
| 36 | if path == "/": |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame^] | 37 | self.__serve_report() |
| 38 | elif path == "/robots.txt" and self.server.allow_robots: |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 39 | self.send_error(404) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 40 | else: |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 41 | super().do_GET() |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 42 | |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame^] | 43 | def __serve_report(self): |
| 44 | """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] | 45 | try: |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame^] | 46 | proc = subprocess.run( |
| 47 | ["nfdc", "status", "report", "xml"], capture_output=True, check=True, text=True, timeout=10 |
| 48 | ) |
| 49 | output = proc.stdout |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 50 | except OSError as err: |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame^] | 51 | super().log_message(f"error invoking nfdc: {err}") |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 52 | self.send_error(500) |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame^] | 53 | except subprocess.SubprocessError as err: |
| 54 | super().log_message(f"error invoking nfdc: {err}") |
| 55 | self.send_error(504, "Cannot connect to NFD") |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 56 | else: |
Junxiao Shi | 9137e9e | 2016-12-15 21:31:50 +0000 | [diff] [blame] | 57 | # add stylesheet processing instruction after the XML document type declaration |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 58 | # (yes, this is a ugly hack) |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame^] | 59 | if (pos := output.find(">") + 1) != 0: |
| 60 | xml = ( |
| 61 | output[:pos] |
| 62 | + '<?xml-stylesheet type="text/xsl" href="nfd-status.xsl"?>' |
| 63 | + output[pos:] |
| 64 | ) |
| 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 | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame^] | 113 | parser.add_argument("-p", "--port", default=8080, 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)") |
| 117 | parser.add_argument("-r", "--robots", action="store_true", |
| 118 | help="allow crawlers and other HTTP bots") |
| 119 | parser.add_argument("-v", "--verbose", action="store_true", |
| 120 | help="turn on verbose logging") |
| 121 | args = parser.parse_args() |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 122 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 123 | os.chdir(args.workdir) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 124 | |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame^] | 125 | with NfdStatusHttpServer(args.address, args.port, robots=args.robots, verbose=args.verbose) as httpd: |
| 126 | if httpd.address_family == socket.AF_INET6: |
| 127 | url = "http://[{}]:{}" |
| 128 | else: |
| 129 | url = "http://{}:{}" |
| 130 | print("Server started at", url.format(*httpd.server_address)) |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 131 | |
Davide Pesavento | decaf50 | 2023-08-30 22:50:03 -0400 | [diff] [blame^] | 132 | try: |
| 133 | httpd.serve_forever() |
| 134 | except KeyboardInterrupt: |
| 135 | pass |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 136 | |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 137 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 138 | if __name__ == "__main__": |
| 139 | main() |