This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
atwss.py
328 lines (240 loc) · 9.74 KB
/
atwss.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""Connects to Aternos WebSocket API
for real-time information"""
import enum
import json
import asyncio
from typing import Iterable
from typing import Union, Any
from typing import Tuple, List, Dict
from typing import Callable, Coroutine
from typing import TYPE_CHECKING
import websockets
from .atlog import log
from .atconnect import REQUA
if TYPE_CHECKING:
from .atserver import AternosServer
OneArgT = Callable[[Any], Coroutine[Any, Any, None]]
TwoArgT = Callable[[Any, Tuple[Any, ...]], Coroutine[Any, Any, None]]
FunctionT = Union[OneArgT, TwoArgT] # pylint: disable=invalid-name
ArgsTuple = Tuple[FunctionT, Tuple[Any, ...]]
class Streams(enum.Enum):
"""WebSocket streams types"""
status = (0, None)
queue = (1, None)
console = (2, 'console')
ram = (3, 'heap')
tps = (4, 'tick')
none = (-1, None)
def __init__(self, num: int, stream: str) -> None:
self.num = num
self.stream = stream
class AternosWss:
"""Class for managing websocket connection"""
def __init__(
self,
atserv: 'AternosServer',
autoconfirm: bool = False) -> None:
"""Class for managing websocket connection
Args:
atserv (AternosServer):
atserver.AternosServer instance
autoconfirm (bool, optional):
Automatically start server status listener
when AternosWss connects to API to confirm
server launching
"""
self.atserv = atserv
self.servid = atserv.servid
cookies = atserv.atconn.session.cookies
self.session = cookies['ATERNOS_SESSION']
self.recv: Dict[Streams, List[ArgsTuple]]
self.recv = {
Streams.status: [],
Streams.queue: [],
Streams.console: [],
Streams.ram: [],
Streams.tps: [],
}
self.autoconfirm = autoconfirm
self.confirmed = False
self.socket: Any = None
self.keep: asyncio.Task
self.msgs: asyncio.Task
async def confirm(self) -> None:
"""Simple way to call
`AternosServer.confirm`
from this class"""
self.atserv.confirm()
def wssreceiver(
self,
stream: Streams,
arg: Tuple[Any, ...] = ()) -> Callable[[FunctionT], Any]:
"""Decorator that marks your function as a stream receiver.
When websocket receives message from the specified stream,
it calls all listeners created with this decorator.
Args:
stream (Streams): Stream that your function should listen
arg (Tuple[Any, ...], optional): Arguments which will be passed to your function
Returns:
...
"""
def decorator(func: FunctionT) -> Callable[[Any, Any], Coroutine[Any, Any, Any]]:
handlers = self.recv.get(stream, None)
if handlers is None:
self.recv[stream] = [(func, arg)]
else:
handlers.append((func, arg))
async def wrapper(*args, **kwargs) -> Any:
return await func(*args, **kwargs)
return wrapper
return decorator
async def connect(self) -> None:
"""Connects to the websocket server
and starts all stream listeners"""
headers = [
('Host', 'aternos.org'),
('User-Agent', REQUA),
(
'Cookie',
f'ATERNOS_SESSION={self.session}; '
f'ATERNOS_SERVER={self.servid}'
)
]
self.socket = await websockets.connect( # type: ignore
'wss://aternos.org/hermes/',
origin='https://aternos.org',
extra_headers=headers
)
@self.wssreceiver(Streams.status)
async def confirmfunc(msg: Dict[str, Any]) -> None:
"""Automatically confirm
Minecraft server launching
Args:
msg (Dict[str, Any]): Server info dictionary
"""
if not self.autoconfirm:
return
in_queue = msg['class'] == 'queueing'
pending = msg['queue']['pending'] == 'pending'
confirmation = in_queue and pending
if confirmation and not self.confirmed:
await self.confirm()
@self.wssreceiver(Streams.status)
async def streamsfunc(msg: Dict[str, Any]) -> None:
"""Automatically starts streams. Detailed description:
https://github.com/DarkCat09/python-aternos/issues/22#issuecomment-1146788496
According to the websocket messages from the web site,
Aternos can't receive any data from a stream (e.g. console) until
it requests this stream via the special message
to the websocket server: `{"stream":"console","type":"start"}`
on which the server responses with: `{"type":"connected"}`
Also, there are RAM (used heap) and TPS (ticks per second)
streams that must be enabled before trying to get information.
Enabling the stream for listening the server status is not needed,
these data is sent from API by default, so there's None value in
the second item of its stream type tuple
(`<Streams.status: (0, None)>`).
Args:
msg (Dict[str, Any]): Server info dictionary
"""
if msg['status'] == 2:
# Automatically start streams
for strm in self.recv:
if not isinstance(strm, Streams):
continue
# If the handlers list is empty
if not self.recv.get(strm):
continue
if strm.stream:
log.debug('Requesting %s stream', strm.stream)
await self.send({
'stream': strm.stream,
'type': 'start'
})
await self.wssworker()
async def close(self) -> None:
"""Closes websocket connection and stops all listeners"""
self.keep.cancel()
self.msgs.cancel()
await self.socket.close()
del self.socket
async def send(self, obj: Union[Dict[str, Any], str]) -> None:
"""Sends a message to websocket server
Args:
obj (Union[Dict[str, Any],str]):
Message, may be a string or a dict
"""
if self.socket is None:
log.warning('Did you forget to call socket.connect?')
await self.connect()
assert self.socket is not None
if isinstance(obj, dict):
obj = json.dumps(obj)
await self.socket.send(obj)
async def command(self, cmd: str) -> None:
"""Sends a Minecraft command
to the websocket server
Args:
cmd (str): Command, slash at
the beginning is not required
"""
await self.send(
{
'stream': 'console',
'type': 'command',
'data': cmd,
}
)
async def wssworker(self) -> None:
"""Starts async tasks in background
for receiving websocket messages
and sending keepalive ping"""
self.keep = asyncio.create_task(self.keepalive())
self.msgs = asyncio.create_task(self.receiver())
async def keepalive(self) -> None:
"""Each 49 seconds sends keepalive ping
to the websocket server"""
try:
while True:
await asyncio.sleep(49)
await self.socket.send('{"type":"\u2764"}')
except asyncio.CancelledError:
pass
async def receiver(self) -> None:
"""Receives messages from websocket servers
and calls user's streams listeners"""
while True:
try:
data = await self.socket.recv()
obj = json.loads(data)
msgtype = Streams.none
if obj['type'] == 'line':
msgtype = Streams.console
msg = obj['data'].strip('\r\n ')
elif obj['type'] == 'heap':
msgtype = Streams.ram
msg = int(obj['data']['usage'])
elif obj['type'] == 'tick':
msgtype = Streams.tps
ticks = 1000 / obj['data']['averageTickTime']
msg = 20 if ticks > 20 else ticks
elif obj['type'] == 'status':
msgtype = Streams.status
msg = json.loads(obj['message'])
if msgtype in self.recv:
# function info tuples
handlers: Iterable[ArgsTuple]
handlers = self.recv.get(msgtype, ())
for func in handlers:
# if arguments is not empty
if func[1]:
# call the function with args
coro = func[0](msg, func[1]) # type: ignore
else:
# mypy error: too few arguments
# looks like a bug, so it is ignored
coro = func[0](msg) # type: ignore
# run
asyncio.create_task(coro)
except asyncio.CancelledError:
break