Merge release/1.4.0's 6171703 into main

This commit is contained in:
Amine Khaldi 2022-06-08 21:16:10 +01:00
commit 11b68de354
No known key found for this signature in database
GPG Key ID: B1C074FFC904E2D9
3 changed files with 37 additions and 4 deletions

View File

@ -14,6 +14,7 @@ from chia.types.peer_info import PeerInfo
from chia.util.byte_types import hexstr_to_bytes
from chia.util.ints import uint16
from chia.util.json_util import dict_to_json_str
from chia.util.network import select_port
from chia.util.ws_message import create_payload, create_payload_dict, format_response, pong
log = logging.getLogger(__name__)
@ -329,7 +330,13 @@ 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 = select_port(root_path, runner.addresses)
async def cleanup():
await rpc_server.stop()

View File

@ -30,7 +30,7 @@ from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.peer_info import PeerInfo
from chia.util.errors import Err, ProtocolError
from chia.util.ints import uint16
from chia.util.network import is_in_network, is_localhost
from chia.util.network import is_in_network, is_localhost, select_port
from chia.util.ssl_check import verify_ssl_certs_and_keys
max_message_size = 50 * 1024 * 1024 # 50MB
@ -267,13 +267,19 @@ 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:
self._port = select_port(self.root_path, self.runner.addresses)
self.log.info(f"Started listening on port: {self._port}")
async def incoming_connection(self, request):

View File

@ -1,9 +1,11 @@
import socket
from pathlib import Path
from ipaddress import ip_address, IPv4Network, IPv6Network
from typing import Iterable, List, Tuple, Union, Any, Optional, Dict
from chia.server.outbound_message import NodeType
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.ints import uint16
@ -84,3 +86,21 @@ def is_trusted_inner(peer_host: str, peer_node_id: bytes32, trusted_peers: Dict,
return False
return True
def select_port(root_path: Path, addresses: List[Any]) -> uint16:
global_config = load_config(root_path, "config.yaml")
prefer_ipv6 = global_config.get("prefer_ipv6", False)
selected_port: uint16
for address_string, port, *_ in addresses:
address = ip_address(address_string)
if address.version == 6 and prefer_ipv6:
selected_port = port
break
elif address.version == 4 and not prefer_ipv6:
selected_port = port
break
else:
selected_port = addresses[0][1] # no matches, just use the first one in the list
return selected_port