websocket.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. #!/usr/bin/env python
  2. '''
  3. Python WebSocket library with support for "wss://" encryption.
  4. Copyright 2011 Joel Martin
  5. Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3)
  6. Supports following protocol versions:
  7. - http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75
  8. - http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76
  9. - http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10
  10. You can make a cert/key with openssl using:
  11. openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem
  12. as taken from http://docs.python.org/dev/library/ssl.html#certificates
  13. '''
  14. import os, sys, time, errno, signal, socket, traceback, select
  15. import array, struct
  16. from cgi import parse_qsl
  17. from base64 import b64encode, b64decode
  18. # Imports that vary by python version
  19. # python 3.0 differences
  20. if sys.hexversion > 0x3000000:
  21. b2s = lambda buf: buf.decode('latin_1')
  22. s2b = lambda s: s.encode('latin_1')
  23. s2a = lambda s: s
  24. else:
  25. b2s = lambda buf: buf # No-op
  26. s2b = lambda s: s # No-op
  27. s2a = lambda s: [ord(c) for c in s]
  28. try: from io import StringIO
  29. except: from cStringIO import StringIO
  30. try: from http.server import SimpleHTTPRequestHandler
  31. except: from SimpleHTTPServer import SimpleHTTPRequestHandler
  32. try: from urllib.parse import urlsplit
  33. except: from urlparse import urlsplit
  34. # python 2.6 differences
  35. try: from hashlib import md5, sha1
  36. except: from md5 import md5; from sha import sha as sha1
  37. # python 2.5 differences
  38. try:
  39. from struct import pack, unpack_from
  40. except:
  41. from struct import pack
  42. def unpack_from(fmt, buf, offset=0):
  43. slice = buffer(buf, offset, struct.calcsize(fmt))
  44. return struct.unpack(fmt, slice)
  45. # Degraded functionality if these imports are missing
  46. for mod, sup in [('numpy', 'HyBi protocol'), ('ssl', 'TLS/SSL/wss'),
  47. ('multiprocessing', 'Multi-Processing'),
  48. ('resource', 'daemonizing')]:
  49. try:
  50. globals()[mod] = __import__(mod)
  51. except ImportError:
  52. globals()[mod] = None
  53. print("WARNING: no '%s' module, %s is slower or disabled" % (
  54. mod, sup))
  55. if multiprocessing and sys.platform == 'win32':
  56. # make sockets pickle-able/inheritable
  57. import multiprocessing.reduction
  58. class WebSocketServer(object):
  59. """
  60. WebSockets server class.
  61. Must be sub-classed with new_client method definition.
  62. """
  63. buffer_size = 65536
  64. server_handshake_hixie = """HTTP/1.1 101 Web Socket Protocol Handshake\r
  65. Upgrade: WebSocket\r
  66. Connection: Upgrade\r
  67. %sWebSocket-Origin: %s\r
  68. %sWebSocket-Location: %s://%s%s\r
  69. """
  70. server_handshake_hybi = """HTTP/1.1 101 Switching Protocols\r
  71. Upgrade: websocket\r
  72. Connection: Upgrade\r
  73. Sec-WebSocket-Accept: %s\r
  74. """
  75. GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
  76. policy_response = """<cross-domain-policy><allow-access-from domain="*" to-ports="*" /></cross-domain-policy>\n"""
  77. # An exception before the WebSocket connection was established
  78. class EClose(Exception):
  79. pass
  80. # An exception while the WebSocket client was connected
  81. class CClose(Exception):
  82. pass
  83. def __init__(self, listen_host='', listen_port=None, source_is_ipv6=False,
  84. verbose=False, cert='', key='', ssl_only=None,
  85. daemon=False, record='', web='',
  86. run_once=False, timeout=0):
  87. # settings
  88. self.verbose = verbose
  89. self.listen_host = listen_host
  90. self.listen_port = listen_port
  91. self.ssl_only = ssl_only
  92. self.daemon = daemon
  93. self.run_once = run_once
  94. self.timeout = timeout
  95. self.launch_time = time.time()
  96. self.ws_connection = False
  97. self.handler_id = 1
  98. # Make paths settings absolute
  99. self.cert = os.path.abspath(cert)
  100. self.key = self.web = self.record = ''
  101. if key:
  102. self.key = os.path.abspath(key)
  103. if web:
  104. self.web = os.path.abspath(web)
  105. if record:
  106. self.record = os.path.abspath(record)
  107. if self.web:
  108. os.chdir(self.web)
  109. # Sanity checks
  110. if not ssl and self.ssl_only:
  111. raise Exception("No 'ssl' module and SSL-only specified")
  112. if self.daemon and not resource:
  113. raise Exception("Module 'resource' required to daemonize")
  114. # Show configuration
  115. print("WebSocket server settings:")
  116. print(" - Listen on %s:%s" % (
  117. self.listen_host, self.listen_port))
  118. print(" - Flash security policy server")
  119. if self.web:
  120. print(" - Web server. Web root: %s" % self.web)
  121. if ssl:
  122. if os.path.exists(self.cert):
  123. print(" - SSL/TLS support")
  124. if self.ssl_only:
  125. print(" - Deny non-SSL/TLS connections")
  126. else:
  127. print(" - No SSL/TLS support (no cert file)")
  128. else:
  129. print(" - No SSL/TLS support (no 'ssl' module)")
  130. if self.daemon:
  131. print(" - Backgrounding (daemon)")
  132. if self.record:
  133. print(" - Recording to '%s.*'" % self.record)
  134. #
  135. # WebSocketServer static methods
  136. #
  137. @staticmethod
  138. def socket(host, port=None, connect=False, prefer_ipv6=False):
  139. """ Resolve a host (and optional port) to an IPv4 or IPv6
  140. address. Create a socket. Bind to it if listen is set,
  141. otherwise connect to it. Return the socket.
  142. """
  143. flags = 0
  144. if host == '':
  145. host = None
  146. if connect and not port:
  147. raise Exception("Connect mode requires a port")
  148. if not connect:
  149. flags = flags | socket.AI_PASSIVE
  150. addrs = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM,
  151. socket.IPPROTO_TCP, flags)
  152. if not addrs:
  153. raise Exception("Could resolve host '%s'" % host)
  154. addrs.sort(key=lambda x: x[0])
  155. if prefer_ipv6:
  156. addrs.reverse()
  157. sock = socket.socket(addrs[0][0], addrs[0][1])
  158. if connect:
  159. sock.connect(addrs[0][4])
  160. else:
  161. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  162. sock.bind(addrs[0][4])
  163. sock.listen(100)
  164. return sock
  165. @staticmethod
  166. def daemonize(keepfd=None, chdir='/'):
  167. os.umask(0)
  168. if chdir:
  169. os.chdir(chdir)
  170. else:
  171. os.chdir('/')
  172. os.setgid(os.getgid()) # relinquish elevations
  173. os.setuid(os.getuid()) # relinquish elevations
  174. # Double fork to daemonize
  175. if os.fork() > 0: os._exit(0) # Parent exits
  176. os.setsid() # Obtain new process group
  177. if os.fork() > 0: os._exit(0) # Parent exits
  178. # Signal handling
  179. def terminate(a,b): os._exit(0)
  180. signal.signal(signal.SIGTERM, terminate)
  181. signal.signal(signal.SIGINT, signal.SIG_IGN)
  182. # Close open files
  183. maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
  184. if maxfd == resource.RLIM_INFINITY: maxfd = 256
  185. for fd in reversed(range(maxfd)):
  186. try:
  187. if fd != keepfd:
  188. os.close(fd)
  189. except OSError:
  190. _, exc, _ = sys.exc_info()
  191. if exc.errno != errno.EBADF: raise
  192. # Redirect I/O to /dev/null
  193. os.dup2(os.open(os.devnull, os.O_RDWR), sys.stdin.fileno())
  194. os.dup2(os.open(os.devnull, os.O_RDWR), sys.stdout.fileno())
  195. os.dup2(os.open(os.devnull, os.O_RDWR), sys.stderr.fileno())
  196. @staticmethod
  197. def unmask(buf, f):
  198. pstart = f['hlen'] + 4
  199. pend = pstart + f['length']
  200. if numpy:
  201. b = c = s2b('')
  202. if f['length'] >= 4:
  203. mask = numpy.frombuffer(buf, dtype=numpy.dtype('<u4'),
  204. offset=f['hlen'], count=1)
  205. data = numpy.frombuffer(buf, dtype=numpy.dtype('<u4'),
  206. offset=pstart, count=int(f['length'] / 4))
  207. #b = numpy.bitwise_xor(data, mask).data
  208. b = numpy.bitwise_xor(data, mask).tostring()
  209. if f['length'] % 4:
  210. #print("Partial unmask")
  211. mask = numpy.frombuffer(buf, dtype=numpy.dtype('B'),
  212. offset=f['hlen'], count=(f['length'] % 4))
  213. data = numpy.frombuffer(buf, dtype=numpy.dtype('B'),
  214. offset=pend - (f['length'] % 4),
  215. count=(f['length'] % 4))
  216. c = numpy.bitwise_xor(data, mask).tostring()
  217. return b + c
  218. else:
  219. # Slower fallback
  220. data = array.array('B')
  221. mask = s2a(f['mask'])
  222. data.fromstring(buf[pstart:pend])
  223. for i in range(len(data)):
  224. data[i] ^= mask[i % 4]
  225. return data.tostring()
  226. @staticmethod
  227. def encode_hybi(buf, opcode, base64=False):
  228. """ Encode a HyBi style WebSocket frame.
  229. Optional opcode:
  230. 0x0 - continuation
  231. 0x1 - text frame (base64 encode buf)
  232. 0x2 - binary frame (use raw buf)
  233. 0x8 - connection close
  234. 0x9 - ping
  235. 0xA - pong
  236. """
  237. if base64:
  238. buf = b64encode(buf)
  239. b1 = 0x80 | (opcode & 0x0f) # FIN + opcode
  240. payload_len = len(buf)
  241. if payload_len <= 125:
  242. header = pack('>BB', b1, payload_len)
  243. elif payload_len > 125 and payload_len < 65536:
  244. header = pack('>BBH', b1, 126, payload_len)
  245. elif payload_len >= 65536:
  246. header = pack('>BBQ', b1, 127, payload_len)
  247. #print("Encoded: %s" % repr(header + buf))
  248. return header + buf, len(header), 0
  249. @staticmethod
  250. def decode_hybi(buf, base64=False):
  251. """ Decode HyBi style WebSocket packets.
  252. Returns:
  253. {'fin' : 0_or_1,
  254. 'opcode' : number,
  255. 'mask' : 32_bit_number,
  256. 'hlen' : header_bytes_number,
  257. 'length' : payload_bytes_number,
  258. 'payload' : decoded_buffer,
  259. 'left' : bytes_left_number,
  260. 'close_code' : number,
  261. 'close_reason' : string}
  262. """
  263. f = {'fin' : 0,
  264. 'opcode' : 0,
  265. 'mask' : 0,
  266. 'hlen' : 2,
  267. 'length' : 0,
  268. 'payload' : None,
  269. 'left' : 0,
  270. 'close_code' : 1000,
  271. 'close_reason' : ''}
  272. blen = len(buf)
  273. f['left'] = blen
  274. if blen < f['hlen']:
  275. return f # Incomplete frame header
  276. b1, b2 = unpack_from(">BB", buf)
  277. f['opcode'] = b1 & 0x0f
  278. f['fin'] = (b1 & 0x80) >> 7
  279. has_mask = (b2 & 0x80) >> 7
  280. f['length'] = b2 & 0x7f
  281. if f['length'] == 126:
  282. f['hlen'] = 4
  283. if blen < f['hlen']:
  284. return f # Incomplete frame header
  285. (f['length'],) = unpack_from('>xxH', buf)
  286. elif f['length'] == 127:
  287. f['hlen'] = 10
  288. if blen < f['hlen']:
  289. return f # Incomplete frame header
  290. (f['length'],) = unpack_from('>xxQ', buf)
  291. full_len = f['hlen'] + has_mask * 4 + f['length']
  292. if blen < full_len: # Incomplete frame
  293. return f # Incomplete frame header
  294. # Number of bytes that are part of the next frame(s)
  295. f['left'] = blen - full_len
  296. # Process 1 frame
  297. if has_mask:
  298. # unmask payload
  299. f['mask'] = buf[f['hlen']:f['hlen']+4]
  300. f['payload'] = WebSocketServer.unmask(buf, f)
  301. else:
  302. print("Unmasked frame: %s" % repr(buf))
  303. f['payload'] = buf[(f['hlen'] + has_mask * 4):full_len]
  304. if base64 and f['opcode'] in [1, 2]:
  305. try:
  306. f['payload'] = b64decode(f['payload'])
  307. except:
  308. print("Exception while b64decoding buffer: %s" %
  309. repr(buf))
  310. raise
  311. if f['opcode'] == 0x08:
  312. if f['length'] >= 2:
  313. f['close_code'] = unpack_from(">H", f['payload'])[0]
  314. if f['length'] > 3:
  315. f['close_reason'] = f['payload'][2:]
  316. return f
  317. @staticmethod
  318. def encode_hixie(buf):
  319. return s2b("\x00" + b2s(b64encode(buf)) + "\xff"), 1, 1
  320. @staticmethod
  321. def decode_hixie(buf):
  322. end = buf.find(s2b('\xff'))
  323. return {'payload': b64decode(buf[1:end]),
  324. 'hlen': 1,
  325. 'length': end - 1,
  326. 'left': len(buf) - (end + 1)}
  327. @staticmethod
  328. def gen_md5(keys):
  329. """ Generate hash value for WebSockets hixie-76. """
  330. key1 = keys['Sec-WebSocket-Key1']
  331. key2 = keys['Sec-WebSocket-Key2']
  332. key3 = keys['key3']
  333. spaces1 = key1.count(" ")
  334. spaces2 = key2.count(" ")
  335. num1 = int("".join([c for c in key1 if c.isdigit()])) / spaces1
  336. num2 = int("".join([c for c in key2 if c.isdigit()])) / spaces2
  337. return b2s(md5(pack('>II8s',
  338. int(num1), int(num2), key3)).digest())
  339. #
  340. # WebSocketServer logging/output functions
  341. #
  342. def traffic(self, token="."):
  343. """ Show traffic flow in verbose mode. """
  344. if self.verbose and not self.daemon:
  345. sys.stdout.write(token)
  346. sys.stdout.flush()
  347. def msg(self, msg):
  348. """ Output message with handler_id prefix. """
  349. if not self.daemon:
  350. print("% 3d: %s" % (self.handler_id, msg))
  351. def vmsg(self, msg):
  352. """ Same as msg() but only if verbose. """
  353. if self.verbose:
  354. self.msg(msg)
  355. #
  356. # Main WebSocketServer methods
  357. #
  358. def send_frames(self, bufs=None):
  359. """ Encode and send WebSocket frames. Any frames already
  360. queued will be sent first. If buf is not set then only queued
  361. frames will be sent. Returns the number of pending frames that
  362. could not be fully sent. If returned pending frames is greater
  363. than 0, then the caller should call again when the socket is
  364. ready. """
  365. tdelta = int(time.time()*1000) - self.start_time
  366. if bufs:
  367. for buf in bufs:
  368. if self.version.startswith("hybi"):
  369. if self.base64:
  370. encbuf, lenhead, lentail = self.encode_hybi(
  371. buf, opcode=1, base64=True)
  372. else:
  373. encbuf, lenhead, lentail = self.encode_hybi(
  374. buf, opcode=2, base64=False)
  375. else:
  376. encbuf, lenhead, lentail = self.encode_hixie(buf)
  377. if self.rec:
  378. self.rec.write("%s,\n" %
  379. repr("{%s{" % tdelta
  380. + encbuf[lenhead:-lentail]))
  381. self.send_parts.append(encbuf)
  382. while self.send_parts:
  383. # Send pending frames
  384. buf = self.send_parts.pop(0)
  385. sent = self.client.send(buf)
  386. if sent == len(buf):
  387. self.traffic("<")
  388. else:
  389. self.traffic("<.")
  390. self.send_parts.insert(0, buf[sent:])
  391. break
  392. return len(self.send_parts)
  393. def recv_frames(self):
  394. """ Receive and decode WebSocket frames.
  395. Returns:
  396. (bufs_list, closed_string)
  397. """
  398. closed = False
  399. bufs = []
  400. tdelta = int(time.time()*1000) - self.start_time
  401. buf = self.client.recv(self.buffer_size)
  402. if len(buf) == 0:
  403. closed = {'code': 1000, 'reason': "Client closed abruptly"}
  404. return bufs, closed
  405. if self.recv_part:
  406. # Add partially received frames to current read buffer
  407. buf = self.recv_part + buf
  408. self.recv_part = None
  409. while buf:
  410. if self.version.startswith("hybi"):
  411. frame = self.decode_hybi(buf, base64=self.base64)
  412. #print("Received buf: %s, frame: %s" % (repr(buf), frame))
  413. if frame['payload'] == None:
  414. # Incomplete/partial frame
  415. self.traffic("}.")
  416. if frame['left'] > 0:
  417. self.recv_part = buf[-frame['left']:]
  418. break
  419. else:
  420. if frame['opcode'] == 0x8: # connection close
  421. closed = {'code': frame['close_code'],
  422. 'reason': frame['close_reason']}
  423. break
  424. else:
  425. if buf[0:2] == s2b('\xff\x00'):
  426. closed = {'code': 1000,
  427. 'reason': "Client sent orderly close frame"}
  428. break
  429. elif buf[0:2] == s2b('\x00\xff'):
  430. buf = buf[2:]
  431. continue # No-op
  432. elif buf.count(s2b('\xff')) == 0:
  433. # Partial frame
  434. self.traffic("}.")
  435. self.recv_part = buf
  436. break
  437. frame = self.decode_hixie(buf)
  438. self.traffic("}")
  439. if self.rec:
  440. start = frame['hlen']
  441. end = frame['hlen'] + frame['length']
  442. self.rec.write("%s,\n" %
  443. repr("}%s}" % tdelta + buf[start:end]))
  444. bufs.append(frame['payload'])
  445. if frame['left']:
  446. buf = buf[-frame['left']:]
  447. else:
  448. buf = ''
  449. return bufs, closed
  450. def send_close(self, code=1000, reason=''):
  451. """ Send a WebSocket orderly close frame. """
  452. if self.version.startswith("hybi"):
  453. msg = pack(">H%ds" % len(reason), code, reason)
  454. buf, h, t = self.encode_hybi(msg, opcode=0x08, base64=False)
  455. self.client.send(buf)
  456. elif self.version == "hixie-76":
  457. buf = s2b('\xff\x00')
  458. self.client.send(buf)
  459. # No orderly close for 75
  460. def do_handshake(self, sock, address):
  461. """
  462. do_handshake does the following:
  463. - Peek at the first few bytes from the socket.
  464. - If the connection is Flash policy request then answer it,
  465. close the socket and return.
  466. - If the connection is an HTTPS/SSL/TLS connection then SSL
  467. wrap the socket.
  468. - Read from the (possibly wrapped) socket.
  469. - If we have received a HTTP GET request and the webserver
  470. functionality is enabled, answer it, close the socket and
  471. return.
  472. - Assume we have a WebSockets connection, parse the client
  473. handshake data.
  474. - Send a WebSockets handshake server response.
  475. - Return the socket for this WebSocket client.
  476. """
  477. stype = ""
  478. ready = select.select([sock], [], [], 3)[0]
  479. if not ready:
  480. raise self.EClose("ignoring socket not ready")
  481. # Peek, but do not read the data so that we have a opportunity
  482. # to SSL wrap the socket first
  483. handshake = sock.recv(1024, socket.MSG_PEEK)
  484. #self.msg("Handshake [%s]" % handshake)
  485. if handshake == "":
  486. raise self.EClose("ignoring empty handshake")
  487. elif handshake.startswith(s2b("<policy-file-request/>")):
  488. # Answer Flash policy request
  489. handshake = sock.recv(1024)
  490. sock.send(s2b(self.policy_response))
  491. raise self.EClose("Sending flash policy response")
  492. elif handshake[0] in ("\x16", "\x80", 22, 128):
  493. # SSL wrap the connection
  494. if not ssl:
  495. raise self.EClose("SSL connection but no 'ssl' module")
  496. if not os.path.exists(self.cert):
  497. raise self.EClose("SSL connection but '%s' not found"
  498. % self.cert)
  499. retsock = None
  500. try:
  501. retsock = ssl.wrap_socket(
  502. sock,
  503. server_side=True,
  504. certfile=self.cert,
  505. keyfile=self.key)
  506. except ssl.SSLError:
  507. _, x, _ = sys.exc_info()
  508. if x.args[0] == ssl.SSL_ERROR_EOF:
  509. if len(x.args) > 1:
  510. raise self.EClose(x.args[1])
  511. else:
  512. raise self.EClose("Got SSL_ERROR_EOF")
  513. else:
  514. raise
  515. scheme = "wss"
  516. stype = "SSL/TLS (wss://)"
  517. elif self.ssl_only:
  518. raise self.EClose("non-SSL connection received but disallowed")
  519. else:
  520. retsock = sock
  521. scheme = "ws"
  522. stype = "Plain non-SSL (ws://)"
  523. wsh = WSRequestHandler(retsock, address, not self.web)
  524. if wsh.last_code == 101:
  525. # Continue on to handle WebSocket upgrade
  526. pass
  527. elif wsh.last_code == 405:
  528. raise self.EClose("Normal web request received but disallowed")
  529. elif wsh.last_code < 200 or wsh.last_code >= 300:
  530. raise self.EClose(wsh.last_message)
  531. elif self.verbose:
  532. raise self.EClose(wsh.last_message)
  533. else:
  534. raise self.EClose("")
  535. h = self.headers = wsh.headers
  536. path = self.path = wsh.path
  537. prot = 'WebSocket-Protocol'
  538. protocols = h.get('Sec-'+prot, h.get(prot, '')).split(',')
  539. ver = h.get('Sec-WebSocket-Version')
  540. if ver:
  541. # HyBi/IETF version of the protocol
  542. # HyBi-07 report version 7
  543. # HyBi-08 - HyBi-12 report version 8
  544. # HyBi-13 reports version 13
  545. if ver in ['7', '8', '13']:
  546. self.version = "hybi-%02d" % int(ver)
  547. else:
  548. raise self.EClose('Unsupported protocol version %s' % ver)
  549. key = h['Sec-WebSocket-Key']
  550. # Choose binary if client supports it
  551. if 'binary' in protocols:
  552. self.base64 = False
  553. elif 'base64' in protocols:
  554. self.base64 = True
  555. else:
  556. raise self.EClose("Client must support 'binary' or 'base64' protocol")
  557. # Generate the hash value for the accept header
  558. accept = b64encode(sha1(s2b(key + self.GUID)).digest())
  559. response = self.server_handshake_hybi % b2s(accept)
  560. if self.base64:
  561. response += "Sec-WebSocket-Protocol: base64\r\n"
  562. else:
  563. response += "Sec-WebSocket-Protocol: binary\r\n"
  564. response += "\r\n"
  565. else:
  566. # Hixie version of the protocol (75 or 76)
  567. if h.get('key3'):
  568. trailer = self.gen_md5(h)
  569. pre = "Sec-"
  570. self.version = "hixie-76"
  571. else:
  572. trailer = ""
  573. pre = ""
  574. self.version = "hixie-75"
  575. # We only support base64 in Hixie era
  576. self.base64 = True
  577. response = self.server_handshake_hixie % (pre,
  578. h['Origin'], pre, scheme, h['Host'], path)
  579. if 'base64' in protocols:
  580. response += "%sWebSocket-Protocol: base64\r\n" % pre
  581. else:
  582. self.msg("Warning: client does not report 'base64' protocol support")
  583. response += "\r\n" + trailer
  584. self.msg("%s: %s WebSocket connection" % (address[0], stype))
  585. self.msg("%s: Version %s, base64: '%s'" % (address[0],
  586. self.version, self.base64))
  587. if self.path != '/':
  588. self.msg("%s: Path: '%s'" % (address[0], self.path))
  589. # Send server WebSockets handshake response
  590. #self.msg("sending response [%s]" % response)
  591. retsock.send(s2b(response))
  592. # Return the WebSockets socket which may be SSL wrapped
  593. return retsock
  594. #
  595. # Events that can/should be overridden in sub-classes
  596. #
  597. def started(self):
  598. """ Called after WebSockets startup """
  599. self.vmsg("WebSockets server started")
  600. def poll(self):
  601. """ Run periodically while waiting for connections. """
  602. #self.vmsg("Running poll()")
  603. pass
  604. def fallback_SIGCHLD(self, sig, stack):
  605. # Reap zombies when using os.fork() (python 2.4)
  606. self.vmsg("Got SIGCHLD, reaping zombies")
  607. try:
  608. result = os.waitpid(-1, os.WNOHANG)
  609. while result[0]:
  610. self.vmsg("Reaped child process %s" % result[0])
  611. result = os.waitpid(-1, os.WNOHANG)
  612. except (OSError):
  613. pass
  614. def do_SIGINT(self, sig, stack):
  615. self.msg("Got SIGINT, exiting")
  616. sys.exit(0)
  617. def top_new_client(self, startsock, address):
  618. """ Do something with a WebSockets client connection. """
  619. # Initialize per client settings
  620. self.send_parts = []
  621. self.recv_part = None
  622. self.base64 = False
  623. self.rec = None
  624. self.start_time = int(time.time()*1000)
  625. # handler process
  626. try:
  627. try:
  628. self.client = self.do_handshake(startsock, address)
  629. if self.record:
  630. # Record raw frame data as JavaScript array
  631. fname = "%s.%s" % (self.record,
  632. self.handler_id)
  633. self.msg("opening record file: %s" % fname)
  634. self.rec = open(fname, 'w+')
  635. self.rec.write("var VNC_frame_data = [\n")
  636. self.ws_connection = True
  637. self.new_client()
  638. except self.CClose:
  639. # Close the client
  640. _, exc, _ = sys.exc_info()
  641. if self.client:
  642. self.send_close(exc.args[0], exc.args[1])
  643. except self.EClose:
  644. _, exc, _ = sys.exc_info()
  645. # Connection was not a WebSockets connection
  646. if exc.args[0]:
  647. self.msg("%s: %s" % (address[0], exc.args[0]))
  648. except Exception:
  649. _, exc, _ = sys.exc_info()
  650. self.msg("handler exception: %s" % str(exc))
  651. if self.verbose:
  652. self.msg(traceback.format_exc())
  653. finally:
  654. if self.rec:
  655. self.rec.write("'EOF']\n")
  656. self.rec.close()
  657. if self.client and self.client != startsock:
  658. # Close the SSL wrapped socket
  659. # Original socket closed by caller
  660. self.client.close()
  661. def new_client(self):
  662. """ Do something with a WebSockets client connection. """
  663. raise("WebSocketServer.new_client() must be overloaded")
  664. def start_server(self):
  665. """
  666. Daemonize if requested. Listen for for connections. Run
  667. do_handshake() method for each connection. If the connection
  668. is a WebSockets client then call new_client() method (which must
  669. be overridden) for each new client connection.
  670. """
  671. lsock = self.socket(self.listen_host, self.listen_port)
  672. if self.daemon:
  673. self.daemonize(keepfd=lsock.fileno(), chdir=self.web)
  674. self.started() # Some things need to happen after daemonizing
  675. # Allow override of SIGINT
  676. signal.signal(signal.SIGINT, self.do_SIGINT)
  677. if not multiprocessing:
  678. # os.fork() (python 2.4) child reaper
  679. signal.signal(signal.SIGCHLD, self.fallback_SIGCHLD)
  680. while True:
  681. try:
  682. try:
  683. self.client = None
  684. startsock = None
  685. pid = err = 0
  686. time_elapsed = time.time() - self.launch_time
  687. if self.timeout and time_elapsed > self.timeout:
  688. self.msg('listener exit due to --timeout %s'
  689. % self.timeout)
  690. break
  691. try:
  692. self.poll()
  693. ready = select.select([lsock], [], [], 1)[0]
  694. if lsock in ready:
  695. startsock, address = lsock.accept()
  696. else:
  697. continue
  698. except Exception:
  699. _, exc, _ = sys.exc_info()
  700. if hasattr(exc, 'errno'):
  701. err = exc.errno
  702. elif hasattr(exc, 'args'):
  703. err = exc.args[0]
  704. else:
  705. err = exc[0]
  706. if err == errno.EINTR:
  707. self.vmsg("Ignoring interrupted syscall")
  708. continue
  709. else:
  710. raise
  711. if self.run_once:
  712. # Run in same process if run_once
  713. self.top_new_client(startsock, address)
  714. if self.ws_connection :
  715. self.msg('%s: exiting due to --run-once'
  716. % address[0])
  717. break
  718. elif multiprocessing:
  719. self.vmsg('%s: new handler Process' % address[0])
  720. p = multiprocessing.Process(
  721. target=self.top_new_client,
  722. args=(startsock, address))
  723. p.start()
  724. # child will not return
  725. else:
  726. # python 2.4
  727. self.vmsg('%s: forking handler' % address[0])
  728. pid = os.fork()
  729. if pid == 0:
  730. # child handler process
  731. self.top_new_client(startsock, address)
  732. break # child process exits
  733. # parent process
  734. self.handler_id += 1
  735. except KeyboardInterrupt:
  736. _, exc, _ = sys.exc_info()
  737. print("In KeyboardInterrupt")
  738. pass
  739. except SystemExit:
  740. _, exc, _ = sys.exc_info()
  741. print("In SystemExit")
  742. break
  743. except Exception:
  744. _, exc, _ = sys.exc_info()
  745. self.msg("handler exception: %s" % str(exc))
  746. if self.verbose:
  747. self.msg(traceback.format_exc())
  748. finally:
  749. if startsock:
  750. startsock.close()
  751. # HTTP handler with WebSocket upgrade support
  752. class WSRequestHandler(SimpleHTTPRequestHandler):
  753. def __init__(self, req, addr, only_upgrade=False):
  754. self.only_upgrade = only_upgrade # only allow upgrades
  755. SimpleHTTPRequestHandler.__init__(self, req, addr, object())
  756. def do_GET(self):
  757. if (self.headers.get('upgrade') and
  758. self.headers.get('upgrade').lower() == 'websocket'):
  759. if (self.headers.get('sec-websocket-key1') or
  760. self.headers.get('websocket-key1')):
  761. # For Hixie-76 read out the key hash
  762. self.headers.__setitem__('key3', self.rfile.read(8))
  763. # Just indicate that an WebSocket upgrade is needed
  764. self.last_code = 101
  765. self.last_message = "101 Switching Protocols"
  766. elif self.only_upgrade:
  767. # Normal web request responses are disabled
  768. self.last_code = 405
  769. self.last_message = "405 Method Not Allowed"
  770. else:
  771. SimpleHTTPRequestHandler.do_GET(self)
  772. def send_response(self, code, message=None):
  773. # Save the status code
  774. self.last_code = code
  775. SimpleHTTPRequestHandler.send_response(self, code, message)
  776. def log_message(self, f, *args):
  777. # Save instead of printing
  778. self.last_message = f % args