def erase():
     try:
         FileReader.erase(ConfigFileReader.filename)
     except Exception as e:
         print("Can't changed")
         print(e)
         return False
 def loadall(type_=''):
     '''
     loadall(type) ~type is a list containing admin,doctor,patient,nurse)
     This method return all the data conatined in recordfile under type condition
     '''
     load = {'nurse': [], 'doctor': [], 'patient': []}
     try:
         if (type_ == ''):
             return FileReader.loadall(DataFileReader.filename)
         else:
             recss = []
             recs = FileReader.loadall(DataFileReader.filename)
             try:
                 for rec in recs:
                     if (isinstance(rec, PersonalData)):
                         recss.append(rec)
             except Exception:
                 recss.append([recs])
             for rec in recss:
                 user = DataFileReader.get_user_by_username(rec.username)
                 if (user == False):
                     return False
                 else:
                     if (user.type in type_):
                         load[user.type].append(rec)
             return load
     except Exception as e:
         print("No data to view")
         print(e)
         return False
def main():
    # print(FileReader.read_config())
    # print(Convert.convert())

    # print(FileReader.read(2))
    input_, result, accepted_data = FilterActions.filter()
    FileReader.write_file(accepted_data)
    for i in range(len(result)):
        print("Source PROTOCOL " + input_[i][0] + "\n" + "Source IP " +
              input_[i][1] + "\n" + "Destination IP " + input_[i][2] + "\n" +
              "Destination PORT " + input_[i][3])
        print("RESULT : -" + result[i] + "\n")
    def filter():
        rules = FileReader.read_config()
        data_, data_list = Convert.convert()
        # print(len(data_))
        checked = []
        accepted_data = []
        result = False
        count = 0
        for data in data_:
            for rule in rules[:-1]:
                for element in range(1, len(rule)):
                    if (element == 1 and rule[element] == data[element - 1]):
                        result = True
                    elif (element in [2, 3]):
                        # print(rule[element],data[element-1],FilterActions.checkIP(rule[element],data[element-1]))
                        result = result and FilterActions.checkIP(
                            rule[element], data[element - 1])
                    else:
                        # print(rule[element],data[element-1],FilterActions.checkport(rule[element],data[element-1]))
                        result = result and FilterActions.checkport(
                            rule[element], data[element - 1])
                if (result == True):
                    # print(result,rule[0])
                    checked.append(rule[0])
                    accepted_data.append(data_list[count])

                    break
            else:
                # print(result,rules[-1][0])
                checked.append(rules[-1][0])
            count += 1
        return data_, checked, accepted_data
 def add(obj):
     '''
     add(object)
     This method add new detail to config file
     '''
     try:
         return FileReader.add(DataFileReader.filename, obj)
     except Exception as e:
         print("Error happen in add")
         print(e)
         return False
 def loadall():
     '''
     loadall()
     This method return all the data conatined in configfile
     '''
     try:
         return FileReader.loadall(ConfigFileReader.filename)
     except Exception as e:
         print("Error happen in loadall")
         print(e)
         return False
 def print_rec(records=False):
     '''
     print all given user records
     default print all
     '''
     try:
         if (records == False):
             records = FileReader.loadall(DataFileReader.filename)
         try:
             for record in records:
                 print(record.username)
         except Exception as ee:
             print(records.username)
     except Exception as e:
         print("Error happen in printrec")
         print(e)
         return False
    def find_by_username(username_):
        '''
        find_by_attribute(username)
        This method find any data record contain in file filter by username
        '''
        try:
            recs = []
            records = FileReader.loadall(DataFileReader.filename)
            for record in records:
                if (record.username == username_):
                    recs.append(record)
            return recs

        except Exception as e:
            print("Error happen in find_by_username")
            print(e)
            return False
 def find_by_username_and_password(username_, password__):
     '''
     find_by_attribute(username,password)
     This method find any data record contain in file filter by username amd password
     '''
     try:
         records = FileReader.loadall(ConfigFileReader.filename)
         for record in records:
             if (record.username == username_
                     and record.getPassword() == password__):
                 return record
                 break
         else:
             return False
     except Exception as e:
         print("Error happen in find_by_username_and_password")
         print(e)
         return False
Esempio n. 10
0
 def convert(selection=1):
     results = []
     data = FileReader.read(selection)
     data_list = []
     for i in range(0, len(data), 54):
         full_data = data[:54]
         ip_header = data[i + 14:i + 34]
         ip_hdr = struct.unpack("!BBHHHBBH4s4s", ip_header)
         tcp_header = data[i + 34:i + 54]
         tcp_hdr = struct.unpack("!HH9ss6s", tcp_header)
         sa = socket.inet_ntoa(ip_hdr[8])
         da = socket.inet_ntoa(ip_hdr[9])
         sp = tcp_hdr[0]
         dp = tcp_hdr[1]
         if (ip_hdr[6] == 6):
             protocol = 'TCP'
         else:
             protocol = 'UDP'
         results.append([protocol, str(sa), str(da), str(dp)])
         data_list.append(full_data)
     return results, data_list
 def loadRecods(type_=''):
     '''
     type = labreport,drug_prscription,sickness_detail\n
     Default value for type = '' which gives the all records
     '''
     try:
         recss = FileReader.loadall(DataFileReader.filename)
         recs = []
         if (type_ == ''):
             for rec in recss:
                 if (isinstance(rec, ReportData)):
                     recs.append(rec)
             return recs
         else:
             if (type_ == 'lab record'):
                 for rec in recss:
                     if (isinstance(rec, LabTestPrescription)):
                         recs.append(rec)
                 return recs
             elif (type_ == 'sickness record'):
                 for rec in recss:
                     if (isinstance(rec, SicknessDetails)):
                         recs.append(rec)
                 return recs
             elif (type_ == 'drug record'):
                 for rec in recss:
                     if (isinstance(rec, DrugPrescription)):
                         recs.append(rec)
                 return recs
             else:
                 print("no Report")
                 return False
     except Exception as e:
         print("No record to get")
         print(e)
         return False