Exemple #1
0
class Radius:

    def __init__(self):
        self._fcc = FeedCtrlConnection()
        self._hostID = self._fcc.get_host_master_id()

    def calculate_radius(self):
        radius = self._fcc.get_radius()
        self.__check_trusted(self._hostID, radius, 'MASTER')

    def __check_trusted(self, master_id, radius, prev_app_name, step=1, ):
        if radius < 1 or step > radius:
            return
        trusted = self._fcc.get_trusted(master_id)
        for trusted_id in trusted:
            application_name = self._fcc.get_application_name(trusted_id)
            master = None
            if application_name == 'MASTER':
                master = trusted_id
                self._fcc.set_feed_ids_radius(master, step)
            elif application_name == prev_app_name or prev_app_name == 'MASTER':
                master = self._fcc.get_master_id_from_feed(trusted_id)
                self._fcc.set_feed_ids_radius(master, step)
            else:
                return
            self.__check_trusted(master, step + 1, radius, application_name)
def test_add_event_and_get_host_master_id():
    with session_scope():
        ecf = EventFactory()
        cf = ChatFunction()
        new_event = ecf.next_event('MASTER/MASTER', {})
        fcc = FeedCtrlConnection()
        fcc.add_event(new_event)
        feed = EventFactory()
        new_event = feed.next_event('chat/MASTER',
                                    {'master_feed': ecf.get_feed_id()})
        fcc.add_event(new_event)
        new_event = feed.next_event('chat/whateveraction', {
            'messagekey': 'hallo zusammen',
            'chat_id': '1',
            'timestampkey': 10
        })
        fcc.add_event(new_event)
        new_event = feed.next_event('chat/whateveraction', {
            'messagekey': 'hallo zusammen',
            'chat_id': '1',
            'timestampkey': 10
        })
        fcc.add_event(new_event)
        trust_id5 = generate_random_feed_id()
        new_event = ecf.next_event('MASTER/Trust', {'feed_id': trust_id5})
        fcc.add_event(new_event)
        trust_id5 = generate_random_feed_id()
        new_event = ecf.next_event('MASTER/Name', {'name': trust_id5})
        fcc.add_event(new_event)
        trust_id5 = generate_random_feed_id()
        new_event = ecf.next_event('MASTER/NewFeed', {
            'feed_id': trust_id5,
            'app_name': 'TestApp'
        })
        fcc.add_event(new_event)
        new_event = ecf.next_event('MASTER/Radius', {'radius': 5})
        fcc.add_event(new_event)
        result = cf.get_full_chat('1')
        logger.error(result)
        result = fcc.get_host_master_id()
        assert result == ecf.get_feed_id()
class UiFunctionHandler:

    def __init__(self):
        self._fcc = FeedCtrlConnection()
        # try catch or if None??
        lastEvent = self._fcc.get_my_last_event()
        if lastEvent is not None:
            self._ecf = EventFactory(lastEvent)
            self._eventCreationWrapper = EventCreationWrapper(self._ecf)
        else:
            self._ecf = EventFactory()
            self._eventCreationWrapper = EventCreationWrapper(self._ecf)
            _firstEvent = self._eventCreationWrapper.create_MASTER()
            _secondEvent = self._eventCreationWrapper.create_radius(1)
            _thirdEvent = self._eventCreationWrapper.create_name('Anon')
            self._fcc.add_event(_firstEvent)
            self._fcc.add_event(_secondEvent)
            self._fcc.add_event(_thirdEvent)
        self._masterID = self._fcc.get_host_master_id()

    def get_host_master_id(self):
        # returns the host masterID
        return self._masterID

    def get_master_ids(self):
        # return list of masterIDs from FeedCtrlConnection
        return self._fcc.get_all_master_ids()

    def get_all_master_ids_feed_ids(self, masterID):
        # return a list of feed_ids which belong to the given masterID
        return self._fcc.get_all_master_ids_feed_ids(masterID)

    def get_radius_list(self):
        # return a list of feed_ids which are inside the radius
        return self._fcc.get_feed_ids_in_radius()

    def get_trusted(self):
        # return a list of trusted feed_ids
        return self._fcc.get_trusted(self._masterID)

    def set_trusted(self, feed_id, state):
        # sets a feed to trusted or untrusted (event)
        if state:
            new_event = self._eventCreationWrapper.create_trust(feed_id)
        else:
            new_event = self._eventCreationWrapper.create_block(feed_id)

        self._fcc.add_event(new_event)

    def get_blocked(self):
        # return a list of blocked feed_ids
        return self._fcc.get_blocked(self._masterID)

    def get_radius(self):
        # return the current radius
        return self._fcc.get_radius()

    def set_radius(self, radius):
        # sets the new radius
        # calls calcRadius() to recalculate the new Elements, which are in the radius
        self._fcc.set_feed_ids_radius(self._masterID, radius)

    def get_username(self, masterID):
        # return username from given masterID
        return self._fcc.get_username(masterID)

    def set_username(self, name):

        new_event = self._eventCreationWrapper.create_name(name)
        self._fcc.add_event(new_event)

    def get_application(self, feed_id):
        # return application name from given feed_id
        return self._fcc.get_application_name(feed_id)
