def __init__(self, *args, **argkw): super(ISPSmartDomain, self).__init__(*args, **argkw) self.isp_a_record = defaultdict(list) self.default_isp = '' self.isp_keys = None from pyip import IPInfo from os.path import realpath,dirname,join,exists QQWry_DB = join(dirname(realpath(__file__)), 'qqwry.dat') self.QQWry = IPInfo(QQWry_DB)
class ISPSmartDomain(StaticDomain): def __init__(self, *args, **argkw): super(ISPSmartDomain, self).__init__(*args, **argkw) self.isp_a_record = defaultdict(list) self.default_isp = '' self.isp_keys = None from pyip import IPInfo from os.path import realpath,dirname,join,exists QQWry_DB = join(dirname(realpath(__file__)), 'qqwry.dat') self.QQWry = IPInfo(QQWry_DB) def set_default_isp(self, default_isp): self.default_isp = default_isp def add_isp_a_record(self, isp, ip): if isinstance(ip, list) or isinstance(ip, tuple): self.isp_a_record[isp].extend([ \ {'qtype': 'A', 'qname': self.qname, 'content': cnt, 'ttl': 0, 'auth': 1} for cnt in ip ]) else: self.isp_a_record[isp].append({'qtype': 'A', 'qname': self.qname, 'content': ip, 'ttl': 0, 'auth': 1}) def query(self, query_args): qtype = query_args['qtype'] if qtype in ('ANY', 'A'): remote = query_args['remote'] if self.isp_keys is None: self.isp_keys = list(self.isp_a_record.keys()) location, isp = self.QQWry.getIPAddr(remote) for key in self.isp_keys: if isp.find(key) != -1: break else: if self.default_isp == '': key = self.isp_keys[0] else: key = self.default_isp syslog.syslog("IP {0} QQWry: {1}-{2}, select {3}".format(remote, location, isp, key)) self.records['A'] = self.isp_a_record.get(key, []) return super(ISPSmartDomain, self).query(query_args)