diff options
| author | handingkang <[email protected]> | 2024-04-22 10:15:30 +0800 |
|---|---|---|
| committer | handingkang <[email protected]> | 2024-04-22 10:15:30 +0800 |
| commit | a1be40004d1cdeaf5b8f6096fb569d0415fdcc41 (patch) | |
| tree | fa34412c44c2706c65ddc34fc307ef3ab9c0e029 | |
| parent | c9700eabaa42c21367221bbffe01ada52fe7362b (diff) | |
完成目标信息获取接口功能实现
| -rw-r--r-- | server/apps/model.py | 14 | ||||
| -rw-r--r-- | server/apps/target.py | 30 | ||||
| -rw-r--r-- | server/apps/util.py | 22 |
3 files changed, 40 insertions, 26 deletions
diff --git a/server/apps/model.py b/server/apps/model.py index 45cf61a..0748f70 100644 --- a/server/apps/model.py +++ b/server/apps/model.py @@ -33,12 +33,24 @@ keymapping = { "DNSSEC": "DNSSEC", "DoH": "DOH", "DoT": "DOT", + "ADDRv4": "ADDRv4", + "ADDRv6": "ADDRv6" })} +# 所有参数在数据库中对应的数据类型,用于拼接sql语句时特殊处理 typemapping = { "atype": "str", "status": "int", - "idle": "int" + "idle": "int", + "cou": "str", + "isp": "str", + "ip": "str", + "IPv6": "int", + "DNSSEC": "int", + "DoH": "int", + "DoT": "int", + "ADDRv4": "str", + "ADDRv6": "str" } # 默认参数 diff --git a/server/apps/target.py b/server/apps/target.py index 05c1263..6c4e667 100644 --- a/server/apps/target.py +++ b/server/apps/target.py @@ -8,6 +8,7 @@ from apiflask import APIBlueprint, Schema from apiflask.fields import Integer, String, List, Nested, IP, DateTime, Dict from requests.exceptions import Timeout +from settings import * from .util import da, debug bp = APIBlueprint("目标信息及状态接口集合", __name__, url_prefix="/target") @@ -240,15 +241,22 @@ def target_info(query_data): # 目标信息列表 target_list = [] + # 普通检索,默认所有条件为单选 if ip is None: - # 普通检索 - res = da.get_data(data_type="target", search={"proto": proto, "cou": cou, "isp": isp}, - offset=(page - 1) * per_page, limit=per_page) - res_count = da.count_data(data_type="target", search={"proto": proto, "cou": cou, "isp": isp}) + # 无协议筛选 + if proto == None: + res = da.get_data(data_type="target", search={"proto": proto, "cou": cou, "isp": isp}, + offset=(page - 1) * per_page, limit=per_page) + res_count = da.count_data(data_type="target", search={"proto": proto, "cou": cou, "isp": isp}) + # 协议筛选,填入筛选的协议 + else: + res = da.get_data(data_type="target", search={proto: True, "cou": cou, "isp": isp}, + offset=(page - 1) * per_page, limit=per_page) + res_count = da.count_data(data_type="target", search={proto: True, "cou": cou, "isp": isp}) else: - # 查询目标 - res = da.get_data(data_type="target", offset=(page - 1) * per_page, limit=per_page, - search={"ip": query_data["ip"]}) + # 查询目标,根据v4、v6地址分类 + res = da.get_data(data_type="target", + search={"ADDRv4": ip} if "." in str(ip) else {"ADDRv6": ip}) res_count = 1 # 结果转换 for r in res: @@ -282,7 +290,13 @@ def target_info(query_data): # TODO:实现 def filter_info(): proto = ["IPv6", "DNSSEC", "DoH", "DoT"] - isp = ["google", "cloudflare", "阿里云", "DNSPod", "quad9"] + isp_sql = """SELECT DISTINCT ISP from %s """ % MYSQL_TAB_TARGETDATA + # 执行查询 + da.cursor.execute(isp_sql) + isp_data = da.cursor.fetchall() + # TODO:测试 + isp = [i[0] for i in isp_data] + cou = ["美国", "中国", "日本", "澳大利亚", "新加坡"] return {"code": 200, "proto": proto, "isp": isp, "cou": cou} diff --git a/server/apps/util.py b/server/apps/util.py index a608782..7a7932f 100644 --- a/server/apps/util.py +++ b/server/apps/util.py @@ -192,24 +192,12 @@ class DataHandler: condition = {} for _ in range(l): key, val = differ.pop() - # target表的协议参数和 ip 单独处理 - if data_type == "target" and key == "proto": - tab_key = model.keymapping[data_type][val] - condition[tab_key] = True - if data_type == "target" and key == "ip": - if "." in val: - condition["ADDRv4"] = "\"" + val + "\"" - elif ":" in val: - condition["ADDRv6"] = "\"" + val + "\"" - else: - error("错误的地址输入: " + str(val)) + # 参数在数据表中对应的字段名 + tab_key = model.keymapping[data_type][key] + if model.typemapping[key] == "str": + condition[tab_key] = "\"".join(["", str(val), ""]) else: - # 参数在数据表中对应的字段名 - tab_key = model.keymapping[data_type][key] - if model.typemapping[key] == "str": - condition[tab_key] = "\"" + val + "\"" - else: - condition[tab_key] = str(val) + condition[tab_key] = str(val) if not count: sql = """SELECT * FROM %s WHERE %s LIMIT %s, %s""" % ( tabname, " AND ".join(["=".join(condition.popitem()) for _ in range(l)]), offset, offset + limit) |
