Пример #1
0
 def disconnect(self, message, id):
     # close threading
     print('disconnect')
     try:
         audit_log = Log.objects.get(
             channel=self.message.reply_channel.name)
         audit_log.is_finished = True
         audit_log.end_time = now()
         audit_log.save()
         cache_key = audit_log.gucamole_client_id
         client = GuacamoleClient(
             settings.GUACD_HOST, settings.GUACD_PORT)
         client.send_instruction(Instruction('select', cache_key))
         instruction = client.read_instruction()
         kwargs = {'width': 1024, 'height': 768, 'read_only': 'true'}
         connection_args = [
             kwargs.get(arg.replace('-', '_'), '') for arg in instruction.args
         ]
         client.send_instruction(Instruction('size', 1024, 768, 96))
         client.send_instruction(Instruction('audio', *list()))
         client.send_instruction(Instruction('video', *list()))
         client.send_instruction(Instruction('image', *list()))
         client.send_instruction(Instruction('connect', *connection_args))
         client.send_instruction(Instruction(
             'disconnect', *connection_args))
     except:
         pass
     finally:
         get_redis_instance().delete(id)
         self.message.reply_channel.send({"accept": False})
Пример #2
0
 def disconnect(self, message, id):
     # close threading
     print('disconnect')
     try:
         log_object = Log.objects.get(id=id)
         cache_key = log_object.gucamole_client_id
         client = GuacamoleClient(
             settings.GUACD_HOST, settings.GUACD_PORT)
         client.send_instruction(Instruction('select', cache_key))
         instruction = client.read_instruction()
         kwargs = {'width': 1024, 'height': 768, 'read_only': 'true'}
         connection_args = [
             kwargs.get(arg.replace('-', '_'), '') for arg in instruction.args
         ]
         client.send_instruction(Instruction('size', 1024, 768, 96))
         client.send_instruction(Instruction('audio', *list()))
         client.send_instruction(Instruction('video', *list()))
         client.send_instruction(Instruction('image', *list()))
         client.send_instruction(Instruction('connect', *connection_args))
         client.send_instruction(Instruction(
             'disconnect', *connection_args))
     except:
         pass
     finally:
         self.message.reply_channel.send({"accept": False})
Пример #3
0
    def test_instruction_valid_encode(self):
        """
        Test valid instruction encoding.
        """
        instruction_str = '4.args,8.hostname,4.port,4.1984;'
        instruction_opcode = 'args'
        instruction_args = ('hostname', 'port', 1984)

        instruction = Instruction('args', 'hostname', 'port', 1984)

        self.assertEqual(instruction_str, instruction.encode())
        self.assertEqual(instruction_opcode, instruction.opcode)
        self.assertEqual(instruction_args, instruction.args)
Пример #4
0
    def connect(self, message, id):
        self.message.reply_channel.send({"accept": True})
        if not self.authenticate:
            self.message.reply_channel.send(
                {
                    "text":
                    json.dumps({
                        'status': False,
                        'message': 'You must login to the system!'
                    })
                },
                immediately=True)
            self.message.reply_channel.send({"accept": False})
        else:
            #permission auth
            if not self.haspermission('common.can_monitor_serverinfo'):
                self.message.reply_channel.send(
                    {
                        "text":
                        json.dumps({
                            'status':
                            False,
                            'message':
                            'You have not permission to monitor user action!'
                        })
                    },
                    immediately=True)
                self.message.reply_channel.send({"accept": False})

            client = GuacamoleClient(settings.GUACD_HOST, settings.GUACD_PORT)
            log_object = Log.objects.get(id=id)
            cache_key = str(log_object.gucamole_client_id)

            data = log_object.server

            #draft version for real time monitor
            client.send_instruction(Instruction('select', cache_key))
            instruction = client.read_instruction()
            kwargs = {'width': 1024, 'height': 768, 'read_only': 'true'}
            connection_args = [
                kwargs.get(arg.replace('-', '_'), '')
                for arg in instruction.args
            ]
            client.send_instruction(Instruction('size', 1024, 768, 96))
            client.send_instruction(Instruction('audio', *list()))
            client.send_instruction(Instruction('video', *list()))
            client.send_instruction(Instruction('image', *list()))
            client.send_instruction(Instruction('connect', *connection_args))

            #self.message.reply_channel.send({"text":'0.,{0}.{1};'.format(len(cache_key),cache_key)},immediately=True)
            guacamolethread = GuacamoleThread(self.message, client)
            guacamolethread.setDaemon = True
            guacamolethread.start()

            guacamolethreadwrite = GuacamoleThreadWrite(self.message, client)
            guacamolethreadwrite.setDaemon = True
            guacamolethreadwrite.start()
