def process_cascade_request(message): ctr_increment('cascades processed') cascade_requests = RAMCollection('cascade requests') try: if len(cascade_requests.find_objuuids( cscuuid=message['cscuuid'])) == 0: cascade_request = cascade_requests.get_object() cascade_request.object = message cascade_request.set() if len(message['ftags']) > 0: if len(list(set(message['ftags']) & set(get_tag_values()))) > 0: Thread(target=forward_cascade_request, args=(message, )).start() else: Thread(target=forward_cascade_request, args=(message, )).start() if len(message['etags']) > 0: if len(list(set(message['etags']) & set(get_tag_values()))) > 0: Thread(target=execute_cascade_request, args=(message, )).start() else: Thread(target=execute_cascade_request, args=(message, )).start() except: pass
def queue(cronuuid): jobs_lock.acquire() if cronuuid not in jobs: jobs[cronuuid] = {} ctr_increment('threads (cron-{0})'.format(cronuuid)) Thread(target=execute, args=(cronuuid, )).start() jobs_lock.release()
def queue(scruuid): jobs_lock.acquire() if scruuid not in jobs: jobs[scruuid] = {} ctr_increment('threads (script-{0})'.format(scruuid)) Thread(target=execute, args=(scruuid, )).start() jobs_lock.release()
def poll_worker(): register_timer(name='poll_worker', target=poll_worker, timeout=0.5).start() ctr_set_name('uptime', int(time() - START_TIME)) for peer in get_peers(): if ctr_get_name('threads (polling-{0})'.format(peer['agtuuid'])) == 0: ctr_increment('threads (polling-{0})'.format(peer['agtuuid'])) Thread(target=poll, args=(peer, )).start()
def ad_worker(): rt = int(random() * 30.0) register_timer(name='ad_worker', target=ad_worker, timeout=rt).start() age_routes(rt) for peer in get_peers(): if ctr_get_name('threads (advertising-{0})'.format( peer['agtuuid'])) == 0: ctr_increment('threads (advertising-{0})'.format(peer['agtuuid'])) Thread(target=advertise, args=(peer, )).start()
def push_message(message): ctr_increment('messages pushed') ctr_increment('messages queued') message_lock.acquire() if 'timestamp' not in message: message['timestamp'] = time() messages = Collection('messages') new_message = messages.get_object() new_message.object = message new_message.set() message_lock.release()
def pop_messages(**kargs): message_lock.acquire() message_list = [] messages = Collection('messages') for message in messages.find(**kargs): message_list.append(message.object) message.destroy() ctr_increment('messages popped') ctr_decrement('messages queued') message_lock.release() return message_list
def create_anonymous_cascade_request(message): ctr_increment('cascades created') cascade_request = { 'cscuuid': sucky_uuid(), 'request': message['request'], 'timestamp': time(), 'etags': message['etags'], 'ftags': message['ftags'], 'src': kvstore.get(name='agtuuid'), 'dest': None, 'type': 'cascade request', 'anonymous': True } Thread(target=process_cascade_request, args=(cascade_request, )).start()
def forward_cascade_request(message): ctr_increment('cascades forwarded') peers = SQLCollection('peers') for objuuid in peers.list_objuuids(): try: message['dest'] = peers.get_object(objuuid).object['agtuuid'] except: peers.get_object(objuuid).destroy() try: if message['dest'] != kvstore.get(name='agtuuid'): push_message(message) except: pass
def worker(): register_timer(name='message_worker', target=worker, timeout=60).start() message_lock.acquire() messages = Collection('messages') for message in messages.find(): try: if time() - message.object['timestamp'] > MESSAGE_TIMEOUT: message.destroy() ctr_increment('messages expired') ctr_decrement('messages queued') except: message.destroy() message_lock.release()
def default(self): cl = cherrypy.request.headers['Content-Length'] cipher_b64 = cherrypy.request.body.read(int(cl)) ctr_increment('bytes recv (cherrypy)', len(cipher_b64)) cipher_text = b64decode(cipher_b64) nonce = b64decode(cherrypy.request.headers['Nonce'].encode()) tag = b64decode(cherrypy.request.headers['Tag'].encode()) key = b64decode(cherrypy.config.get('server.secret_digest'))[:16] request_cipher = AES.new(key, AES.MODE_EAX, nonce=nonce) raw_message = request_cipher.decrypt(cipher_text) request_cipher.verify(tag) message_in = json.loads(raw_message.decode()) message_in['timestamp'] = time() if 'isrc' in message_in: touch_peer(message_in['isrc']) if 'dest' not in message_in: message_in['dest'] = cherrypy.config.get('agtuuid') elif message_in['dest'] == None: message_in['dest'] = cherrypy.config.get('agtuuid') message_out = process(message_in) raw_message = json.dumps(message_out).encode() response_cipher = AES.new(key, AES.MODE_EAX) cipher_text, tag = response_cipher.encrypt_and_digest(raw_message) cipher_b64 = b64encode(cipher_text) ctr_increment('bytes sent (cherrypy)', len(cipher_b64)) cherrypy.response.headers['Nonce'] = b64encode( response_cipher.nonce).decode() cherrypy.response.headers['Tag'] = b64encode(tag).decode() return cipher_b64
def service_cascade_request(message): ctr_increment('cascades serviced') cascade_response = RAMCollection('cascade responses').get_object() cascade_response.object = message cascade_response.set()
def execute_cascade_request(message): ctr_increment('cascades executed') request = message['request'] response = {} try: if request['type'] == 'process sync': if 'timeout' in request: status, stdout, stderr = process_sync( request['command'], timeout=request['timeout']) else: status, stdout, stderr = process_sync(request['command']) response['type'] = request['type'] response['stdout'] = b64encode(stdout).decode() response['stderr'] = b64encode(stderr).decode() response['status'] = status if message['anonymous'] == False: push_message({ 'type': 'cascade response', 'dest': message['src'], 'cscuuid': message['cscuuid'], 'response': response, 'src': kvstore.get(name='agtuuid') }) elif request['type'] == 'file read': response['type'] = request['type'] response['b64data'] = b64encode(file_read( request['filename'])).decode() push_message({ 'type': 'cascade response', 'dest': message['src'], 'cscuuid': message['cscuuid'], 'response': response, 'src': kvstore.get(name='agtuuid') }) elif request['type'] == 'file write': file_write(request['filename'], b64decode(request['b64data'])) elif request['type'] == 'delete route': for route in RAMCollection('routes').find( agtuuid=request['agtuuid']): route.destroy() for route in RAMCollection('routes').find( gtwuuid=request['agtuuid']): route.destroy() elif request['type'] == 'find collection objects': collection = SQLCollection(request['name']) response = [] for objuuid in collection.find_objuuids(**request['query']): response.append(collection.get_object(objuuid).object) if len(response) > 0: push_message({ 'type': 'cascade response', 'dest': message['src'], 'cscuuid': message['cscuuid'], 'response': response, 'src': kvstore.get(name='agtuuid') }) elif request['type'] == 'execute python': response['status'], response['stdout'], response[ 'stderr'] = interpret(request['body']) if message['anonymous'] == False: push_message({ 'type': 'cascade response', 'dest': message['src'], 'cscuuid': message['cscuuid'], 'response': response, 'src': kvstore.get(name='agtuuid') }) except: pass
def process(message_in): ctr_increment('threads (processing)') message_out = __process(message_in) ctr_decrement('threads (processing)') return message_out
def __forward(message): peers = RAMCollection('peers').find(agtuuid=message['dest']) if message['dest'] == cherrypy.config.get('agtuuid'): process(message) ctr_increment('messages forwarded') elif len(peers) > 0: try: if peers[0].object['url'] != None: MPIClient(peers[0].object['url'], cherrypy.config.get( 'server.secret_digest')).send_json(message) ctr_increment('messages forwarded') else: push_message(message) except: ctr_increment('messages dropped') else: weight = None best_route = None for route in RAMCollection('routes').find(agtuuid=message['dest']): if weight == None or float(route.object['weight']) < float(weight): weight = route.object['weight'] best_route = route if best_route is not None: gtwuuid = best_route.object['gtwuuid'] else: gtwuuid = None peers = RAMCollection('peers').find(agtuuid=gtwuuid) if len(peers) > 0: try: if peers[0].object['url'] != None: MPIClient(peers[0].object['url'], cherrypy.config.get( 'server.secret_digest')).send_json(message) ctr_increment('messages forwarded') else: push_message(message) except: ctr_increment('messages dropped') else: ctr_increment('messages dropped') ctr_decrement('threads (forwarding)')
def forward(message): ctr_increment('threads (forwarding)') Thread(target=__forward, args=(message, )).start()
def __process(message): ctr_increment('messages processed') if message['dest'] == cherrypy.config.get('agtuuid'): if message['type'] == 'create peer': if 'url' in message: url = message['url'] else: url = None if 'ttl' in message: ttl = message['ttl'] else: ttl = None if 'polling' in message: polling = message['polling'] else: polling = False create_peer(message['agtuuid'], url=url, ttl=ttl, polling=polling) return message elif message['type'] == 'delete peers': delete_peers() return message elif message['type'] == 'delete peer': delete_peer(message['agtuuid']) return message elif message['type'] == 'get peers': return get_peers() elif message['type'] == 'get routes': return get_routes() elif message['type'] == 'route advertisement': process_route_advertisement(message) return message elif message['type'] == 'discover peer': if 'ttl' in message: ttl = message['ttl'] else: ttl = None if 'polling' in message: polling = message['polling'] else: polling = False return discover_peer(message['url'], ttl=ttl, polling=polling) elif message['type'] == 'create info event': return message elif message['type'] == 'get counters': return ctr_get_all() elif message['type'] == 'pull messages': st = time() messages = pull_messages(message['isrc']) while len(messages) == 0 and \ time() - st < 5.0: sleep(0.5) messages = pull_messages(message['isrc']) return messages elif message['type'] == 'ticket request': process(process_ticket(message)) return message elif message['type'] == 'ticket response': service_ticket(message) return message elif message['type'] == 'create sync ticket': ticket_message = create_ticket(message['request']) forward(ticket_message) if 'timeout' in message: return wait_on_ticket_response(ticket_message['tckuuid'], message['timeout']) else: return wait_on_ticket_response(ticket_message['tckuuid']) elif message['type'] == 'create async ticket': ticket_message = create_ticket(message['request']) forward(ticket_message) return ticket_message elif message['type'] == 'get ticket response': return get_ticket_response(message['tckuuid']) elif message['type'] == 'delete ticket': delete_ticket(message['tckuuid']) return message elif message['type'] == 'cascade request': process_cascade_request(message) return message elif message['type'] == 'cascade response': service_cascade_request(message) return message elif message['type'] == 'create cascade sync': if 'timeout' in message: return wait_on_cascade_responses( create_cascade_request(message)['cscuuid'], message['timeout']) else: return wait_on_cascade_responses( create_cascade_request(message)['cscuuid']) else: forward(message) return message