def handle_api_notification_request(socket, address, req): notification = req['data'] notification['subject'] = '[%(application)s] %(subject)s' % notification role = notification.get('role') if not role: reject_api_request(socket, address, 'INVALID role') logger.warn('Dropping OOB message with invalid role "%s" from app %s', role, notification.get('application')) return target = notification.get('target') if not target: reject_api_request(socket, address, 'INVALID target') logger.warn('Dropping OOB message with invalid target "%s" from app %s', target, notification.get('application')) return expanded_targets = cache.targets_for_role(role, target) if not expanded_targets: reject_api_request(socket, address, 'INVALID role:target') logger.warn('Dropping OOB message with invalid role:target "%s:%s" from app %s', role, target, notification.get('application')) return # If we're rendering this using templates+context instead of body, fill in the # needed iris key. if notification.get('template') and notification.get('context'): notification['context']['iris'] = notification['context'].get('iris', {}) logger.info('-> %s OK, to %s:%s (%s:%s)', address, role, target, notification.get('application', '?'), notification.get('priority', notification.get('mode', '?'))) for _target in expanded_targets: temp_notification = notification.copy() temp_notification['target'] = _target send_queue.put(temp_notification) metrics.incr('notification_cnt') socket.sendall(msgpack.packb('OK'))
def _send_loop(self, socket): while True: message = self._send_queue.get() if message is self: self.finish() break socket.sendall(message)
def reverse_relay(relay_task, socket, via_socket): while True: try: data = via_socket.recv(gvars.PACKET_SIZE) except gevent.GreenletExit: break except: relay_task.kill(block=False) if slowdown.gvars.logger.level <= slowdown.logging.DEBUG: slowdown.gvars.logger.debug(utils.exc()) break if not data: relay_task.kill(block=False) break if socket.closed: return try: socket.sendall(data) except gevent.GreenletExit: break except: relay_task.kill(block=False) if slowdown.gvars.logger.level <= slowdown.logging.DEBUG: slowdown.gvars.logger.debug(utils.exc()) break
def handle_api_notification_request(socket, address, req): notification = req['data'] notification['subject'] = '[%(application)s] %(subject)s' % notification role = notification.get('role') if not role: reject_api_request(socket, address, 'INVALID role') return target = notification.get('target') if not target: reject_api_request(socket, address, 'INVALID target') return expanded_targets = cache.targets_for_role(role, target) if not expanded_targets: reject_api_request(socket, address, 'INVALID role:target') return logger.info('-> %s OK, to %s:%s (%s)', address, role, target, notification.get('priority', notification.get('mode', '?'))) for _target in expanded_targets: temp_notification = notification.copy() temp_notification['target'] = _target send_queue.put(temp_notification) stats['notification_cnt'] += 1 socket.sendall(msgpack.packb('OK'))
def handle_slave_send(socket, address, req): message = req['data'] message_id = message.get('message_id', '?') try: runtime = send_funcs['send_message'](message) add_mode_stat(message['mode'], runtime) metrics_key = 'app_%(application)s_mode_%(mode)s_cnt' % message metrics.add_new_metrics({metrics_key: 0}) metrics.incr(metrics_key) if runtime is not None: response = 'OK' access_logger.info( 'Message (ID %s) from master %s sent successfully', message_id, address) metrics.incr('slave_message_send_success_cnt') else: response = 'FAIL' access_logger.error( 'Got falsy value from send_message for message (ID %s) from master %s: %s', message_id, address, runtime) metrics.incr('slave_message_send_fail_cnt') except Exception: response = 'FAIL' logger.exception('Sending message (ID %s) from master %s failed.') metrics.incr('slave_message_send_fail_cnt') socket.sendall(msgpack.packb(response))
def tcp_echo_func(socket, address): while True: r, _, _ = select.select([socket], [], [], 3.) if r: msg = socket.recv(8192) if not msg: return socket.sendall(msg)
def tcp_echo_func(socket,address): while True: r,_,_ = select.select([socket],[],[],3.) if r: msg = socket.recv(8192) if not msg: return socket.sendall(msg)
def on_handle(socket, address): try: fileobj = socket.makefile() while True: data = fileobj.readline() if not data: # client disconnected break socket.sendall(data) finally: socket.close()
def on_handle(socket, address): try: fileobj = socket.makefile() #encoding="utf-8", errors="ignore" while True: data = fileobj.readline() if not data: # client disconnected break socket.sendall(bytes(data, "utf-8")) finally: socket.close()
def handle_feedback(socket, address): print 'New FEEDBACK connection from %s:%s' % address feedback_handled.append("") if not feedback_saved: socket.recv(1) socket.sendall("") else: response = feedback_saved.popleft() for x in response: print ">>> FEEDBACK SENT TO DISPATCHER: " + repr(x) socket.sendall(x) socket.recv(1)
def do_request(): siteid = random.choice(siteids) oids = set( int(random.gauss(0, settings.objects_per_site/4)) for i in range(settings.objects_per_request) ) socket = gevent.socket.create_connection(waddr) try: socket.sendall( request_template % dict( data='_'.join(map(str, oids)), host='h%s' % siteid, ) ) response = '' while '\r\n\r\n' not in response: data = socket.recv(9999) if not data: stats.truncated += 1 return response += data headers, body = response.split('\r\n\r\n') headers = headers.split('\r\n') status = headers.pop(0) headers = dict(l.strip().lower().split(':', 1) for l in headers if ':' in l) content_length = int(headers['content-length']) while len(body) < content_length: data = socket.recv(9999) if not data: stats.truncated += 1 return body += data pid, n, nhit, nmiss, nevict = map(int, body.strip().split()) stats.requests += 1 stats.nobs += n stats.nhits += nhit bypid = stats.bypid.get(pid) if bypid is None: bypid = stats.bypid[pid] = dict(nr=0, n=0, nhit=0) bypid['nr'] += 1 bypid['n'] += n bypid['nhit'] += nhit logger.info(' '.join(map(str, ( 100*stats.nhits/stats.nobs, pid, n, nhit, 100*nhit/n, )))) finally: socket.close()
def handle_APN(socket, address): print 'New APN connection from %s:%s' % address received_data = socket.recv() unpack_received_data(received_data) if not responses_saved: socket.settimeout(1) try: socket.recv(64) except: pass else: response = responses_saved.popleft() socket.sendall(response)
def echo(socket, address): print ('New connection from %s:%s' % address) socket.sendall('Welcome to the echo server! Type quit to exit.\r\n'.encode('ascii')) # using a makefile because we want to use readline() fileobj = socket.makefile(mode='rw') while True: line = fileobj.readline() if not line: print ("client disconnected") break if line.strip().lower() == 'quit': print ("client quit") break fileobj.write(line) fileobj.flush() print ("echoed %r" % line)
def echo(socket, address): print('New connection from %s:%s' % address) socket.sendall( 'Welcome to the echo server! Type quit to exit.\r\n'.encode('ascii')) # using a makefile because we want to use readline() fileobj = socket.makefile(mode='rw') while True: line = fileobj.readline() if not line: print("client disconnected") break if line.strip().lower() == 'quit': print("client quit") break fileobj.write(line) fileobj.flush() print("echoed %r" % line)
def handle_slave_send(socket, address, req): message = req['data'] message_id = message.get('message_id', '?') message['to_slave'] = True try: runtime = send_funcs['message_send_enqueue'](message) response = 'OK' access_logger.info('Message (ID %s) from master %s queued successfully', message_id, address) except Exception: response = 'FAIL' logger.exception('Queueing message (ID %s) from master %s failed.') access_logger.error('Failed queueing message (ID %s) from master %s: %s', message_id, address, runtime) metrics.incr('slave_message_send_fail_cnt') socket.sendall(msgpack.packb(response))
def handle_api_request(socket, address): stats['api_request_cnt'] += 1 timeout = Timeout.start_new(rpc_timeout) try: req = msgpack_unpack_msg_from_socket(socket) logger.info('%s %s', address, req['endpoint']) handler = api_request_handlers.get(req['endpoint']) if handler is not None: handler(socket, address, req) else: logger.info('-> %s unknown request', address) socket.sendall(msgpack.packb('UNKNOWN')) except Timeout: stats['api_request_timeout_cnt'] += 1 logger.info('-> %s timeout', address) socket.sendall(msgpack.packb('TIMEOUT')) finally: timeout.cancel() socket.close()
def create_connection(self, target_addr): socket = gevent.socket.create_connection(self.bind_addr) reader = socket.makefile(mode='rb') socket.sendall((f'GET {self.path}{gvars.gen_filename()} HTTP/1.1\r\n' f'Host: {self.host}\r\n' f'User-Agent: {ins.random()}\r\n' f'Accept: {gvars.accept}\r\n' f'Cookie: {gvars.token_name}=' f'{utils.make_token(self.password)}\r\n' f'Content-Type: {gvars.content_type}\r\n' 'Transfer-Encoding: chunked\r\n' f'Accept-Language: {gvars.accept_language}\r\n' 'Accept-Encoding: gzip, deflate\r\n' 'DNT: 1\r\n' 'Connection: keep-alive\r\n\r\n').encode()) environ = slowdown.http.new_environ(reader, server_side=False) if '200' != environ['RESPONSE_STATUS']: raise BrokenPipeError(errno.EPIPE, 'Authentication failed') rw = SSRWPair(socket, reader, environ) via_socket = ShadowSocksSocket(self.cipher, rw) via_socket.sendall(utils.pack_addr(target_addr)) return via_socket
def handle_api_request(socket, address): metrics.incr('api_request_cnt') timeout = Timeout.start_new(rpc_timeout) try: req = msgpack_unpack_msg_from_socket(socket) if not req: logger.warning('Couldn\'t get msgpack data from %s', address) socket.close() return access_logger.info('%s %s', address, req['endpoint']) handler = api_request_handlers.get(req['endpoint']) if handler is not None: handler(socket, address, req) else: logger.info('-> %s unknown request', address) socket.sendall(msgpack.packb('UNKNOWN')) except Timeout: metrics.incr('api_request_timeout_cnt') logger.warning('-> %s timeout', address) socket.sendall(msgpack.packb('TIMEOUT')) finally: timeout.cancel() socket.close()
def handler(socket, address): host = peek_http_host(socket) hostname = host.split(':')[0] if not hostname: logging.debug("!no hostname, closing") socket.close() return redirect_url = lookup_txt_attribute(hostname, 'location', '_redirect') if redirect_url: # only append path in request if redirect location # is completely pathless. ex: http://example.com # however, we don't pass query params... if redirect_url.count('/') == 2: req_tip = socket.recv(256) method, path, _ = req_tip.split(' ', 2) redirect_url = '{0}{1}'.format(redirect_url, urlparse.urlparse(path).path) resp = """ HTTP/1.1 301 Moved Permanently\r\nLocation: {0}\r\nConnection: close\r\nContent-Length: 0\r\n\r\n """.format(redirect_url).strip() socket.sendall(resp) socket.close() return proxy_to = lookup_txt_attribute(hostname, 'address', '_proxy') if proxy_to: address = proxy_to.split(':') if len(address) == 1: address = (address[0], 80) try: backend = gevent.socket.create_connection(address) # TODO: insert headers: Via, X-Forwarded-For, Host join_sockets(socket, backend) except IOError: socket.close() return
def handle_slave_send(socket, address, req): message = req['data'] message_id = message.get('message_id', '?') try: runtime = send_funcs['send_message'](message) send_funcs['add_stat'](message['mode'], runtime) if runtime is not None: response = 'OK' logger.info('Message (ID %s) from master %s sent successfully', message_id, address) stats['slave_message_send_success_cnt'] += 1 else: response = 'FAIL' logger.error( 'Got falsy value from send_message for message (ID %s) from master %s: %s', message_id, address, runtime) stats['slave_message_send_fail_cnt'] += 1 except Exception: response = 'FAIL' logger.exception('Sending message (ID %s) from master %s failed.') stats['slave_message_send_fail_cnt'] += 1 socket.sendall(msgpack.packb(response))
def handle(self, socket, address): socket.sendall("hello and goodbye!") socket.shutdown(0)
def command_server(self, socket, address): self.log('New connection from %s:%s' % address) socket.sendall('Welcome to the Democratic Daemon server!\n') help_message = 'Commands are "stop", "list", "json" and "approve"\n' socket.sendall(help_message) fileobj = socket.makefile() while True: try: line = fileobj.readline() if not line: self.log("client disconnected") return command = line.decode().strip().lower() single_cmd = False if command.startswith('$'): command = command[1:] single_cmd = True self.log('Received single command ' + repr(command)) if command == 'stop': self.log("client told server to stop") fileobj.write(('STOPPING SERVER\n').encode()) fileobj.flush() self.quit_event.set() self.server.stop() socket.shutdown(gevent.socket.SHUT_RDWR) return elif command == 'list': empty = True for repo in self.pr_db.repos(): for pr in self.pr_db.pull_requests(repo): fileobj.write(pr.pretty_str().encode()) empty = False if empty: fileobj.write('No pull requests\n'.encode()) elif command == 'json': data = prs_to_json(self.pr_db.pull_requests()).encode() fileobj.write(data) elif command.startswith('approve'): r = re.match('approve\s+(?P<repo>\S+)\s+(?P<issue_id>\d+)\s*$', command) issue_id = None repo = None if r: repo = r.group('repo') try: issue_id = int(r.group('issue_id')) except Exception as e: fileobj.write(('error parsing integer issue number\n' + str(e) + '\n').encode()) else: fileobj.write(('error - usage is "approve [repo] [issue_id]"\n').encode()) found_pr = None if issue_id and repo and repo in self.pr_db.repos(): for pr in self.pr_db.pull_requests(repo): if pr.state == pr.state_idx('COMMENTED'): if pr.issue_id == issue_id: found_pr = pr break if found_pr: fileobj.write(('PULL REQUEST APPROVED\n').encode()) fileobj.write(found_pr.pretty_str().encode()) found_pr.set_state('APPROVED') self.pr_db.write_pull_requests(found_pr.repo) self.build_queue.put((found_pr.repo, found_pr.key())) else: fileobj.write(('No pull request "' + str(repo) + '/issue #' + str(issue_id) + '" ready for merging\n').encode()) else: fileobj.write(('Unknown command "' + command + '"\n').encode()) fileobj.write(help_message.encode()) fileobj.flush() if single_cmd: socket.shutdown(gevent.socket.SHUT_RDWR) return except Exception as e: self.log(e) try: socket.shutdown(gevent.socket.SHUT_RDWR) except Exception as e2: self.log(e2) return
def handle_api_notification_request(socket, address, req): notification = req['data'] if 'application' not in notification: reject_api_request(socket, address, 'INVALID application') logger.warning('Dropping OOB message due to missing application key') return notification['subject'] = '[%s] %s' % (notification['application'], notification.get('subject', '')) target_list = notification.get('target_list') role = notification.get('role') if not role and not target_list: reject_api_request(socket, address, 'INVALID role') logger.warning( 'Dropping OOB message with invalid role "%s" from app %s', role, notification['application']) return target = notification.get('target') if not (target or target_list): reject_api_request(socket, address, 'INVALID target') logger.warning( 'Dropping OOB message with invalid target "%s" from app %s', target, notification['application']) return expanded_targets = None # if role is literal_target skip unrolling if not notification.get('unexpanded'): # For multi-recipient notifications, pre-populate destination with literal targets, # then expand the remaining has_literal_target = False if target_list: expanded_targets = [] notification['destination'] = [] notification['bcc_destination'] = [] for t in target_list: role = t['role'] target = t['target'] bcc = t.get('bcc') try: if role == 'literal_target': if bcc: notification['bcc_destination'].append(target) else: notification['destination'].append(target) has_literal_target = True else: expanded = cache.targets_for_role(role, target) expanded_targets += [{ 'target': e, 'bcc': bcc } for e in expanded] except IrisRoleLookupException: # Maintain best-effort delivery for remaining targets if one fails to resolve continue else: try: expanded_targets = cache.targets_for_role(role, target) except IrisRoleLookupException: expanded_targets = None if not expanded_targets and not has_literal_target: reject_api_request(socket, address, 'INVALID role:target') logger.warning( 'Dropping OOB message with invalid role:target "%s:%s" from app %s', role, target, notification['application']) return sanitize_unicode_dict(notification) # If we're rendering this using templates+context instead of body, fill in the # needed iris key. if 'template' in notification: if 'context' not in notification: logger.warning( 'Dropping OOB message due to missing context from app %s', notification['application']) reject_api_request(socket, address, 'INVALID context') return else: # fill in dummy iris meta data notification['context']['iris'] = {} elif 'email_html' in notification: if not isinstance(notification['email_html'], str): logger.warning( 'Dropping OOB message with invalid email_html from app %s: %s', notification['application'], notification['email_html']) reject_api_request(socket, address, 'INVALID email_html') return elif 'body' not in notification: reject_api_request(socket, address, 'INVALID body') logger.warning('Dropping OOB message with invalid body from app %s', notification['application']) return access_logger.info( '-> %s OK, to %s:%s (%s:%s)', address, role, target, notification['application'], notification.get('priority', notification.get('mode', '?'))) notification_count = 1 if notification.get('unexpanded'): notification['destination'] = notification['target'] send_funcs['message_send_enqueue'](notification) elif notification.get('multi-recipient'): notification['target'] = expanded_targets send_funcs['message_send_enqueue'](notification) notification_count = len(expanded_targets) else: for _target in expanded_targets: temp_notification = notification.copy() temp_notification['target'] = _target send_funcs['message_send_enqueue'](temp_notification) metrics.incr('notification_cnt', inc=notification_count) socket.sendall(msgpack.packb('OK'))
def command_server(self, socket, address): self.log('New connection from %s:%s' % address) socket.sendall('Welcome to the Democratic Daemon server!\n') help_message = 'Commands are "stop", "list", "json" and "approve"\n' socket.sendall(help_message) fileobj = socket.makefile() while True: try: line = fileobj.readline() if not line: self.log("client disconnected") return command = line.decode().strip().lower() single_cmd = False if command.startswith('$'): command = command[1:] single_cmd = True self.log('Received single command ' + repr(command)) if command == 'stop': self.log("client told server to stop") fileobj.write(('STOPPING SERVER\n').encode()) fileobj.flush() self.quit_event.set() self.server.stop() socket.shutdown(gevent.socket.SHUT_RDWR) return elif command == 'list': empty = True for repo in self.pr_db.repos(): for pr in self.pr_db.pull_requests(repo): fileobj.write(pr.pretty_str().encode()) empty = False if empty: fileobj.write('No pull requests\n'.encode()) elif command == 'json': data = prs_to_json(self.pr_db.pull_requests()).encode() fileobj.write(data) elif command.startswith('approve'): r = re.match( 'approve\s+(?P<repo>\S+)\s+(?P<issue_id>\d+)\s*$', command) issue_id = None repo = None if r: repo = r.group('repo') try: issue_id = int(r.group('issue_id')) except Exception as e: fileobj.write( ('error parsing integer issue number\n' + str(e) + '\n').encode()) else: fileobj.write( ('error - usage is "approve [repo] [issue_id]"\n' ).encode()) found_pr = None if issue_id and repo and repo in self.pr_db.repos(): for pr in self.pr_db.pull_requests(repo): if pr.state == pr.state_idx('COMMENTED'): if pr.issue_id == issue_id: found_pr = pr break if found_pr: fileobj.write(('PULL REQUEST APPROVED\n').encode()) fileobj.write(found_pr.pretty_str().encode()) found_pr.set_state('APPROVED') self.pr_db.write_pull_requests(found_pr.repo) self.build_queue.put((found_pr.repo, found_pr.key())) else: fileobj.write( ('No pull request "' + str(repo) + '/issue #' + str(issue_id) + '" ready for merging\n').encode()) else: fileobj.write( ('Unknown command "' + command + '"\n').encode()) fileobj.write(help_message.encode()) fileobj.flush() if single_cmd: socket.shutdown(gevent.socket.SHUT_RDWR) return except Exception as e: self.log(e) try: socket.shutdown(gevent.socket.SHUT_RDWR) except Exception as e2: self.log(e2) return
def handle(self, socket, address): socket.sendall(''.join([one_line for n in xrange(number_lines)])) socket.sendall('\n') socket.shutdown(0)
def handle(self, socket, address): socket.sendall(one_line) socket.shutdown(0)
def handle_api_notification_request(socket, address, req): notification = req['data'] if 'application' not in notification: reject_api_request(socket, address, 'INVALID application') logger.warn('Dropping OOB message due to missing application key') return notification['subject'] = '[%s] %s' % (notification['application'], notification.get('subject', '')) role = notification.get('role') if not role: reject_api_request(socket, address, 'INVALID role') logger.warn('Dropping OOB message with invalid role "%s" from app %s', role, notification['application']) return target = notification.get('target') if not target: reject_api_request(socket, address, 'INVALID target') logger.warn( 'Dropping OOB message with invalid target "%s" from app %s', target, notification['application']) return expanded_targets = None # if role is literal_target skip unrolling if not notification.get('unexpanded'): try: expanded_targets = cache.targets_for_role(role, target) except IrisRoleLookupException: expanded_targets = None if not expanded_targets: reject_api_request(socket, address, 'INVALID role:target') logger.warn( 'Dropping OOB message with invalid role:target "%s:%s" from app %s', role, target, notification['application']) return sanitize_unicode_dict(notification) # If we're rendering this using templates+context instead of body, fill in the # needed iris key. if 'template' in notification: if 'context' not in notification: logger.warn( 'Dropping OOB message due to missing context from app %s', notification['application']) reject_api_request(socket, address, 'INVALID context') return else: # fill in dummy iris meta data notification['context']['iris'] = {} elif 'email_html' in notification: if not isinstance(notification['email_html'], basestring): logger.warn( 'Dropping OOB message with invalid email_html from app %s: %s', notification['application'], notification['email_html']) reject_api_request(socket, address, 'INVALID email_html') return elif 'body' not in notification: reject_api_request(socket, address, 'INVALID body') logger.warn('Dropping OOB message with invalid body from app %s', notification['application']) return access_logger.info( '-> %s OK, to %s:%s (%s:%s)', address, role, target, notification['application'], notification.get('priority', notification.get('mode', '?'))) if notification.get('unexpanded'): notification['destination'] = notification['target'] send_funcs['message_send_enqueue'](notification) else: for _target in expanded_targets: temp_notification = notification.copy() temp_notification['target'] = _target send_funcs['message_send_enqueue'](temp_notification) metrics.incr('notification_cnt') socket.sendall(msgpack.packb('OK'))
def reject_api_request(socket, address, err_msg): logger.info('-> %s %s', address, err_msg) socket.sendall(msgpack.packb(err_msg))
def put(socket, value): """Sends a Python value through the socket.""" data = pickle.dumps(value) socket.sendall(struct.pack(HEADER_SPEC, len(data))) socket.sendall(data)