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

switch from IPHub to IP-API #298

Merged
merged 2 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion rest_api/v0/emojis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
@emojis_bp.get("/<lang>")
async def get_default_emojis(lang: str):
if lang in DEFAULT_EMOJIS:
return DEFAULT_EMOJIS[lang], 200
return {"error": False, "groups": DEFAULT_EMOJIS[lang]}, 200
else:
abort(404)
32 changes: 19 additions & 13 deletions security.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,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 @@ -441,21 +441,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 @@ -464,7 +466,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