Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIREWALL: Allow to block hosts by IP address #90

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions pytest_mh/utils/firewall.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from abc import abstractmethod
from ipaddress import IPv4Address, IPv6Address, ip_address
from random import randrange
from typing import Any, Literal, TypeAlias

Expand Down Expand Up @@ -605,9 +606,19 @@ def __add_host(
self.firewall.add_rich_rule(f"family=ipv6 destination address={ip} {action}")

def __resolve_hostname(self, hostname: str, type: Literal["A", "AAAA"]) -> list[str]:
result = self.firewall.host.conn.exec(["dig", "+short", "-t", type, hostname], log_level=ProcessLogLevel.Error)
addrs = []
try:
ip = ip_address(hostname)
ip_type = IPv4Address if type == "A" else IPv6Address
if isinstance(ip, ip_type):
addrs = [hostname]
except ValueError:
result = self.firewall.host.conn.exec(
["dig", "+short", "-t", type, hostname], log_level=ProcessLogLevel.Error
)
addrs = result.stdout_lines

return result.stdout_lines
return addrs


class WindowsFirewall(Firewall):
Expand Down