Skip to content

Commit

Permalink
fix(ip): punycode
Browse files Browse the repository at this point in the history
  • Loading branch information
KimmyXYC committed Mar 16, 2024
1 parent d039b57 commit 19788bd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions App/Event.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ async def handle_ip_ali(bot, message, _config):
async def handle_ip(bot, message, _config):
msg = await bot.reply_to(message, f"正在查询 {message.text.split()[1]} ...", disable_web_page_preview=True)
url = message.text.split()[1]
url = convert_to_punycode(url)
status, data = await ipapi_ip(url)
if status:
if url == data["query"]:
Expand Down
10 changes: 10 additions & 0 deletions Utils/IP.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ipaddress
import socket
import aiohttp
import idna
from loguru import logger


Expand All @@ -20,6 +21,15 @@ def check_url(url):
return get_ip_address(url), None


def convert_to_punycode(domain):
try:
domain.encode('ascii')
except UnicodeEncodeError:
return idna.encode(domain).decode('ascii')
else:
return domain


def get_ip_address(domain):
try:
addr_info = socket.getaddrinfo(domain, None, socket.AF_UNSPEC)
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pathlib>=1.0.1
aiohttp>=3.9.1
loguru>=0.7.2
requests>=2.31.0
Pillow>=10.1.0
Pillow>=10.1.0
idna>=3.4

0 comments on commit 19788bd

Please sign in to comment.