Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
switch from IPHub to IP-API
Browse files Browse the repository at this point in the history
  • Loading branch information
tnix100 committed Aug 26, 2024
1 parent 3cf8adf commit 5c436bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ MONGO_URI=mongodb://127.0.0.1:27017
MONGO_DB=meowerserver
REDIS_URI=redis://127.0.0.1:6379/0
REAL_IP_HEADER=
IPHUB_KEY=
CL3_HOST="0.0.0.0"
CL3_PORT=3000
API_HOST="0.0.0.0"
Expand Down
32 changes: 19 additions & 13 deletions security.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def delete_account(username, purge=False):

def get_netinfo(ip_address):
"""
Get IP info from IPHub.
Get IP info from IP-API.
Returns:
```json
Expand All @@ -451,21 +451,23 @@ def get_netinfo(ip_address):
# Get IP hash
ip_hash = sha256(ip_address.encode()).hexdigest()

# Get from database or IPHub if not cached
# Get from database or IP-API if not cached
netinfo = db.netinfo.find_one({"_id": ip_hash})
if not netinfo:
iphub_key = os.getenv("IPHUB_KEY")
if iphub_key:
iphub_info = requests.get(f"http://v2.api.iphub.info/ip/{ip_address}", headers={
"X-Key": iphub_key
}).json()
resp = requests.get(f"http://ip-api.com/json/{ip_address}?fields=25349915")
if resp.ok:
resp_json = resp.json()
netinfo = {
"_id": ip_hash,
"country_code": iphub_info["countryCode"],
"country_name": iphub_info["countryName"],
"asn": iphub_info["asn"],
"isp": iphub_info["isp"],
"vpn": (iphub_info["block"] == 1),
"country_code": resp_json["countryCode"],
"country_name": resp_json["country"],
"region": resp_json["regionName"],
"city": resp_json["city"],
"timezone": resp_json["timezone"],
"currency": resp_json["currency"],
"as": resp_json["as"],
"isp": resp_json["isp"],
"vpn": (resp_json.get("hosting") or resp_json.get("proxy")),
"last_refreshed": int(time.time())
}
db.netinfo.update_one({"_id": ip_hash}, {"$set": netinfo}, upsert=True)
Expand All @@ -474,7 +476,11 @@ def get_netinfo(ip_address):
"_id": ip_hash,
"country_code": "Unknown",
"country_name": "Unknown",
"asn": "Unknown",
"region": "Unknown",
"city": "Unknown",
"timezone": "Unknown",
"currency": "Unknown",
"as": "Unknown",
"isp": "Unknown",
"vpn": False,
"last_refreshed": int(time.time())
Expand Down

0 comments on commit 5c436bd

Please sign in to comment.