diff --git a/scripts/utils/internet_detect_tool.py b/scripts/utils/internet_detect_tool.py new file mode 100644 index 000000000..872863a2f --- /dev/null +++ b/scripts/utils/internet_detect_tool.py @@ -0,0 +1,272 @@ +import threading +import requests +import win32api +import win32gui +import win32con +import urllib3 +import hashlib +import winreg +import struct +import signal +import ctypes +import time +import os +import re + + +# Define constants +CHECK_INTERVAL = 2 # Seconds +CONNECT_TEST_URLS_AND_RESPONSES = { + "http://www.google.com": 'ce770667e5f9b0d8f55367bb79419689d90c48451bb33f079f3a9a72ae132de8', # HTTP Test + "https://www.wikipedia.com": 'd38b38a2dd476e045c299e8ee5d6466834456d97bd592a71746b423a6a05f386', # HTTPS Test #2 + "https://www.youtube.com": 'fb7accfff8c6f8ea9b03c91ee5576d0d08080e9ba35918d801aaeb6020dbc88c' # HTTPS Test #3 + } +CONNECT_TEST_URL = "https://www.msftconnecttest.com/connecttest.txt" +EXPECTED_RESPONSE = "Microsoft Connect Test" +SPI_SETDESKWALLPAPER = 20 +SPIF_UPDATEINIFILE = 0x01 +SPIF_SENDWININICHANGE = 0x02 +COLOR_DESKTOP = 1 +ICON_RED_X = os.path.join(os.environ.get('VM_COMMON_DIR'), "red_circle.ico") +ICON_INDICATOR = os.path.join(os.environ.get('VM_COMMON_DIR'), "indicator.ico") +DEFAULT_BACKGROUND = os.path.join(os.environ.get('VM_COMMON_DIR'), "background.png") +INTERNET_BACKGROUND = os.path.join(os.environ.get('VM_COMMON_DIR'),"background-internet.png") + +# Global variables +tray_icon = None +stop_event = threading.Event() # To signal the background thread to stop +hwnd = None # We'll assign the window handle here later +check_thread = None +tray_icon_thread = None +# Win32 API icon handles +hicon_indicator = None +hicon_red_x = None + +def signal_handler(sig, frame): + print("Ctrl+C detected. Exiting...") + stop_event.set() # Signal the background thread to stop + if check_thread: + check_thread.join() + if tray_icon_thread: + tray_icon_thread.join() + exit(0) + +def load_icon(icon_path): + try: + return win32gui.LoadImage(None, icon_path, win32con.IMAGE_ICON, 0, 0, win32con.LR_LOADFROMFILE) + except Exception as e: + print(f"Error loading indicator icon: {e}") + return None + +class SysTrayIcon: + def __init__(self, hwnd, icon, tooltip): + self.hwnd = hwnd + self.icon = icon + self.tooltip = tooltip + + # System tray icon data structure + self.nid = (self.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, + win32con.WM_USER + 20, self.icon, self.tooltip) + + # Add the icon to the system tray + win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, self.nid) + + # Create a context menu + self.menu = win32gui.CreatePopupMenu() + win32gui.AppendMenu(self.menu, win32con.MF_STRING, 1023, "Exit") # Example menu item + + def show_context_menu(self, x, y): + # Display the context menu at the specified coordinates + win32gui.SetForegroundWindow(self.hwnd) + win32gui.TrackPopupMenu(self.menu, win32con.TPM_LEFTALIGN, x, y, 0, self.hwnd, None) + win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0) + + def set_icon(self, icon): + self.icon = icon + self.nid = (self.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, + win32con.WM_USER + 20, self.icon, self.tooltip) + win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, self.nid) + + def show_balloon_tip(self, title, msg): + # Display a balloon tip notification + win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, ( + self.hwnd, + 0, + win32gui.NIF_INFO, + win32con.WM_USER + 20, + self.icon, + self.tooltip, + msg, + 200, + title, + )) + + def __del__(self): + # Remove the icon from the system tray when the object is destroyed + win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, self.nid) + +def extract_title(data): + match = re.search(r'