-
Notifications
You must be signed in to change notification settings - Fork 7
/
geetest.py
60 lines (46 loc) · 1.9 KB
/
geetest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import jinja2
import hoshino
from hoshino import config
from quart import Blueprint
template_folder = os.path.join(os.path.dirname(__file__), 'geetest')
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(template_folder),
enable_async=True
)
async def render_template(template, **kwargs):
t = env.get_template(template)
return await t.render_async(**kwargs)
geetest_validate = Blueprint('geetest_validate', __name__)
@geetest_validate.route('/geetest')
async def geetest():
return await render_template('geetest.html')
bot_ = hoshino.get_bot()
bot_.server_app.register_blueprint(geetest_validate)
@bot_.on_startup
async def get_real_ip() -> str:
public_address = f"127.0.0.1:{config.PORT}"
try:
if config.public_address:
public_address = config.public_address
elif config.IP:
public_address = f"{config.IP}:{config.PORT}"
except AttributeError:
# from .query import acinfo
# try:
# resp = await aiorequests.get(url="https://4.ipw.cn", timeout=3)
# real_ip = await resp.text
# public_address = f"{real_ip}:{config.PORT}"
# hoshino.logger.info(f"using fetched real ip as public address: {public_address}")
# await bot_.send_private_msg(
# user_id=acinfo[0]['admin'],
# message=f"成功获取公网IP:{real_ip}\n注意:本IP仅在你有公网IP时才有效!如果你没有公网IP,请使用127.0.0.1作为你的IP!"
# )
# except Exception as e:
# hoshino.logger.error(f"获取公网IP失败\n{e}")
# await bot_.send_private_msg(user_id=acinfo[0]['admin'], message=f"获取公网IP失败,使用默认IP\n{e}")
pass
hoshino.logger.info(f"current public address: {public_address}")
return public_address
async def get_public_address():
return await get_real_ip()