This Python script automates the process of connecting to a captive portal, often encountered in public Wi-Fi networks. Here’s a summary of how the code works and its usefulness:
Summary:
-
Setup and Configuration:
- The script imports various libraries, including requests for HTTP requests and JSON for handling JSON data.
- It defines several enums (Client_type, Request_type, Portal_action, Server_response) to manage different client types, request methods, portal actions, and server responses.
-
Global Variables:
- Global class holds key variables like username, password, client type, portal URL, and timestamps of login and acknowledgement.
-
Utility Functions:
-Credential Management: get_creds_from_json(path_to_json) reads the username and password from a JSON file.
-Connectivity Tests: gstatic_connect_test() and msft_connect_test() check if the internet is accessible.
-Captive Portal Detection: find_captive_portal() attempts to find and return the captive portal URL.
-Header and Payload Generation: header_generator() and payload_generator() generate HTTP headers and payloads for requests.
-WAN State Monitoring: is_wan_up() checks and logs changes in the WAN state.
-
Request Functions:
-GET and POST Requests: get_req(uni_host) and post_req(uni_host, portal_action) handle GET and POST requests to the portal.
-Server Response Parsing: parse_server_response(response) interprets the server’s response and updates global state variables accordingly.
-
Main Logic:
-Program Loop: program_loop() continuously checks the WAN state and attempts to login to the portal if the internet is down. It also sends acknowledgement requests periodically if the client is a desktop.
-Pause Menu: pause_menu() provides a user interface for actions like logging out, exiting, and changing the client type.
Usefulness Compared to Software:
1.Automation:
The script automatically handles login and maintains connectivity, unlike manual software methods.
def program_loop():
while not exit_flag:
if not Utility.is_wan_up():
post_req(uni_host=portal_url, portal_action=Portal_action.LOGIN)
2.Customizability:
Easily adjustable for different portals by modifying headers, payloads, and URLs.
def header_generator(uni_host: str, payload_length: int, request_type: Request_type):
headers = { 'Host': url_parse.hostname, 'User-Agent': Global.user_agent, ... }
3.Lightweight: Minimal dependencies and resources compared to GUI-based applications.
import requests, json, logging
4.Logging and Monitoring: Provides detailed logs and real-time status updates.
logging.basicConfig(level=logging.INFO, filename='app.log', ...)
5.Cross-Platform Compatibility: Runs on various operating systems using Python.
os.system('cls' if os.name == 'nt' else 'clear')
Top comments (0)