Список выгрузки вынесен в отдельный файл. добвлена возможность обновления одного/несколько списков. Понижен порог проверки выгрузки. Файл списка выгрузки может содержать ссылку на файл, а не json структуру.
This commit is contained in:
87
download.py
87
download.py
@@ -3,49 +3,10 @@
|
|||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import ast
|
||||||
import net_tree
|
import net_tree
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
# массив выгружаемых списков
|
|
||||||
ip_list = {
|
|
||||||
'RU': [
|
|
||||||
# Большая часть RU сегмента
|
|
||||||
{ 'url': ['https://stat.ripe.net/data/country-resource-list/data.json?resource=RU'], 'ipv4': True, 'ipv6': False },
|
|
||||||
{ 'url': ['https://ipv4.fetus.jp/ru.txt'], 'ipv4': True, 'ipv6': False },
|
|
||||||
{ 'url': ['https://github.com/ipverse/rir-ip/blob/master/country/ru/aggregated.json'], 'ipv4': True, 'ipv6': False },
|
|
||||||
# HLL LLC
|
|
||||||
{ 'url': ['https://bgp.he.net/AS51115#_prefixes', 'https://ipinfo.io/widget/demo/AS51115?dataset=asn', 'https://api.hackertarget.com/aslookup/?q=AS51115'], 'ipv4': True, 'ipv6': False },
|
|
||||||
# STATIC
|
|
||||||
{ 'static4': '188.130.255.0/24', 'ipv4': True, 'ipv6': False },
|
|
||||||
],
|
|
||||||
'CHINA': [
|
|
||||||
# Большая часть CH сегмента
|
|
||||||
{ 'url': ['https://stat.ripe.net/data/country-resource-list/data.json?resource=CN'], 'ipv4': True, 'ipv6': False },
|
|
||||||
{ 'url': ['https://ipv4.fetus.jp/cn.txt'], 'ipv4': True, 'ipv6': False },
|
|
||||||
{ 'url': ['https://github.com/ipverse/rir-ip/blob/master/country/cn/aggregated.json'], 'ipv4': True, 'ipv6': False },
|
|
||||||
# Hong Kong
|
|
||||||
{ 'url': ['https://stat.ripe.net/data/country-resource-list/data.json?resource=HK'], 'ipv4': True, 'ipv6': False },
|
|
||||||
{ 'url': ['https://ipv4.fetus.jp/hk.txt'], 'ipv4': True, 'ipv6': False },
|
|
||||||
{ 'url': ['https://github.com/ipverse/rir-ip/blob/master/country/hk/aggregated.json'], 'ipv4': True, 'ipv6': False },
|
|
||||||
# Alibaba (US) Technology Co., Ltd.
|
|
||||||
{ 'url': ['https://bgp.he.net/AS45102#_prefixes', 'https://ipinfo.io/widget/demo/AS45102?dataset=asn', 'https://api.hackertarget.com/aslookup/?q=AS45102'], 'ipv4': True, 'ipv6': False },
|
|
||||||
],
|
|
||||||
'JAPAN': [
|
|
||||||
# Большая часть KR сегмента
|
|
||||||
{ 'url': ['https://stat.ripe.net/data/country-resource-list/data.json?resource=JP'], 'ipv4': True, 'ipv6': False },
|
|
||||||
{ 'url': ['https://ipv4.fetus.jp/jp.txt'], 'ipv4': True, 'ipv6': False },
|
|
||||||
{ 'url': ['https://github.com/ipverse/rir-ip/blob/master/country/jp/aggregated.json'], 'ipv4': True, 'ipv6': False },
|
|
||||||
],
|
|
||||||
'KOREA': [
|
|
||||||
# Большая часть KR сегмента
|
|
||||||
{ 'url': ['https://stat.ripe.net/data/country-resource-list/data.json?resource=KR'], 'ipv4': True, 'ipv6': False },
|
|
||||||
{ 'url': ['https://ipv4.fetus.jp/kr.txt'], 'ipv4': True, 'ipv6': False },
|
|
||||||
{ 'url': ['https://github.com/ipverse/rir-ip/blob/master/country/kr/aggregated.json'], 'ipv4': True, 'ipv6': False },
|
|
||||||
# LG DACOM Corporation
|
|
||||||
{ 'url': ['https://bgp.he.net/AS3786#_prefixes', 'https://ipinfo.io/widget/demo/AS3786?dataset=asn', 'https://api.hackertarget.com/aslookup/?q=AS3786'], 'ipv4': True, 'ipv6': False },
|
|
||||||
],
|
|
||||||
}
|
|
||||||
|
|
||||||
# компилируем регулярку поиска ipv4 адреса
|
# компилируем регулярку поиска ipv4 адреса
|
||||||
ipv4_find_str=re.compile(r"[^0-9.]?(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]|[0-9])(/([0-9]{1}[0-9]*))?[^0-9.]?")
|
ipv4_find_str=re.compile(r"[^0-9.]?(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]|[0-9])(/([0-9]{1}[0-9]*))?[^0-9.]?")
|
||||||
|
|
||||||
@@ -103,6 +64,7 @@ def list_ip(c_dict: dict = []):
|
|||||||
if 'url' in list(c_list):
|
if 'url' in list(c_list):
|
||||||
# бежим весь список ссылок пока не код 200
|
# бежим весь список ссылок пока не код 200
|
||||||
for c_url in c_list['url']:
|
for c_url in c_list['url']:
|
||||||
|
try:
|
||||||
if (result:=requests.get(c_url)) and result.status_code == 200 and result.text:
|
if (result:=requests.get(c_url)) and result.status_code == 200 and result.text:
|
||||||
print(f"URL: {c_url}")
|
print(f"URL: {c_url}")
|
||||||
# пополняем словарь ipv4_list
|
# пополняем словарь ipv4_list
|
||||||
@@ -110,6 +72,7 @@ def list_ip(c_dict: dict = []):
|
|||||||
# пополняем словарь ipv6_list
|
# пополняем словарь ipv6_list
|
||||||
if ipv6: ipv6_list.update(ipv6_find(result.text,ipv6))
|
if ipv6: ipv6_list.update(ipv6_find(result.text,ipv6))
|
||||||
break
|
break
|
||||||
|
except requests.exceptions.MissingSchema: pass
|
||||||
print("Ошибка соединения")
|
print("Ошибка соединения")
|
||||||
# если есть статичные записи ipv4
|
# если есть статичные записи ipv4
|
||||||
if ipv4 and 'static4' in list(c_list):
|
if ipv4 and 'static4' in list(c_list):
|
||||||
@@ -155,6 +118,46 @@ def list_ip(c_dict: dict = []):
|
|||||||
|
|
||||||
# главная фукция
|
# главная фукция
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
# словарь выгружаемых списков
|
||||||
|
ip_list = dict()
|
||||||
|
try:
|
||||||
|
# если файл list содержет json структуру, парсим его
|
||||||
|
with open(list_file:=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'list'), "r") as file:
|
||||||
|
ip_list = ast.literal_eval(file.read())
|
||||||
|
print(f"Список выгрузки из файла: {list_file}")
|
||||||
|
except (ValueError, SyntaxError):
|
||||||
|
try:
|
||||||
|
# если файл list ссылка, загружаем и парсим его
|
||||||
|
with open(list_file, "r") as file:
|
||||||
|
if (result:=requests.get(url_list_file:=file.readline().strip())) and result.status_code == 200 and result.text:
|
||||||
|
ip_list = ast.literal_eval(result.text)
|
||||||
|
print(f"Список выгрузки по url: {url_list_file}")
|
||||||
|
except requests.exceptions.MissingSchema:
|
||||||
|
print(f"Невалидный URL на список выгрузки", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
except (ValueError, SyntaxError):
|
||||||
|
print(f"Ошибочная структура json", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Ошибка: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
except FileNotFoundError:
|
||||||
|
print(f"Файл со списками не найден", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Ошибка: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# если передан аргумент(ы) запуска,
|
||||||
|
# значит пытаемся обновить только указанные списки
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
ip_list = {k: v for k, v in ip_list.items() if k in sys.argv[1:]}
|
||||||
|
|
||||||
|
# проверяем на пустой список выгрузки
|
||||||
|
if (not ip_list):
|
||||||
|
print(f"Список выгрузки пустой", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
# создаем дерриктори. для сохранения
|
# создаем дерриктори. для сохранения
|
||||||
outdir=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'unloading')
|
outdir=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'unloading')
|
||||||
if not os.path.exists(outdir):
|
if not os.path.exists(outdir):
|
||||||
@@ -171,13 +174,13 @@ if __name__ == "__main__":
|
|||||||
print(f"Выгружаю список IP: {clist}")
|
print(f"Выгружаю список IP: {clist}")
|
||||||
ipv4_list, ipv6_list=list_ip(ip_list[clist])
|
ipv4_list, ipv6_list=list_ip(ip_list[clist])
|
||||||
# сохраняем ipv4
|
# сохраняем ipv4
|
||||||
if ipv4_list and len(ipv4_list.splitlines()) >= ipv4_count_old * 0.7:
|
if ipv4_list and len(ipv4_list.splitlines()) >= ipv4_count_old * 0.5:
|
||||||
# сохраняем в файл
|
# сохраняем в файл
|
||||||
with open(ipv4_out_file, "w") as file:
|
with open(ipv4_out_file, "w") as file:
|
||||||
file.write(ipv4_list)
|
file.write(ipv4_list)
|
||||||
print(f"Файл {ipv4_out_file} сохранён")
|
print(f"Файл {ipv4_out_file} сохранён")
|
||||||
# сохраняем ipv6
|
# сохраняем ipv6
|
||||||
if ipv6_list and len(ipv6_list.splitlines()) >= ipv6_count_old * 0.7:
|
if ipv6_list and len(ipv6_list.splitlines()) >= ipv6_count_old * 0.5:
|
||||||
# сохраняем в файл
|
# сохраняем в файл
|
||||||
with open(ipv6_out_file, "w") as file:
|
with open(ipv6_out_file, "w") as file:
|
||||||
file.write(ipv6_list)
|
file.write(ipv6_list)
|
||||||
|
|||||||
32
list
Normal file
32
list
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
{
|
||||||
|
'RU': [
|
||||||
|
# Большая часть RU сегмента
|
||||||
|
{ 'url': ['https://stat.ripe.net/data/country-resource-list/data.json?resource=RU'], 'ipv4': True, 'ipv6': False },
|
||||||
|
{ 'url': ['https://ipv4.fetus.jp/ru.txt'], 'ipv4': True, 'ipv6': False },
|
||||||
|
{ 'url': ['https://github.com/ipverse/rir-ip/blob/master/country/ru/aggregated.json'], 'ipv4': True, 'ipv6': False },
|
||||||
|
# HLL LLC
|
||||||
|
{ 'url': ['https://bgp.he.net/AS51115#_prefixes', 'https://ipinfo.io/widget/demo/AS51115?dataset=asn', 'https://api.hackertarget.com/aslookup/?q=AS51115'], 'ipv4': True, 'ipv6': False },
|
||||||
|
# STATIC
|
||||||
|
{ 'static4': '188.130.255.0/24', 'ipv4': True, 'ipv6': False },
|
||||||
|
],
|
||||||
|
'CHINA': [
|
||||||
|
# Большая часть CH сегмента
|
||||||
|
{ 'url': ['https://stat.ripe.net/data/country-resource-list/data.json?resource=CN'], 'ipv4': True, 'ipv6': False },
|
||||||
|
{ 'url': ['https://ipv4.fetus.jp/cn.txt'], 'ipv4': True, 'ipv6': False },
|
||||||
|
{ 'url': ['https://github.com/ipverse/rir-ip/blob/master/country/cn/aggregated.json'], 'ipv4': True, 'ipv6': False },
|
||||||
|
],
|
||||||
|
'JAPAN': [
|
||||||
|
# Большая часть KR сегмента
|
||||||
|
{ 'url': ['https://stat.ripe.net/data/country-resource-list/data.json?resource=JP'], 'ipv4': True, 'ipv6': False },
|
||||||
|
{ 'url': ['https://ipv4.fetus.jp/jp.txt'], 'ipv4': True, 'ipv6': False },
|
||||||
|
{ 'url': ['https://github.com/ipverse/rir-ip/blob/master/country/jp/aggregated.json'], 'ipv4': True, 'ipv6': False },
|
||||||
|
],
|
||||||
|
'KOREA': [
|
||||||
|
# Большая часть KR сегмента
|
||||||
|
{ 'url': ['https://stat.ripe.net/data/country-resource-list/data.json?resource=KR'], 'ipv4': True, 'ipv6': False },
|
||||||
|
{ 'url': ['https://ipv4.fetus.jp/kr.txt'], 'ipv4': True, 'ipv6': False },
|
||||||
|
{ 'url': ['https://github.com/ipverse/rir-ip/blob/master/country/kr/aggregated.json'], 'ipv4': True, 'ipv6': False },
|
||||||
|
# LG DACOM Corporation
|
||||||
|
{ 'url': ['https://bgp.he.net/AS3786#_prefixes', 'https://ipinfo.io/widget/demo/AS3786?dataset=asn', 'https://api.hackertarget.com/aslookup/?q=AS3786'], 'ipv4': True, 'ipv6': False },
|
||||||
|
],
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user