Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,61 @@
|
|
| 1 |
-
from fastapi import FastAPI, Request
|
| 2 |
import time
|
| 3 |
import uvicorn
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
@app.get("/")
|
| 8 |
async def root(request: Request):
|
|
|
|
| 9 |
headers = request.headers
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
return {
|
| 13 |
"fl": "998f87",
|
| 14 |
"h": headers.get("host", "huggingface.co"),
|
| 15 |
"ip": client_ip,
|
| 16 |
-
"ts":
|
| 17 |
"visit_scheme": "https",
|
| 18 |
"uag": headers.get("user-agent", "unknown"),
|
| 19 |
-
"loc":
|
| 20 |
"tls": "TLSv1.3",
|
| 21 |
"status": "active"
|
| 22 |
}
|
| 23 |
|
| 24 |
if __name__ == "__main__":
|
| 25 |
-
#
|
| 26 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 2 |
import time
|
| 3 |
import uvicorn
|
| 4 |
+
import httpx
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
# Lưu trữ thời gian request cuối cùng của mỗi IP
|
| 9 |
+
# { "1.2.3.4": 1712345678.9 }
|
| 10 |
+
last_request_time = {}
|
| 11 |
+
|
| 12 |
+
async def get_real_location(ip: str):
|
| 13 |
+
"""Gọi API bên thứ 3 để lấy thông tin vị trí thật từ IP"""
|
| 14 |
+
if ip == "unknown" or ip.startswith("127."):
|
| 15 |
+
return "Local/Unknown"
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
async with httpx.AsyncClient() as client:
|
| 19 |
+
# Sử dụng ip-api.com (miễn phí cho thử nghiệm)
|
| 20 |
+
response = await client.get(f"http://ip-api.com/json/{ip}")
|
| 21 |
+
data = response.json()
|
| 22 |
+
if data.get("status") == "success":
|
| 23 |
+
return f"{data.get('city')}, {data.get('country')}"
|
| 24 |
+
except Exception:
|
| 25 |
+
pass
|
| 26 |
+
return "Unknown"
|
| 27 |
+
|
| 28 |
@app.get("/")
|
| 29 |
async def root(request: Request):
|
| 30 |
+
# 1. Lấy Client IP chuẩn
|
| 31 |
headers = request.headers
|
| 32 |
+
forwarded = headers.get("x-forwarded-for")
|
| 33 |
+
client_ip = forwarded.split(",")[0] if forwarded else "unknown"
|
| 34 |
+
|
| 35 |
+
# 2. Rate Limiting: 1 request / 0.5s
|
| 36 |
+
current_time = time.time()
|
| 37 |
+
last_time = last_request_time.get(client_ip, 0)
|
| 38 |
+
|
| 39 |
+
if current_time - last_time < 0.5:
|
| 40 |
+
raise HTTPException(status_code=429, detail="Too Many Requests - Wait 0.5s")
|
| 41 |
+
|
| 42 |
+
last_request_time[client_ip] = current_time
|
| 43 |
+
|
| 44 |
+
# 3. Lấy vị trí thật (City, Country)
|
| 45 |
+
real_loc = await get_real_location(client_ip)
|
| 46 |
|
| 47 |
return {
|
| 48 |
"fl": "998f87",
|
| 49 |
"h": headers.get("host", "huggingface.co"),
|
| 50 |
"ip": client_ip,
|
| 51 |
+
"ts": current_time,
|
| 52 |
"visit_scheme": "https",
|
| 53 |
"uag": headers.get("user-agent", "unknown"),
|
| 54 |
+
"loc": real_loc, # Vị trí thật từ API
|
| 55 |
"tls": "TLSv1.3",
|
| 56 |
"status": "active"
|
| 57 |
}
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
+
# Giữ nguyên cấu hình cho Hugging Face Spaces
|
| 61 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|