Update main.py
Browse files
main.py
CHANGED
@@ -1,6 +1,26 @@
|
|
1 |
import ipaddress
|
2 |
import maxminddb
|
3 |
from fastapi import FastAPI, Request
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
city_reader = maxminddb.open_database('GeoLite2-City.mmdb')
|
6 |
asn_reader = maxminddb.open_database('GeoLite2-ASN.mmdb')
|
@@ -25,7 +45,7 @@ asn_map = {
|
|
25 |
4134:"中国电信",4812:"中国电信",23724:"中国电信",136188:"中国电信",137693:"中国电信",17638:"中国电信",
|
26 |
140553:"中国电信",4847:"中国电信",140061:"中国电信",136195:"中国电信",17799:"中国电信",139018:"中国电信",
|
27 |
133776:"中国电信",58772:"中国电信",146966:"中国电信",63527:"中国电信",58539:"中国电信",58540:"中国电信",
|
28 |
-
141998:"中国电信",138169:"中国电信",139203:"中国电信",58563:"
|
29 |
137694:"中国电信",137698:"中国电信",136167:"中国电信",148969:"中国电信",134764:"中国电信",
|
30 |
134770:"中国电信",148981:"中国电信",134774:"中国电信",136190:"中国电信",140647:"中国电信",
|
31 |
132225:"中国电信",140485:"中国电信",4811:"中国电信",131285:"中国电信",137689:"中国电信",
|
@@ -192,16 +212,51 @@ def query():
|
|
192 |
app = FastAPI()
|
193 |
|
194 |
@app.get("/")
|
195 |
-
def api(request: Request, ip: str = None):
|
196 |
-
|
197 |
-
|
198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
|
200 |
@app.get("/{ip}")
|
201 |
-
def path_api(ip):
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
if __name__ == '__main__':
|
205 |
query()
|
206 |
import uvicorn
|
207 |
-
uvicorn.run(app, host="0.0.0.0", port=8080, server_header=False, proxy_headers=True)
|
|
|
1 |
import ipaddress
|
2 |
import maxminddb
|
3 |
from fastapi import FastAPI, Request
|
4 |
+
import json
|
5 |
+
import datetime
|
6 |
+
import logging
|
7 |
+
import sys
|
8 |
+
|
9 |
+
# 配置日志
|
10 |
+
try:
|
11 |
+
logging.basicConfig(
|
12 |
+
filename='ip_query.log',
|
13 |
+
level=logging.INFO,
|
14 |
+
format='%(message)s'
|
15 |
+
)
|
16 |
+
logging.info(json.dumps({
|
17 |
+
"时间": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
18 |
+
"事件": "系统启动",
|
19 |
+
"状态": "成功"
|
20 |
+
}, ensure_ascii=False))
|
21 |
+
except Exception as e:
|
22 |
+
print(f"日志初始化失败: {e}")
|
23 |
+
sys.exit(1)
|
24 |
|
25 |
city_reader = maxminddb.open_database('GeoLite2-City.mmdb')
|
26 |
asn_reader = maxminddb.open_database('GeoLite2-ASN.mmdb')
|
|
|
45 |
4134:"中国电信",4812:"中国电信",23724:"中国电信",136188:"中国电信",137693:"中国电信",17638:"中国电信",
|
46 |
140553:"中国电信",4847:"中国电信",140061:"中国电信",136195:"中国电信",17799:"中国电信",139018:"中国电信",
|
47 |
133776:"中国电信",58772:"中国电信",146966:"中国电信",63527:"中国电信",58539:"中国电信",58540:"中国电信",
|
48 |
+
141998:"中国电信",138169:"中国电信",139203:"中国电信",58563:"中电信",137690:"中国电信",63838:"中国电信",
|
49 |
137694:"中国电信",137698:"中国电信",136167:"中国电信",148969:"中国电信",134764:"中国电信",
|
50 |
134770:"中国电信",148981:"中国电信",134774:"中国电信",136190:"中国电信",140647:"中国电信",
|
51 |
132225:"中国电信",140485:"中国电信",4811:"中国电信",131285:"中国电信",137689:"中国电信",
|
|
|
212 |
app = FastAPI()
|
213 |
|
214 |
@app.get("/")
|
215 |
+
async def api(request: Request, ip: str = None):
|
216 |
+
# 获取请求信息
|
217 |
+
client_ip = request.headers.get("x-forwarded-for") or request.headers.get("x-real-ip") or request.client.host
|
218 |
+
query_ip = ip.strip() if ip else client_ip
|
219 |
+
|
220 |
+
# 获取IP信息
|
221 |
+
result = get_ip_info(query_ip)
|
222 |
+
|
223 |
+
# 构建日志信息
|
224 |
+
log_data = {
|
225 |
+
"时间": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
226 |
+
"访问IP": client_ip,
|
227 |
+
"查询IP": query_ip,
|
228 |
+
"请求头": dict(request.headers),
|
229 |
+
"查询结果": result
|
230 |
+
}
|
231 |
+
|
232 |
+
# 写入日志
|
233 |
+
logging.info(json.dumps(log_data, ensure_ascii=False))
|
234 |
+
|
235 |
+
return result
|
236 |
|
237 |
@app.get("/{ip}")
|
238 |
+
async def path_api(request: Request, ip: str):
|
239 |
+
# 获取请求信息
|
240 |
+
client_ip = request.headers.get("x-forwarded-for") or request.headers.get("x-real-ip") or request.client.host
|
241 |
+
|
242 |
+
# 获取IP信息
|
243 |
+
result = get_ip_info(ip)
|
244 |
+
|
245 |
+
# 构建日志信息
|
246 |
+
log_data = {
|
247 |
+
"时间": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
248 |
+
"访问IP": client_ip,
|
249 |
+
"查询IP": ip,
|
250 |
+
"请求头": dict(request.headers),
|
251 |
+
"查询结果": result
|
252 |
+
}
|
253 |
+
|
254 |
+
# 写入日志
|
255 |
+
logging.info(json.dumps(log_data, ensure_ascii=False))
|
256 |
+
|
257 |
+
return result
|
258 |
|
259 |
if __name__ == '__main__':
|
260 |
query()
|
261 |
import uvicorn
|
262 |
+
uvicorn.run(app, host="0.0.0.0", port=8080, server_header=False, proxy_headers=True)
|