def _do_set_contacts_override(self, *args, **kwargs): if _Debug: lg.out( _DebugLevel, 'proxy_router.doSetContactsOverride identity for %s' % args[0].CreatorID) user_id = args[0].CreatorID idsrc = args[0].Payload try: new_ident = identity.identity(xmlsrc=idsrc) except: lg.out(_DebugLevel, 'payload: [%s]' % idsrc) lg.exc() return if not new_ident.isCorrect() or not new_ident.Valid(): lg.warn('incoming identity is not valid') return current_overridden_identity = identitycache.ReadOverriddenIdentityXMLSource( user_id) try: current_contacts = identity.identity( xmlsrc=current_overridden_identity).getContacts() except: current_contacts = [] identitycache.StopOverridingIdentity(user_id) result = identitycache.OverrideIdentity(args[0].CreatorID, idsrc) if _Debug: lg.out( _DebugLevel, ' current overridden contacts is : %s' % current_contacts) lg.out( _DebugLevel, ' new override contacts will be : %s' % new_ident.getContacts()) lg.out(_DebugLevel, ' result=%s' % result)
def doUnregisterAllRouts(self, arg): """ Action method. """ for idurl in self.routes.keys(): identitycache.StopOverridingIdentity(idurl) self.routes.clear() self._clear_routes()
def doUnregisterRoute(self, *args, **kwargs): """ Action method. """ idurl = args[0] identitycache.StopOverridingIdentity(idurl) self.routes.pop(idurl) self._remove_route(idurl)
def doClearContactsOverride(self, *args, **kwargs): """ Action method. """ result = identitycache.StopOverridingIdentity(args[0].CreatorID) if _Debug: lg.out( _DebugLevel, 'proxy_router.doClearContactsOverride identity for %s, result=%s' % ( args[0].CreatorID, result, ))
def doCheckOverride(self, arg): """ Action method. """ if _Debug: lg.out( _DebugLevel, 'proxy_router.doCheckOverride identity for %s' % arg.CreatorID) user_id = arg.CreatorID idsrc = arg.Payload try: new_ident = identity.identity(xmlsrc=idsrc) except: lg.out(_DebugLevel, 'payload: [%s]' % idsrc) lg.exc() return if not new_ident.isCorrect() or not new_ident.Valid(): lg.warn('incoming identity is not valid') return current_overridden_identity = identitycache.ReadOverriddenIdentityXMLSource( user_id) try: current_contacts = identity.identity( xmlsrc=current_overridden_identity).getContacts() except: current_contacts = [] if _Debug: lg.out(_DebugLevel, ' current override contacts is : %s' % current_contacts) identitycache.StopOverridingIdentity(user_id) if not self._is_my_contacts_present_in_identity(new_ident): if _Debug: lg.out(_DebugLevel, ' DO OVERRIDE identity for %s' % arg.CreatorID) lg.out(_DebugLevel, ' new contacts is : %s' % new_ident.getContacts()) identitycache.OverrideIdentity(arg.CreatorID, idsrc) else: if _Debug: lg.out(_DebugLevel, ' SKIP OVERRIDE identity from %s' % arg.CreatorID) lg.out(_DebugLevel, ' new contacts was : %s' % new_ident.getContacts())
def doProcessRequest(self, arg): """ Action method. """ global _MaxRoutesNumber request, _ = arg user_id = request.CreatorID if request.Command == commands.RequestService(): if len(self.routes) >= _MaxRoutesNumber: if _Debug: lg.out( _DebugLevel, 'proxy_server.doProcessRequest RequestService rejected: too many routes' ) lg.out(_DebugLevel, ' %s' % pprint.pformat(self.routes)) p2p_service.SendAck(request, 'rejected', wide=True) else: try: service_info = request.Payload idsrc = service_info.lstrip('service_proxy_server').strip() cached_id = identity.identity(xmlsrc=idsrc) except: lg.out(_DebugLevel, 'payload: [%s]' % request.Payload) lg.exc() return if not cached_id.isCorrect() or not cached_id.Valid(): lg.warn('incoming identity is not valid') return oldnew = '' if user_id not in self.routes.keys(): # accept new route oldnew = 'NEW' self.routes[user_id] = {} else: # accept existing router oldnew = 'OLD' if not self._is_my_contacts_present_in_identity(cached_id): if _Debug: lg.out(_DebugLevel, ' DO OVERRIDE identity for %s' % user_id) identitycache.OverrideIdentity(user_id, idsrc) else: if _Debug: lg.out( _DebugLevel, ' SKIP OVERRIDE identity for %s' % user_id) self.routes[user_id]['time'] = time.time() self.routes[user_id]['identity'] = idsrc self.routes[user_id]['publickey'] = cached_id.publickey self.routes[user_id][ 'contacts'] = cached_id.getContactsAsTuples() self.routes[user_id]['address'] = [] self._write_route(user_id) self.acks.append( p2p_service.SendAck(request, 'accepted', wide=True, packetid=request.PacketID)) if _Debug: lg.out( _DebugLevel, 'proxy_server.doProcessRequest !!!!!!! ACCEPTED %s ROUTE for %s' % (oldnew, user_id)) elif request.Command == commands.CancelService(): if user_id in self.routes: # cancel existing route self._remove_route(user_id) self.routes.pop(user_id) identitycache.StopOverridingIdentity(user_id) p2p_service.SendAck(request, 'accepted', wide=True) if _Debug: lg.out( _DebugLevel, 'proxy_server.doProcessRequest !!!!!!! CANCELLED ROUTE for %s' % user_id) else: p2p_service.SendAck(request, 'rejected', wide=True) if _Debug: lg.out( _DebugLevel, 'proxy_server.doProcessRequest CancelService rejected : %s is not found in routes' % user_id) lg.out(_DebugLevel, ' %s' % pprint.pformat(self.routes)) else: p2p_service.SendFail(request, 'wrong command or payload') # , wide=True)
def _do_process_request(self, *args, **kwargs): global _MaxRoutesNumber json_payload, request, info = args[0] user_id = request.CreatorID #--- commands.RequestService() if request.Command == commands.RequestService(): if len(self.routes) >= _MaxRoutesNumber: if _Debug: lg.out( _DebugLevel, 'proxy_server.doProcessRequest RequestService rejected: too many routes' ) lg.out(_DebugLevel, ' %r' % self.routes) p2p_service.SendAck(request, 'rejected', wide=True) else: try: # idsrc = strng.to_bin(json_payload['identity']) idsrc = json_payload['identity'] cached_id = identity.identity(xmlsrc=idsrc) except: lg.out(_DebugLevel, 'payload: [%s]' % request.Payload) lg.exc() return if not cached_id.Valid(): lg.warn('incoming identity is not valid') return if not cached_id.isCorrect(): lg.warn('incoming identity is not correct') return if user_id != cached_id.getIDURL(): lg.warn( 'incoming identity is not belong to request packet creator' ) return if contactsdb.is_supplier(user_id): if _Debug: lg.out( _DebugLevel, 'proxy_server.doProcessRequest RequestService rejected: this user is my supplier' ) p2p_service.SendAck(request, 'rejected', wide=True) return oldnew = '' if user_id not in list(self.routes.keys()): # accept new route oldnew = 'NEW' self.routes[user_id] = {} else: # accept existing routed user oldnew = 'OLD' if not self._is_my_contacts_present_in_identity(cached_id): if _Debug: lg.out(_DebugLevel, ' DO OVERRIDE identity for %s' % user_id) identitycache.OverrideIdentity(user_id, cached_id.serialize()) else: if _Debug: lg.out( _DebugLevel, ' SKIP OVERRIDE identity for %s' % user_id) self.routes[user_id]['time'] = time.time() self.routes[user_id]['identity'] = cached_id.serialize( as_text=True) self.routes[user_id]['publickey'] = strng.to_text( cached_id.publickey) self.routes[user_id][ 'contacts'] = cached_id.getContactsAsTuples(as_text=True) self.routes[user_id]['address'] = [] self._write_route(user_id) active_user_sessions = gateway.find_active_session( info.proto, info.host) if active_user_sessions: user_connection_info = { 'id': active_user_sessions[0].id, 'index': active_user_sessions[0].index, 'proto': info.proto, 'host': info.host, 'idurl': user_id, } active_user_session_machine = automat.objects().get( user_connection_info['index'], None) if active_user_session_machine: active_user_session_machine.addStateChangedCallback( lambda o, n, e, a: self. _on_user_session_disconnected(user_id, o, n, e, a), oldstate='CONNECTED', ) if _Debug: lg.out( _DebugLevel, 'proxy_server.doProcessRequest connected %s routed user, set active session: %s' % (oldnew.capitalize(), user_connection_info)) else: lg.err('not found session state machine: %s' % user_connection_info['index']) else: if _Debug: lg.out( _DebugLevel, 'proxy_server.doProcessRequest active connection with user %s at %s:%s not yet exist' % ( user_id, info.proto, info.host, )) lg.out( _DebugLevel, ' current active sessions: %d' % len(gateway.list_active_sessions(info.proto))) self.acks.append( p2p_service.SendAck(request, 'accepted', wide=True)) if _Debug: lg.out( _DebugLevel, 'proxy_server.doProcessRequest !!!!!!! ACCEPTED %s ROUTE for %s contacts=%s' % ( oldnew.capitalize(), user_id, self.routes[user_id]['contacts'], )) #--- commands.CancelService() elif request.Command == commands.CancelService(): if user_id in self.routes: # cancel existing route self._remove_route(user_id) self.routes.pop(user_id) identitycache.StopOverridingIdentity(user_id) p2p_service.SendAck(request, 'accepted', wide=True) if _Debug: lg.out( _DebugLevel, 'proxy_server.doProcessRequest !!!!!!! CANCELLED ROUTE for %s' % user_id) else: p2p_service.SendAck(request, 'rejected', wide=True) if _Debug: lg.out( _DebugLevel, 'proxy_server.doProcessRequest CancelService rejected : %s is not found in routes' % user_id) lg.out(_DebugLevel, ' %r' % self.routes) else: p2p_service.SendFail(request, 'rejected', wide=True)