tokenhub.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. self.socket.shutdown(socket.SHUT_RDWR)
  59. self.socket.close()
  60. self.running = False
  61. self._armed = False
  62. self.ready = False
  63. def run(self):
  64. while not self.running and self._armed:
  65. try:
  66. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  67. s.bind(('localhost', 19084))
  68. self.running = True
  69. except socket.error as e:
  70. #print("socket busy, retry opening")
  71. if e.errno == 98: # its in use wait a bit and retry
  72. time.sleep(3)
  73. if not self._armed:
  74. return
  75. self.socket = s
  76. self.ready = True
  77. s.listen(1)
  78. while self.running and self._armed:
  79. try:
  80. conn, addr = s.accept()
  81. except socket.timeout:
  82. pass
  83. except socket.error, e:
  84. if self.ready == False:
  85. return
  86. else:
  87. break
  88. else:
  89. t = ReceiverThread(conn, addr, self)
  90. t.start()
  91. self._clients.append(t)
  92. if self.ready:
  93. s.close()
  94. self.ready = False