diff --git a/f2b-responder.py b/f2b-responder.py index db3d13b..ea17c9f 100755 --- a/f2b-responder.py +++ b/f2b-responder.py @@ -1,11 +1,12 @@ #!/usr/bin/env python +from contextlib import suppress from http import HTTPStatus from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer from pathlib import Path from random import choices from signal import SIGTERM, signal -from socket import socket +from socket import AF_INET6, IPPROTO_IPV6, IPV6_V6ONLY, socket from sqlite3 import connect from string import ascii_lowercase from tempfile import gettempdir @@ -111,7 +112,17 @@ class F2bRemoteRequestHandler(SimpleHTTPRequestHandler): do_HEAD = do_POST = __send_403 -server = ThreadingHTTPServer( +class DualStackThreadingHttpServer(ThreadingHTTPServer): + address_family = AF_INET6 + + def server_bind(self): + # suppress exception when protocol is IPv4-only + with suppress(OSError): + self.socket.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, 0) + return super().server_bind() + + +server = DualStackThreadingHttpServer( server_address=('', 8000), RequestHandlerClass=F2bRemoteRequestHandler, bind_and_activate=True)