|
| 1 | +""" |
| 2 | +The strategy which searches for MAC addresses using Admin Panel. |
| 3 | +""" |
| 4 | +from core.utils.logger import get_logger |
| 5 | +from bs4 import BeautifulSoup |
| 6 | +import requests |
| 7 | +from typing import Any |
| 8 | +from core.strategies.wifi.base_wifi_strategy import BaseWiFiStrategy |
| 9 | +from core.utils.datatypes import WiFiStrategyResult, ConnectedDeviceResult |
| 10 | + |
| 11 | +# Add logging support. |
| 12 | +logger = get_logger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +class AdminPanelStrategy(BaseWiFiStrategy): |
| 16 | + """ |
| 17 | + The strategy which searches for MAC addresses using Admin Panel. |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__(self, login_data: dict[str, Any]) -> None: |
| 21 | + """Constructor for AdminPanelStrategy.""" |
| 22 | + super().__init__() |
| 23 | + self._login_data: dict[str, Any] = login_data |
| 24 | + |
| 25 | + def check_protectors(self) -> WiFiStrategyResult: |
| 26 | + """This method checks if there are any protectors around.""" |
| 27 | + for protector in self.protectors: |
| 28 | + # Check if the protector is connected to the network. |
| 29 | + if protector.address in [device.address for device in self._get_all_connected()]: |
| 30 | + logger.debug("Protector found: " + protector.name) |
| 31 | + return WiFiStrategyResult(protector, True) |
| 32 | + logger.debug("No protectors found.") |
| 33 | + return WiFiStrategyResult(None, False) |
| 34 | + |
| 35 | + # Internal methods |
| 36 | + def _get_all_connected(self) -> list[ConnectedDeviceResult]: |
| 37 | + """This method returns a list of addresses of the clients connected to the network.""" |
| 38 | + # Create a session to store cookies. |
| 39 | + session = requests.Session() |
| 40 | + session.get("http://192.168.1.95/login_security.html") |
| 41 | + session.post( |
| 42 | + "http://192.168.1.95/Forms/login_security_1", |
| 43 | + headers={ |
| 44 | + "Content-Type": "application/x-www-form-urlencoded", |
| 45 | + "Referer": "http://192.168.1.95/login_security.html", |
| 46 | + }, |
| 47 | + data=self._login_data |
| 48 | + ) |
| 49 | + |
| 50 | + # Get the response for the page with MAC address list. |
| 51 | + response = session.get("http://192.168.1.95/status/status_deviceinfo.htm") |
| 52 | + |
| 53 | + # Parse the response. |
| 54 | + soup = BeautifulSoup(response.text, "html.parser") |
| 55 | + tabdata = soup.find_all("td", class_="tabdata") |
| 56 | + |
| 57 | + # Get the MAC addresses. |
| 58 | + mac_addrs: list[str] = [ |
| 59 | + element.text |
| 60 | + for element in tabdata |
| 61 | + if len(element.text) == 17 |
| 62 | + ] |
| 63 | + session.close() |
| 64 | + |
| 65 | + logger.debug("Connected devices: " + str(mac_addrs)) |
| 66 | + return [ConnectedDeviceResult(mac_addr.upper()) for mac_addr in mac_addrs] |
0 commit comments