Exemple #1
0
    def __init__(self, config_path):
        """

        :param str config_path: Path to config file
        """
        self.config = Config(config_path)
        self.settings = self.config.get('settings', {})
        self.devices = {key.lower(): Device(key, value) for key, value in self.config['devices'].items()}
        assert len(self.devices) == len(self.config['devices']), "Duplicate(s) MAC(s) on devices config."
Exemple #2
0
def test_device(device, file, root_allowed=False):
    """Test the execution of a device without pressing the associated button

    :param str device: mac address
    :param str file: config file
    :param bool root_allowed: only used for ExecuteCmd
    :return: None
    """
    config = Config(file)
    config.read()
    if not device in config['devices']:
        raise InvalidDevice('Device {} is not in config file.'.format(device))
    Device(device, config['devices'][device], config).execute(root_allowed)
class Listener(object):
    root_allowed = False

    def __init__(self, config_path):
        self.config = Config(config_path)
        self.settings = self.config.get('settings', {})
        self.devices = {
            key.lower(): Device(key, value)
            for key, value in self.config['devices'].items()
        }
        assert len(self.devices) == len(
            self.config['devices']), "Duplicate(s) MAC(s) on devices config."

    def on_push(self, device):
        src = device.src.lower()
        if last_execution[src] + self.settings.get(
                'delay', DEFAULT_DELAY) > time.time():
            return
        last_execution[src] = time.time()
        self.execute(device)

    def execute(self, device):
        src = device.src.lower()
        device = self.devices[src]
        device.execute(root_allowed=self.root_allowed)

    def run(self, root_allowed=False):
        self.root_allowed = root_allowed
        scan(self.on_push, lambda d: d.src.lower() in self.devices)
Exemple #4
0
 def test_invalid_config(self, getuid_mock):
     file = 'amazon-dash.yml'
     with Patcher() as patcher:
         patcher.fs.CreateFile(file, contents='invalid config')
         os.chown(file, 1000, 1000)
         os.chmod(file, 0o660)
         with self.assertRaises(InvalidConfig):
             Config(file)
Exemple #5
0
 def test_root(self, getuid_mock):
     file = 'amazon-dash.yml'
     with Patcher() as patcher:
         patcher.fs.CreateFile(file, contents=config_data)
         os.chown(file, 0, 0)
         os.chmod(file, 0o660)
         Config(file)
     patcher.tearDown()
Exemple #6
0
 def test_yaml_exception(self):
     file = 'config.yml'
     with Patcher() as patcher:
         patcher.fs.CreateFile(file, contents='\x00')
         os.chown(file, 0, 0)
         os.chmod(file, 0o660)
         with self.assertRaises(InvalidConfig):
             Config('config.yml')
Exemple #7
0
 def test_root_error(self, getuid_mock, file_owner_mock, file_group_mock):
     file = 'amazon-dash.yml'
     with Patcher() as patcher:
         patcher.fs.CreateFile(file, contents=config_data)
         os.chown(file, 1000, 1000)
         os.chmod(file, 0o660)
         with self.assertRaises(SecurityException):
             Config(file)
     patcher.tearDown()
Exemple #8
0
class Listener(object):
    """Start listener daemon for execute on button press
    """

    root_allowed = False  #: Only used for ExecuteCmd

    def __init__(self, config_path, ignore_perms=False):
        """

        :param str config_path: Path to config file
        """
        self.config = Config(config_path, ignore_perms)
        self.settings = self.config.get('settings', {})
        self.devices = {
            key.lower(): Device(key, value, self.config)
            for key, value in self.config['devices'].items()
        }
        assert len(self.devices) == len(
            self.config['devices']), "Duplicate(s) MAC(s) on devices config."

    def on_push(self, device):
        """Press button. Check DEFAULT_DELAY.

        :param scapy.packet.Packet device: Scapy packet
        :return: None
        """
        src = device.src.lower()
        if last_execution[src] + self.settings.get(
                'delay', DEFAULT_DELAY) > time.time():
            return
        last_execution[src] = time.time()
        self.execute(device)

    def execute(self, device):
        """Execute a device. Used if the time between executions is greater than DEFAULT_DELAY

        :param scapy.packet.Packet device: Scapy packet
        :return: None
        """
        src = device.src.lower()
        device = self.devices[src]
        threading.Thread(target=device.execute,
                         kwargs={
                             'root_allowed': self.root_allowed
                         }).start()

    def run(self, root_allowed=False):
        """Start daemon mode

        :param bool root_allowed: Only used for ExecuteCmd
        :return: loop
        """
        self.root_allowed = root_allowed
        scan_devices(self.on_push, lambda d: d.src.lower() in self.devices,
                     self.settings.get('interface'))
Exemple #9
0
 def test_not_found(self, mock_method):
     with self.assertRaises(FileNotFoundError):
         Config('config.yml')
     mock_method.assert_called_once()