| 12 | |
| 13 | |
| 14 | class IPFactory: |
| 15 | def __init__(self): |
| 16 | cdnFile = os.path.join(path.library, 'cdn_ip_cidr.json') |
| 17 | dbFile = os.path.join(path.library, "data", "ip2region.db") |
| 18 | self.searcher = Ip2Region(dbFile) |
| 19 | with open(cdnFile, 'r', encoding='utf-8') as file: |
| 20 | self.cdns = json.load(file) |
| 21 | |
| 22 | def parse_host(self,url): |
| 23 | host = urlsplit(url).netloc |
| 24 | if ':' in host: |
| 25 | host = re.sub(r':\d+', '', host) |
| 26 | return host |
| 27 | |
| 28 | def factory(self, url): |
| 29 | try: |
| 30 | ip_list = [] |
| 31 | host = self.parse_host(url) |
| 32 | items = socket.getaddrinfo(host, None) |
| 33 | for ip in items: |
| 34 | if ip[4][0] not in ip_list: |
| 35 | ip_list.append(ip[4][0]) |
| 36 | if len(ip_list) > 1: |
| 37 | return 1, ip_list |
| 38 | else: |
| 39 | for cdn in self.cdns: |
| 40 | if ipaddress.ip_address(ip_list[0]) in ipaddress.ip_network(cdn): |
| 41 | return 1, ip_list |
| 42 | return 0, ip_list |
| 43 | except Exception as e: |
| 44 | return 0, ip_list |
| 45 | |