Files
krawl.es/src/firewall/fwtype.py

43 lines
1.3 KiB
Python
Raw Normal View History

2026-01-27 17:41:41 +01:00
from abc import ABC, abstractmethod
from typing import Dict, Type
class FWType(ABC):
"""Abstract base class for firewall types."""
# Registry to store child classes
2026-01-27 17:53:11 +01:00
_registry: Dict[str, Type["FWType"]] = {}
2026-01-27 17:41:41 +01:00
def __init_subclass__(cls, **kwargs):
"""Automatically register subclasses with their class name."""
super().__init_subclass__(**kwargs)
cls._registry[cls.__name__.lower()] = cls
@classmethod
2026-01-27 17:53:11 +01:00
def create(cls, fw_type: str, **kwargs) -> "FWType":
2026-01-27 17:41:41 +01:00
"""
Factory method to create instances of child classes.
Args:
fw_type: String name of the firewall type class to instantiate
**kwargs: Arguments to pass to the child class constructor
Returns:
Instance of the requested child class
Raises:
ValueError: If fw_type is not registered
"""
fw_type = fw_type.lower()
if fw_type not in cls._registry:
2026-01-27 17:53:11 +01:00
available = ", ".join(cls._registry.keys())
raise ValueError(
f"Unknown firewall type: '{fw_type}'. Available: {available}"
)
2026-01-27 17:41:41 +01:00
return cls._registry[fw_type](**kwargs)
@abstractmethod
2026-01-27 17:53:11 +01:00
def getBanlist(self, ips):
2026-01-27 17:41:41 +01:00
"""Return the ruleset for the specific server"""