Listen on dual stack

This commit is contained in:
László Károlyi 2024-08-06 20:39:11 +02:00
parent 4e8d4b93dd
commit 2de7107e55
Signed by: karolyi
GPG key ID: 2DCAF25E55735BFE

View file

@ -1,11 +1,12 @@
#!/usr/bin/env python #!/usr/bin/env python
from contextlib import suppress
from http import HTTPStatus from http import HTTPStatus
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path from pathlib import Path
from random import choices from random import choices
from signal import SIGTERM, signal from signal import SIGTERM, signal
from socket import socket from socket import AF_INET6, IPPROTO_IPV6, IPV6_V6ONLY, socket
from sqlite3 import connect from sqlite3 import connect
from string import ascii_lowercase from string import ascii_lowercase
from tempfile import gettempdir from tempfile import gettempdir
@ -111,7 +112,17 @@ class F2bRemoteRequestHandler(SimpleHTTPRequestHandler):
do_HEAD = do_POST = __send_403 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, server_address=('', 8000), RequestHandlerClass=F2bRemoteRequestHandler,
bind_and_activate=True) bind_and_activate=True)