Example #1
0
    def ignore(self, addr):
        '''Read self.IGNORE_FILE to check if addr (an IP) is in it.

        The file should consist of blank-separated addresses or address ranges
        in CIDR format to ignore.'''
        bits = inet_pton(valid_addr(addr), addr)
        try:
            with open(self.IGNORE_FILE) as ignore:
                for line in ignore:
                    # comments
                    if line.lstrip().startswith('#'):
                        continue
                    for word in line.split():
                        try:
                            # is this CIDR?
                            iaddr, mask = word.split('/', 1)
                            mask = int(mask)
                        except ValueError:
                            try:
                                # exact match only
                                if bits == inet_pton(valid_addr(word), word):
                                    return True
                            except EnvironmentError as err:
                                # inet_pton failed
                                self.log(LOG_PRINT, 'ignore.txt token', word,
                                         'could not be parsed:', err)
                            continue
                        try:
                            ibits = inet_pton(valid_addr(iaddr), iaddr)
                        except EnvironmentError:
                            continue
                        for byte, ibyte in zip(bits, ibits):
                            if not mask:
                                return True
                            elif mask >= 8:
                                if byte != ibyte:
                                    return False
                                mask -= 8
                            else:
                                b, i = ord(byte), ord(ibyte)
                                m = 0xff00 >> mask
                                return b & m == i & m

        except IOError as err:
            if err.errno != ENOENT:
                raise
    def ignore(self, addr):
        '''Read self.IGNORE_FILE to check if addr (an IP) is in it.

        The file should consist of blank-separated addresses or address ranges
        in CIDR format to ignore.'''
        bits = inet_pton(valid_addr(addr), addr)
        try:
            with open(self.IGNORE_FILE) as ignore:
                for line in ignore:
                    # comments
                    if line.lstrip().startswith('#'):
                        continue
                    for word in line.split():
                        try:
                            # is this CIDR?
                            iaddr, mask = word.split('/', 1)
                            mask = int(mask)
                        except ValueError:
                            try:
                                # exact match only
                                if bits == inet_pton(valid_addr(word), word):
                                    return True
                            except EnvironmentError as err:
                                # inet_pton failed
                                self.log(LOG_PRINT, 'ignore.txt token', word,
                                         'could not be parsed:', err)
                            continue
                        try:
                            ibits = inet_pton(valid_addr(iaddr), iaddr)
                        except EnvironmentError:
                            continue
                        for byte, ibyte in zip(bits, ibits):
                            if not mask:
                                return True
                            elif mask >= 8:
                                if byte != ibyte:
                                    return False
                                mask -= 8
                            else:
                                b, i = ord(byte), ord(ibyte)
                                m = 0xff00 >> mask
                                return b & m == i & m

        except IOError as err:
            if err.errno != ENOENT:
                raise
Example #3
0
 def __init__(self, *args):
     '''Adds the host, port and family attributes to the addr tuple.
     If a single parameter is given, tries to parse it as an address string
     '''
     try:
         addr, family = args
         self.host, self.port = self[:2]
         self.family = family
     except ValueError:
         self.host, self.port = self[:2]
         self.family = valid_addr(self.host)
Example #4
0
 def __init__(self, *args):
     '''Adds the host, port and family attributes to the addr tuple.
     If a single parameter is given, tries to parse it as an address string
     '''
     try:
         addr, family = args
         self.host, self.port = self[:2]
         self.family = family
     except ValueError:
         self.host, self.port = self[:2]
         self.family = valid_addr(self.host)
Example #5
0
 def __init__(self, hostname, hostport, username, passcode, sender_mail, receiver_mails):
     if hostname == "" or (0 < hostport <= 65535) is False:
         log("Email hostname and host port is invalid. Check config file.")
         raise EmailConfigException
     if username == "" or passcode == "":
         log("Username and passcode is invalid. Check config file.")
         raise EmailConfigException
     if valid_addr(sender_mail) is False:
         log("Invalid sender email. Check config file.")
         raise EmailConfigException
     for receiver in receiver_mails:
         if valid_addr(receiver) is False:
             log("Invalid receiver email. Check config file.")
             raise EmailConfigException
     self.hostname = hostname
     self.hostport = hostport
     self.username = username
     self.passcode = passcode
     self.sender_mail = sender_mail
     self.receiver_mails = receiver_mails
Example #6
0
    def validate_proxyconfig(self, mod: WAModule, config: dict) -> dict:
        cleaned_config = {'proxy': [], 'host': None, 'port': None}
        if config.get('proxy', False):
            cleaned_config['proxy'] = mod.make_proxy_config(config['proxy'])

        if config.get('host', False):
            if not valid_addr(config['host']):
                raise ValueError(
                    f"configuration error: invalid host address `{config['addr']}"
                )
            cleaned_config['host'] = config['host']
        if config.get('port', False):
            port = config['port']
            if not isinstance(port, int) or port <= 0:
                raise ValueError(
                    f'configuration error: configured an incorrect port number for proxy requests'
                )
            cleaned_config['port'] = port

        return cleaned_config