예제 #1
0
    def __init__(self, app_name: str):
        """Create a spawned process."""
        parser = argparse.ArgumentParser(
            description="Run %s through a filtering proxy." % app_name)
        parser.add_argument('cmd',
                            metavar='ARGS',
                            nargs='+',
                            help='%s command with arguments' % app_name)
        parser.add_argument('-a',
                            '--address',
                            metavar='ADDR',
                            help='A file to dump the side channel UDP port.')
        args = parser.parse_args()

        self.exitstatus = 0
        self.server_address: str = args.address
        self.argv = args.cmd
        log_handler = logging.NullHandler() if not os.environ.get('CI') \
            else logging.FileHandler("proxy.log")
        logging.basicConfig(
            level=logging.DEBUG,
            format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
            handlers=[log_handler])
        self.logger = logging.getLogger(type(self).__name__)
        self.logger.info("Starting proxy: %s", app_name)

        self.sock: Union[socket.socket, None] = None
        if self.server_address:
            # Create a UDS socket
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            self.sock.bind(('127.0.0.1', 0))
            self.sock.settimeout(0.5)
            _, port = self.sock.getsockname()
            with open(self.server_address, 'w') as f:
                f.write(f"{port}")

        # Create the filter
        self.filter = [(stream_filter.Filter(), lambda _: None)]
        # Where was the last command received from?
        self.last_addr = None

        # Last user command
        self.last_command = b''
        self.command_buffer = bytearray()

        # Spawn the process in a PTY
        self.pid, self.master_fd = pty.fork()
        if self.pid == pty.CHILD:
            try:
                os.execvp(self.argv[0], self.argv)
            except OSError as e:
                sys.stderr.write(f"Failed to launch: {e}\n")
                os._exit(1)
예제 #2
0
    def __init__(self, app_name: str):
        """Create a spawned process."""
        parser = argparse.ArgumentParser(
            description="Run %s through a filtering proxy." % app_name)
        parser.add_argument('cmd', metavar='ARGS', nargs='+',
                            help='%s command with arguments' % app_name)
        parser.add_argument('-a', '--address', metavar='ADDR',
                            help='Local socket to receive commands.')
        args = parser.parse_args()

        self.server_address: str = args.address
        self.argv = args.cmd
        log_handler = logging.NullHandler() if not os.environ.get('CI') \
            else logging.FileHandler("proxy.log")
        logging.basicConfig(
            level=logging.DEBUG,
            format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
            handlers=[log_handler])
        self.logger = logging.getLogger(type(self).__name__)
        self.logger.info("Starting proxy: %s", app_name)

        self.sock: Union[socket.socket, None] = None
        if self.server_address:
            # Create a UDS socket
            self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
            self.sock.bind(self.server_address)
            self.sock.settimeout(0.5)

        # Create the filter
        self.filter = [(stream_filter.Filter(), lambda _: None)]
        # Where was the last command received from?
        self.last_addr = None

        # Last user command
        self.last_command = b''
        self.command_buffer = bytearray()

        # Spawn the process in a PTY
        pid, self.master_fd = pty.fork()
        if pid == pty.CHILD:
            os.execvp(self.argv[0], self.argv)
예제 #3
0
    def __init__(self, app_name):
        """Create a spawned process."""

        parser = argparse.ArgumentParser(
            description="Run %s through a filtering proxy." % app_name)
        parser.add_argument('cmd',
                            metavar='ARGS',
                            nargs='+',
                            help='%s command with arguments' % app_name)
        parser.add_argument('-a',
                            '--address',
                            metavar='ADDR',
                            help='Local socket to receive commands.')
        args = parser.parse_args()

        self.server_address = args.address
        self.argv = args.cmd
        self.logfile = None
        # self.logfile = open("/tmp/log.txt", "w")

        if self.server_address:
            # Create a UDS socket
            self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
            self.sock.bind(self.server_address)
            self.sock.settimeout(0.5)
        else:
            self.sock = None

        # Create the filter
        self.filter = [(stream_filter.Filter(), lambda _: None)]
        # Where was the last command received from?
        self.last_addr = None

        # Spawn the process in a PTY
        pid, self.master_fd = pty.fork()
        if pid == pty.CHILD:
            os.execvp(self.argv[0], self.argv)