예제 #1
0
 def _add_chunks_to_queue(self,
                          chunks,
                          start_send_cb=None,
                          end_send_cb=None):
     """ the write_lock must be held when calling this function """
     counter = 0
     items = []
     for proto_flags, index, level, data in chunks:
         scb, ecb = None, None
         #fire the start_send_callback just before the first packet is processed:
         if counter == 0:
             scb = start_send_cb
         #fire the end_send callback when the last packet (index==0) makes it out:
         if index == 0:
             ecb = end_send_cb
         payload_size = len(data)
         actual_size = payload_size
         if self.cipher_out:
             proto_flags |= FLAGS_CIPHER
             #note: since we are padding: l!=len(data)
             padding_size = self.cipher_out_block_size - (
                 payload_size % self.cipher_out_block_size)
             if padding_size == 0:
                 padded = data
             else:
                 # pad byte value is number of padding bytes added
                 padded = memoryview_to_bytes(data) + pad(
                     self.cipher_out_padding, padding_size)
                 actual_size += padding_size
             assert len(
                 padded
             ) == actual_size, "expected padded size to be %i, but got %i" % (
                 len(padded), actual_size)
             data = self.cipher_out.encrypt(padded)
             assert len(
                 data
             ) == actual_size, "expected encrypted size to be %i, but got %i" % (
                 len(data), actual_size)
             cryptolog("sending %s bytes %s encrypted with %s padding",
                       payload_size, self.cipher_out_name, padding_size)
         if proto_flags & FLAGS_NOHEADER:
             assert not self.cipher_out
             #for plain/text packets (ie: gibberish response)
             log("sending %s bytes without header", payload_size)
             items.append((data, scb, ecb))
         elif actual_size < PACKET_JOIN_SIZE:
             if type(data) not in JOIN_TYPES:
                 data = bytes(data)
             header_and_data = pack_header(proto_flags, level, index,
                                           payload_size) + data
             items.append((header_and_data, scb, ecb))
         else:
             header = pack_header(proto_flags, level, index, payload_size)
             items.append((header, scb, None))
             items.append((strtobytes(data), None, ecb))
         counter += 1
     if self._write_thread is None:
         self.start_write_thread()
     self._write_queue.put(items)
     self.output_packetcount += 1
예제 #2
0
 def test_roundtrip(self):
     print("hello")
     for encode_flag in (FLAGS_BENCODE, FLAGS_RENCODE, FLAGS_YAML):
         for comp_flag in (ZLIB_FLAG, LZ4_FLAG, LZO_FLAG, BROTLI_FLAG):
             for cipher in (0, FLAGS_CIPHER):
                 for level in (0, 1, 10):
                     for index in (0, 1, 255):
                         for size in (0, 1, 2**8, 2**16, 2**24):
                             proto_flags = encode_flag | comp_flag | cipher
                             header = pack_header(proto_flags, level, index,
                                                  size)
                             assert header
                             uproto_flags, ulevel, uindex, usize = unpack_header(
                                 header)[1:]
                             assert uproto_flags == proto_flags
                             assert ulevel == level
                             assert uindex == index
                             assert usize == size
                             for i in range(0, len(header) - 1):
                                 try:
                                     unpack_header(header[:i])
                                 except Exception:
                                     pass
                                 else:
                                     raise Exception(
                                         "header unpacking should have failed for size %i"
                                         % i)
예제 #3
0
def pack_one_packet(packet):
    ee = get_enabled_encoders()
    if ee:
        e = get_encoder(ee[0])
        data, flags = e(packet)
        return pack_header(flags, 0, 0, len(data)) + data
    return str(packet)
예제 #4
0
def pack_one_packet(packet):
    from xpra.net.header import pack_header
    ee = get_enabled_encoders()
    if ee:
        e = get_encoder(ee[0])
        data, flags = e(packet)
        return pack_header(flags, 0, 0, len(data)) + data
    return str(packet)
예제 #5
0
파일: protocol.py 프로젝트: svn2github/Xpra
 def _add_chunks_to_queue(self, chunks, start_send_cb=None, end_send_cb=None):
     """ the write_lock must be held when calling this function """
     counter = 0
     items = []
     for proto_flags,index,level,data in chunks:
         scb, ecb = None, None
         #fire the start_send_callback just before the first packet is processed:
         if counter==0:
             scb = start_send_cb
         #fire the end_send callback when the last packet (index==0) makes it out:
         if index==0:
             ecb = end_send_cb
         payload_size = len(data)
         actual_size = payload_size
         if self.cipher_out:
             proto_flags |= FLAGS_CIPHER
             #note: since we are padding: l!=len(data)
             padding_size = self.cipher_out_block_size - (payload_size % self.cipher_out_block_size)
             if padding_size==0:
                 padded = data
             else:
                 # pad byte value is number of padding bytes added
                 padded = data + pad(self.cipher_out_padding, padding_size)
                 actual_size += padding_size
             assert len(padded)==actual_size, "expected padded size to be %i, but got %i" % (len(padded), actual_size)
             data = self.cipher_out.encrypt(padded)
             assert len(data)==actual_size, "expected encrypted size to be %i, but got %i" % (len(data), actual_size)
             cryptolog("sending %s bytes %s encrypted with %s padding", payload_size, self.cipher_out_name, padding_size)
         if proto_flags & FLAGS_NOHEADER:
             assert not self.cipher_out
             #for plain/text packets (ie: gibberish response)
             log("sending %s bytes without header", payload_size)
             items.append((data, scb, ecb))
         elif actual_size<PACKET_JOIN_SIZE:
             if type(data) not in JOIN_TYPES:
                 data = bytes(data)
             header_and_data = pack_header(proto_flags, level, index, payload_size) + data
             items.append((header_and_data, scb, ecb))
         else:
             header = pack_header(proto_flags, level, index, payload_size)
             items.append((header, scb, None))
             items.append((strtobytes(data), None, ecb))
         counter += 1
     self._write_queue.put(items)
     self.output_packetcount += 1
