#!/usr/bin/env python3
"""
A diagnostic tool for gemini servers.

This program will barrage your server with a series of requests in
an attempt to uncover unexpected behavior. Not all of these checks
adhere strictly to the gemini specification. Some of them are
general best practices, and some trigger undefined behavior. Results
should be taken with a grain of salt and analyzed on their own merit.
"""
import argparse
import contextlib
import datetime
from datetime import timezone
import ipaddress
import socket
import ssl
import sys
import time
import re
import difflib
import subprocess
from urllib3.util.ssl_match_hostname import match_hostname

if sys.version_info < (3, 7):
    sys.exit("Fatal Error: script requires Python 3.7+")


__title__ = "Gemini Diagnostics"
__author__ = "Michael Lazar"
__license__ = "MIT"
__copyright__ = "(c) 2020 Michael Lazar"
__version__ = "1.0.0"


socket.setdefaulttimeout(5)


# ANSI color codes
A_BOLD = 1
FG_BLACK = 30
FG_RED = 31
FG_GREEN = 32
FG_YELLOW = 33
FG_BLUE = 34
FG_MAGENTA = 35
FG_CYAN = 36
FG_WHITE = 37


LOG_STYLES = {
    "normal": ["", None],
    "title": ["", A_BOLD],
    "warning": ["  ", FG_YELLOW],
    "info": ["  ", FG_CYAN],
    "success": ["  ✓ ", FG_GREEN],
    "failure": ["  x ", FG_RED]
}


CHECKS = []

def show_diff(left_text, right_text):
    for left, right in zip(left_text.splitlines(True), right_text.splitlines(True)):
        if left != right:
            print(f"\033[{FG_GREEN}m", "<", repr(left)[1:-1], "\033[0m")
            print(f"\033[{FG_RED}m", ">", repr(right)[1:-1], "\033[0m")


def log(text, style="normal"):
    prefix, color = LOG_STYLES[style]
    if prefix:
        text = prefix + text
    if color and sys.stdout.isatty():
        text = f"\033[{color}m{text}\033[0m"

    print(text)


def log_error(err) -> None:
    text = str(err)
    if isinstance(err, Warning):
        log(text, style="warning")
    else:
        log(text, style="failure")


class GeminiResponse:

    def __init__(self):
        self.header = None
        self.status = None
        self.meta = None
        self.body = None

        # Extra info for 2x success responses
        self.mime = None
        self.params = {}

    def read(self, fp):
        self.header = fp.readline().decode("utf-8")

        log("Response header")
        log(f"{self.header!r}", style="info")

        self.status, *header_rest = self.header.strip().split(maxsplit=1)
        self.meta = header_rest[0] if header_rest else ""

        if not self.meta:
            raise Exception("Status should include a <META> line")

        if self.status.startswith("2"):
            meta_parts = self.meta.split(";")
            self.mime = meta_parts[0].strip()
            for part in meta_parts[1:]:
                name, value = part.strip().split("=", maxsplit=1)
                self.params[name.lower()] = value

        charset = self.params.get("charset", "utf-8")
        self.body = fp.read().decode(charset)


class CheckRegistry(type):

    def __new__(cls, name, bases, namespace):
        cls = type.__new__(cls, name, bases, namespace)
        if name != 'BaseCheck':
            CHECKS.append(cls)
        return cls


