Handle ephemeral ports and dual stack (ipv4 & ipv6)

This commit is contained in:
Earle Lowe
2022-06-02 15:51:23 -07:00
parent 5b49a574e4
commit 0b7295c25e
2 changed files with 39 additions and 3 deletions
+19 -1
View File
@@ -1,4 +1,5 @@
import asyncio
import ipaddress
import json
import logging
import traceback
@@ -12,6 +13,7 @@ from chia.server.outbound_message import NodeType
from chia.server.server import ssl_context_for_client, ssl_context_for_server
from chia.types.peer_info import PeerInfo
from chia.util.byte_types import hexstr_to_bytes
from chia.util.config import load_config
from chia.util.ints import uint16
from chia.util.json_util import dict_to_json_str
from chia.util.ws_message import create_payload, create_payload_dict, format_response, pong
@@ -328,7 +330,23 @@ async def start_rpc_server(
site = web.TCPSite(runner, self_hostname, int(rpc_port), ssl_context=rpc_server.ssl_context)
await site.start()
rpc_port = runner.addresses[0][1]
#
# On a dual-stack system, we want to get the (first) IPv4 port unless
# prefer_ipv6 is set in which case we use the IPv6 port
#
if rpc_port == 0:
rpc_port = runner.addresses[0][1] # Set the port by default to the first thing
global_config = load_config(root_path, "config.yaml")
prefer_ipv6 = global_config.get("prefer_ipv6", False)
for x in runner.addresses:
ip_addy = ipaddress.ip_address(x[0])
if ip_addy.version == 6 and prefer_ipv6:
rpc_port = x[1]
break
elif ip_addy.version == 4 and not prefer_ipv6:
rpc_port = x[1]
break
async def cleanup():
await rpc_server.stop()
+20 -2
View File
@@ -1,4 +1,5 @@
import asyncio
import ipaddress
import logging
import ssl
import time
@@ -28,6 +29,7 @@ from chia.server.ssl_context import private_ssl_paths, public_ssl_paths
from chia.server.ws_connection import WSChiaConnection
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.peer_info import PeerInfo
from chia.util.config import load_config
from chia.util.errors import Err, ProtocolError
from chia.util.ints import uint16
from chia.util.network import is_in_network, is_localhost
@@ -267,13 +269,29 @@ class ChiaServer:
# this port from the socket itself and update self._port.
self.site = web.TCPSite(
self.runner,
host="0.0.0.0",
host="", # should listen to both IPv4 and IPv6 on a dual-stack system
port=int(self._port),
shutdown_timeout=3,
ssl_context=ssl_context,
)
await self.site.start()
self._port = self.runner.addresses[0][1]
#
# On a dual-stack system, we want to get the (first) IPv4 port unless
# prefer_ipv6 is set in which case we use the IPv6 port
#
if self._port == 0:
global_config = load_config(self.root_path, "config.yaml")
prefer_ipv6 = global_config.get("prefer_ipv6", False)
self._port = self.runner.addresses[0][1] # sets the default to handle some edge cases
for x in self.runner.addresses:
ip_addy = ipaddress.ip_address(x[0])
if ip_addy.version == 6 and prefer_ipv6:
self._port = x[1]
break
elif ip_addy.version == 4 and not prefer_ipv6:
self._port = x[1]
break
self.log.info(f"Started listening on port: {self._port}")
async def incoming_connection(self, request):