chia-blockchain/chia/util/network.py
Peter Tripp 178f63a3a3
exempt_peer_networks: allow exceeding target_peer_count. (#3234)
* exempt_peer_networks: allow exceeding target_peer_count.

* Flake8.

* Cleanup incorrect types.

* More typing.

* Added configuration examples for exempt_peer_networks.

* Be generous in the IP network definitions we allow as input.

* Correctly define 192.168.1.0/24 without host bits.

* Trivial change to re-trigger tests.

Co-authored-by: wjblanke <wjb98672@gmail.com>
2021-05-12 17:44:26 -07:00

44 lines
1.4 KiB
Python

from ipaddress import ip_address, IPv4Network, IPv6Network
from typing import Iterable, Union, Any
from chia.server.outbound_message import NodeType
def is_in_network(peer_host: str, networks: Iterable[Union[IPv4Network, IPv6Network]]) -> bool:
try:
peer_host_ip = ip_address(peer_host)
return any(peer_host_ip in network for network in networks)
except ValueError:
return False
def is_localhost(peer_host: str) -> bool:
return peer_host == "127.0.0.1" or peer_host == "localhost" or peer_host == "::1" or peer_host == "0:0:0:0:0:0:0:1"
def class_for_type(type: NodeType) -> Any:
if type is NodeType.FULL_NODE:
from chia.full_node.full_node_api import FullNodeAPI
return FullNodeAPI
elif type is NodeType.WALLET:
from chia.wallet.wallet_node_api import WalletNodeAPI
return WalletNodeAPI
elif type is NodeType.INTRODUCER:
from chia.introducer.introducer_api import IntroducerAPI
return IntroducerAPI
elif type is NodeType.TIMELORD:
from chia.timelord.timelord_api import TimelordAPI
return TimelordAPI
elif type is NodeType.FARMER:
from chia.farmer.farmer_api import FarmerAPI
return FarmerAPI
elif type is NodeType.HARVESTER:
from chia.harvester.harvester_api import HarvesterAPI
return HarvesterAPI
raise ValueError("No class for type")