class BaseCheck(metaclass=CheckRegistry):

    def __init__(self, args):
        self.args = args
        self.success = False


    def run(self):
        log(f"[{self.__class__.__name__}] {self.__doc__}", style="title")
        try:
            self.check()
        except Exception as e:
            self.success = False
            log_error(e)
        log("")
        return self.success

    def check(self):
        raise NotImplemented

    @property
    def netloc(self):
        if self.args.port == 1965:
            return self.args.host
        else:
            return f"{self.args.host}:{self.args.port}"

    def resolve_host(self, family):
        host = self.args.host
        port = self.args.port
        type_ = socket.SOCK_STREAM
        proto = socket.IPPROTO_TCP
        addr_info = socket.getaddrinfo(host, port, family, type_, proto)
        if not addr_info:
            raise UserWarning(f"No {family} address found for host")

        # Gemini IPv6
        return addr_info[0][4]

    @contextlib.contextmanager
    def connection(self, context=None, client_cert=None):
        """
        Setup an unverified TLS socket connection with the host.
        """
        if context is None:
            if hasattr(ssl, 'PROTOCOL_TLS_CLIENT'):
                context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
            elif hasattr(ssl, 'PROTOCOL_TLS'):
                context = ssl.SSLContext(ssl.PROTOCOL_TLS)
            elif hasattr(ssl, 'PROTOCOL_TLSv1_2'):
                context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
            else:
                raise Exception("TLS support is required")
            context.check_hostname = False
            context.verify_mode = ssl.CERT_NONE
            if client_cert:
                context.load_cert_chain(
                    certfile=f"./test_data/client_cert/{client_cert}.crt",
                    keyfile=f"./test_data/client_cert/{client_cert}.key")

        with socket.create_connection(self.args.addr, timeout=5) as sock:
            with context.wrap_socket(sock, server_hostname=self.args.host) as ssock:
                yield ssock

    def make_request(self, url, context=None, client_cert=None):
        log("Request URL")
        log(f"{url!r}", style="info")
        with self.connection(context=context, client_cert=client_cert) as sock:
            sock.sendall(url.encode(errors="surrogateescape"))
            fp = sock.makefile("rb")
            response = GeminiResponse()
            response.read(fp)
            return response

    def assert_success(self, status):
        log("Status should return a success code (20 SUCCESS)")
        self.log_test(f"Received status of {status!r}", status == "20")

    def assert_permanent_failure(self, status):
        log("Status should return a failure code (5X PERMANENT FAILURE)")
        self.log_test(f"Received status of {status!r}", status.startswith("5"))

    def assert_proxy_refused(self, status):
        log("Status should return a failure code (53 PROXY REQUEST REFUSED)")
        self.log_test(f"Received status of {status!r}", status == "53")

    def assert_bad_request(self, status):
        log("Status should return a failure code (59 BAD REQUEST)")
        self.log_test(f"Received status of {status!r}", status == "59")

    def log_test(self, text, success):
        self.success = success
        if success:
            log(text, "success")
        else:
            log(text, "failure")


class IPv4Address(BaseCheck):
    """Establish a connection over an IPv4 address"""

    def check(self):
        log(f"Looking up IPv4 address for {self.args.host!r}")
        addr = self.resolve_host(socket.AF_INET)
        log(f"{addr[0]!r}", style="success")

        log(f"Attempting to connect to {addr[0]}:{addr[1]}")
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
            sock.connect(addr)
            sock.close()
        log(f"Successfully established connection", style="success")
        self.success = True


class IPv6Address(BaseCheck):
    """Establish a connection over an IPv6 address"""

    def check(self):
        log(f"Looking up IPv6 address for {self.args.host!r}")
        addr = self.resolve_host(socket.AF_INET6)
        if ipaddress.ip_address(addr[0]).ipv4_mapped:
            raise UserWarning("Found IPv4-mapped address, skipping check")
        log(f"{addr[0]!r}", style="success")

        log(f"Attempting to connect to [{addr[0]}]:{addr[1]}")
        with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as sock:
            sock.connect(addr)
            sock.close()
        log(f"Successfully established connection", style="success")
        self.success = True


class TLSVersion(BaseCheck):
    """Server must negotiate at least TLS v1.2, ideally TLS v1.3"""

    def check(self):
        log(f"Checking client library")
        log(f"{ssl.OPENSSL_VERSION!r}", style="info")

        log("Determining highest supported TLS version")
        with self.connection() as sock:
            version = sock.version()
            if version in ("SSLv2", "SSLv3", "TLSv1", "TLSv1.1"):
                log(f"Negotiated {version}", style="failure")
            elif version == "TLSv1.2":
                log(f"Negotiated {version}", style="warning")
                self.success = True
            else:
                log(f"Negotiated {version}", style="success")
                self.success = True


