diff --git a/ChaChaIPToCountryDaemon/_version.py b/ChaChaIPToCountryDaemon/_version.py index 89089c1..20a5c78 100644 --- a/ChaChaIPToCountryDaemon/_version.py +++ b/ChaChaIPToCountryDaemon/_version.py @@ -8,4 +8,4 @@ You should have received a copy of the license along with this work. If not, see . """ -__version__ = "0.2" \ No newline at end of file +__version__ = "0.2.1" \ No newline at end of file diff --git a/ChaChaIPToCountryDaemon/core.py b/ChaChaIPToCountryDaemon/core.py index cf4136f..4caa862 100644 --- a/ChaChaIPToCountryDaemon/core.py +++ b/ChaChaIPToCountryDaemon/core.py @@ -39,6 +39,8 @@ DEFAULT__EnableIPV6 = True DEFAULT__IpDataSet_ipv4_subdir = "ipv4" DEFAULT__IpDataSet_ipv6_subdir = "ipv6" +DEFAULT_onlyFR = False + def processfile(_entry_path,_entry_code) : with open(_entry_path) as fp: @@ -60,11 +62,11 @@ def timed_lru_cache(seconds: int, maxsize: int = None): func.lifetime = timedelta(seconds=seconds) func.expiration = datetime.utcnow() + func.lifetime @wraps(func) - def wrapped_func(*args, **kwargs): + async def wrapped_func(*args, **kwargs): if datetime.utcnow() >= func.expiration: func.cache_clear() func.expiration = datetime.utcnow() + func.lifetime - return func(*args, **kwargs) + return await func(*args, **kwargs) return wrapped_func return wrapper_cache @@ -88,18 +90,20 @@ class ChaChaIPToCountryStorage: async def test_ip(self,ip): validity = validIPAddress(ip) + set = dict() + + await self.DataSetlock.acquire() if validity == "IPv4": - set =self.ipv4set + set = self.ipv4set elif validity == "IPv6" and DEFAULT__EnableIPV6: - set =self.ipv6set + set = self.ipv6set else: RuntimeError("Invalid ip address received") - await self.DataSetlock.acquire() try: for key,_set in set.items(): if IPAddress(ip) in _set: - return key + return str(key) finally: self.DataSetlock.release() @@ -129,7 +133,9 @@ class ChaChaIPToCountryStorage: with os.scandir(self.ipv4_dir) as entries: for entry in entries: entry_path = os.path.join(self.ipv4_dir,entry.name) - entry_code = Path(entry_path).stem + entry_code = Path(entry_path).stem + if DEFAULT_onlyFR and (entry_code != "fr"): + continue task = loop.run_in_executor(_executor,processfile,entry_path,entry_code) tasksIPV4.append(task) print("Done") @@ -139,7 +145,9 @@ class ChaChaIPToCountryStorage: with os.scandir(self.ipv6_dir) as entries: for entry in entries: entry_path = os.path.join(self.ipv6_dir,entry.name) - entry_code = Path(entry_path).stem + entry_code = Path(entry_path).stem + if DEFAULT_onlyFR and (entry_code != "fr"): + continue task = loop.run_in_executor(_executor,processfile,entry_path,entry_code) tasksIPV6.append(task) print("Done") @@ -226,10 +234,15 @@ class ChaChaIPToCountryDaemon_Handler(tornado.web.RequestHandler): result["alpha_2"] = await self.getIP2Country(ip) print(result["alpha_2"]) country = pycountry.countries.get(alpha_2=result["alpha_2"]) - print(country) - result["coutry_name"]=country.name.upper() - result["alpha_3"]=country.alpha_3 - result["coutry_official_name"]=country.official_name + print(country) + if country: + result["coutry_name"]=country.name.upper() + result["alpha_3"]=country.alpha_3 + result["coutry_official_name"]=country.official_name + else: + result["coutry_name"]="Unknown" + result["alpha_3"]="ZZZ" + result["coutry_official_name"]="Unknown" result["utc_time"]= datetime.now(timezone("UTC")).strftime('%H:%M') result["ip"]=ip addr=reversename.from_address(ip)