Bug fixes, improvements, DNS fix

This commit is contained in:
Deniz Şafak
2024-03-12 03:55:25 +03:00
committed by GitHub
parent 93731a0c31
commit 38f88a0ab1
2 changed files with 73 additions and 92 deletions
+71 -91
View File
@@ -1,7 +1,6 @@
import json import json
import os import os
import requests import requests
import socket
import time import time
import subprocess import subprocess
import pystray import pystray
@@ -11,6 +10,9 @@ from tkinter import Text, Scrollbar
from PIL import Image from PIL import Image
from collections import deque from collections import deque
from datetime import datetime from datetime import datetime
import dns.resolver
dns.resolver.override_system_resolver(dns.resolver.Resolver())
dns.resolver.default_resolver = dns.resolver.Resolver()
from urllib3.exceptions import InsecureRequestWarning from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
@@ -158,7 +160,6 @@ if selected_profile:
check_every_second = selected_profile.get('check_every_second', "") check_every_second = selected_profile.get('check_every_second', "")
dialog_geometry = selected_profile.get('dialog_geometry', "") dialog_geometry = selected_profile.get('dialog_geometry', "")
headers = selected_profile.get('headers', "") headers = selected_profile.get('headers', "")
if ssid: if ssid:
expected_ssid = ssid expected_ssid = ssid
expected_ssid_lower = expected_ssid.lower() expected_ssid_lower = expected_ssid.lower()
@@ -167,30 +168,24 @@ if selected_profile:
expected_ssid = ssid expected_ssid = ssid
expected_ssid_lower = None expected_ssid_lower = None
# Function to check if the URL is valid
def check_url():
try:
socket.gethostbyname(url)
return True
except Exception:
return False
session = requests.Session()
# Function to send the request # Function to send the request
def send_request(): def send_request():
response = session.post(url, json.dumps(payload), headers=headers, allow_redirects=True, timeout=5) session = requests.Session()
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += 'HIGH:!DH:!aNULL'
response = session.post(url, json.dumps(payload), headers=headers, allow_redirects=True, verify=False, timeout=3)
response.raise_for_status() # Check for HTTP errors response.raise_for_status() # Check for HTTP errors
print(response.text)
return response return response
# Function to check if internet is available # Function to check if internet is available
def is_internet_available(): def is_internet_available():
try: try:
# Try to send a simple HTTP GET request to a known website # -n is the number of pings
response = session.get("https://www.google.com", timeout=5, allow_redirects=False) # -l is the size of the packet
response.raise_for_status() # Check for HTTP errors # -w is the timeout in seconds
subprocess.run(['ping', '8.8.8.8', '-n', '3', '-l', '32', '-w', '3'], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess.CREATE_NO_WINDOW)
return True return True
except: except subprocess.CalledProcessError:
return False return False
# Function to get the currently connected SSID using system commands (for Windows) # Function to get the currently connected SSID using system commands (for Windows)
@@ -310,7 +305,7 @@ def save_to_file(message):
# Create the system tray icon # Create the system tray icon
def create_system_tray_icon(): def create_system_tray_icon():
image = Image.open("icon.png") image = Image.open("icon.ico")
menu = pystray.Menu( menu = pystray.Menu(
pystray.MenuItem('Show Log', show_log_dialog, default=True), pystray.MenuItem('Show Log', show_log_dialog, default=True),
pystray.MenuItem('Exit', exit_application) pystray.MenuItem('Exit', exit_application)
@@ -324,7 +319,7 @@ errorcount = 0
sleepcount = check_every_second sleepcount = check_every_second
connected_ssid_lower = None connected_ssid_lower = None
def check_network_status(): def check_network_status():
global running, errorcount, sleepcount, connected_ssid_lower, ssid global running, errorcount, sleepcount, connected_ssid_lower, ssid, response
while running and errorcount < 10: while running and errorcount < 10:
connected_ssid = get_connected_network() connected_ssid = get_connected_network()
if (connected_ssid): if (connected_ssid):
@@ -336,91 +331,76 @@ def check_network_status():
errorcount = 0 errorcount = 0
add_to_log(message) add_to_log(message)
else: else:
sleepcount = 3 sleepcount = 1
message = "Connected to {} but internet is down. Sending the request in {} seconds...".format(connected_ssid, str(sleepcount)) message = "Connected to {} but internet is down. Sending the request...".format(connected_ssid)
add_to_log(message) add_to_log(message)
save_to_file(message) save_to_file(message)
time.sleep(sleepcount) time.sleep(sleepcount)
# Send the request and handle potential errors # Send the request and handle potential errors
try: try:
# Update SSL cipher list
try:
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += 'HIGH:!DH:!aNULL'
except AttributeError as e:
# no pyopenssl support used / needed / available
add_to_log("Error when adding to DEFAULT_CIPHERS: {}".format(e))
save_to_file("Error when adding to DEFAULT_CIPHERS: {}".format(e))
pass
# Function to send request # Function to send request
response = send_request() response = send_request()
if response and response.ok: if response.ok:
errorcount = 0 errorcount = 0
sleepcount = 10 sleepcount = 1
message = "Request was successful. Connection established! Checking the internet connection in " + str(sleepcount) + " seconds..." message = "Request was successful. Checking the internet connection..."
add_to_log(message)
save_to_file(message)
# Check the response status code
elif response and (not response.ok):
errorcount += 1
sleepcount = 10 # Sleep ... seconds before trying again
message = "Request failed with status code: {}. Trying again in {} seconds... (Errors: {}/10)".format(response.status_code, str(sleepcount), errorcount)
# Print the response message
add_to_log(message)
save_to_file(message)
else:
errorcount += 1
sleepcount = 10
message = "Request failed. Trying again in {} seconds... (Errors: {}/10)".format(str(sleepcount), errorcount)
add_to_log(message) add_to_log(message)
save_to_file(message) save_to_file(message)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
if not check_url(): errorcount += 1
errorcount += 1 sleepcount = 1
sleepcount = 3 if isinstance(e, requests.exceptions.HTTPError):
message = "Payload URL is not reachable. Trying to reconnect to the network... (Errors: {}/10)".format(errorcount) # Handle HTTP errors with different messages for different status codes
add_to_log(message) status_code = e.response.status_code
save_to_file(message) if status_code == 400:
# If the payload URL is not reachable, try to reconnect to the network message = "Request failed with 400 Bad Request error. Please check your payload and URL. Trying to reconnect to the network... (Errors: {}/10)".format(errorcount)
startupinfo = subprocess.STARTUPINFO() elif status_code == 404:
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW message = "Request failed with 404 Not Found error. Trying to reconnect to the network... (Errors: {}/10)".format(errorcount)
if connected_ssid == "Ethernet": elif status_code == 500:
try: message = "Request failed with 500 Internal Server Error. Trying to reconnect to the network... (Errors: {}/10)".format(errorcount)
# Disable and enable the Ethernet adapter elif status_code == 503:
subprocess.check_output(['netsh', 'interface', 'set', 'interface', 'Ethernet', 'admin=disable'], startupinfo=startupinfo).decode("utf-8") message = "Request failed with 503 Service Unavailable error. Trying to reconnect to the network... (Errors: {}/10)".format(errorcount)
message = "Disconnected from network. Please wait..."
add_to_log(message)
time.sleep(60)
subprocess.check_output(['netsh', 'interface', 'set', 'interface', 'Ethernet', 'admin=enable'], startupinfo=startupinfo).decode("utf-8")
message = "Connected. Running the script again..."
add_to_log(message)
except Exception as e:
message += "\nFailed to reconnect to Ethernet: {}".format(e)
add_to_log(message)
save_to_file(message)
else: else:
try: message = "Request failed with HTTP error ({}). Trying to reconnect to the network... (Errors: {}/10)".format(status_code, errorcount)
# Disconnect and reconnect to the Wi-Fi network
subprocess.check_output(['netsh', 'wlan', 'disconnect', 'interface=' + "Wi-Fi"], startupinfo=startupinfo).decode("utf-8")
message = "Disconnected. Please wait..."
add_to_log(message)
time.sleep(60)
subprocess.check_output(['netsh', 'wlan', 'connect', 'name=' + ssid], startupinfo=startupinfo).decode("utf-8")
message = "Connected. Running the script again..."
add_to_log(message)
except Exception as e:
message += "\nFailed to reconnect to Ethernet: {}".format(e)
add_to_log(message)
save_to_file(message)
else: else:
# Catch the exception and add a delay before trying again message = "Request failed with error:{}. Trying to reconnect to the network... (Errors: {}/10)".format(e, errorcount)
errorcount += 1 # Print the response message
sleepcount = 10 add_to_log(message)
message = "Failed to send request: {}. Trying again in {} seconds... (Errors: {}/10)".format(e, str(sleepcount), str(errorcount)) save_to_file(message)
add_to_log(message)
save_to_file(message)
time.sleep(sleepcount) time.sleep(sleepcount)
# If the payload URL is not reachable, try to reconnect to the network
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
if connected_ssid == "Ethernet":
try:
# Disable and enable the Ethernet adapter
message = "Disconnecting from Ethernet..."
add_to_log(message)
subprocess.check_output(['ipconfig', '/release', "Ethernet"], shell=True, text=True)
time.sleep(5)
subprocess.check_output(['ipconfig', '/renew', "Ethernet"], shell=True, text=True)
time.sleep(2)
message = "Connected to Ethernet. Running the script again..."
add_to_log(message)
except Exception as e:
message += "\nFailed to reconnect to Ethernet: {}".format(e)
add_to_log(message)
save_to_file(message)
else:
try:
# Disconnect and reconnect to the Wi-Fi network
message = "Disconnecting from {}...".format(ssid)
add_to_log(message)
subprocess.check_output(['netsh', 'wlan', 'disconnect', 'interface=' + "Wi-Fi"], startupinfo=startupinfo).decode("utf-8")
time.sleep(5)
subprocess.check_output(['netsh', 'wlan', 'connect', 'name=' + ssid], startupinfo=startupinfo).decode("utf-8")
time.sleep(2)
message = "Connected to {}. Running the script again...".format(ssid)
add_to_log(message)
except Exception as e:
message += "\nFailed to reconnect to {}: {}".format(ssid, e)
add_to_log(message)
save_to_file(message)
continue continue
else: else:
sleepcount = 10 # Sleep ... seconds before trying again sleepcount = 10 # Sleep ... seconds before trying again
@@ -450,4 +430,4 @@ if __name__ == '__main__':
check_network_status() check_network_status()
# Wait for all threads to finish # Wait for all threads to finish
tray_thread.join() tray_thread.join()
+2 -1
View File
@@ -1,3 +1,4 @@
urllib3==1.26.18 urllib3==1.26.18
requests==2.31.0 requests==2.31.0
pystray==0.19.5 pystray==0.19.5
dnspython==2.6.1