generated from pavel.muhortov/template-python
	Compare commits
	
		
			3 Commits
		
	
	
		
			75b65f85c0
			...
			8b2b1d3de0
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 8b2b1d3de0 | |||
| 7375df5a2c | |||
| f9d50ef65d | 
|  | @ -34,6 +34,12 @@ amazon = true | |||
| # - contain only atlassian ip ranges | ||||
| 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) | ||||
| # + absoutely free | ||||
| # + updated hourly | ||||
|  | @ -59,13 +65,13 @@ ip2l_download_token = ip2location_TOKEN | |||
| # - access to updates only by paid subscription | ||||
| 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 lists ready to use | ||||
| # - you need account | ||||
| wwwhmptoday = true | ||||
| wwwhmptoday_user = username | ||||
| wwwhmptoday_pass = password | ||||
| githmptoday = true | ||||
| githmptoday_user = username | ||||
| githmptoday_pass = password | ||||
| 
 | ||||
| 
 | ||||
| [enable-gateway] | ||||
|  | @ -77,6 +83,9 @@ dev-wg1 = true | |||
| 
 | ||||
| [via-192.168.0.1] | ||||
| # 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. | ||||
| ipv4/via-192.168.0.1.cidr = true | ||||
| ipv4/atlassian.cidr = true | ||||
|  | @ -87,6 +96,9 @@ ipv4/private.cidr | |||
| 
 | ||||
| [dev-wg1] | ||||
| # 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. | ||||
| ipv4/dev-wg1.cidr = true | ||||
| ipv4/public.cidr = true | ||||
|  |  | |||
							
								
								
									
										97
									
								
								my_route.py
									
									
									
									
									
								
							
							
						
						
									
										97
									
								
								my_route.py
									
									
									
									
									
								
							|  | @ -300,10 +300,10 @@ class Route(Connect): | |||
|             apply_counter = 0 | ||||
|             gways_counter = 0 | ||||
|             files_counter = 0 | ||||
|             commands_list = [] | ||||
|             for gw, cidr_apply in self._gw.items(): | ||||
|                 gways_counter += 1 | ||||
|                 for cidr in cidr_apply: | ||||
|                     local_logger = logging.getLogger(cidr) | ||||
|                     for cidr_file in cidr_current: | ||||
|                         if cidr in cidr_file: | ||||
|                             with open(cidr_file, mode='r', encoding='utf-8') as file: | ||||
|  | @ -317,10 +317,16 @@ class Route(Connect): | |||
|                                         pass | ||||
|                                     else: | ||||
|                                         command = ['ip', 'ro', action, route, gw_type, gw_name] | ||||
|                                     local_logger.info(msg=' '.join(command)) | ||||
|                                     if not imitate: | ||||
|                                         if self.__cmd(command=command) == 0: | ||||
|                                             apply_counter += 1 | ||||
|                                         commands_list.append({'cidr': cidr, 'command': command}) | ||||
| 
 | ||||
|             if action == 'delete': | ||||
|                 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.info(msg="" | ||||
|  | @ -426,6 +432,12 @@ class Route(Connect): | |||
|                     force_download=force_download, | ||||
|                     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': | ||||
|                 return self.__update_source_atlassian( | ||||
|                     db_root_path=db_root_path, | ||||
|  | @ -446,8 +458,8 @@ class Route(Connect): | |||
|                     force_download=force_download, | ||||
|                     logger_alias=logger_alias | ||||
|                     ) | ||||
|             elif name == 'wwwhmptoday': | ||||
|                 return self.__update_source_wwwhmptoday( | ||||
|             elif name == 'githmptoday': | ||||
|                 return self.__update_source_githmptoday( | ||||
|                     db_root_path=db_root_path, | ||||
|                     download_user=download_user, | ||||
|                     download_pass=download_pass, | ||||
|  | @ -658,6 +670,63 @@ class Route(Connect): | |||
|             return True | ||||
|         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( | ||||
|         self, | ||||
|         db_root_path: str, | ||||
|  | @ -846,7 +915,7 @@ class Route(Connect): | |||
|                 remove(db_source_file) | ||||
|             return False | ||||
| 
 | ||||
|     def __update_source_wwwhmptoday( | ||||
|     def __update_source_githmptoday( | ||||
|         self, | ||||
|         db_root_path: str, | ||||
|         download_user: str, | ||||
|  | @ -855,7 +924,7 @@ class Route(Connect): | |||
|         logger_alias: str = inspect.stack()[0].function | ||||
|         ) -> bool: | ||||
|         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'): | ||||
|                 force_download = True | ||||
|             if force_download: | ||||
|  | @ -1024,12 +1093,12 @@ if __name__ == "__main__": | |||
|             enable_sources['ip2location']['db_source_code'] = ( | ||||
|                 conf_sources.data['ip2l_database_code'] | ||||
|                 ) | ||||
|         if 'wwwhmptoday' in enable_sources: | ||||
|             enable_sources['wwwhmptoday']['download_user'] = ( | ||||
|                 conf_sources.data['wwwhmptoday_user'] | ||||
|         if 'githmptoday' in enable_sources: | ||||
|             enable_sources['githmptoday']['download_user'] = ( | ||||
|                 conf_sources.data['githmptoday_user'] | ||||
|                 ) | ||||
|             enable_sources['wwwhmptoday']['download_pass'] = ( | ||||
|                 conf_sources.data['wwwhmptoday_pass'] | ||||
|             enable_sources['githmptoday']['download_pass'] = ( | ||||
|                 conf_sources.data['githmptoday_pass'] | ||||
|                 ) | ||||
| 
 | ||||
|     logging.basicConfig( | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user