Submit traced TCP flows.
167944
def submit(self):
        """"""
        self._newflg = False
        ret = list()
        for buf in self._buffer.values():
            buf = copy.deepcopy(buf)
            if self._fdpext:
                buf['fpout'] = f"{self._fproot}/{buf['label']}.{self._fdpext}"
            else:
                del buf['fpout']
            buf['index'] = tuple(buf['index'])
            ret.append(Info(buf))
        ret += self._stream
        return tuple(ret)
779512
def net_send(out_data, conn):
    '''  '''
    print('Sending {} bytes'.format(len(out_data)))
    conn.send(out_data)
720914
async def tcp_echo_client(message, loop, host, port):
    """"""
    print("Connecting to server at %s:%d" % (host, port))
    reader, writer = await asyncio.open_connection(host, port, loop=loop)

    writer.write(message.encode())
    print('Sent: %r' % message)

    data = await reader.read(100)
    print('Received: %r' % data.decode())

    writer.close()
5212
def wrap_submit(submit_func):
    """"""

    def call(self, func, *args, **kwargs):
        wrapped_func = wrap_task_func(func)
        _tracer = execution_context.get_opencensus_tracer()
        propagator = binary_format.BinaryFormatPropagator()

        wrapped_kwargs = {}
        wrapped_kwargs["span_context_binary"] = propagator.to_header(
            _tracer.span_context
        )
        wrapped_kwargs["kwds"] = kwargs
        wrapped_kwargs["sampler"] = _tracer.sampler
        wrapped_kwargs["exporter"] = _tracer.exporter
        wrapped_kwargs["propagator"] = _tracer.propagator

        return submit_func(self, wrapped_func, *args, **wrapped_kwargs)

    return call
797217
def submit(self):
        """"""
        if self._dirty:
            self.save()

        self._connection.run(['submit', '-c', str(self._change)], marshal_output=False)
229813
def trace(self, urls=None, **overrides):
        """"""
        if urls is not None:
            overrides['urls'] = urls
        return self.where(accept='TRACE', **overrides)
995123
def _log_request(request):
    """"""
    logger.debug("Inbound email received")

    for k, v in list(request.POST.items()):
        logger.debug("- POST['%s']='%s'" % (k, v))

    for n, f in list(request.FILES.items()):
        logger.debug("- FILES['%s']: '%s', %sB", n, f.content_type, f.size)
23714
def outgoing_connections(self):
        """"""

        # Outgoing connections are on the right
        return list(
            dropwhile(lambda c: c.direction != OUTGOING, self.connections)
        )
1026417
def tls_meta_data(self):
        """"""

        # For each flow process the contents
        for flow in self.input_stream:

            # Just TCP for now
            if flow['protocol'] != 'TCP':
                continue

            # Client to Server
            if flow['direction'] == 'CTS':

                # Try to process the payload as a set of TLS records
                try:
                    tls_records, bytes_consumed = dpkt.ssl.tls_multi_factory(flow['payload'])
                    if bytes_consumed != len(flow['payload']):
                        logger.warning('Incomplete TLS record at the end...')

                    # Process the TLS records
                    flow['tls'] = {'type':'TLS_CTS', 'data':{'tls_records': tls_records, 'uri':None, 'headers':None}}
                except (dpkt.dpkt.NeedData, dpkt.dpkt.UnpackError, dpkt.ssl.SSL3Exception):
                    flow['tls'] = None

            # Server to Client
            else:
                try:
                    tls_records, bytes_consumed = dpkt.ssl.tls_multi_factory(flow['payload'])
                    if bytes_consumed != len(flow['payload']):
                        logger.warning('Incomplete TLS record at the end...')

                    # Process the TLS records
                    flow['tls'] = {'type':'TLS_STC', 'data':{'tls_records': tls_records, 'uri':None, 'headers':None}}
                except (dpkt.dpkt.NeedData, dpkt.dpkt.UnpackError, dpkt.ssl.SSL3Exception):
                    flow['tls'] = None

            # All done
            yield flow
211827
def _inbound(self, From, to, protocol, udp_source=None):
        
        # Verify stuff!

        self.verifyCertificateAllowed(to, From)
        return self.service.verifyHook(From, to, protocol
                                       ).addCallback(self._inboundimpl,
                                                     From,
                                                     to,
                                                     protocol,
                                                     udp_source).addErrback(
            lambda f: f.trap(KeyError) and dict(listeners=[]))
