Fallback to IPv4 when IPv6 is unsupported when using the default address (::1)

This commit is contained in:
Firepup Sixfifty 2024-07-31 21:47:54 +00:00
parent aaeb313181
commit a44e7c29fd
Signed by: Firepup650
SSH key fingerprint: SHA256:U0Zp8EhEe3CMqFSrC79CqatzaEiL4sjta80/RSX2XrY

View file

@ -603,13 +603,21 @@ async def connectServer(hostname: str, port: int):
async def runServer(address: str, port: int): async def runServer(address: str, port: int):
global G global G
if ":" in address: sock = None
INET = socket.AF_INET6 try:
else: if ":" in address:
INET = socket.AF_INET INET = socket.AF_INET6
sock = socket.socket(INET, socket.SOCK_STREAM) else:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Reuse socket INET = socket.AF_INET
sock.bind((address, port)) sock = socket.socket(INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Reuse socket
sock.bind((address, port))
except OSError as E:
if E.errno != 97 or hostname != '::1':
raise E
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Reuse socket
sock.bind(('0.0.0.0', port))
server = await asyncio.start_server(handleClient, sock=sock) server = await asyncio.start_server(handleClient, sock=sock)
log(f"Listening on port {port}...") log(f"Listening on port {port}...")
G.msgs.append(log("Server startup")) G.msgs.append(log("Server startup"))