def send_enforcer_telemetry_activity(config, update_reason): details = { 'enforcer_configs': config.telemetry_config, 'node_name': socket.gethostname(), 'os_name': sys.platform, 'update_reason': update_reason, 'module_version': config.module_version } body = { 'type': px_constants.TELEMETRY_ACTIVITY, 'timestamp': time.time(), 'px_app_id': config.app_id, 'details': details } headers = { 'Authorization': 'Bearer ' + config.auth_token, 'Content-Type': 'application/json' } config.logger.debug('Sending telemetry activity to PerimeterX servers') px_httpc.send(full_url=config.server_host + px_constants.API_ENFORCER_TELEMETRY, body=json.dumps(body), headers=headers, config=config, method='POST')
def send_activities(): global ACTIVITIES_BUFFER while True: if len(ACTIVITIES_BUFFER) > 0: chunk = ACTIVITIES_BUFFER[:10] ACTIVITIES_BUFFER = ACTIVITIES_BUFFER[10:] px_httpc.send('/api/v1/collector/s2s', chunk, CONFIG) time.sleep(1)
def _send_activities_chunk(): global ACTIVITIES_BUFFER default_headers = { 'Authorization': 'Bearer ' + CONFIG.auth_token, 'Content-Type': 'application/json' } full_url = CONFIG.server_host + px_constants.API_ACTIVITIES chunk = ACTIVITIES_BUFFER[:10] for _ in range(len(chunk)): ACTIVITIES_BUFFER.pop(0) px_httpc.send(full_url=full_url, body=json.dumps(chunk), headers=default_headers, config=CONFIG, method='POST')
def send_reverse_xhr_request(self, config, ctx, body): uri = ctx.uri if not config.first_party or not config.first_party_xhr_enabled: body, response_headers = self.return_default_response(uri) return '200 OK', response_headers, body xhr_path_index = uri.find('/' + px_constants.XHR_FP_PATH) suffix_uri = uri[xhr_path_index + 4:] host = config.collector_host headers = {'host': host, px_constants.FIRST_PARTY_HEADER: '1', px_constants.ENFORCER_TRUE_IP_HEADER: ctx.ip} if ctx.vid is not None: headers['Cookies'] = '_pxvid=' + ctx.vid filtered_headers = px_utils.handle_proxy_headers(ctx.headers, ctx.ip) filtered_headers = px_utils.merge_two_dicts(filtered_headers, headers) msg = 'Forwarding request from {} to client at {}{}' self._logger.debug(msg.format(ctx.uri.lower(), host, suffix_uri)) px_response = px_httpc.send(full_url=host + suffix_uri + "?" + ctx.query_params, body=body, headers=filtered_headers, config=config, method=ctx.http_method) if px_response.status_code >= 400: data, response_headers = self.return_default_response(uri) self._logger.debug('Error reversing the http call: {}'.format(px_response.reason)) return '200 OK', response_headers, data data = px_response.content headers = px_response.headers status = str(px_response.status_code) + ' ' + px_response.reason return status, headers, data
def send_risk_request(ctx, config): """ :param PxContext ctx: :param PxConfig config: :return dict: """ start = time.time() body = prepare_risk_body(ctx, config) default_headers = { 'Authorization': 'Bearer ' + config.auth_token, 'Content-Type': 'application/json' } try: response = px_httpc.send(full_url=config.server_host + px_constants.API_RISK, body=json.dumps(body), config=config, headers=default_headers, method='POST', raise_error = True) if response: return json.loads(response.content) return False except requests.exceptions.Timeout: ctx.pass_reason = 's2s_timeout' risk_rtt = time.time() - start config.logger.debug('Risk API timed out, round_trip_time: {}'.format(risk_rtt)) return False except requests.exceptions.RequestException as e: ctx.pass_reason = 's2s_error' config.logger.debug('Unexpected exception in Risk API call: {}'.format(e)) return False
def send_risk_request(ctx, config): """ :param PxContext ctx: :param PxConfig config: :return dict: """ start = time.time() body = prepare_risk_body(ctx, config) default_headers = { 'Authorization': 'Bearer ' + config.auth_token, 'Content-Type': 'application/json' } try: response = px_httpc.send(full_url=config.server_host + px_constants.API_RISK, body=json.dumps(body), config=config, headers=default_headers, method='POST') if response: return json.loads(response.content) return False except requests.exceptions.Timeout: risk_rtt = time.time() - start config.logger( 'Risk API timed out, round_trip_time: {}'.format(risk_rtt))
def send_reverse_client_request(self, config, ctx): if not config.first_party: headers = {'Content-Type': 'application/javascript'} return '200 OK', headers, '' client_request_uri = '/{}/main.min.js'.format(config.app_id) msg = 'Forwarding request from {} to client at {}{}' self._logger.debug( msg.format(ctx.uri.lower(), px_constants.CLIENT_HOST, client_request_uri)) headers = { 'host': px_constants.CLIENT_HOST, px_constants.FIRST_PARTY_HEADER: '1', px_constants.ENFORCER_TRUE_IP_HEADER: ctx.ip } filtered_headers = px_utils.handle_proxy_headers( ctx.headers, ctx.ip, self.is_gae) filtered_headers = px_utils.merge_two_dicts(filtered_headers, headers) delete_extra_headers(filtered_headers) px_response = px_httpc.send(full_url=px_constants.CLIENT_HOST + client_request_uri, body='', headers=filtered_headers, config=config, method='GET') if self.is_gae: data = px_response.raw.read(decode_content=True) else: data = px_response.raw.read() headers = px_response.headers status = str(px_response.status_code) + ' ' + str(px_response.reason) return status, headers, data
def send_reverse_captcha_request(self, config, ctx): if not config.first_party: status = '200 OK' response_headers = {'Content-Type': 'application/javascript'} return status, response_headers, '' uri = '/{}{}?{}'.format( config.app_id, ctx.uri.lower().replace(self.captcha_reverse_prefix, ''), ctx.query_params) host = px_constants.CAPTCHA_HOST headers = { 'host': px_constants.CAPTCHA_HOST, px_constants.FIRST_PARTY_HEADER: '1', px_constants.ENFORCER_TRUE_IP_HEADER: ctx.ip } filtered_headers = px_utils.handle_proxy_headers( ctx.headers, ctx.ip, self.is_gae) filtered_headers = px_utils.merge_two_dicts(filtered_headers, headers) delete_extra_headers(filtered_headers) self._logger.debug( 'Forwarding request from {} to client at {}{}'.format( ctx.uri.lower(), host, uri)) px_response = px_httpc.send(full_url=host + uri, body='', headers=filtered_headers, config=config, method='GET') if self.is_gae: data = px_response.raw.read(decode_content=True) else: data = px_response.raw.read() headers = px_response.headers status = str(px_response.status_code) + ' ' + str(px_response.reason) return status, headers, data
def send_captcha_request(vid, uuid, captcha_value, ctx, config): body = { 'request': { 'ip': ctx.get('socket_ip'), 'headers': format_headers(ctx.get('headers')), 'uri': ctx.get('uri') }, 'pxCaptcha': captcha_value, 'vid': vid, 'uuid': uuid, 'hostname': ctx.get('hostname') } response = px_httpc.send('/api/v1/risk/captcha', body=body, config=config) return response
def send_risk_request(ctx, config): body = prepare_risk_body(ctx, config) return px_httpc.send('/api/v1/risk', body, config)