|
|
| import socket
|
| import struct
|
| import time
|
| import os
|
| import tempfile
|
| from datetime import datetime
|
| from pynput import mouse
|
| from PIL import ImageGrab
|
|
|
| HOST_IP = "192.168.124.1"
|
| HOST_PORT = 9999
|
| client_socket = None
|
| click_count = 0
|
|
|
|
|
| def connect_to_server():
|
| """Connect to Windows host"""
|
| global client_socket
|
| while True:
|
| try:
|
| client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
| client_socket.settimeout(5)
|
| client_socket.connect((HOST_IP, HOST_PORT))
|
| print("✓ Connected to server!")
|
| return
|
| except Exception as e:
|
| print(f"Connection failed. Retrying in 5 seconds...")
|
| time.sleep(5)
|
|
|
|
|
| def send_click(x, y):
|
| """Send click data to server"""
|
| global client_socket
|
| try:
|
| timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
|
| click_record = f"pg.click({x}, {y}) # {timestamp}"
|
|
|
| click_bytes = click_record.encode("utf-8")
|
|
|
|
|
| message = b"C" + struct.pack("!H", len(click_bytes)) + click_bytes
|
| client_socket.sendall(message)
|
|
|
| print(f"Sent: {click_record}")
|
| return True
|
|
|
| except Exception as e:
|
| print(f"Error sending click: {e}")
|
| connect_to_server()
|
| return False
|
|
|
|
|
| def send_screenshot(filename):
|
| """Send screenshot file to server"""
|
| global client_socket
|
| try:
|
| if not os.path.exists(filename):
|
| return False
|
|
|
| file_size = os.path.getsize(filename)
|
| basename = os.path.basename(filename)
|
| basename_bytes = basename.encode("utf-8")
|
|
|
|
|
| message = b"S" + struct.pack("!B", len(basename_bytes)) + basename_bytes
|
| client_socket.sendall(message)
|
|
|
|
|
| client_socket.sendall(struct.pack("!I", file_size))
|
|
|
|
|
| with open(filename, "rb") as f:
|
| while True:
|
| chunk = f.read(8192)
|
| if not chunk:
|
| break
|
| client_socket.sendall(chunk)
|
|
|
| print(f"Screenshot sent: {basename}")
|
| return True
|
|
|
| except Exception as e:
|
| print(f"Error sending screenshot: {e}")
|
| connect_to_server()
|
| return False
|
|
|
|
|
| def take_and_send_screenshot():
|
| """Take screenshot and send to server"""
|
| global click_count
|
| try:
|
| click_count += 1
|
|
|
|
|
| temp_dir = tempfile.gettempdir()
|
| filename = os.path.join(temp_dir, f"vm_{click_count}.png")
|
|
|
| print("Taking screenshot...")
|
| img = ImageGrab.grab()
|
| img.save(filename)
|
|
|
| send_screenshot(filename)
|
|
|
|
|
| try:
|
| os.remove(filename)
|
| except:
|
| pass
|
|
|
| except Exception as e:
|
| print(f"Screenshot error: {e}")
|
|
|
|
|
| def on_click(x, y, button, pressed):
|
| """Mouse click callback"""
|
| if pressed and button.name == "left":
|
| send_click(x, y)
|
| take_and_send_screenshot()
|
|
|
|
|
| def main():
|
| print("=" * 60)
|
| print("Click Recorder Client (Ubuntu VM)")
|
| print("=" * 60)
|
| print(f"Connecting to {HOST_IP}:{HOST_PORT}...\n")
|
|
|
| connect_to_server()
|
|
|
| print("Recording clicks... Press Ctrl+C to stop\n")
|
|
|
| listener = mouse.Listener(on_click=on_click)
|
| listener.start()
|
|
|
| try:
|
| while True:
|
| time.sleep(1)
|
| except KeyboardInterrupt:
|
| print("\n\nStopped!")
|
|
|
|
|
| if __name__ == "__main__":
|
| main()
|
|
|