Пример #5
0
    def test_instruction_valid_encode_with_protocol_chars(self):
        """
        Test valid instruction encoding with arg containing
        protocol characters.
        """
        # arg includes ARG_SEP, ELEM_SEP, INST_TERM and a white space.
        arg_protocol_chars = 'p,.; t'
        instruction_str = '4.args,8.hostname,%s.%s;' %\
            (len(arg_protocol_chars), arg_protocol_chars)

        instruction_opcode = 'args'
        instruction_args = ('hostname', arg_protocol_chars)

        instruction = Instruction('args', 'hostname', arg_protocol_chars)

        self.assertEqual(instruction_str, instruction.encode())
        self.assertEqual(instruction_opcode, instruction.opcode)
        self.assertEqual(instruction_args, instruction.args)
Пример #6
0
    def test_instruction_valid_encode_unicode(self):
        """
        Test valid instruction encoding with unicode characters.
        """
        # instruction str for validation!
        instruction_str = '4.args,8.hostname,%s.%s;' %\
            (self.u_arg_len, self.u_arg_utf8)

        instruction_opcode = 'args'
        instruction_args = ('hostname', self.u_arg)

        # passing a unicode arg!
        instruction = Instruction('args', 'hostname', self.u_arg)

        self.assertEqual(instruction_opcode, instruction.opcode)
        self.assertEqual(instruction_args, instruction.args)

        # all args should be utf_8 after instruction.encode()
        self.assertEqual(instruction_str, instruction.encode())
Пример #7
0
    def connect(self, message, id):
        self.message.reply_channel.send({"accept": True})
        if not self.authenticate:
            self.message.reply_channel.send(
                {
                    "text":
                    json.dumps({
                        'status': False,
                        'message': 'You must login to the system!'
                    })
                },
                immediately=True)
            self.message.reply_channel.send({"accept": False})
        else:
            client = GuacamoleClient(settings.GUACD_HOST, settings.GUACD_PORT)
            log_object = Log.objects.get(id=id)
            cache_key = str(log_object.gucamole_client_id)

            data = log_object.server
            print 'cache_key', cache_key
            client.handshake(
                width=data.credential.width,
                height=data.credential.height,
                protocol=data.credential.protocol,
                hostname=data.ip,
                port=data.credential.port,
                username=data.credential.username,
                password=data.credential.password,
                read_only=True,
            )

            client.send_instruction(Instruction('select', cache_key))
            #self.message.reply_channel.send({"text":'0.,{0}.{1};'.format(len(cache_key),cache_key)},immediately=True)
            guacamolethread = GuacamoleThread(self.message, client)
            guacamolethread.setDaemon = True
            guacamolethread.start()

            guacamolethreadwrite = GuacamoleThreadWrite(self.message, client)
            guacamolethreadwrite.setDaemon = True
            guacamolethreadwrite.start()
Пример #8
0
    def handshake(self,
                  protocol='vnc',
                  width=1024,
                  height=768,
                  dpi=96,
                  audio=None,
                  video=None,
                  image=None,
                  **kwargs):
        """
        Establish connection with Guacamole guacd server via handshake.
        """
        if protocol not in PROTOCOLS:
            self.logger.debug('Invalid protocol: %s' % protocol)
            raise GuacamoleError('Cannot start Handshake. Missing protocol.')

        if audio is None:
            audio = list()

        if video is None:
            video = list()

        if image is None:
            image = list()

        # 1. Send 'select' instruction
        self.logger.debug('Send `select` instruction.')
        self.send_instruction(Instruction('select', protocol))

        # 2. Receive `args` instruction
        instruction = self.read_instruction()
        self.logger.debug('Expecting `args` instruction, received: %s' %
                          str(instruction))

        if not instruction:
            self.close()
            raise GuacamoleError(
                'Cannot establish Handshake. Connection Lost!')

        if instruction.opcode != 'args':
            self.close()
            raise GuacamoleError(
                'Cannot establish Handshake. Expected opcode `args`, '
                'received `%s` instead.' % instruction.opcode)

        # 3. Respond with size, audio & video support
        self.logger.debug('Send `size` instruction (%s, %s, %s)' %
                          (width, height, dpi))
        self.send_instruction(Instruction('size', width, height, dpi))

        self.logger.debug('Send `audio` instruction (%s)' % audio)
        self.send_instruction(Instruction('audio', *audio))

        self.logger.debug('Send `video` instruction (%s)' % video)
        self.send_instruction(Instruction('video', *video))

        self.logger.debug('Send `image` instruction (%s)' % image)
        self.send_instruction(Instruction('image', *image))

        # 4. Send `connect` instruction with proper values
        connection_args = [
            kwargs.get(arg.replace('-', '_'), '') for arg in instruction.args
        ]

        self.logger.debug('Send `connect` instruction (%s)' % connection_args)
        self.send_instruction(Instruction('connect', *connection_args))

        # 5. Receive ``ready`` instruction, with client ID.
        instruction = self.read_instruction()
        self.logger.debug('Expecting `ready` instruction, received: %s' %
                          str(instruction))

        if instruction.opcode != 'ready':
            self.logger.warning(
                'Expected `ready` instruction, received: %s instead')

        if instruction.args:
            self._id = instruction.args[0]
            self.logger.debug('Established connection with client id: %s' %
                              self.id)

        self.logger.debug('Handshake completed.')
        self.connected = True