Compare commits

...

3 Commits

2 changed files with 99 additions and 18 deletions

View File

@ -34,6 +34,12 @@ amazon = true
# - contain only atlassian ip ranges # - contain only atlassian ip ranges
atlassian = true atlassian = true
# #
# antifilter - it's a parsed RKN block list (https://antifilter.network/download/ipsmart.lst)
# + absoutely free
# + simple parsing
# - it's necessary only in Russia
antifilter = true
#
# herrbischoff - it's a GitHub repository (https://github.com/herrbischoff/country-ip-blocks) # herrbischoff - it's a GitHub repository (https://github.com/herrbischoff/country-ip-blocks)
# + absoutely free # + absoutely free
# + updated hourly # + updated hourly
@ -59,13 +65,13 @@ ip2l_download_token = ip2location_TOKEN
# - access to updates only by paid subscription # - access to updates only by paid subscription
ip2l_database_code = DB1CIDR ip2l_database_code = DB1CIDR
# #
# wwwhmptoday - it's compilation from all other sources (https://git.hmp.today/pavel.muhortov/my_route.db) # githmptoday - it's compilation from all other sources (https://git.hmp.today/pavel.muhortov/my_route.db)
# + all the benefits of other sources # + all the benefits of other sources
# + all lists ready to use # + all lists ready to use
# - you need account # - you need account
wwwhmptoday = true githmptoday = true
wwwhmptoday_user = username githmptoday_user = username
wwwhmptoday_pass = password githmptoday_pass = password
[enable-gateway] [enable-gateway]
@ -77,6 +83,9 @@ dev-wg1 = true
[via-192.168.0.1] [via-192.168.0.1]
# List of CIDR. Only CIDR with the TRUE value will be used. # List of CIDR. Only CIDR with the TRUE value will be used.
# The order of the entries is important:
# - the "add" action applies entries from top to bottom,
# - the "del" action applies entries from bottom to top.
# If different sources contain the same cidr, use the path part to define your preference. # If different sources contain the same cidr, use the path part to define your preference.
ipv4/via-192.168.0.1.cidr = true ipv4/via-192.168.0.1.cidr = true
ipv4/atlassian.cidr = true ipv4/atlassian.cidr = true
@ -87,6 +96,9 @@ ipv4/private.cidr
[dev-wg1] [dev-wg1]
# List of CIDR. Only CIDR with the TRUE value will be used. # List of CIDR. Only CIDR with the TRUE value will be used.
# The order of the entries is important:
# - the "add" action applies entries from top to bottom,
# - the "del" action applies entries from bottom to top.
# If different sources contain the same cidr, use the path part to define your preference. # If different sources contain the same cidr, use the path part to define your preference.
ipv4/dev-wg1.cidr = true ipv4/dev-wg1.cidr = true
ipv4/public.cidr = true ipv4/public.cidr = true

View File

