def on_wapt_pong(): uuid = None try: uuid = session.get('uuid') if not uuid: logger.critical(u'SocketIO %s connected but no host uuid in session: asking connected host to reconnect' % (request.sid)) emit('wapt_force_reconnect') return False else: logger.debug(u'Socket.IO pong from wapt client sid %s (uuid: %s)' % (request.sid, session.get('uuid',None))) # stores sid in database try: if wapt_db.is_closed(): wapt_db.connect() with wapt_db.atomic() as trans: hostcount = Hosts.update( server_uuid=get_server_uuid(), listening_timestamp=datetime2isodate(), listening_protocol='websockets', listening_address=request.sid, reachable='OK', ).where(Hosts.uuid == uuid).execute() # if not known, reject the connection if hostcount == 0: logger.warning(u'SocketIO sid %s connected but no match in database for uuid %s : asking to reconnect' % (request.sid,uuid)) emit('wapt_force_reconnect') return False finally: if not wapt_db.is_closed(): wapt_db.close() return True except Exception as e: logger.critical(u'SocketIO pong error for uuid %s and sid %s : %s, instance: %s' % (uuid,request.sid,traceback.format_exc(),app.conf.get('application_root'))) return False
def decorated(*args, **kwargs): if wapt_db and wapt_db.is_closed(): logger.info('Connecting to waptdb before request. Count: %s' % len(wapt_db._in_use)) wapt_db.connect() try: r = f(*args,**kwargs); return r; finally: if wapt_db and not wapt_db.is_closed(): wapt_db.close()
def on_waptclient_connect(): try: uuid = request.args.get('uuid', None) if not uuid: raise EWaptForbiddden('Missing source host uuid') allow_unauthenticated_connect = app.conf.get('allow_unauthenticated_connect',False) if not allow_unauthenticated_connect: try: token_gen = get_secured_token_generator() token_data = token_gen.loads(request.args['token']) uuid = token_data.get('uuid', None) if not uuid: raise EWaptAuthenticationFailure('Bad host UUID') if token_data['server_uuid'] != get_server_uuid(): raise EWaptAuthenticationFailure('Bad server UUID') except Exception as e: raise EWaptAuthenticationFailure(u'SocketIO connection not authorized, invalid token: %s' % e) logger.info(u'Socket.IO connection from wapt client sid %s (uuid: %s fqdn:%s)' % (request.sid,uuid,token_data.get('computer_fqdn'))) else: logger.info(u'Unauthenticated Socket.IO connection from wapt client sid %s (uuid: %s)' % (request.sid,uuid)) try: if wapt_db.is_closed(): wapt_db.connect() # update the db with wapt_db.atomic() as trans: # stores sid in database hostcount = Hosts.update( server_uuid=get_server_uuid(), listening_protocol='websockets', listening_address=request.sid, listening_timestamp=datetime2isodate(), last_seen_on=datetime2isodate() ).where(Hosts.uuid == uuid).execute() # if not known, reject the connection if hostcount == 0: raise EWaptForbiddden('Host is not registered') session['uuid'] = uuid finally: if not wapt_db.is_closed(): wapt_db.close() return True except Exception as e: if 'uuid' in session: session.pop('uuid') logger.warning(u'SocketIO connection refused for uuid %s, sid %s: %s, instance %s' % (uuid,request.sid,e,app.conf.get('application_root'))) disconnect() return False
def decorated(*args, **kwargs): if wapt_db and wapt_db.is_closed(): wapt_db.connect() cnx = wapt_db.connection(); b = cnx.readonly; if not b: cnx.set_session( readonly=True ); try: r = f(*args,**kwargs); return r; finally: if not b: cnx.set_session( readonly=False ); if wapt_db and not wapt_db.is_closed(): wapt_db.close()
def on_waptclient_disconnect(): uuid = session.get('uuid', None) logger.info(u'Socket.IO disconnection from wapt client sid %s (uuid: %s)' % (request.sid, uuid)) # clear sid in database try: if wapt_db.is_closed(): wapt_db.connect() with wapt_db.atomic() as trans: Hosts.update( listening_timestamp=datetime2isodate(), listening_protocol=None, listening_address=None, reachable='DISCONNECTED', ).where((Hosts.uuid == uuid) & (Hosts.listening_address == request.sid)).execute() finally: if not wapt_db.is_closed(): wapt_db.close() return True