Exemple #4
0
class BACCore:
    def __init__(self):
        self.pickle_file_names = ['personList.pkl', 'username.pkl'
                                  ]  # use to reset user or create new one
        self.switch = ["", "", ""]

    # checks if there is already an existing database by checking if there exists a masterfeed ID
    def exists_db(self):
        self._fcc = FeedCtrlConnection()
        master_feed_id = self._fcc.get_host_master_id()
        if master_feed_id is not None:
            return 1
        return 0

    # creates a new database and its first three masterfeed events, if there isn't already one.
    # otherwise it doesn't create a new one
    def setup_db(self, user_name=''):
        mas_id = self._fcc.get_host_master_id()
        if mas_id is not None:
            self.master_feed_id = self._fcc.get_host_master_id()
            self.db_connector = DatabaseConnector()
            self.user_name = self.get_user_name()
        else:
            make_dirs()
            self._ecf = EventFactory(None, DIR_MAIN + '/' + 'Keys', False)
            self._eventCreationWrapper = EventCreationWrapper(self._ecf)
            _firstEvent = self._eventCreationWrapper.create_MASTER()
            _secondEvent = self._eventCreationWrapper.create_radius(1)
            _thirdEvent = self._eventCreationWrapper.create_name(user_name)
            self._fcc.add_event(_firstEvent)
            self._fcc.add_event(_secondEvent)
            self._fcc.add_event(_thirdEvent)
            self.master_feed_id = self._fcc.get_host_master_id()
            self.db_connector = DatabaseConnector()
            self.user_name = user_name

    # creates a new feed (adds 2 events to the master feed and creates the first and second event of the new feed)
    def create_feed(self, article_feed_name):
        fcc = FeedCtrlConnection()
        ect = EventCreationTool()
        ect.set_path_to_keys(DIR_MAIN + '/' + 'Keys', False)

        event = self.db_connector.get_current_event(self.master_feed_id)
        ecf_master = EventFactory(event, DIR_MAIN + '/' + 'Keys', False)
        eventCreationWrapper = EventCreationWrapper(ecf_master)

        public_key = ect.generate_feed()
        new_feed_event = eventCreationWrapper.create_newFeed(
            public_key, 'bac_news')
        trust_feed_event = eventCreationWrapper.create_trust(public_key)
        first_event = ect.create_first_event(
            public_key, 'bac_news/new_article',
            {'master_feed': self.master_feed_id})

        fcc.add_event(new_feed_event)
        fcc.add_event(trust_feed_event)
        fcc.add_event(first_event)

        # creates event containing list name, host name and creation date (second event of the newly created feed)
        ect = EventCreationTool()
        ect.set_path_to_keys(DIR_MAIN + '/' + 'Keys', False)
        dictionary = {
            'host': self.get_event_content(self.master_feed_id, 2)[1]['name'],
            'list_name': article_feed_name,
            'date': datetime.now().isoformat()
        }
        second_event = ect.create_event_from_previous(first_event,
                                                      'bac_news/new_article',
                                                      dictionary)
        fcc.add_event(second_event)

    # creates an event, that is appended at the given feed (given with the feedname) with the content given as json file
    def create_event(self, feed_name, json_file):
        feed_id = self.get_id_from_feed_name(feed_name)
        event = self.db_connector.get_current_event(feed_id)

        ect = EventCreationTool()
        ect.set_path_to_keys(DIR_MAIN + '/' + 'Keys', False)
        new_event = ect.create_event_from_previous(event,
                                                   'bac_news/new_article',
                                                   {'json': json_file})
        fcc = FeedCtrlConnection()
        fcc.add_event(new_event)

    # exports the content of the database to the given path as one or more pcap files
    def export_db_to_pcap(self, path):
        dictionary = {}
        feed_ids = self.get_all_feed_ids()
        for f_id in feed_ids:
            dictionary[f_id] = -1
        lm = LogMerge()
        lm.export_logs(path, dictionary)

    # imports pcap files from the given path to the database
    def import_from_pcap_to_db(self, path):
        lm = LogMerge()
        lm.import_logs(path)

    def get_all_feed_ids(self):
        return self.db_connector.get_all_feed_ids()

    def get_all_feed_name_host_tuples(self):
        feed_names = list()
        feed_ids = self.get_all_feed_ids()
        for feed_id in feed_ids:
            if self.get_event_content(feed_id, 0)[0] == "MASTER/MASTER":
                continue
            host = self.get_host_from_feed(feed_id)
            feed_names.append((self.get_feedname_from_id(feed_id), host))
        return feed_names

    def get_event_content(self, feed_id, seq_no):
        cbor_event = self.db_connector.get_event(feed_id, seq_no)
        event = Event.from_cbor(cbor_event)
        return event.content.content

    def get_feednames_from_host(self):
        feed_names = list()
        feed_ids = self.get_all_feed_ids()
        for feed_id in feed_ids:
            if self.get_event_content(feed_id, 0)[0] == "MASTER/MASTER":
                continue
            host = self.get_host_from_feed(feed_id)
            if host == self.user_name:  #host of this feed is also host of this app
                feed_names.append(self.get_feedname_from_id(feed_id))
        return feed_names

    def get_feedname_from_id(self, feed_id):
        return self.get_event_content(feed_id, 1)[1]["list_name"]

    def get_host_from_feed(self, feed_id):
        return self.get_event_content(feed_id, 1)[1]["host"]

    def get_id_from_feed_name(self, feed_name):  #for own feed_ids
        feed_ids = self.get_all_feed_ids()
        for feed_id in feed_ids:
            if self.get_event_content(
                    feed_id, 0
            )[0] == "MASTER/MASTER":  # still need to do this, because master feed of other user could be in front of new feed in db of own host after import.
                continue
            host = self.get_host_from_feed(feed_id)
            if host == self.user_name:
                if feed_name == self.get_feedname_from_id(feed_id):
                    return feed_id
        return None

    def get_id_from_feed_name_and_host(self, feedname_host):
        feed_name = feedname_host[0]
        host = feedname_host[1]
        feed_ids = self.get_all_feed_ids()
        for feed_id in feed_ids:
            if self.get_event_content(feed_id, 0)[0] == "MASTER/MASTER":
                continue
            feed_host = self.get_host_from_feed(feed_id)
            if host == feed_host:
                if feed_name == self.get_feedname_from_id(feed_id):
                    return feed_id
        return None

    def get_json_files_from_feed(
            self,
            feedname_host):  #feedname_host = tuple with feed_name and its host
        json_files = list()
        feed_name = feedname_host[0]
        host = feedname_host[1]
        feed_id = self.get_id_from_feed_name_and_host((feed_name, host))
        max_seq_no = self.db_connector.get_current_seq_no(feed_id)
        if max_seq_no is None:
            max_seq_no = -1
        for i in range(2, max_seq_no + 1):
            json_files.append(self.get_json_from_event(feed_id, i))
        return json_files

    def get_json_from_event(self, feed_id, seq_no):
        return self.get_event_content(feed_id, seq_no)[1]['json']

    def get_user_name(self):
        return (self.get_event_content(self.master_feed_id, 2)[1]["name"])