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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. ###############################################################################
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) Crossbar.io Technologies 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. from __future__ import absolute_import
  27. try:
  28. import asyncio
  29. except ImportError:
  30. # trollious for py2 support - however it has been deprecated
  31. import trollius as asyncio
  32. import struct
  33. import math
  34. from autobahn.util import public, _LazyHexFormatter
  35. from autobahn.wamp.exception import ProtocolError, SerializationError, TransportLost
  36. from autobahn.asyncio.util import peer2str, get_serializers
  37. import txaio
  38. __all__ = (
  39. 'WampRawSocketServerProtocol',
  40. 'WampRawSocketClientProtocol',
  41. 'WampRawSocketServerFactory',
  42. 'WampRawSocketClientFactory'
  43. )
  44. txaio.use_asyncio()
  45. FRAME_TYPE_DATA = 0
  46. FRAME_TYPE_PING = 1
  47. FRAME_TYPE_PONG = 2
  48. MAGIC_BYTE = 0x7F
  49. class PrefixProtocol(asyncio.Protocol):
  50. prefix_format = '!L'
  51. prefix_length = struct.calcsize(prefix_format)
  52. max_length = 16 * 1024 * 1024
  53. max_length_send = max_length
  54. log = txaio.make_logger() # @UndefinedVariable
  55. def connection_made(self, transport):
  56. self.transport = transport
  57. peer = transport.get_extra_info('peername')
  58. self.peer = peer2str(peer)
  59. self.log.debug('RawSocker Asyncio: Connection made with peer {peer}', peer=self.peer)
  60. self._buffer = b''
  61. self._header = None
  62. self._wait_closed = txaio.create_future()
  63. @property
  64. def is_closed(self):
  65. if hasattr(self, '_wait_closed'):
  66. return self._wait_closed
  67. else:
  68. f = txaio.create_future()
  69. f.set_result(True)
  70. return f
  71. def connection_lost(self, exc):
  72. self.log.debug('RawSocker Asyncio: Connection lost')
  73. self.transport = None
  74. self._wait_closed.set_result(True)
  75. self._on_connection_lost(exc)
  76. def _on_connection_lost(self, exc):
  77. pass
  78. def protocol_error(self, msg):
  79. self.log.error(msg)
  80. self.transport.close()
  81. def sendString(self, data):
  82. l = len(data)
  83. if l > self.max_length_send:
  84. raise ValueError('Data too big')
  85. header = struct.pack(self.prefix_format, len(data))
  86. self.transport.write(header)
  87. self.transport.write(data)
  88. def ping(self, data):
  89. raise NotImplementedError()
  90. def pong(self, data):
  91. raise NotImplementedError()
  92. def data_received(self, data):
  93. self._buffer += data
  94. pos = 0
  95. remaining = len(self._buffer)
  96. while remaining >= self.prefix_length:
  97. # do not recalculate header if available from previous call
  98. if self._header:
  99. frame_type, frame_length = self._header
  100. else:
  101. header = self._buffer[pos:pos + self.prefix_length]
  102. frame_type = ord(header[0:1]) & 0b00000111
  103. if frame_type > FRAME_TYPE_PONG:
  104. self.protocol_error('Invalid frame type')
  105. return
  106. frame_length = struct.unpack(self.prefix_format, b'\0' + header[1:])[0]
  107. if frame_length > self.max_length:
  108. self.protocol_error('Frame too big')
  109. return
  110. if remaining - self.prefix_length >= frame_length:
  111. self._header = None
  112. pos += self.prefix_length
  113. remaining -= self.prefix_length
  114. data = self._buffer[pos:pos + frame_length]
  115. pos += frame_length
  116. remaining -= frame_length
  117. if frame_type == FRAME_TYPE_DATA:
  118. self.stringReceived(data)
  119. elif frame_type == FRAME_TYPE_PING:
  120. self.ping(data)
  121. elif frame_type == FRAME_TYPE_PONG:
  122. self.pong(data)
  123. else:
  124. # save heaader
  125. self._header = frame_type, frame_length
  126. break
  127. self._buffer = self._buffer[pos:]
  128. def stringReceived(self, data):
  129. raise NotImplementedError()
  130. class RawSocketProtocol(PrefixProtocol):
  131. def __init__(self):
  132. max_size = None
  133. if max_size:
  134. exp = int(math.ceil(math.log(max_size, 2))) - 9
  135. if exp > 15:
  136. raise ValueError('Maximum length is 16M')
  137. self.max_length = 2**(exp + 9)
  138. self._length_exp = exp
  139. else:
  140. self._length_exp = 15
  141. self.max_length = 2**24
  142. def connection_made(self, transport):
  143. PrefixProtocol.connection_made(self, transport)
  144. self._handshake_done = False
  145. def _on_handshake_complete(self):
  146. raise NotImplementedError()
  147. def parse_handshake(self):
  148. buf = bytearray(self._buffer[:4])
  149. if buf[0] != MAGIC_BYTE:
  150. raise HandshakeError('Invalid magic byte in handshake')
  151. return
  152. ser = buf[1] & 0x0F
  153. lexp = buf[1] >> 4
  154. self.max_length_send = 2**(lexp + 9)
  155. if buf[2] != 0 or buf[3] != 0:
  156. raise HandshakeError('Reserved bytes must be zero')
  157. return ser, lexp
  158. def process_handshake(self):
  159. raise NotImplementedError()
  160. def data_received(self, data):
  161. self.log.debug('RawSocker Asyncio: data received {data}', data=_LazyHexFormatter(data))
  162. if self._handshake_done:
  163. return PrefixProtocol.data_received(self, data)
  164. else:
  165. self._buffer += data
  166. if len(self._buffer) >= 4:
  167. try:
  168. self.process_handshake()
  169. except HandshakeError as e:
  170. self.protocol_error('Handshake error : {err}'.format(err=e))
  171. return
  172. self._handshake_done = True
  173. self._on_handshake_complete()
  174. data = self._buffer[4:]
  175. self._buffer = b''
  176. if data:
  177. PrefixProtocol.data_received(self, data)
  178. ERR_SERIALIZER_UNSUPPORTED = 1
  179. ERRMAP = {
  180. 0: "illegal (must not be used)",
  181. 1: "serializer unsupported",
  182. 2: "maximum message length unacceptable",
  183. 3: "use of reserved bits (unsupported feature)",
  184. 4: "maximum connection count reached"
  185. }
  186. class HandshakeError(Exception):
  187. def __init__(self, msg, code=0):
  188. Exception.__init__(self, msg if not code else msg + ' : %s' % ERRMAP.get(code))
  189. class RawSocketClientProtocol(RawSocketProtocol):
  190. def check_serializer(self, ser_id):
  191. return True
  192. def process_handshake(self):
  193. ser_id, err = self.parse_handshake()
  194. if ser_id == 0:
  195. raise HandshakeError('Server returned handshake error', err)
  196. if self.serializer_id != ser_id:
  197. raise HandshakeError('Server returned different serializer {0} then requested {1}'
  198. .format(ser_id, self.serializer_id))
  199. @property
  200. def serializer_id(self):
  201. raise NotImplementedError()
  202. def connection_made(self, transport):
  203. RawSocketProtocol.connection_made(self, transport)
  204. # start handshake
  205. hs = bytes(bytearray([MAGIC_BYTE,
  206. self._length_exp << 4 | self.serializer_id,
  207. 0, 0]))
  208. transport.write(hs)
  209. self.log.debug('RawSocket Asyncio: Client handshake sent')
  210. class RawSocketServerProtocol(RawSocketProtocol):
  211. def supports_serializer(self, ser_id):
  212. raise NotImplementedError()
  213. def process_handshake(self):
  214. def send_response(lexp, ser_id):
  215. b2 = lexp << 4 | (ser_id & 0x0f)
  216. self.transport.write(bytes(bytearray([MAGIC_BYTE, b2, 0, 0])))
  217. ser_id, _lexp = self.parse_handshake()
  218. if not self.supports_serializer(ser_id):
  219. send_response(ERR_SERIALIZER_UNSUPPORTED, 0)
  220. raise HandshakeError('Serializer unsupported : {ser_id}'.format(ser_id=ser_id))
  221. send_response(self._length_exp, ser_id)
  222. # this is transport independent part of WAMP protocol
  223. class WampRawSocketMixinGeneral(object):
  224. def _on_handshake_complete(self):
  225. self.log.debug("WampRawSocketProtocol: Handshake complete")
  226. try:
  227. self._session = self.factory._factory()
  228. self._session.onOpen(self)
  229. except Exception as e:
  230. # Exceptions raised in onOpen are fatal ..
  231. self.log.warn("WampRawSocketProtocol: ApplicationSession constructor / onOpen raised ({err})", err=e)
  232. self.abort()
  233. else:
  234. self.log.info("ApplicationSession started.")
  235. def stringReceived(self, payload):
  236. self.log.debug("WampRawSocketProtocol: RX octets: {octets}", octets=_LazyHexFormatter(payload))
  237. try:
  238. for msg in self._serializer.unserialize(payload):
  239. self.log.debug("WampRawSocketProtocol: RX WAMP message: {msg}", msg=msg)
  240. self._session.onMessage(msg)
  241. except ProtocolError as e:
  242. self.log.warn("WampRawSocketProtocol: WAMP Protocol Error ({err}) - aborting connection", err=e)
  243. self.abort()
  244. except Exception as e:
  245. self.log.warn("WampRawSocketProtocol: WAMP Internal Error ({err}) - aborting connection", err=e)
  246. self.abort()
  247. def send(self, msg):
  248. """
  249. Implements :func:`autobahn.wamp.interfaces.ITransport.send`
  250. """
  251. if self.isOpen():
  252. self.log.debug("WampRawSocketProtocol: TX WAMP message: {msg}", msg=msg)
  253. try:
  254. payload, _ = self._serializer.serialize(msg)
  255. except Exception as e:
  256. # all exceptions raised from above should be serialization errors ..
  257. raise SerializationError("WampRawSocketProtocol: unable to serialize WAMP application payload ({0})"
  258. .format(e))
  259. else:
  260. self.sendString(payload)
  261. self.log.debug("WampRawSocketProtocol: TX octets: {octets}", octets=_LazyHexFormatter(payload))
  262. else:
  263. raise TransportLost()
  264. def isOpen(self):
  265. """
  266. Implements :func:`autobahn.wamp.interfaces.ITransport.isOpen`
  267. """
  268. return hasattr(self, '_session') and self._session is not None
  269. # this is asyncio dependent part of WAMP protocol
  270. class WampRawSocketMixinAsyncio(object):
  271. """
  272. Base class for asyncio-based WAMP-over-RawSocket protocols.
  273. """
  274. def _on_connection_lost(self, exc):
  275. try:
  276. wasClean = exc is None
  277. self._session.onClose(wasClean)
  278. except Exception as e:
  279. # silently ignore exceptions raised here ..
  280. self.log.warn("WampRawSocketProtocol: ApplicationSession.onClose raised ({err})", err=e)
  281. self._session = None
  282. def close(self):
  283. """
  284. Implements :func:`autobahn.wamp.interfaces.ITransport.close`
  285. """
  286. if self.isOpen():
  287. self.transport.close()
  288. else:
  289. raise TransportLost()
  290. def abort(self):
  291. """
  292. Implements :func:`autobahn.wamp.interfaces.ITransport.abort`
  293. """
  294. if self.isOpen():
  295. if hasattr(self.transport, 'abort'):
  296. # ProcessProtocol lacks abortConnection()
  297. self.transport.abort()
  298. else:
  299. self.transport.close()
  300. else:
  301. raise TransportLost()
  302. @public
  303. class WampRawSocketServerProtocol(WampRawSocketMixinGeneral, WampRawSocketMixinAsyncio, RawSocketServerProtocol):
  304. """
  305. asyncio-based WAMP-over-RawSocket server protocol.
  306. Implements:
  307. * :class:`autobahn.wamp.interfaces.ITransport`
  308. """
  309. def supports_serializer(self, ser_id):
  310. if ser_id in self.factory._serializers:
  311. self._serializer = self.factory._serializers[ser_id]()
  312. self.log.debug(
  313. "WampRawSocketProtocol: client wants to use serializer '{serializer}'",
  314. serializer=ser_id,
  315. )
  316. return True
  317. else:
  318. self.log.debug(
  319. "WampRawSocketProtocol: opening handshake - no suitable serializer found (client requested {serializer}, and we have {serializers}",
  320. serializer=ser_id,
  321. serializers=self.factory._serializers.keys(),
  322. )
  323. self.abort()
  324. return False
  325. def get_channel_id(self, channel_id_type=u'tls-unique'):
  326. """
  327. Implements :func:`autobahn.wamp.interfaces.ITransport.get_channel_id`
  328. """
  329. return None
  330. # return transport_channel_id(self.transport, is_server=True, channel_id_type=channel_id_type)
  331. @public
  332. class WampRawSocketClientProtocol(WampRawSocketMixinGeneral, WampRawSocketMixinAsyncio, RawSocketClientProtocol):
  333. """
  334. asyncio-based WAMP-over-RawSocket client protocol.
  335. Implements:
  336. * :class:`autobahn.wamp.interfaces.ITransport`
  337. """
  338. @property
  339. def serializer_id(self):
  340. if not hasattr(self, '_serializer'):
  341. self._serializer = self.factory._serializer
  342. return self._serializer.RAWSOCKET_SERIALIZER_ID
  343. def get_channel_id(self, channel_id_type=u'tls-unique'):
  344. """
  345. Implements :func:`autobahn.wamp.interfaces.ITransport.get_channel_id`
  346. """
  347. return None
  348. # return transport_channel_id(self.transport, is_server=False, channel_id_type=channel_id_type)
  349. class WampRawSocketFactory(object):
  350. """
  351. Adapter class for asyncio-based WebSocket client and server factories.def dataReceived(self, data):
  352. """
  353. log = txaio.make_logger()
  354. @public
  355. def __call__(self):
  356. proto = self.protocol()
  357. proto.factory = self
  358. return proto
  359. @public
  360. class WampRawSocketServerFactory(WampRawSocketFactory):
  361. """
  362. asyncio-based WAMP-over-RawSocket server protocol factory.
  363. """
  364. protocol = WampRawSocketServerProtocol
  365. def __init__(self, factory, serializers=None):
  366. """
  367. :param factory: A callable that produces instances that implement
  368. :class:`autobahn.wamp.interfaces.ITransportHandler`
  369. :type factory: callable
  370. :param serializers: A list of WAMP serializers to use (or ``None``
  371. for all available serializers).
  372. :type serializers: list of objects implementing
  373. :class:`autobahn.wamp.interfaces.ISerializer`
  374. """
  375. if callable(factory):
  376. self._factory = factory
  377. else:
  378. self._factory = lambda: factory
  379. # when no serializers were requested specifically, then support
  380. # all that are available
  381. if serializers is None:
  382. serializers = get_serializers()
  383. if not serializers:
  384. raise Exception("could not import any WAMP serializers")
  385. self._serializers = {ser.RAWSOCKET_SERIALIZER_ID: ser for ser in serializers}
  386. @public
  387. class WampRawSocketClientFactory(WampRawSocketFactory):
  388. """
  389. asyncio-based WAMP-over-RawSocket client factory.
  390. """
  391. protocol = WampRawSocketClientProtocol
  392. def __init__(self, factory, serializer=None):
  393. """
  394. :param factory: A callable that produces instances that implement
  395. :class:`autobahn.wamp.interfaces.ITransportHandler`
  396. :type factory: callable
  397. :param serializer: The WAMP serializer to use (or ``None`` for
  398. "best" serializer, chosen as the first serializer available from
  399. this list: CBOR, MessagePack, UBJSON, JSON).
  400. :type serializer: object implementing :class:`autobahn.wamp.interfaces.ISerializer`
  401. """
  402. if callable(factory):
  403. self._factory = factory
  404. else:
  405. self._factory = lambda: factory
  406. # when no serializer was requested specifically, use the first
  407. # one available
  408. if serializer is None:
  409. serializers = get_serializers()
  410. if serializers:
  411. serializer = serializers[0]()
  412. if serializer is None:
  413. raise Exception("could not import any WAMP serializer")
  414. self._serializer = serializer