PagerMaid_Plugins_Pyro/ip/main.py

88 lines
3.2 KiB
Python
Raw Normal View History

2022-08-10 15:31:44 +00:00
import contextlib
from urllib.parse import urlparse
from pagermaid.listener import listener
from pagermaid.enums import Message
from pagermaid.services import client as requests
async def get_ip_info(url: str) -> str:
data = await requests.get(
f"http://ip-api.com/json/{url}?fields=status,message,country,regionName,"
2023-07-01 12:18:58 +00:00
f"city,lat,lon,isp,org,as,mobile,proxy,hosting,query"
)
2022-08-10 15:31:44 +00:00
ipinfo_json = data.json()
2023-07-01 12:18:58 +00:00
if ipinfo_json["status"] == "fail":
2022-08-10 15:31:44 +00:00
return ""
2023-07-01 12:18:58 +00:00
elif ipinfo_json["status"] == "success":
2022-08-10 15:31:44 +00:00
ipinfo_list = [f"查询目标: `{url}`"]
2023-07-01 12:18:58 +00:00
if ipinfo_json["query"] != url:
ipinfo_list.extend(["解析地址: `" + ipinfo_json["query"] + "`"])
2022-08-10 15:31:44 +00:00
ipinfo_list.extend(
2023-07-01 12:18:58 +00:00
[
(
(
"地区: `"
+ ipinfo_json["country"]
+ " - "
+ ipinfo_json["regionName"]
+ " - "
+ ipinfo_json["city"]
)
+ "`"
),
"经纬度: `"
+ str(ipinfo_json["lat"])
+ ","
+ str(ipinfo_json["lon"])
+ "`",
"ISP `" + ipinfo_json["isp"] + "`",
]
)
if ipinfo_json["org"] != "":
ipinfo_list.extend(["组织: `" + ipinfo_json["org"] + "`"])
2022-08-10 15:31:44 +00:00
with contextlib.suppress(Exception):
2023-07-01 12:18:58 +00:00
ipinfo_list.extend(
[
"["
+ ipinfo_json["as"]
+ "](https://bgp.he.net/"
+ ipinfo_json["as"].split()[0]
+ ")"
]
)
if ipinfo_json["mobile"]:
ipinfo_list.extend(["此 IP 可能为**蜂窝移动数据 IP**"])
if ipinfo_json["proxy"]:
ipinfo_list.extend(["此 IP 可能为**代理 IP**"])
if ipinfo_json["hosting"]:
ipinfo_list.extend(["此 IP 可能为**数据中心 IP**"])
2022-08-10 15:31:44 +00:00
return "\n".join(ipinfo_list)
2023-07-01 12:18:58 +00:00
@listener(command="ip", description="IPINFO (或者回复一句话)", parameters="[ip/域名]")
2022-08-10 15:31:44 +00:00
async def ipinfo(message: Message):
reply = message.reply_to_message
2023-07-01 12:18:58 +00:00
message: Message = await message.edit("正在查询中...")
2022-08-10 15:31:44 +00:00
try:
if reply:
for num in range(len(reply.entities)):
2023-07-01 12:18:58 +00:00
url = reply.text[
reply.entities[num].offset : reply.entities[num].offset
+ reply.entities[num].length
]
2022-08-10 15:31:44 +00:00
url = urlparse(url)
url = url.hostname or url.path
await message.edit(await get_ip_info(url))
return
2022-09-25 08:09:21 +00:00
elif message.arguments:
url_ = urlparse(message.arguments)
url = url_.hostname or url_.path
if ":" in url and "." not in url and not url_.hostname:
url = message.arguments
2022-08-10 15:31:44 +00:00
await message.edit(await get_ip_info(url))
return
2023-07-01 12:18:58 +00:00
await message.edit("没有找到要查询的 ip/域名 ...")
2022-08-10 15:31:44 +00:00
except Exception:
2023-07-01 12:18:58 +00:00
await message.edit("没有找到要查询的 ip/域名 ...")