~zethra/stargazer

ref: f639b8c3f92c8c3c9a51b6c1bdf621325d938a05 stargazer/scripts/gemini-diagnostics -rwxr-xr-x 31.4 KiB
f639b8c3 — Ben Aaron Goldberg Fixed #24 directory slash redirect a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
#!/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
import ipaddress
import socket
import ssl
import sys
import time
import typing
import re
import difflib
import subprocess

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(text, n_text):
    """
    http://stackoverflow.com/a/788780
    Unify operations between two compared strings seqm is a difflib.
    SequenceMatcher instance whose a & b are strings
    """
    seqm = difflib.SequenceMatcher(None, text, n_text)
    output= []
    for opcode, a0, a1, b0, b1 in seqm.get_opcodes():
        if opcode == 'equal':
            output.append(seqm.a[a0:a1])
        elif opcode == 'insert':
            output.append(f"\033[{FG_RED}m{seqm.b[b0:b1]}\033[0m")
        elif opcode == 'delete':
            output.append(f"\033[{FG_BLUE}m{seqm.b[a0:a1]}\033[0m")
        elif opcode == 'replace':
            # seqm.a[a0:a1] -> seqm.b[b0:b1]
            output.append(f"\033[{FG_GREEN}m{seqm.b[b0:b1]}\033[0m")
        else:
            raise RuntimeError
    return ''.join(output)


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=False):
        """
        Setup an unverified TLS socket connection with the host.
        """
        if context is None:
            context = ssl.SSLContext(ssl.PROTOCOL_TLS)
            context.check_hostname = False
            context.verify_mode = ssl.CERT_NONE
            if client_cert:
                context.load_cert_chain(
                    certfile="./test_data/client-cert.pem", keyfile="./test_data/client-key.pem")

        address = (self.args.host, self.args.port)
        with socket.create_connection(address, 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=False):
        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.hazmat.backends import default_backend
            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 = default_backend().load_der_x509_certificate(der_x509)
            now = datetime.datetime.utcnow()

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

            log('Checking "Not Valid After" timestamp')
            self.log_test(f"{cert.not_valid_after} UTC",
                          cert.not_valid_after >= 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")
            ssl.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)
        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().decode()
                if header:
                    log(f"Received unexpected response {header!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 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 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#tag\r\n"
        response = self.make_request(url, client_cert=True)

        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/0.5.0
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()
        log("Body should match")
        self.log_test("", body == correct_body)
        if body != correct_body:
            print("Got:")
            print(body)
            print("\nDiff:")
            print(show_diff(body, correct_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#tag\r\n"
        response = self.make_request(url, client_cert=True)

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

        correct_body = """
Hello world!
AUTH_TYPE: CERTIFICATE
CONTENT_LENGTH: 0
GEMINI_URL: gemini://localhost/scgi/testing123?hello%20world#tag
HOSTNAME: localhost
PATH_INFO: /testing123
QUERY_STRING: hello%20world
REMOTE_ADDR: ::1
REMOTE_HOST: ::1
REMOTE_USER: gemini user
SCGI: 1
SCRIPT_NAME: /scgi
SERVER_NAME: localhost
SERVER_PORT: 1965
SERVER_PROTOCOL: GEMINI
SERVER_SOFTWARE: stargazer/0.5.0
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()
        log("Body should match")
        self.log_test("", body == correct_body)
        if body != correct_body:
            print("Got:")
            print(body)
            print("\nDiff:")
            print(show_diff(body, correct_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)

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

        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)

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

        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)

        log("Status should return code 20")
        self.log_test(f"{response.status!r}", response.status == "20")
        
        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:")
            print(show_diff(body, correct_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/")

# 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=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")
    all_pass = True
    for check in check_list:
        time.sleep(args.delay)
        all_pass = check(args).run() and all_pass
    log("Done!")
    return 0 if all_pass else 1


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