class TLSClaims(BaseCheck):
    """Certificate claims must be valid"""

    def check(self):
        try:
            import cryptography
            from cryptography import x509
            from cryptography.x509.oid import NameOID, ExtensionOID
        except ImportError:
            raise UserWarning(
                "cryptography library not installed, skipping check")

        with self.connection() as sock:
            # Python refuses to parse a certificate unless the issuer is validated.
            # Because many gemini servers use self-signed certs, we need to use
            # a third-party library to parse the certs from their binary form.
            der_x509 = sock.getpeercert(binary_form=True)
            cert = x509.load_der_x509_certificate(der_x509)
            now = datetime.datetime.now(timezone.utc)

            log('Checking "Not Valid Before" timestamp')
            self.log_test(f"{cert.not_valid_before_utc} UTC",
                          cert.not_valid_before_utc <= now)

            log('Checking "Not Valid After" timestamp')
            self.log_test(f"{cert.not_valid_after_utc} UTC",
                          cert.not_valid_after_utc >= now)

            log("Checking subject claim matches server hostname")
            subject = []
            for cn in cert.subject.get_attributes_for_oid(NameOID.COMMON_NAME):
                subject.append(("commonName", cn.value))

            subject_alt_name = []
            try:
                ext = cert.extensions.get_extension_for_oid(
                    ExtensionOID.SUBJECT_ALTERNATIVE_NAME
                )
            except cryptography.x509.ExtensionNotFound:
                pass
            else:
                for dns in ext.value.get_values_for_type(cryptography.x509.DNSName):
                    subject_alt_name.append(("DNS", dns))
                for ip_address in ext.value.get_values_for_type(
                    cryptography.x509.IPAddress
                ):
                    subject_alt_name.append(("IP Address", ip_address))

            cert_dict = {
                "subject": (tuple(subject),),
                "subjectAltName": tuple(subject_alt_name),
            }
            log(f"{cert_dict!r}", style="info")
            match_hostname(cert_dict, self.args.host)
            log(f"Hostname {self.args.host!r} matches claim", style="success")
            self.success = True


class TLSVerified(BaseCheck):
    """Certificate should be self-signed or have a trusted issuer"""

    def check(self):
        log("Connecting over verified SSL socket")
        context = ssl.create_default_context()
        try:
            with socket.create_connection((self.args.host, self.args.port)) as sock:
                with context.wrap_socket(sock, server_hostname=self.args.host) as ssock:
                    ssock.sendall(f"gemini://{self.netloc}\r\n".encode())
        except Exception as e:
            if getattr(e, "verify_code", None) == 18:
                log("Self-signed TLS certificate detected", style="success")
                self.success = True
            else:
                raise
        else:
            log("CA-signed TLS certificate detected", style="warning")


class TLSCloseNotify(BaseCheck):
    """Server should send a close_notify alert before closing the connection"""

    def check(self):
        log("Checking for close_notify TLS signal")

        context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
        context.check_hostname = False
        context.verify_mode = ssl.CERT_NONE

        close_notify_received = False

        def msg_cb(connection, direction, v, c, m, data):
            if m == ssl._TLSAlertType.CLOSE_NOTIFY and direction == 'read':
                nonlocal close_notify_received
                close_notify_received = True

        context._msg_callback = msg_cb

        url = f"gemini://{self.netloc}/\r\n"
        self.make_request(url, context)

        if close_notify_received:
            log("TLS close_notify signal was received successfully", style="success")
            self.success = True
        else:
            log("TLS close_notify signal was not received", style="failure")


class TLSRequired(BaseCheck):
    """Non-TLS requests should be refused"""

    def check(self):
        log("Sending non-TLS request")
        try:
            with socket.create_connection((self.args.host, self.args.port)) as sock:
                sock.sendall(f"gemini://{self.netloc}\r\n".encode())
                fp = sock.makefile("rb")
                header = fp.readline()
                if header:
                    if header[0] == 0x15 and header[5] == 0x02:
                        log(f"Server sent fatal TLS alert", style="success")
                        self.success = True
                    else:
                        log(f"Received unexpected response {header.decode()!r}",
                            style="failure")
                else:
                    log(f"Connection closed by server", style="success")
                    self.success = True
        except Exception as e:
            # A connection error is a valid response
            log(f"{e!r}", style="success")


class ConcurrentConnections(BaseCheck):
    """Server should support concurrent connections"""

    def check(self):
        url = f"gemini://{self.netloc}/\r\n"

        log(f"Attempting to establish two connections")
        with self.connection() as sock:
            log("Opening socket 1", style="info")
            sock.send(url[0].encode())
            with self.connection() as sock2:
                log("Opening socket 2", style="info")
                sock2.sendall(url.encode())
                log("Closing socket 2", style="info")
            sock.sendall(url[1:].encode())
            log("Closing socket 1", style="info")

        log(f"Concurrent connections supported", style="success")
        self.success = True