@ -300,10 +300,10 @@ class Route(Connect):
apply_counter = 0 apply_counter = 0
gways_counter = 0 gways_counter = 0
files_counter = 0 files_counter = 0
commands_list = []
for gw, cidr_apply in self._gw.items(): for gw, cidr_apply in self._gw.items():
gways_counter += 1 gways_counter += 1
for cidr in cidr_apply: for cidr in cidr_apply:
local_logger = logging.getLogger(cidr)
for cidr_file in cidr_current: for cidr_file in cidr_current:
if cidr in cidr_file: if cidr in cidr_file:
with open(cidr_file, mode='r', encoding='utf-8') as file: with open(cidr_file, mode='r', encoding='utf-8') as file:
@ -317,10 +317,16 @@ class Route(Connect):
pass pass
else: else:
command = ['ip', 'ro', action, route, gw_type, gw_name] command = ['ip', 'ro', action, route, gw_type, gw_name]
local_logger.info(msg=' '.join(command)) commands_list.append({'cidr': cidr, 'command': command})
if not imitate:
if self.__cmd(command=command) == 0: if action == 'delete':
apply_counter += 1 commands_list = reversed(commands_list)
for command in commands_list:
local_logger = logging.getLogger(command['cidr'])
local_logger.info(msg=' '.join(command['command']))
if not imitate:
if self.__cmd(command=command['command']) == 0:
apply_counter += 1
local_logger = logging.getLogger(logger_alias) local_logger = logging.getLogger(logger_alias)
local_logger.info(msg="" local_logger.info(msg=""
@ -426,6 +432,12 @@ class Route(Connect):
force_download=force_download, force_download=force_download,
logger_alias=logger_alias logger_alias=logger_alias
) )
elif name == 'antifilter':
return self.__update_source_antifilter(
db_root_path=db_root_path,
force_download=force_download,
logger_alias=logger_alias
)
elif name == 'atlassian': elif name == 'atlassian':
return self.__update_source_atlassian( return self.__update_source_atlassian(
db_root_path=db_root_path, db_root_path=db_root_path,
@ -446,8 +458,8 @@ class Route(Connect):
force_download=force_download, force_download=force_download,
logger_alias=logger_alias logger_alias=logger_alias
) )
elif name == 'wwwhmptoday': elif name == 'githmptoday':
return self.__update_source_wwwhmptoday( return self.__update_source_githmptoday(
db_root_path=db_root_path, db_root_path=db_root_path,
download_user=download_user, download_user=download_user,
download_pass=download_pass, download_pass=download_pass,
@ -658,6 +670,63 @@ class Route(Connect):
return True return True
return False return False
def __update_source_antifilter(
self,
db_root_path: str,
force_download: bool = False,
logger_alias: str = inspect.stack()[0].function
) -> bool:
local_logger = logging.getLogger(logger_alias)
if Do.args_valid(locals(), self.__update_source_antifilter.__annotations__):
db_source_url = "https://antifilter.network/download/ipsmart.lst"
db_source_name = "antifilter"
db_source_root = db_root_path + sep + "sources" + sep + db_source_name
db_source_file = db_source_root + sep + "ipsmart.lst"
db_source_cidr_root = db_source_root + sep + "cidr"
if not path.exists(db_source_file):
force_download = True
if force_download:
if not self.__download_db(
url=db_source_url,
dst=db_source_file,
logger_alias=logger_alias
):
return False
with open(db_source_file, mode='r', encoding='utf-8') as db_source_raw:
db_source_data = db_source_raw.read().splitlines()
db_parsed_data_ipv4 = []
db_parsed_data_ipv6 = []
for item in db_source_data:
if not ":" in item:
db_parsed_data_ipv4.append(item)
else:
db_parsed_data_ipv6.append(item)
makedirs(db_source_cidr_root + sep + "ipv4", exist_ok=True)
db_source_cidr_ipv4_file = (""
+ db_source_cidr_root + sep + "ipv4"
+ sep + db_source_name + ".cidr"
)
with open(db_source_cidr_ipv4_file, mode='w+', encoding='utf-8') as cidr_dump:
cidr_dump.write('\n'.join(db_parsed_data_ipv4))
local_logger.info(msg=db_source_cidr_ipv4_file + ' saved')
makedirs(db_source_cidr_root + sep + "ipv6", exist_ok=True)
db_source_cidr_ipv6_file = (""
+ db_source_cidr_root + sep + "ipv6"
+ sep + db_source_name + ".cidr"
)
with open(db_source_cidr_ipv6_file, mode='w+', encoding='utf-8') as cidr_dump:
cidr_dump.write('\n'.join(db_parsed_data_ipv6))
local_logger.info(msg=db_source_cidr_ipv6_file + ' saved')
return True
return False
def __update_source_herrbischoff( def __update_source_herrbischoff(
self, self,
db_root_path: str, db_root_path: str,
@ -846,7 +915,7 @@ class Route(Connect):
remove(db_source_file) remove(db_source_file)
return False return False
def __update_source_wwwhmptoday( def __update_source_githmptoday(
self, self,
db_root_path: str, db_root_path: str,
download_user: str, download_user: str,
@ -855,7 +924,7 @@ class Route(Connect):
logger_alias: str = inspect.stack()[0].function logger_alias: str = inspect.stack()[0].function
) -> bool: ) -> bool:
local_logger = logging.getLogger(logger_alias) local_logger = logging.getLogger(logger_alias)
if Do.args_valid(locals(), self.__update_source_wwwhmptoday.__annotations__): if Do.args_valid(locals(), self.__update_source_githmptoday.__annotations__):
if not path.exists(db_root_path + sep + 'sources'): if not path.exists(db_root_path + sep + 'sources'):
force_download = True force_download = True
if force_download: if force_download:
@ -1024,12 +1093,12 @@ if __name__ == "__main__":
enable_sources['ip2location']['db_source_code'] = ( enable_sources['ip2location']['db_source_code'] = (
conf_sources.data['ip2l_database_code'] conf_sources.data['ip2l_database_code']
) )
if 'wwwhmptoday' in enable_sources: if 'githmptoday' in enable_sources:
enable_sources['wwwhmptoday']['download_user'] = ( enable_sources['githmptoday']['download_user'] = (
conf_sources.data['wwwhmptoday_user'] conf_sources.data['githmptoday_user']
) )
enable_sources['wwwhmptoday']['download_pass'] = ( enable_sources['githmptoday']['download_pass'] = (
conf_sources.data['wwwhmptoday_pass'] conf_sources.data['githmptoday_pass']
) )
logging.basicConfig( logging.basicConfig(