예제 #6
0
 def _add_chunks_to_queue(self, chunks, proto_flags, start_send_cb=None, end_send_cb=None):
     """ the write_lock must be held when calling this function """
     counter = 0
     items = []
     for index,level,data in chunks:
         scb, ecb = None, None
         #fire the start_send_callback just before the first packet is processed:
         if counter==0:
             scb = start_send_cb
         #fire the end_send callback when the last packet (index==0) makes it out:
         if index==0:
             ecb = end_send_cb
         payload_size = len(data)
         actual_size = payload_size
         if self.cipher_out:
             proto_flags |= FLAGS_CIPHER
             #note: since we are padding: l!=len(data)
             padding = (self.cipher_out_block_size - len(data) % self.cipher_out_block_size) * " "
             if len(padding)==0:
                 padded = data
             else:
                 padded = data+padding
             actual_size = payload_size + len(padding)
             assert len(padded)==actual_size
             data = self.cipher_out.encrypt(padded)
             assert len(data)==actual_size
             log("sending %s bytes encrypted with %s padding", payload_size, len(padding))
         if proto_flags & FLAGS_NOHEADER:
             #for plain/text packets (ie: gibberish response)
             items.append((data, scb, ecb))
         elif actual_size<PACKET_JOIN_SIZE:
             if type(data) not in JOIN_TYPES:
                 data = bytes(data)
             header_and_data = pack_header(proto_flags, level, index, payload_size) + data
             items.append((header_and_data, scb, ecb))
         else:
             header = pack_header(proto_flags, level, index, payload_size)
             items.append((header, scb, None))
             items.append((strtobytes(data), None, ecb))
         counter += 1
     self._write_queue.put(items)
     self.output_packetcount += 1
예제 #7
0
 def _add_chunks_to_queue(self, chunks, start_send_cb=None, end_send_cb=None, fail_cb=None, synchronous=True, more=False):
     """ the write_lock must be held when calling this function """
     counter = 0
     items = []
     for proto_flags,index,level,data in chunks:
         payload_size = len(data)
         actual_size = payload_size
         if self.cipher_out:
             proto_flags |= FLAGS_CIPHER
             #note: since we are padding: l!=len(data)
             padding_size = self.cipher_out_block_size - (payload_size % self.cipher_out_block_size)
             if padding_size==0:
                 padded = data
             else:
                 # pad byte value is number of padding bytes added
                 padded = memoryview_to_bytes(data) + pad(self.cipher_out_padding, padding_size)
                 actual_size += padding_size
             assert len(padded)==actual_size, "expected padded size to be %i, but got %i" % (len(padded), actual_size)
             data = self.cipher_out.encrypt(padded)
             assert len(data)==actual_size, "expected encrypted size to be %i, but got %i" % (len(data), actual_size)
             cryptolog("sending %s bytes %s encrypted with %s padding", payload_size, self.cipher_out_name, padding_size)
         if proto_flags & FLAGS_NOHEADER:
             assert not self.cipher_out
             #for plain/text packets (ie: gibberish response)
             log("sending %s bytes without header", payload_size)
             items.append(data)
         elif actual_size<PACKET_JOIN_SIZE:
             if not isinstance(data, JOIN_TYPES):
                 data = memoryview_to_bytes(data)
             header_and_data = pack_header(proto_flags, level, index, payload_size) + data
             items.append(header_and_data)
         else:
             header = pack_header(proto_flags, level, index, payload_size)
             items.append(header)
             items.append(data)
         counter += 1
     self.raw_write(items, start_send_cb, end_send_cb, fail_cb, synchronous, more)
예제 #8
0
 def _send_async(self, packet, sync=False, fail_cb=None):
     """ used by send_control to bypass the regular queuing functions,
         and force enable synchronous=False
     """
     #log("_send_async(%s, %s) encoder=%s, compressor=%s", packet, sync, self._encoder, self._compress)
     log("_send_async(%s, %s)", packet, sync)
     chunks = self.encode(packet)
     if len(chunks) > 1:
         return Protocol.send_now(self, packet)
     proto_flags, index, level, data = chunks[0]
     from xpra.net.header import pack_header
     payload_size = len(data)
     header_and_data = pack_header(proto_flags, level, index,
                                   payload_size) + data
     with self._write_lock:
         if self._write_thread is None:
             self.start_write_thread()
         self._write_queue.put((header_and_data, None, None, fail_cb, sync))