class ResponseFormat(BaseCheck):
    """Validate the response header and body for the root URL"""

    def check(self):
        url = f"gemini://{self.netloc}/\r\n"
        response = self.make_request(url)

        self.assert_success(response.status)

        log("There should be a single space between <STATUS> and <META>")
        success = response.header[2] == " " and response.header[3] != " "
        self.log_test(f"{response.header[1:4]!r}", success)

        log('Mime type should be "text/gemini"')
        self.log_test(f"{response.mime!r}", response.mime == "text/gemini")

        log(r'Header should end with "\r\n"')
        self.log_test(f"{response.header[-2:]!r}",
                      response.header.endswith("\r\n"))

        log("Body should be non-empty")
        self.log_test(f"{response.body[:50]!r}", response.body)

        log(r'Body should use consistent line endings')
        lines = {"crlf": [], "lf": [], "other": []}
        for line in response.body.splitlines(True):
            if line.endswith("\r\n"):
                lines["crlf"].append(line)
            elif line.endswith("\n"):
                lines["lf"].append(line)
            elif line:
                lines["other"].append(line)

        if lines["other"]:
            log(f"Invalid line ending: {lines['other'][0]!r}", style="failure")
        elif lines["crlf"] and lines["lf"]:
            log("Mixed line endings detected", style="failure")
            log(f"Line 1: {lines['crlf'][0]!r}")
            log(f"Line 2: {lines['lf'][0]!r}")
        elif lines["crlf"]:
            log(r"All lines end with \r\n", style="success")
        elif lines["lf"]:
            log(r"All lines end with \n", style="success")


class HomepageNoRedirect(BaseCheck):
    """The root URL should return the same resource with or without the trailing slash."""

    def check(self):
        url = f"gemini://{self.netloc}/\r\n"
        response = self.make_request(url)
        self.assert_success(response.status)

class PageNotFound(BaseCheck):
    """Request a gemini URL that does not exist"""

    def check(self):
        url = f"gemini://{self.netloc}/09pdsakjo73hjn12id78\r\n"
        response = self.make_request(url)

        log("Status should return code 51 (NOT FOUND)")
        self.log_test(f"{response.status!r}", response.status == "51")

        log('Header should end with "\\r\\n"')
        self.log_test(f"{response.header[-2:]!r}",
                      response.header.endswith("\r\n"))

        log("Body should be empty")
        self.log_test(f"{response.body[:50]!r}", response.body == "")

class RootDoesntExist(BaseCheck):
    """Request a gemini URL that does not exist"""

    def check(self):
        url = f"gemini://{self.netloc}/no-exist\r\n"
        response = self.make_request(url)

        log("Status should return code 51 (NOT FOUND)")
        self.log_test(f"{response.status!r}", response.status == "51")

        log('Header should end with "\\r\\n"')
        self.log_test(f"{response.header[-2:]!r}",
                      response.header.endswith("\r\n"))

        log("Body should be empty")
        self.log_test(f"{response.body[:50]!r}", response.body == "")


class RequestMissingCR(BaseCheck):
    """A request without a <CR> should timeout"""

    def check(self):
        url = f"gemini://{self.netloc}/\n"
        try:
            response = self.make_request(url)
        except Exception as _:
            log("No response received", style="failure")
        else:
            self.assert_bad_request(response.status)


class URLIncludePort(BaseCheck):
    """Send the URL with the port explicitly defined"""

    def check(self):
        url = f"gemini://{self.args.host}:{self.args.port}/\r\n"
        response = self.make_request(url)
        self.assert_success(response.status)


class URLSchemeMissing(BaseCheck):
    """A URL without a scheme should be inferred as gemini"""

    def check(self):
        url = f"//{self.netloc}/\r\n"
        response = self.make_request(url)
        self.assert_bad_request(response.status)


class URLByIPAddress(BaseCheck):
    """Send the URL using the IPv4 address"""

    def check(self):
        addr = self.resolve_host(socket.AF_INET)
        url = f"gemini://{addr[0]}:{addr[1]}/\r\n"
        response = self.make_request(url)

        log("Verify that the status matches your desired behavior")
        log(f"{response.status!r}", style="success")
        self.success = True


