# NOTE: Untested on Linux, known broken on macOS.

from threading import Timer
from pynput import mouse
import sys
sys.path.insert(0, "..")
from utils.print import *

# Globals

mouse_action_count = 0
time = 3


def count_action():
	"""
	This increments the global counter for every mouse action observed.
	"""
	global mouse_action_count
	mouse_action_count += 1


def decide_if_in_sandbox():
	if mouse_action_count > 2:
		print_green("Not in an automated system!\n")
	else:
		print_red("Automated system!\n")


def main():
	"""
	Autoanalysis VMs often won't have emulated keyboards or mice. A method
	that can sometimes be used to detect if you're running inside a VM
	is to observe the mouse and keyboard activity. If it's non-zero, it's
	more likely that you're not in a sanbox.

	Note: This will not work on macOS 10.14.4, and will only work on
	Linux if an X-server is running. This is a pretty flaky test for
	autoanalysis, but the concept still stands.
	"""
	print_blue("Setting up mouse listener...")

	# Set up mouse listener
	listener = mouse.Listener(
		on_move=count_action,
		on_click=count_action,
		on_scroll=count_action)

	listener.start()

	# Collect a minute of data, and then decide if we're in a sandbox.
	global time
	Timer(time, decide_if_in_sandbox, ()).start()


if __name__ == '__main__':
	main()
