Repository navigation
Expand file tree
/
Copy pathipasn.py
More file actions
54 lines (46 loc) · 1.64 KB
/
Copy pathipasn.py
File metadata and controls
54 lines (46 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import sys
import queue
import zipfile
import sqlite3
from ipaddress import IPv4Address
from lib.config import define
class IPAsnInfo:
def __init__(self):
self.path = self.get_db_path()
def get_db_path(self):
zip_path = define.data_storage_dir.joinpath('ip2location.zip')
db_path = define.data_storage_dir.joinpath('ip2location.db')
if db_path.exists():
return db_path
zf = zipfile.ZipFile(str(zip_path))
zf.extract('ip2location.db', define.data_storage_dir)
return db_path
def ip_to_int(self, ip):
if isinstance(ip, int):
return ip
try:
ipv4 = IPv4Address(ip)
except Exception as e:
logger.log('ERROR', e.args)
return 0
return int(ipv4)
def run(self, q_targets, q_targets_ex):
conn = sqlite3.connect(self.path)
c = conn.cursor()
while True:
try:
target = q_targets.get(timeout=0.2)
ip = self.ip_to_int(target['ip'])
cursor = c.execute(f"SELECT * FROM asn WHERE ip_from <= {ip} AND ip_to >= {ip} LIMIT 1;")
for s in cursor:
ipc = target['ip'].split('.')
target['cidr'] = ipc[0] + '.' + ipc[1] + '.' + ipc[2] + '.0/24'
target['asn'] = s[3]
target['org'] = s[4]
q_targets_ex.put(target)
except queue.Empty:
break
conn.close()
if __name__ == "__main__":
asn_info = IPAsnInfo()
print(asn_info.find("188.81.94.77"))