class URLInvalidUTF8Byte(BaseCheck):
    """Send a URL containing a non-UTF8 byte sequence"""

    def check(self):
        non_utf8_character = "\udcdc"  # Surrogate-escaped byte sequence
        url = f"gemini://{self.netloc}/{non_utf8_character}\r\n"

        try:
            response = self.make_request(url)
        except Exception:
            response = None

        log("Connection should either drop, or return 59 (BAD REQUEST)")
        if response is None:
            log("Connection closed without response", style="success")
        else:
            self.log_test(f"{response.status!r}", response.status == "59")


class URLMaxSize(BaseCheck):
    """Send a 1024 byte URL, the maximum allowed size"""

    def check(self):
        # Per the spec, the <CR><LF> are not included in the total size
        base_url = f"gemini://{self.netloc}/"
        buffer = "0" * (1024 - len(base_url.encode("utf-8")))
        url = base_url + buffer + "\r\n"

        response = self.make_request(url)
        log("Status should return code 51 (NOT FOUND)")
        self.log_test(f"{response.status!r}", response.status == "51")


class URLAboveMaxSize(BaseCheck):
    """Send a 1025 byte URL, above the maximum allowed size"""

    def check(self):
        # Per the spec, the <CR><LF> are not included in the total size
        base_url = f"gemini://{self.netloc}/"
        buffer = "0" * (1025 - len(base_url.encode("utf-8")))
        url = base_url + buffer + "\r\n"

        try:
            response = self.make_request(url)
        except Exception:
            response = None

        log("Connection should either drop, or return 59 (BAD REQUEST)")
        if response is None:
            log("Connection closed without response", style="success")
        else:
            self.log_test(f"{response.status!r}", response.status == "59")


class URLWrongPort(BaseCheck):
    """A URL with an incorrect port number should be rejected"""

    def check(self):
        url = f"gemini://{self.args.host}:443/\r\n"
        response = self.make_request(url)
        self.assert_proxy_refused(response.status)


class URLWrongHost(BaseCheck):
    """A URL with a foreign hostname should be rejected"""

    def check(self):
        url = f"gemini://wikipedia.org/\r\n"
        response = self.make_request(url)
        self.assert_proxy_refused(response.status)


class URLSchemeHTTP(BaseCheck):
    """Send a URL with an HTTP scheme"""

    def check(self):
        url = f"http://{self.netloc}/\r\n"
        response = self.make_request(url)
        self.assert_proxy_refused(response.status)


class URLSchemeHTTPS(BaseCheck):
    """Send a URL with an HTTPS scheme"""

    def check(self):
        url = f"https://{self.netloc}/\r\n"
        response = self.make_request(url)
        self.assert_proxy_refused(response.status)


class URLSchemeGopher(BaseCheck):
    """Send a URL with a Gopher scheme"""

    def check(self):
        url = f"gopher://{self.netloc}/\r\n"
        response = self.make_request(url)
        self.assert_proxy_refused(response.status)


class URLEmpty(BaseCheck):
    """Empty URLs should not be accepted by the server"""

    def check(self):
        url = f"\r\n"
        response = self.make_request(url)
        self.assert_bad_request(response.status)


class URLRelative(BaseCheck):
    """Relative URLs should not be accepted by the server"""

    def check(self):
        url = f"/\r\n"
        response = self.make_request(url)
        self.assert_bad_request(response.status)


class URLInvalid(BaseCheck):
    """Random text should not be accepted by the server"""

    def check(self):
        url = f"Hello Gemini!\r\n"
        response = self.make_request(url)
        self.assert_bad_request(response.status)

class URLFragment(BaseCheck):
    """Server should reject a URL with a fragment"""

    def check(self):
        url = f"gemini://{self.netloc}#fragment/\r\n"
        response = self.make_request(url)
        self.assert_bad_request(response.status)


class URLDotEscape(BaseCheck):
    """A URL should not be able to escape the root using dot notation"""

    def check(self):
        url = f"gemini://{self.netloc}/../../\r\n"
        response = self.make_request(url)

        log("Status should return code 31 (REDIRECT PERMANENT)")
        self.log_test(f"{response.status!r}", response.status == "31")

        log('Meta should redirect to location "gemini://[hostname]/"')
        self.log_test(f"{response.meta!r}", response.meta ==
                      f"gemini://{self.netloc}/")

        log(r'Header should end with "\r\n"')
        self.log_test(f"{response.header[-2:]!r}",
                      response.header.endswith("\r\n"))

        log("Body should be empty")
        self.log_test(f"{response.body[:50]!r}", response.body == "")