1252223
def _submit(primitive, port_index, tuple_):
    """"""
    args = (_get_opc(primitive), port_index, tuple_)
    _ec._submit(args)
559011
def data_received(self, data):
        """"""
        data = data.decode()
        log.debug('received data: %s', data.strip())
        self.buffer += data
        self.handle_lines()
843460
def ApplicationReceiving(self, app, streams):
        """"""

        # we should only proceed if we are in TCP mode
        if stype != socket.SOCK_STREAM:
            return

        # handle all streams
        for stream in streams:
            # read object from the stream
            obj = StreamRead(stream)
            if obj:
                if obj[0] == cmdData:
                    # data were received, reroute it to the tunnel based on the tunnel ID
                    try:
                        TCPTunnel.threads[obj[1]].send(obj[2])
                    except KeyError:
                        pass
                elif obj[0] == cmdConnect:
                    # a connection request received, connect the socket
                    n = obj[1]
                    sock = socket.socket(type=stype)
                    try:
                        sock.connect(addr)
                        # start the tunnel thread
                        TCPTunnel(sock, stream, n).start()
                    except socket.error, e:
                        # connection failed, send an error report back through the stream
                        print 'error (%s): %s' % (n, e)
                        StreamWrite(stream, cmdError, n, tuple(e))
                        StreamWrite(stream, cmdDisconnect, n)
                elif obj[0] == cmdDisconnect:
                    # an disconnection request received, close the tunnel
                    try:
                        TCPTunnel.threads[obj[1]].close()
                    except KeyError:
                        pass
                elif obj[0] == cmdError:
                    # connection failed on the other side, display the error
                    print 'error (%s): %s' % obj[1:2]
742923
def _ReceiveFetchedFile(self, tracker):
    """"""
    index = tracker["index"]

    _, request_data = self._RemoveCompletedPathspec(index)

    # Report the request_data for this flow's caller.
    self.ReceiveFetchedFile(
        tracker["stat_entry"], tracker["hash_obj"], request_data=request_data)
766726
def http_meta_data(self):
        """"""

        # For each flow process the contents
        for flow in self.input_stream:

            # Client to Server
            if flow['direction'] == 'CTS':
                try:
                    request = dpkt.http.Request(flow['payload'])
                    request_data = data_utils.make_dict(request)
                    request_data['uri'] = self._clean_uri(request['uri'])
                    flow['http'] = {'type':'HTTP_REQUEST', 'data':request_data}
                except (dpkt.dpkt.NeedData, dpkt.dpkt.UnpackError):
                    flow['http'] = None

            # Server to Client
            else:
                try:
                    response = dpkt.http.Response(flow['payload'])
                    flow['http'] = {'type': 'HTTP_RESPONSE', 'data': data_utils.make_dict(response)}
                except (dpkt.dpkt.NeedData, dpkt.dpkt.UnpackError):
                    flow['http'] = None

            # Mark non-TCP HTTP
            if flow['http'] and flow['protocol'] != 'TCP':
                flow['http'].update({'weird': 'UDP-HTTP'})

            # All done
            yield flow
1226758
def flow_tuple(data):
    """"""
    src = net_utils.inet_to_str(data['packet'].get('src')) if data['packet'].get('src') else None
    dst = net_utils.inet_to_str(data['packet'].get('dst')) if data['packet'].get('dst') else None
    sport = data['transport'].get('sport') if data.get('transport') else None
    dport = data['transport'].get('dport') if data.get('transport') else None
    proto = data['transport'].get('type') if data.get('transport') else data['packet']['type']
    return (src, dst, sport, dport, proto)
375819
def _send_accum_trace(self, device_uuid):
        """"""

        if device_uuid not in self._connections:
            self._logger.debug("Dropping trace data for device without an active connection, uuid=0x%X", device_uuid)
            return

        conn_data = self._connections[device_uuid]

        trace = conn_data['trace_accum']

        if len(trace) > 0:
            slug = self._build_device_slug(device_uuid)
            tracing_topic = self.topics.prefix + 'devices/{}/data/tracing'.format(slug)

            data = {'type': 'notification', 'operation': 'trace'}
            data['trace'] = binascii.hexlify(trace)
            data['trace_origin'] = device_uuid

            self._logger.debug('Publishing trace: (topic=%s)', tracing_topic)
            self.client.publish(tracing_topic, data)

        conn_data['trace_scheduled'] = False
        conn_data['last_trace'] = monotonic()
        conn_data['trace_accum'] = bytes()