-
Notifications
You must be signed in to change notification settings - Fork 4
/
QUICAdapterMirai.py
209 lines (174 loc) · 6.88 KB
/
QUICAdapterMirai.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import aioquic
import json
import logging
import asyncio
from typing import NoReturn
from basicutils.taskutils import ArgumentParser
from loguru import logger
import sys
import aiohttp
logger.add(lambda msg: print())
# logger.add(sys.stdout, format='>')
from prompt_toolkit.patch_stdout import patch_stdout
from prompt_toolkit.shortcuts import PromptSession
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s<%(filename)s:%(lineno)d>[%(levelname)s]%(message)s',
datefmt='%H:%M:%S'
)
import cfg
sport = cfg.quic_port
hostname = cfg.quic_host
from basicutils.socketutils import *
class QUICMiraiSession(QUICSessionBase):
def initialize(self):
self.ases = aiohttp.ClientSession()
async def pulling_loop(self):
while 1:
ent = await self.recv()
plaintext = ent.chain.onlyplain()
if plaintext:
logger.info('文本内容:\n{}', plaintext) # 处理irori server的信息
await self.upload_chain(ent)
sysinfo = ent.unpack_rawstring()
if sysinfo:
logger.warning('系统消息:\n{}', sysinfo)
async def command_loop(self):
app = ArgumentParser('command')
app.add_argument('cmd', choices=[
'send', 'raw', 'hex', 'eval', 'sudo', 'api'
])
session = PromptSession('(irori - Mirai)>')
while 1:
try:
cmdlist = (await session.prompt_async()).split()
session.message = '(irori - Mirai OwO)>'
if not cmdlist:
continue
cmd, *ato = cmdlist
logger.debug(ato)
if cmd == 'send':
# player, message = cmdlist
ent = CoreEntity(
chain=MessageChain.auto_make(' '.join(ato)),
player=self.playerid,
source=self.syncid,
meta={}
)
# tosend = ent.json()
logger.debug('SEND {}', ent.json())
await self.send(ent)
elif cmd == 'sudo':
ent = CoreEntity(
chain=MessageChain.auto_make(' '.join(['sudo'] + ato)),
player=self.playerid,
source=self.syncid,
meta={}
)
logger.debug('SEND {}', ent.json())
await self.send(ent)
elif cmd == 'api':
content = ' '.join(ato)
logger.debug('SEND {}', content)
await self.ws.send_json(content)
except KeyboardInterrupt:
break
except:
logger.debug(traceback.format_exc())
session.message = '(irori - Mirai QwQ)>'
app.print_help()
async def mirai_loop(self, syncid: str):
self.syncid = syncid
self.ws = await self.ases.ws_connect(cfg.authdata['url'] + f'all?qq={cfg.authdata["qq"]}&verifyKey={cfg.authdata["token"]}')
asyncio.ensure_future(self.pulling_loop())
asyncio.ensure_future(self.command_loop())
async for msg in self.ws:
if msg.type == aiohttp.WSMsgType.TEXT:
logger.critical(msg.data)
j = json.loads(msg.data)
if 'data' in j and 'type' in j['data']:
if j['data']['type'] == 'GroupMessage':
ent = CoreEntity(
player=str(j['data']['sender']['group']['id'] + (1<<39)),
source=str(self.syncid),
meta={'mem': j['data']['sender']['id']},
chain=MessageChain.auto_make(j['data']['messageChain'])
)
await self.send(ent)
elif j['data']['type'] == 'FriendMessage':
ent = CoreEntity(
player=str(j['data']['sender']['id']),
source=str(self.syncid),
meta={'mem': j['data']['sender']['id']},
chain=MessageChain.auto_make(j['data']['messageChain'])
)
await self.send(ent)
elif msg.type == aiohttp.WSMsgType.ERROR:
break
async def upload_chain(self, ent: CoreEntity):
"""将消息链往mirai发送"""
try:
pi = int(ent.player)
payload = {
"syncId": self.syncid,
"content": {
"messageChain": ent.chain.dict()["__root__"]
},
}
if pi > (1<<32):
pi -= 1<<39
payload['command'] = "sendGroupMessage"
payload['content']['target'] = pi
logger.warning(json.dumps(payload))
await self.ws.send_json(payload)
else:
payload['command'] = "sendFriendMessage"
payload['content']['target'] = pi
logger.warning(json.dumps(payload))
await self.ws.send_json(payload)
except ValueError:
return "not mirai"
import concurrent.futures
import datetime
import traceback
from basicutils.socketutils import *
import ssl
from aioquic.quic.configuration import QuicConfiguration
from aioquic.h3.connection import H3_ALPN
from aioquic.asyncio.client import connect
import functools
syncid: str
playerid: str
import uuid
async def run():
with patch_stdout():
conf = QuicConfiguration(
alpn_protocols=H3_ALPN,
is_client=True,
max_datagram_frame_size=cfg.buffer,
idle_timeout=cfg.idle_tle,
verify_mode=ssl.CERT_NONE
# quic_logger=logger.Logger,
# secrets_log_file=secrets_log_file,
)
loop = asyncio.get_running_loop()
print(hostname, sport)
# while 1:
async with connect(
cfg.quic_host,
cfg.quic_port,
configuration=conf
) as C:
reader, writer = await C.create_stream()
pid = 'Irori - ' + str(uuid.uuid1())
writer.write(f'A {cfg.quic_admin_key} {pid}'.encode('utf-8'))
ses = QUICMiraiSession(reader, writer)
ses.playerid = pid
syncid = (await ses.recv()).unpack_rawstring()
logger.critical("您的终端号:{}", syncid)
await ses.mirai_loop(syncid)
# async with aiohttp.ClientSession() as ases:
if __name__ == "__main__":
# playerid = 'T' + input('随便取一个playerid?>>>')
loop = asyncio.get_event_loop()
loop.run_until_complete(run())