grider.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-"
  3. """
  4. UFONet - Denial of Service Toolkit - 2017/2018 - by psy (epsylon@riseup.net)
  5. You should have received a copy of the GNU General Public License along
  6. with UFONet; if not, write to the Free Software Foundation, Inc., 51
  7. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  8. """
  9. import socket
  10. import re
  11. import time
  12. import string
  13. import sys
  14. import urlparse
  15. import os
  16. import traceback
  17. from threading import *
  18. class Paster(Thread):
  19. def __init__(self, parent):
  20. Thread.__init__(self)
  21. self.parent = parent
  22. self.active = False
  23. self.sock = None
  24. self.clean = False
  25. def run( self ):
  26. conn = None
  27. addr = None
  28. self.sock = self.parent.try_bind(9992)
  29. if self.sock is not None:
  30. self.sock.listen(1)
  31. print '[Paster] clean on port 9992'
  32. self.clean = True
  33. else:
  34. print '[Paster ERROR] no paste on port 9992'
  35. while self.clean:
  36. try:
  37. conn,addr = self.sock.accept()
  38. print '[Paster] Got copy from', addr
  39. except socket.timeout:
  40. pass
  41. except socket.error, e:
  42. if self.clean == False:
  43. print "[Paster] Socket Error /return : "+str(e)
  44. return
  45. else:
  46. print "[Paster] Socket Error /break : "+str(e)
  47. break
  48. else:
  49. data = conn.recv(4096)
  50. if data :
  51. l=len(data)
  52. print "[DEBUG] received data : "+data+"/"+str(l)+"/"+str(data.find("\n"))
  53. if data.find('\n')==-1 and data.find('\r')==-1:
  54. if data.find("#?#")!=-1:
  55. print "[DEBUG] adding to grid"
  56. fc=open(self.parent.target_dir+"grid.txt","a")
  57. fc.write(data+"\n")
  58. fc.close()
  59. elif data.find("#!#")!=-1:
  60. print "[DEBUG] adding to board"
  61. fc=open(self.parent.target_dir+"board.txt","a")
  62. fc.write(data+"\n")
  63. fc.close()
  64. elif data.find("#-#")!=-1:
  65. print "[DEBUG] adding to wargames"
  66. fc=open(self.parent.target_dir+"wargames.txt","a")
  67. fc.write(data+"\n")
  68. fc.close()
  69. conn.close()
  70. print '[Paster] done'
  71. self.sock.close()
  72. class Grider ( Thread ):
  73. def __init__(self):
  74. Thread.__init__( self )
  75. self.daemon = True
  76. self.awake = True
  77. self.tmp_dir = "/tmp/"
  78. self.target_dir = '/var/www/ufonet/'
  79. self.blackray = None
  80. self.absorber = None
  81. self.computer = None
  82. def dream(self):
  83. if not os.path.exists(self.target_dir+"grid.txt"):
  84. grid_fail = 0
  85. try:
  86. fc = open(self.target_dir+'grid.txt', 'wb')
  87. fc.close()
  88. except:
  89. print "[Error] no grid.txt file in "+self.target_dir
  90. grid_fail = grid_fail + 1
  91. else:
  92. grid_fail = 0
  93. if not os.path.exists(self.target_dir+"board.txt"):
  94. board_fail = 0
  95. try:
  96. fc = open(self.target_dir+'board.txt', 'wb')
  97. fc.close()
  98. except:
  99. print "[Error] no board.txt file in "+self.target_dir
  100. board_fail = board_fail + 1
  101. else:
  102. board_fail = 0
  103. if not os.path.exists(self.target_dir+"wargames.txt"):
  104. wargames_fail = 0
  105. try:
  106. fc = open(self.target_dir+'wargames.txt', 'wb')
  107. fc.close()
  108. except:
  109. print "[Error] no wargames.txt file in "+self.target_dir
  110. wargames_fail = wargames_fail + 1
  111. else:
  112. wargames_fail = 0
  113. if not os.access(self.target_dir+"grid.txt",os.W_OK):
  114. print "[Error] write access denied for grid file in "+self.target_dir
  115. grid_fail = grid_fail + 1
  116. if not os.access(self.target_dir+"board.txt",os.W_OK):
  117. print "[Error] write access denied for board file in "+self.target_dir
  118. board_fail = board_fail + 1
  119. if not os.access(self.target_dir+"wargames.txt",os.W_OK):
  120. print "[Error] write access denied for wargames file in "+self.target_dir
  121. wargames_fail = wargames_fail + 1
  122. if grid_fail > 0 and board_fail > 0 and wargames_fail > 0:
  123. print "[Error] grid, board and wargames are unuseable. Aborting..."
  124. sys.exit(2)
  125. self.paster = Paster(self)
  126. self.awake = False
  127. print "[Grider] Having sweet dreams..."
  128. def try_bind(self, port):
  129. s=None
  130. try:
  131. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  132. s.settimeout(10)
  133. s.bind(('', port))
  134. except socket.error as e:
  135. if e.errno == 98: # if is in use wait a bit and retry
  136. time.sleep(3)
  137. return self.try_bind(port)
  138. print("[Grider Warning] socket busy, connection failed on port " + str(port))
  139. return s
  140. def run(self):
  141. self.dream()
  142. try:
  143. self.paster.start()
  144. if self.paster.clean:
  145. print "[Grider] Advancing time in another space (waiting for server)"+os.linesep
  146. time.sleep(1)
  147. while self.paster.clean:
  148. print "[Grider] Advancing time in another space (waiting for server)"+os.linesep
  149. time.sleep(1)
  150. print "\n[Grider] sheets are all up and ready"
  151. while self.paster.clean:
  152. time.sleep(1)
  153. except:
  154. traceback.print_exc()
  155. self.cut()
  156. print "[Grider] finished"
  157. def cut(self):
  158. self.paster.clean=False
  159. self.paster.join()
  160. if __name__ == "__main__":
  161. try:
  162. print("\nInitiating copy/paste functions ...\n")
  163. print '='*22 + '\n'
  164. app = Grider()
  165. app.start()
  166. while True: time.sleep(1)
  167. except KeyboardInterrupt:
  168. print("\nTerminating copy/paste functions...\n")
  169. app.cut()
  170. except Exception, e:
  171. traceback.print_exc()
  172. print ("\n[Error] - Something wrong trying to copy/paste to the grid...!\n")