Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rawsocket.py 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. ###############################################################################
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) typedef int GmbH
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE.
  24. #
  25. ###############################################################################
  26. import asyncio
  27. import struct
  28. import math
  29. import copy
  30. from typing import Optional
  31. import txaio
  32. from autobahn.util import public, _LazyHexFormatter, hltype
  33. from autobahn.wamp.exception import ProtocolError, SerializationError, TransportLost
  34. from autobahn.wamp.types import TransportDetails
  35. from autobahn.asyncio.util import get_serializers, create_transport_details, transport_channel_id
  36. __all__ = (
  37. 'WampRawSocketServerProtocol',
  38. 'WampRawSocketClientProtocol',
  39. 'WampRawSocketServerFactory',
  40. 'WampRawSocketClientFactory'
  41. )
  42. FRAME_TYPE_DATA = 0
  43. FRAME_TYPE_PING = 1
  44. FRAME_TYPE_PONG = 2
  45. MAGIC_BYTE = 0x7F
  46. class PrefixProtocol(asyncio.Protocol):
  47. prefix_format = '!L'
  48. prefix_length = struct.calcsize(prefix_format)
  49. max_length = 16 * 1024 * 1024
  50. max_length_send = max_length
  51. log = txaio.make_logger() # @UndefinedVariable
  52. peer: Optional[str] = None
  53. is_server: Optional[bool] = None
  54. @property
  55. def transport_details(self) -> Optional[TransportDetails]:
  56. """
  57. Implements :func:`autobahn.wamp.interfaces.ITransport.transport_details`
  58. """
  59. return self._transport_details
  60. def connection_made(self, transport):
  61. # asyncio networking framework entry point, called by asyncio
  62. # when the connection is established (either a client or a server)
  63. self.log.debug('RawSocker Asyncio: Connection made with peer {peer}', peer=self.peer)
  64. self.transport = transport
  65. # determine preliminary transport details (what is known at this point)
  66. self._transport_details = create_transport_details(self.transport, self.is_server)
  67. self._transport_details.channel_framing = TransportDetails.CHANNEL_FRAMING_RAWSOCKET
  68. # backward compatibility
  69. self.peer = self._transport_details.peer
  70. self._buffer = b''
  71. self._header = None
  72. self._wait_closed = txaio.create_future()
  73. @property
  74. def is_closed(self):
  75. if hasattr(self, '_wait_closed'):
  76. return self._wait_closed
  77. else:
  78. f = txaio.create_future()
  79. f.set_result(True)
  80. return f
  81. def connection_lost(self, exc):
  82. self.log.debug('RawSocker Asyncio: Connection lost')
  83. self.transport = None
  84. self._wait_closed.set_result(True)
  85. self._on_connection_lost(exc)
  86. def _on_connection_lost(self, exc):
  87. pass
  88. def protocol_error(self, msg):
  89. self.log.error(msg)
  90. self.transport.close()
  91. def sendString(self, data):
  92. l = len(data)
  93. if l > self.max_length_send:
  94. raise ValueError('Data too big')
  95. header = struct.pack(self.prefix_format, len(data))
  96. self.transport.write(header)
  97. self.transport.write(data)
  98. def ping(self, data):
  99. raise NotImplementedError()
  100. def pong(self, data):
  101. raise NotImplementedError()
  102. def data_received(self, data):
  103. self._buffer += data
  104. pos = 0
  105. remaining = len(self._buffer)
  106. while remaining >= self.prefix_length:
  107. # do not recalculate header if available from previous call
  108. if self._header:
  109. frame_type, frame_length = self._header
  110. else:
  111. header = self._buffer[pos:pos + self.prefix_length]
  112. frame_type = ord(header[0:1]) & 0b00000111
  113. if frame_type > FRAME_TYPE_PONG:
  114. self.protocol_error('Invalid frame type')
  115. return
  116. frame_length = struct.unpack(self.prefix_format, b'\0' + header[1:])[0]
  117. if frame_length > self.max_length:
  118. self.protocol_error('Frame too big')
  119. return
  120. if remaining - self.prefix_length >= frame_length:
  121. self._header = None
  122. pos += self.prefix_length
  123. remaining -= self.prefix_length
  124. data = self._buffer[pos:pos + frame_length]
  125. pos += frame_length
  126. remaining -= frame_length
  127. if frame_type == FRAME_TYPE_DATA:
  128. self.stringReceived(data)
  129. elif frame_type == FRAME_TYPE_PING:
  130. self.ping(data)
  131. elif frame_type == FRAME_TYPE_PONG:
  132. self.pong(data)
  133. else:
  134. # save heaader
  135. self._header = frame_type, frame_length
  136. break
  137. self._buffer = self._buffer[pos:]
  138. def stringReceived(self, data):
  139. raise NotImplementedError()
  140. class RawSocketProtocol(PrefixProtocol):
  141. def __init__(self):
  142. max_size = None
  143. if max_size:
  144. exp = int(math.ceil(math.log(max_size, 2))) - 9
  145. if exp > 15:
  146. raise ValueError('Maximum length is 16M')
  147. self.max_length = 2**(exp + 9)
  148. self._length_exp = exp
  149. else:
  150. self._length_exp = 15
  151. self.max_length = 2**24
  152. def connection_made(self, transport):
  153. PrefixProtocol.connection_made(self, transport)
  154. self._handshake_done = False
  155. def _on_handshake_complete(self):
  156. raise NotImplementedError()
  157. def parse_handshake(self):
  158. buf = bytearray(self._buffer[:4])
  159. if buf[0] != MAGIC_BYTE:
  160. raise HandshakeError('Invalid magic byte in handshake')
  161. ser = buf[1] & 0x0F
  162. lexp = buf[1] >> 4
  163. self.max_length_send = 2**(lexp + 9)
  164. if buf[2] != 0 or buf[3] != 0:
  165. raise HandshakeError('Reserved bytes must be zero')
  166. return ser, lexp
  167. def process_handshake(self):
  168. raise NotImplementedError()
  169. def data_received(self, data):
  170. self.log.debug('RawSocker Asyncio: data received {data}', data=_LazyHexFormatter(data))
  171. if self._handshake_done:
  172. return PrefixProtocol.data_received(self, data)
  173. else:
  174. self._buffer += data
  175. if len(self._buffer) >= 4:
  176. try:
  177. self.process_handshake()
  178. except HandshakeError as e:
  179. self.protocol_error('Handshake error : {err}'.format(err=e))
  180. return
  181. self._handshake_done = True
  182. self._on_handshake_complete()
  183. data = self._buffer[4:]
  184. self._buffer = b''
  185. if data:
  186. PrefixProtocol.data_received(self, data)
  187. ERR_SERIALIZER_UNSUPPORTED = 1
  188. ERRMAP = {
  189. 0: "illegal (must not be used)",
  190. 1: "serializer unsupported",
  191. 2: "maximum message length unacceptable",
  192. 3: "use of reserved bits (unsupported feature)",
  193. 4: "maximum connection count reached"
  194. }
  195. class HandshakeError(Exception):
  196. def __init__(self, msg, code=0):
  197. Exception.__init__(self, msg if not code else msg + ' : %s' % ERRMAP.get(code))
  198. class RawSocketClientProtocol(RawSocketProtocol):
  199. is_server = False
  200. def check_serializer(self, ser_id):
  201. return True
  202. def process_handshake(self):
  203. ser_id, err = self.parse_handshake()
  204. if ser_id == 0:
  205. raise HandshakeError('Server returned handshake error', err)
  206. if self.serializer_id != ser_id:
  207. raise HandshakeError('Server returned different serializer {0} then requested {1}'
  208. .format(ser_id, self.serializer_id))
  209. @property
  210. def serializer_id(self):
  211. raise NotImplementedError()
  212. def connection_made(self, transport):
  213. RawSocketProtocol.connection_made(self, transport)
  214. # start handshake
  215. hs = bytes(bytearray([MAGIC_BYTE,
  216. self._length_exp << 4 | self.serializer_id,
  217. 0, 0]))
  218. transport.write(hs)
  219. self.log.debug('RawSocket Asyncio: Client handshake sent')
  220. class RawSocketServerProtocol(RawSocketProtocol):
  221. is_server = True
  222. def supports_serializer(self, ser_id):
  223. raise NotImplementedError()
  224. def process_handshake(self):
  225. def send_response(lexp, ser_id):
  226. b2 = lexp << 4 | (ser_id & 0x0f)
  227. self.transport.write(bytes(bytearray([MAGIC_BYTE, b2, 0, 0])))
  228. ser_id, _lexp = self.parse_handshake()
  229. if not self.supports_serializer(ser_id):
  230. send_response(ERR_SERIALIZER_UNSUPPORTED, 0)
  231. raise HandshakeError('Serializer unsupported : {ser_id}'.format(ser_id=ser_id))
  232. send_response(self._length_exp, ser_id)
  233. # this is transport independent part of WAMP protocol
  234. class WampRawSocketMixinGeneral(object):
  235. def _on_handshake_complete(self):
  236. self.log.debug("WampRawSocketProtocol: Handshake complete")
  237. # RawSocket connection established. Now let the user WAMP session factory
  238. # create a new WAMP session and fire off session open callback.
  239. try:
  240. if self._transport_details.is_secure:
  241. # now that the TLS opening handshake is complete, the actual TLS channel ID
  242. # will be available. make sure to set it!
  243. channel_id = {
  244. 'tls-unique': transport_channel_id(self.transport, self._transport_details.is_server, 'tls-unique'),
  245. }
  246. self._transport_details.channel_id = channel_id
  247. self._session = self.factory._factory()
  248. self._session.onOpen(self)
  249. except Exception as e:
  250. # Exceptions raised in onOpen are fatal ..
  251. self.log.warn("WampRawSocketProtocol: ApplicationSession constructor / onOpen raised ({err})", err=e)
  252. self.abort()
  253. else:
  254. self.log.info("ApplicationSession started.")
  255. def stringReceived(self, payload):
  256. self.log.debug("WampRawSocketProtocol: RX octets: {octets}", octets=_LazyHexFormatter(payload))
  257. try:
  258. for msg in self._serializer.unserialize(payload):
  259. self.log.debug("WampRawSocketProtocol: RX WAMP message: {msg}", msg=msg)
  260. self._session.onMessage(msg)
  261. except ProtocolError as e:
  262. self.log.warn("WampRawSocketProtocol: WAMP Protocol Error ({err}) - aborting connection", err=e)
  263. self.abort()
  264. except Exception as e:
  265. self.log.warn("WampRawSocketProtocol: WAMP Internal Error ({err}) - aborting connection", err=e)
  266. self.abort()
  267. def send(self, msg):
  268. """
  269. Implements :func:`autobahn.wamp.interfaces.ITransport.send`
  270. """
  271. if self.isOpen():
  272. self.log.debug('{func}: TX WAMP message: {msg}', func=hltype(self.send), msg=msg)
  273. try:
  274. payload, _ = self._serializer.serialize(msg)
  275. except Exception as e:
  276. # all exceptions raised from above should be serialization errors ..
  277. raise SerializationError("WampRawSocketProtocol: unable to serialize WAMP application payload ({0})"
  278. .format(e))
  279. else:
  280. self.sendString(payload)
  281. self.log.debug("WampRawSocketProtocol: TX octets: {octets}", octets=_LazyHexFormatter(payload))
  282. else:
  283. raise TransportLost()
  284. def isOpen(self):
  285. """
  286. Implements :func:`autobahn.wamp.interfaces.ITransport.isOpen`
  287. """
  288. return hasattr(self, '_session') and self._session is not None
  289. # this is asyncio dependent part of WAMP protocol
  290. class WampRawSocketMixinAsyncio(object):
  291. """
  292. Base class for asyncio-based WAMP-over-RawSocket protocols.
  293. """
  294. def _on_connection_lost(self, exc):
  295. try:
  296. wasClean = exc is None
  297. self._session.onClose(wasClean)
  298. except Exception as e:
  299. # silently ignore exceptions raised here ..
  300. self.log.warn("WampRawSocketProtocol: ApplicationSession.onClose raised ({err})", err=e)
  301. self._session = None
  302. def close(self):
  303. """
  304. Implements :func:`autobahn.wamp.interfaces.ITransport.close`
  305. """
  306. if self.isOpen():
  307. self.transport.close()
  308. else:
  309. raise TransportLost()
  310. def abort(self):
  311. """
  312. Implements :func:`autobahn.wamp.interfaces.ITransport.abort`
  313. """
  314. if self.isOpen():
  315. if hasattr(self.transport, 'abort'):
  316. # ProcessProtocol lacks abortConnection()
  317. self.transport.abort()
  318. else:
  319. self.transport.close()
  320. else:
  321. raise TransportLost()
  322. @public
  323. class WampRawSocketServerProtocol(WampRawSocketMixinGeneral, WampRawSocketMixinAsyncio, RawSocketServerProtocol):
  324. """
  325. asyncio-based WAMP-over-RawSocket server protocol.
  326. Implements:
  327. * :class:`autobahn.wamp.interfaces.ITransport`
  328. """
  329. def supports_serializer(self, ser_id):
  330. if ser_id in self.factory._serializers:
  331. self._serializer = copy.copy(self.factory._serializers[ser_id])
  332. self.log.debug(
  333. "WampRawSocketProtocol: client wants to use serializer '{serializer}'",
  334. serializer=ser_id,
  335. )
  336. return True
  337. else:
  338. self.log.debug(
  339. "WampRawSocketProtocol: opening handshake - no suitable serializer found (client requested {serializer}, and we have {serializers}",
  340. serializer=ser_id,
  341. serializers=self.factory._serializers.keys(),
  342. )
  343. self.abort()
  344. return False
  345. @public
  346. class WampRawSocketClientProtocol(WampRawSocketMixinGeneral, WampRawSocketMixinAsyncio, RawSocketClientProtocol):
  347. """
  348. asyncio-based WAMP-over-RawSocket client protocol.
  349. Implements:
  350. * :class:`autobahn.wamp.interfaces.ITransport`
  351. """
  352. @property
  353. def serializer_id(self):
  354. if not hasattr(self, '_serializer'):
  355. self._serializer = copy.copy(self.factory._serializer)
  356. return self._serializer.RAWSOCKET_SERIALIZER_ID
  357. class WampRawSocketFactory(object):
  358. """
  359. Adapter class for asyncio-based WebSocket client and server factories.def dataReceived(self, data):
  360. """
  361. log = txaio.make_logger()
  362. @public
  363. def __call__(self):
  364. proto = self.protocol()
  365. proto.factory = self
  366. return proto
  367. @public
  368. class WampRawSocketServerFactory(WampRawSocketFactory):
  369. """
  370. asyncio-based WAMP-over-RawSocket server protocol factory.
  371. """
  372. protocol = WampRawSocketServerProtocol
  373. def __init__(self, factory, serializers=None):
  374. """
  375. :param factory: A callable that produces instances that implement
  376. :class:`autobahn.wamp.interfaces.ITransportHandler`
  377. :type factory: callable
  378. :param serializers: A list of WAMP serializers to use (or ``None``
  379. for all available serializers).
  380. :type serializers: list of objects implementing
  381. :class:`autobahn.wamp.interfaces.ISerializer`
  382. """
  383. if callable(factory):
  384. self._factory = factory
  385. else:
  386. self._factory = lambda: factory
  387. # when no serializers were requested specifically, then support
  388. # all that are available
  389. if serializers is None:
  390. serializers = [serializer() for serializer in get_serializers()]
  391. if not serializers:
  392. raise Exception("could not import any WAMP serializers")
  393. self._serializers = {ser.RAWSOCKET_SERIALIZER_ID: ser for ser in serializers}
  394. @public
  395. class WampRawSocketClientFactory(WampRawSocketFactory):
  396. """
  397. asyncio-based WAMP-over-RawSocket client factory.
  398. """
  399. protocol = WampRawSocketClientProtocol
  400. def __init__(self, factory, serializer=None):
  401. """
  402. :param factory: A callable that produces instances that implement
  403. :class:`autobahn.wamp.interfaces.ITransportHandler`
  404. :type factory: callable
  405. :param serializer: The WAMP serializer to use (or ``None`` for
  406. "best" serializer, chosen as the first serializer available from
  407. this list: CBOR, MessagePack, UBJSON, JSON).
  408. :type serializer: object implementing :class:`autobahn.wamp.interfaces.ISerializer`
  409. """
  410. if callable(factory):
  411. self._factory = factory
  412. else:
  413. self._factory = lambda: factory
  414. # when no serializer was requested specifically, use the first
  415. # one available
  416. if serializer is None:
  417. serializers = get_serializers()
  418. if serializers:
  419. serializer = serializers[0]()
  420. if serializer is None:
  421. raise Exception("could not import any WAMP serializer")
  422. self._serializer = serializer