class CGIVars(BaseCheck):
    """test.cgi should print all of the CGI environment variable with the right values"""

    def check(self):
        url = "gemini://localhost/cgi-bin/test.cgi/testing123?hello%20world\r\n"
        response = self.make_request(url, client_cert="cgi")

        log("Status should return code 20")
        self.log_test(f"{response.status!r}", response.status == "20")

        correct_body = """
Hello world!
SCRIPT_NAME: /cgi-bin/test.cgi
PATH_INFO: /testing123
QUERY_STRING: hello%20world
SERVER_SOFTWARE: stargazer/
AUTH_TYPE: CERTIFICATE
TLS_CLIENT_HASH: 5A7FpBaRak0L7VcjRzq136hfBbKbTGFikbrNF3IjEGQ=
TLS_CLIENT_NOT_BEFORE: 2020-12-02T01:31:22Z
TLS_CLIENT_NOT_AFTER: 2020-12-03T01:31:22Z
REMOTE_USER: gemini user
SUBJECT: CN=gemini user
ISSUER: CN=gemini user
        """.strip()
        body = response.body.strip()
        del_version = re.compile(r'(SERVER_SOFTWARE: stargazer/).*$', re.MULTILINE)
        body = del_version.sub(r'\1', body)
        log("Body should match")
        self.log_test("", body == correct_body)
        if body != correct_body:
            print("Got:")
            print(body)
            print("\nDiff:")
            show_diff(correct_body, body)

class CGIIndex(BaseCheck):
    """Test that index CGI scripts are run when a directory is requestes"""

    def check(self):
        url = "gemini://localhost/cgi-bin/123456\r\n"
        response = self.make_request(url, client_cert="cgi")

        log("Status should return code 20")
        self.log_test(f"{response.status!r}", response.status == "20")

        correct_body = "Hello world! /123456"
        body = response.body.strip()
        log("Body should match")
        self.log_test("", body == correct_body)
        if body != correct_body:
            print("Got:")
            print(body)
            print("\nDiff:")
            show_diff(correct_body, body)

class CGITimeout(BaseCheck):
    """Test that CGI script that takes too long to response times out and it killed"""

    def check(self):
        url = "gemini://localhost/cgi-bin/loop\r\n"
        if subprocess.run(["pgrep", "loop"], capture_output=True).returncode == 0:
            log("loop already running, test cannot commence", style="failure")
            return
        log(f"{url!r}", style="info")
        with self.connection() as sock:
            sock.sendall(url.encode(errors="surrogateescape"))
            if subprocess.run(["pgrep", "loop"], capture_output=True).returncode != 0:
                log("loop not started", style="failure")
                return
            time.sleep(1)
            log("Waiting 5 seconds for timeout")
            time.sleep(5)
            fp = sock.makefile("rb")
            response = GeminiResponse()
            response.read(fp)
            self.log_test(f"{response.status!r}", response.status == "42")
            time.sleep(2)
            exit_code = subprocess.run(["pgrep", "loop"]).returncode
            self.log_test("loop stopped", exit_code != 0)
        


