__author__ = '@tomereyz'

from pwn import *


class ArmExploit(object):
    def __init__(self, challenge_name, execution, debug_port=20000, remote_port=20000):
        self.r = None
        if execution == 'debug':
            r = process(['qemu-arm-static', '-g', '{port}'.format(port=debug_port), './{challenge_name}'.format(challenge_name=challenge_name)])
            log.info('xhelp - help in gdb')
            os.system('x-terminal-emulator -e "gdb-multiarch -x ../gdb_plugin.sh {challenge_name}"'.format(
                challenge_name=challenge_name))
        elif execution == 'local':
            r = process(['qemu-arm-static', './{challenge_name}'.format(challenge_name=challenge_name)])
        elif execution == 'remote':
            ssh_client = ssh(user=challenge_name, host='exercises-arm32v7.zenysec.com', port=remote_port,
                             password=challenge_name)
            r = ssh_client.process(['./{challenge_name}'.format(challenge_name=challenge_name)])
        else:
            log.error('Unknown execution type {execution}'.format(execution=execution))
            raise Exception('ArmExploit: Unknown execution type {execution}'.format(execution=execution))
        self.r = r

