- fix lru wrapper (async)
- fix mutex
- init key before assignment
- force cast str
- return ZZ if record is invalid
- add FRonly mode
This commit is contained in:
cclecle
2022-06-08 00:04:26 +02:00
parent 00755f7d84
commit f3834b5206
2 changed files with 26 additions and 13 deletions

View File

@@ -8,4 +8,4 @@ You should have received a copy of the license along with this
work. If not, see <https://creativecommons.org/licenses/by-nc-sa/4.0/>. work. If not, see <https://creativecommons.org/licenses/by-nc-sa/4.0/>.
""" """
__version__ = "0.2" __version__ = "0.2.1"

View File

@@ -39,6 +39,8 @@ DEFAULT__EnableIPV6 = True
DEFAULT__IpDataSet_ipv4_subdir = "ipv4" DEFAULT__IpDataSet_ipv4_subdir = "ipv4"
DEFAULT__IpDataSet_ipv6_subdir = "ipv6" DEFAULT__IpDataSet_ipv6_subdir = "ipv6"
DEFAULT_onlyFR = False
def processfile(_entry_path,_entry_code) : def processfile(_entry_path,_entry_code) :
with open(_entry_path) as fp: 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.lifetime = timedelta(seconds=seconds)
func.expiration = datetime.utcnow() + func.lifetime func.expiration = datetime.utcnow() + func.lifetime
@wraps(func) @wraps(func)
def wrapped_func(*args, **kwargs): async def wrapped_func(*args, **kwargs):
if datetime.utcnow() >= func.expiration: if datetime.utcnow() >= func.expiration:
func.cache_clear() func.cache_clear()
func.expiration = datetime.utcnow() + func.lifetime func.expiration = datetime.utcnow() + func.lifetime
return func(*args, **kwargs) return await func(*args, **kwargs)
return wrapped_func return wrapped_func
return wrapper_cache return wrapper_cache
@@ -88,18 +90,20 @@ class ChaChaIPToCountryStorage:
async def test_ip(self,ip): async def test_ip(self,ip):
validity = validIPAddress(ip) validity = validIPAddress(ip)
set = dict()
await self.DataSetlock.acquire()
if validity == "IPv4": if validity == "IPv4":
set =self.ipv4set set = self.ipv4set
elif validity == "IPv6" and DEFAULT__EnableIPV6: elif validity == "IPv6" and DEFAULT__EnableIPV6:
set =self.ipv6set set = self.ipv6set
else: else:
RuntimeError("Invalid ip address received") RuntimeError("Invalid ip address received")
await self.DataSetlock.acquire()
try: try:
for key,_set in set.items(): for key,_set in set.items():
if IPAddress(ip) in _set: if IPAddress(ip) in _set:
return key return str(key)
finally: finally:
self.DataSetlock.release() self.DataSetlock.release()
@@ -129,7 +133,9 @@ class ChaChaIPToCountryStorage:
with os.scandir(self.ipv4_dir) as entries: with os.scandir(self.ipv4_dir) as entries:
for entry in entries: for entry in entries:
entry_path = os.path.join(self.ipv4_dir,entry.name) 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) task = loop.run_in_executor(_executor,processfile,entry_path,entry_code)
tasksIPV4.append(task) tasksIPV4.append(task)
print("Done") print("Done")
@@ -139,7 +145,9 @@ class ChaChaIPToCountryStorage:
with os.scandir(self.ipv6_dir) as entries: with os.scandir(self.ipv6_dir) as entries:
for entry in entries: for entry in entries:
entry_path = os.path.join(self.ipv6_dir,entry.name) 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) task = loop.run_in_executor(_executor,processfile,entry_path,entry_code)
tasksIPV6.append(task) tasksIPV6.append(task)
print("Done") print("Done")
@@ -226,10 +234,15 @@ class ChaChaIPToCountryDaemon_Handler(tornado.web.RequestHandler):
result["alpha_2"] = await self.getIP2Country(ip) result["alpha_2"] = await self.getIP2Country(ip)
print(result["alpha_2"]) print(result["alpha_2"])
country = pycountry.countries.get(alpha_2=result["alpha_2"]) country = pycountry.countries.get(alpha_2=result["alpha_2"])
print(country) print(country)
result["coutry_name"]=country.name.upper() if country:
result["alpha_3"]=country.alpha_3 result["coutry_name"]=country.name.upper()
result["coutry_official_name"]=country.official_name 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["utc_time"]= datetime.now(timezone("UTC")).strftime('%H:%M')
result["ip"]=ip result["ip"]=ip
addr=reversename.from_address(ip) addr=reversename.from_address(ip)