class SCGIVars(BaseCheck):
    """test.cgi should print all of the CGI environment variable with the right values"""

    def check(self):
        url = "gemini://localhost/scgi/testing123?hello%20world\r\n"
        response = self.make_request(url, client_cert="cgi")

        self.assert_success(response.status)

        correct_body = """
Hello world!
AUTH_TYPE: CERTIFICATE
CONTENT_LENGTH: 0
GEMINI_URL: gemini://localhost/scgi/testing123?hello%20world
HOSTNAME: localhost
PATH_INFO: /testing123
QUERY_STRING: hello%20world
REMOTE_ADDR: 127.0.0.1
REMOTE_HOST: 127.0.0.1
REMOTE_USER: gemini user
SCGI: 1
SCRIPT_NAME: /scgi
SERVER_NAME: localhost
SERVER_PORT: 1965
SERVER_PROTOCOL: GEMINI
SERVER_SOFTWARE: stargazer/
TLS_CIPHER: TLS_AES_256_GCM_SHA384
TLS_CLIENT_HASH: 5A7FpBaRak0L7VcjRzq136hfBbKbTGFikbrNF3IjEGQ=
TLS_CLIENT_ISSUER: CN=gemini user
TLS_CLIENT_NOT_AFTER: 2020-12-03T01:31:22Z
TLS_CLIENT_NOT_BEFORE: 2020-12-02T01:31:22Z
TLS_CLIENT_SUBJECT: CN=gemini user
TLS_VERSION: TLSv1.3
        """.strip()
        body = response.body.strip()
        del_version = re.compile(r'(SERVER_SOFTWARE: stargazer/).*$', re.MULTILINE)
        body = del_version.sub(r'\1', body)
        log("Body should match")
        self.log_test("", body == correct_body)
        if body != correct_body:
            print("Got:")
            print(body)
            print("\nDiff:")
            show_diff(correct_body, body)

class RedirectRoute(BaseCheck):
    """Request redirect route"""

    def check(self):
        url = "gemini://localhost/root\r\n"
        response = self.make_request(url)

        log("Status should return code 31")
        self.log_test(f"{response.status!r}", response.status == "31")

        log("Meta should be ..")
        self.log_test(f"{response.meta!r}", response.meta == "..")

class LangParam(BaseCheck):
    """Check that the lang param is included in the MIME type"""

    def check(self):
        url = "gemini://localhost/en.gmi\r\n"
        response = self.make_request(url)

        self.assert_success(response.status)

        log("Meta should be text/gemini; lang=en")
        self.log_test(f"{response.meta!r}", response.meta == "text/gemini; lang=en")
        
class CharsetParam(BaseCheck):
    """Check that the charset param is included in the MIME type"""

    def check(self):
        url = "gemini://localhost/plain.txt\r\n"
        response = self.make_request(url)

        self.assert_success(response.status)

        log("Meta should be text/plain; charset=ascii")
        self.log_test(f"{response.meta!r}", response.meta == "text/plain; charset=ascii")

class DirectoryListing(BaseCheck):
    """Check that directory listing works and is in order"""

    def check(self):
        url = "gemini://localhost/list/\r\n"
        response = self.make_request(url)

        self.assert_success(response.status)
        
        correct_body = """
=> ..
=>sub1/ sub1
=>sub2/ sub2
=>%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF%E4%B8%96%E7%95%8C%EF%BC%81%2Etxt こんにちは世界！.txt
=>1%2Etxt 1.txt
=>10%2Etxt 10.txt
=>2%2Etxt 2.txt
=>3%2Etxt 3.txt
=>4%2Etxt 4.txt
=>5%2Etxt 5.txt
=>6%2Etxt 6.txt
=>7%2Etxt 7.txt
=>8%2Etxt 8.txt
=>9%2Etxt 9.txt
=>hello%2530%23%5E%5C%26%23%24%25%5F%2B%5F%21%21%7E%7E%7E%60%60%60%2Etxt hello%30#^\&#$%_+_!!~~~```.txt
        """.strip()
        body = response.body.strip()
        log("Body should match")
        self.log_test("", body == correct_body)
        if body != correct_body:
            print("Got:")
            print(body)
            print("\nDiff:")
            show_diff(correct_body, body)

class RedirectDirectory(BaseCheck):
    """Directories without a trailing slash should be redirected excluding root"""

    def check(self):
        url = "gemini://localhost/dir\r\n"
        response = self.make_request(url)

        log("Status should return code 31")
        self.log_test(f"{response.status!r}", response.status == "31")

        log("Meta should be ..")
        self.log_test(f"{response.meta!r}", response.meta == "/dir/")

class NoClientCert(BaseCheck):
    """Request a page the requires a client cert without providing one"""

    def check(self):
        url = "gemini://localhost/priv.gmi\r\n"
        response = self.make_request(url)

        log("Status should return code 60")
        self.log_test(f"{response.status!r}", response.status == "60")
        
class ClientCert(BaseCheck):
    """Request a page the requires a client cert"""

    def check(self):
        url = "gemini://localhost/priv.gmi\r\n"
        response = self.make_request(url, client_cert="good")
        self.assert_success(response.status)

