tokenhub.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-"
  3. # vim: set expandtab tabstop=4 shiftwidth=4:
  4. """
  5. This file is part of the XSSer project, https://xsser.03c8.net
  6. Copyright (c) 2010/2019 | psy <epsylon@riseup.net>
  7. xsser is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation version 3 of the License.
  10. xsser is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  13. details.
  14. You should have received a copy of the GNU General Public License along
  15. with xsser; if not, write to the Free Software Foundation, Inc., 51
  16. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. """
  18. from threading import Thread
  19. import socket
  20. import time
  21. class ReceiverThread(Thread):
  22. def __init__(self, client, addr, parent):
  23. Thread.__init__(self)
  24. self.daemon = True
  25. self.client = client
  26. self.parent = parent
  27. def run(self):
  28. data = self.client.recv(1024)
  29. if data:
  30. self.parent.data_arrived(data)
  31. self.client.send('thanks for coming!')
  32. self.client.close()
  33. self.parent.client_finished(self)
  34. class HubThread(Thread):
  35. def __init__(self, parent):
  36. Thread.__init__(self)
  37. self.daemon = True
  38. self._clients = []
  39. self._armed = True
  40. self.ready = False
  41. self.running =False
  42. self.parent = parent
  43. def url_request(self, url):
  44. split_url = url.split("/")
  45. if len(split_url) > 2:
  46. if split_url[1] == 'success':
  47. self.parent.token_arrived(split_url[2])
  48. def data_arrived(self, data):
  49. data.split("\n")[0]
  50. if data.startswith("GET"):
  51. split_data = data.split()
  52. if len(split_data) > 1:
  53. self.url_request(split_data[1])
  54. def client_finished(self, _thread):
  55. self._clients.remove(_thread)
  56. def shutdown(self):
  57. if self.ready:
  58. try:
  59. self.socket.shutdown(socket.SHUT_RDWR)
  60. self.socket.close()
  61. except OSError:
  62. pass
  63. self.running = False
  64. self._armed = False
  65. self.ready = False
  66. def run(self):
  67. while not self.running and self._armed:
  68. try:
  69. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  70. s.bind(('localhost', 19084))
  71. self.running = True
  72. except socket.error as e:
  73. #print("socket busy, retry opening")
  74. if e.errno == 98: # its in use wait a bit and retry
  75. time.sleep(3)
  76. if not self._armed:
  77. return
  78. self.socket = s
  79. self.ready = True
  80. s.listen(1)
  81. while self.running and self._armed:
  82. try:
  83. conn, addr = s.accept()
  84. except socket.timeout:
  85. pass
  86. except socket.error as e:
  87. if self.ready == False:
  88. return
  89. else:
  90. break
  91. else:
  92. t = ReceiverThread(conn, addr, self)
  93. t.start()
  94. self._clients.append(t)
  95. if self.ready:
  96. s.close()
  97. self.ready = False