grider.py 6.4 KB

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