class BadClientCert(BaseCheck):
    """Request a page the requires a client cert"""

    def check(self):
        url = "gemini://localhost/priv.gmi\r\n"
        response = self.make_request(url, client_cert="bad")

        log("Status should return code 61")
        self.log_test(f"{response.status!r}", response.status == "61")

class RewriteURL(BaseCheck):
    """Request a URL that does a rewrite"""

    def check(self):
        url = "gemini://localhost/en.gemini\r\n"
        response = self.make_request(url)

        self.assert_success(response.status)

        log("Meta should be text/gemini; lang=en")
        self.log_test(f"{response.meta!r}", response.meta == "text/gemini; lang=en")
        
class RedirectRewrite(BaseCheck):
    """Request a URL that performs a rewrite on a redirect"""

    def check(self):
        url = "gemini://example.com/index.gmi\r\n"
        response = self.make_request(url)

        log("Status should return code 30")
        self.log_test(f"{response.status!r}", response.status == "30")

        log("Meta should be ..")
        self.log_test(f"{response.meta!r}", response.meta == "gemini://localhost/index.gmi")

class MimeOverride(BaseCheck):
    """Request an rss feed with a mime override"""

    def check(self):
        url = "gemini://localhost/rss.xml\r\n"
        response = self.make_request(url)

        self.assert_success(response.status)

        log("Meta should be ..")
        self.log_test(f"{response.meta!r}", response.meta == "application/atom+xml")


# noinspection PyTypeChecker
# fmt: off
parser = argparse.ArgumentParser(
    usage="%(prog)s [host] [port] [--help]",
    description=__doc__,
    formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
    "host",
    nargs="?",
    default="localhost",
    help="server hostname (default: localhost)"
)
parser.add_argument(
    "port",
    nargs="?",
    type=int,
    default=1965,
    help="server port (default: 1965)",
)
parser.add_argument(
    "-V", "--version",
    action="version",
    version="gemini-diagnostics " + __version__
)
parser.add_argument(
    "--checks",
    help="comma separated list of checks to apply"
)
parser.add_argument(
    "--skip",
    help="comma separated list of checks to skip"
)
parser.add_argument(
    "--show-checks",
    action="store_true",
    help="display the complete list of checks and exit",
)
parser.add_argument(
    "--export-checks-as-html",
    action="store_true",
    help=argparse.SUPPRESS,
)
parser.add_argument(
    "--delay",
    type=float,
    default=0.1,
    help="seconds to sleep between checks (default: 1)",
)
# fmt: on


def run():
    args = parser.parse_args()

    if args.export_checks_as_html:
        # Hidden option, used to generate the text for the README page.
        print("<dl>")
        for check in CHECKS:
            print(f"<dt>[{check.__name__}]</dt>")
            print(f"<dd>{check.__doc__}.</dd>")
            print("")
        print("</dl>")
        return

    if args.show_checks:
        for check in CHECKS:
            log(f"[{check.__name__}]", style="title")
            log(f"{check.__doc__}\n")
        return

    if args.checks:
        check_names = {cls.__name__: cls for cls in CHECKS}
        check_list = []
        for name in args.checks.split(","):
            name = name.strip()
            if not name:
                continue
            if name not in check_names:
                raise ValueError(f"unknown check {name!r}")
            check_list.append(check_names[name])
    elif args.skip:
        check_names = {cls.__name__: cls for cls in CHECKS}
        check_list = []
        for name in args.skip.split(","):
            name = name.strip()
            if not name:
                continue
            if name not in check_names:
                raise ValueError(f"unknown check {name!r}")
            check_names.pop(name, None)
        for name in check_names:
            check_list.append(check_names[name])
    else:
        check_list = CHECKS

    log(f"Running server diagnostics check against {args.host}:{args.port}")
    log("...\n")
    host = args.host
    port = args.port
    addr_info = socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
    if not addr_info:
        raise UserWarning(f"No IPv4 address found for host")
    args.addr = addr_info[0][4]
    failed = []
    all_pass = True
    for check in check_list:
        time.sleep(args.delay)
        if not check(args).run():
            all_pass = False
            failed.append(check.__name__)
    log("Done!")
    if failed:
        log(f"Failed tests: {','.join(failed)}")
    return 0 if all_pass else 1


if __name__ == "__main__":
    sys.exit(run())
