From 2de7107e55cfa14caca1582fd6f9ef3abd570e2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20K=C3=A1rolyi?= Date: Tue, 6 Aug 2024 20:39:11 +0200 Subject: [PATCH] Listen on dual stack --- f2b-responder.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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)