def _start_collect(self, stream, result): ''' Start the COLLECT phase ''' STATE.update('collect') logging.debug('raw_negotiate: collect in progress...') context = stream.opaque extra = context.extra extra['local_result'] = result body = six.b(json.dumps(result)) host_header = utils_net.format_epnt((extra['address'], extra['port'])) self.append_request(stream, 'POST', '/collect/raw', 'HTTP/1.1') self.append_header(stream, 'Host', host_header) self.append_header(stream, 'User-Agent', utils_version.HTTP_HEADER) self.append_header(stream, 'Content-Type', 'application/json') self.append_header(stream, 'Content-Length', str(len(body))) self.append_header(stream, 'Cache-Control', 'no-cache') self.append_header(stream, 'Pragma', 'no-cache') self.append_header(stream, 'Connection', 'close') if extra['authorization']: self.append_header(stream, 'Authorization', extra['authorization']) self.append_end_of_headers(stream) self.append_bytes(stream, body) http_utils.prettyprint_json(result, '>') self.send_message(stream) context.body = six.StringIO() # Want to save body extra['requests'] += 1
def handle_connection_made(self, stream): ''' Invoked when the connection is established ''' # Note: this function MUST be callable multiple times logging.debug('skype_negotiate: negotiation in progress...') context = stream.opaque extra = context.extra # TODO(claudiu) Choose a random configuration from: # voice-upstream, voice-downstream, voice-bidirectional. # Thie assumes that neubot only runs on desktop clients, so # there is no need to do a bandwidth check in order to select # which test should be performed. request = {} # No options for now request['type'] = 'voice' request['direction'] = 'upstream' request['src-port'] = 5060 body = six.b(json.dumps(request)) host_header = utils_net.format_epnt((extra['address'], extra['port'])) self.append_request(stream, 'GET', '/negotiate/skype', 'HTTP/1.1') self.append_header(stream, 'Host', host_header) self.append_header(stream, 'User-Agent', utils_version.HTTP_HEADER) self.append_header(stream, 'Content-Type', 'application/json') self.append_header(stream, 'Content-Length', str(len(body))) self.append_header(stream, 'Cache-Control', 'no-cache') self.append_header(stream, 'Pragma', 'no-cache') if extra['authorization']: self.append_header(stream, 'Authorization', extra['authorization']) self.append_end_of_headers(stream) self.append_bytes(stream, body) http_utils.prettyprint_json(request, '>') self.send_message(stream) extra['requests'] += 1
def handle_end_of_body(self, stream): HttpClient.handle_end_of_body(self, stream) context = stream.opaque extra = context.extra if extra['requests'] <= 0: raise RuntimeError('runner_mlabns: unexpected response') extra['requests'] -= 1 tmp = context.headers.get(CONTENT_TYPE) if context.code != CODE200 or tmp != APPLICATION_JSON: logging.error('runner_mlabns: bad response') stream.close() return content = six.bytes_to_string(context.body.getvalue(), 'utf-8') response = json.loads(content) http_utils.prettyprint_json(response, '<') if extra['policy'] == 'random': RUNNER_HOSTS.set_random_host(response) else: RUNNER_HOSTS.set_closest_host(response) stream.close()
def handle_end_of_body(self, stream): # Note: this function MUST be callable multiple times context = stream.opaque extra = context.extra if extra['requests'] <= 0: raise RuntimeError('skype_negotiate: unexpected response') extra['requests'] -= 1 tmp = context.headers.get(CONTENT_TYPE) if context.code != CODE200 or tmp != APPLICATION_JSON: logging.error('skype_negotiate: bad response') stream.close() return response = json.loads(six.u(extra['body'].getvalue())) http_utils.prettyprint_json(response, '<') if STATE.current == 'negotiate': self._process_negotiate_response(stream, response) elif STATE.current == 'collect': self._process_collect_response(stream, response) else: raise RuntimeError('skype_negotiate: internal error')
def handle_connection_made(self, stream): ''' Invoked when the connection is established ''' # Note: this function MUST be callable multiple times logging.debug('raw_negotiate: negotiation in progress...') context = stream.opaque extra = context.extra request = {} # No options for now body = six.b(json.dumps(request)) host_header = utils_net.format_epnt((extra['address'], extra['port'])) self.append_request(stream, 'POST', '/negotiate/raw', 'HTTP/1.1') self.append_header(stream, 'Host', host_header) self.append_header(stream, 'User-Agent', utils_version.HTTP_HEADER) self.append_header(stream, 'Content-Type', 'application/json') self.append_header(stream, 'Content-Length', str(len(body))) self.append_header(stream, 'Cache-Control', 'no-cache') self.append_header(stream, 'Pragma', 'no-cache') if extra['authorization']: self.append_header(stream, 'Authorization', extra['authorization']) self.append_end_of_headers(stream) self.append_bytes(stream, body) http_utils.prettyprint_json(request, '>') self.send_message(stream) context.body = six.StringIO() # Want to save body extra['requests'] += 1