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from datetime import datetime
|
| 8 |
+
from pynput import mouse
|
| 9 |
+
from PIL import ImageGrab
|
| 10 |
+
|
| 11 |
+
HOST_IP = "192.168.124.1"
|
| 12 |
+
HOST_PORT = 9999
|
| 13 |
+
client_socket = None
|
| 14 |
+
click_count = 0
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def connect_to_server():
|
| 18 |
+
"""Connect to Windows host"""
|
| 19 |
+
global client_socket
|
| 20 |
+
while True:
|
| 21 |
+
try:
|
| 22 |
+
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
| 23 |
+
client_socket.settimeout(5)
|
| 24 |
+
client_socket.connect((HOST_IP, HOST_PORT))
|
| 25 |
+
print("✓ Connected to server!")
|
| 26 |
+
return
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"Connection failed. Retrying in 5 seconds...")
|
| 29 |
+
time.sleep(5)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def send_click(x, y):
|
| 33 |
+
"""Send click data to server"""
|
| 34 |
+
global client_socket
|
| 35 |
+
try:
|
| 36 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
|
| 37 |
+
click_record = f"pg.click({x}, {y}) # {timestamp}"
|
| 38 |
+
|
| 39 |
+
click_bytes = click_record.encode("utf-8")
|
| 40 |
+
|
| 41 |
+
# Send: 'C' + length (2 bytes) + data
|
| 42 |
+
message = b"C" + struct.pack("!H", len(click_bytes)) + click_bytes
|
| 43 |
+
client_socket.sendall(message)
|
| 44 |
+
|
| 45 |
+
print(f"Sent: {click_record}")
|
| 46 |
+
return True
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Error sending click: {e}")
|
| 50 |
+
connect_to_server()
|
| 51 |
+
return False
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def send_screenshot(filename):
|
| 55 |
+
"""Send screenshot file to server"""
|
| 56 |
+
global client_socket
|
| 57 |
+
try:
|
| 58 |
+
if not os.path.exists(filename):
|
| 59 |
+
return False
|
| 60 |
+
|
| 61 |
+
file_size = os.path.getsize(filename)
|
| 62 |
+
basename = os.path.basename(filename)
|
| 63 |
+
basename_bytes = basename.encode("utf-8")
|
| 64 |
+
|
| 65 |
+
# Send: 'S' + fname_len (1 byte) + filename + file_size (4 bytes) + file_data
|
| 66 |
+
message = b"S" + struct.pack("!B", len(basename_bytes)) + basename_bytes
|
| 67 |
+
client_socket.sendall(message)
|
| 68 |
+
|
| 69 |
+
# Send file size
|
| 70 |
+
client_socket.sendall(struct.pack("!I", file_size))
|
| 71 |
+
|
| 72 |
+
# Send file content
|
| 73 |
+
with open(filename, "rb") as f:
|
| 74 |
+
while True:
|
| 75 |
+
chunk = f.read(8192)
|
| 76 |
+
if not chunk:
|
| 77 |
+
break
|
| 78 |
+
client_socket.sendall(chunk)
|
| 79 |
+
|
| 80 |
+
print(f"Screenshot sent: {basename}")
|
| 81 |
+
return True
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"Error sending screenshot: {e}")
|
| 85 |
+
connect_to_server()
|
| 86 |
+
return False
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def take_and_send_screenshot():
|
| 90 |
+
"""Take screenshot and send to server"""
|
| 91 |
+
global click_count
|
| 92 |
+
try:
|
| 93 |
+
click_count += 1
|
| 94 |
+
|
| 95 |
+
# Use temp directory to avoid permission issues
|
| 96 |
+
temp_dir = tempfile.gettempdir()
|
| 97 |
+
filename = os.path.join(temp_dir, f"vm_{click_count}.png")
|
| 98 |
+
|
| 99 |
+
print("Taking screenshot...")
|
| 100 |
+
img = ImageGrab.grab()
|
| 101 |
+
img.save(filename)
|
| 102 |
+
|
| 103 |
+
send_screenshot(filename)
|
| 104 |
+
|
| 105 |
+
# Clean up local file
|
| 106 |
+
try:
|
| 107 |
+
os.remove(filename)
|
| 108 |
+
except:
|
| 109 |
+
pass
|
| 110 |
+
|
| 111 |
+
except Exception as e:
|
| 112 |
+
print(f"Screenshot error: {e}")
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def on_click(x, y, button, pressed):
|
| 116 |
+
"""Mouse click callback"""
|
| 117 |
+
if pressed and button.name == "left":
|
| 118 |
+
send_click(x, y)
|
| 119 |
+
take_and_send_screenshot()
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def main():
|
| 123 |
+
print("=" * 60)
|
| 124 |
+
print("Click Recorder Client (Ubuntu VM)")
|
| 125 |
+
print("=" * 60)
|
| 126 |
+
print(f"Connecting to {HOST_IP}:{HOST_PORT}...\n")
|
| 127 |
+
|
| 128 |
+
connect_to_server()
|
| 129 |
+
|
| 130 |
+
print("Recording clicks... Press Ctrl+C to stop\n")
|
| 131 |
+
|
| 132 |
+
listener = mouse.Listener(on_click=on_click)
|
| 133 |
+
listener.start()
|
| 134 |
+
|
| 135 |
+
try:
|
| 136 |
+
while True:
|
| 137 |
+
time.sleep(1)
|
| 138 |
+
except KeyboardInterrupt:
|
| 139 |
+
print("\n\nStopped!")
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
if __name__ == "__main__":
|
| 143 |
+
main()
|