blackhole.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-"
  3. """
  4. This file is part of the UFONet project, https://ufonet.03c8.net
  5. Copyright (c) 2013/2020 | psy <epsylon@riseup.net>
  6. You should have received a copy of the GNU General Public License along
  7. with UFONet; if not, write to the Free Software Foundation, Inc., 51
  8. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  9. """
  10. import socket, re, time, string, sys, os, traceback, gzip, shutil
  11. from threading import *
  12. class AI(Thread):
  13. def __init__(self, parent):
  14. Thread.__init__(self)
  15. self.power_on = False
  16. self.parent = parent
  17. self.tmp_dir = parent.tmp_dir
  18. self.target_dir = parent.target_dir
  19. def find_meat(self):
  20. self.meats = []
  21. try:
  22. for f in os.listdir(self.tmp_dir + "blackhole"):
  23. if f[-3:] == '.gz':
  24. print("[Info] [AI] Found meat in "+str(f))
  25. self.meats.append(f)
  26. except:
  27. print("[info] [AI] No meat in the fridge "+self.tmp_dir + "blackhole")
  28. traceback.print_exc()
  29. return self.meats != []
  30. def process( self ):
  31. zombies_incoming=[]
  32. aliens_incoming=[]
  33. droids_incoming=[]
  34. ucavs_incoming=[]
  35. rpcs_incoming=[]
  36. ntps_incoming=[]
  37. dnss_incoming=[]
  38. snmps_incoming=[]
  39. for meat in self.meats:
  40. f_in = gzip.open(self.tmp_dir+"blackhole/"+meat)
  41. if 'community_zombies.txt.gz' in f_in: # zombies found
  42. f_out = open(self.tmp_dir+'meat.txt', 'wb')
  43. for line in f_in.readlines():
  44. zombies_incoming.append(line)
  45. f_out.write(line.strip()+os.linesep)
  46. f_out.close()
  47. elif 'community_aliens.txt.gz' in f_in: # aliens found
  48. f_out = open(self.tmp_dir+'larva.txt', 'wb')
  49. for line in f_in.readlines():
  50. aliens_incoming.append(line)
  51. f_out.write(line.strip()+os.linesep)
  52. f_out.close()
  53. elif 'community_droids.txt.gz' in f_in: # droids found
  54. f_out = open(self.tmp_dir+'chip.txt', 'wb')
  55. for line in f_in.readlines():
  56. droids_incoming.append(line)
  57. f_out.write(line.strip()+os.linesep)
  58. f_out.close()
  59. elif 'community_ucavs.txt.gz' in f_in: # ucavs found
  60. f_out = open(self.tmp_dir+'arduino.txt', 'wb')
  61. for line in f_in.readlines():
  62. ucavs_incoming.append(line)
  63. f_out.write(line.strip()+os.linesep)
  64. f_out.close()
  65. elif 'community_rpcs.txt.gz' in f_in: # rpcs found
  66. f_out = open(self.tmp_dir+'mirror.txt', 'wb')
  67. for line in f_in.readlines():
  68. rpcs_incoming.append(line)
  69. f_out.write(line.strip()+os.linesep)
  70. f_out.close()
  71. elif 'community_ntps.txt.gz' in f_in: # ntps found
  72. f_out = open(self.tmp_dir+'clock.txt', 'wb')
  73. for line in f_in.readlines():
  74. ntps_incoming.append(line)
  75. f_out.write(line.strip()+os.linesep)
  76. f_out.close()
  77. elif 'community_dnss.txt.gz' in f_in: # dnss found
  78. f_out = open(self.tmp_dir+'label.txt', 'wb')
  79. for line in f_in.readlines():
  80. dnss_incoming.append(line)
  81. f_out.write(line.strip()+os.linesep)
  82. f_out.close()
  83. elif 'community_snmps.txt.gz' in f_in: # snmps found
  84. f_out = open(self.tmp_dir+'glass.txt', 'wb')
  85. for line in f_in.readlines():
  86. snmps_incoming.append(line)
  87. f_out.write(line.strip()+os.linesep)
  88. f_out.close()
  89. f_in.close()
  90. os.remove(self.tmp_dir+"blackhole/"+meat)
  91. import subprocess
  92. f_tmp = open(self.tmp_dir + 'meat.tmp','wb')
  93. try:
  94. subprocess.call('../../ufonet --force-yes -t "'+self.tmp_dir+'meat.txt"', shell=True, stdout=f_tmp) # testing zombies (GET) (path: core/tools/blackhole.py)
  95. except:
  96. pass
  97. f_tmp.close()
  98. f_tmp = open(self.tmp_dir + 'meat.tmp')
  99. testoutput=f_tmp.read()
  100. f_tmp.close()
  101. if "Not any zombie active" in testoutput:
  102. if not aliens_incoming and not droids_incoming and not ucavs_incoming: # not any valid zombie (GET or POST)
  103. print("[Info] [AI] no valid zombies !")
  104. return
  105. else:
  106. f_tested = open(self.tmp_dir+'meat.txt')
  107. zombies_community = f_tested.readlines()
  108. f_tested.close()
  109. o_in = gzip.open(self.target_dir+'abductions.txt.gz', 'rb')
  110. zombies_army = o_in.readlines()
  111. initial = len(zombies_army)
  112. o_in.close()
  113. for zombie in zombies_community:
  114. if zombie.strip() not in zombies_army:
  115. zombies_army.append(zombie)
  116. else:
  117. pass
  118. fc = gzip.open(self.tmp_dir+'newzombies.txt.gz', 'wb')
  119. for zombie in zombies_army:
  120. fc.write(zombie.strip()+os.linesep)
  121. fc.close()
  122. shutil.move(self.tmp_dir+'newzombies.txt.gz', self.target_dir+'abductions.txt.gz')
  123. print("[info] [AI] Zombies tested : " +str(len(zombies_community)) + " / initial : " +str(initial) + " / final : " + str(len(zombies_army)))
  124. f_tested = open(self.tmp_dir+'larva.txt')
  125. aliens_community = f_tested.readlines()
  126. f_tested.close()
  127. o_in = gzip.open(self.target_dir+'troops.txt.gz', 'rb')
  128. aliens_army = o_in.readlines()
  129. initial = len(aliens_army)
  130. o_in.close()
  131. for alien in aliens_community:
  132. if alien.strip() not in aliens_army:
  133. aliens_army.append(alien)
  134. else:
  135. pass
  136. fc = gzip.open(self.tmp_dir+'newaliens.txt.gz', 'wb')
  137. for alien in aliens_army:
  138. fc.write(alien.strip()+os.linesep)
  139. fc.close()
  140. shutil.move(self.tmp_dir+'newaliens.txt.gz', self.target_dir+'troops.txt.gz')
  141. print("[Info] [AI] Aliens tested : " +str(len(aliens_community)) + " / initial : " +str(initial) + " / final : " + str(len(aliens_army)))
  142. f_tested = open(self.tmp_dir+'chip.txt')
  143. droids_community = f_tested.readlines()
  144. f_tested.close()
  145. o_in = gzip.open(self.target_dir+'robots.txt.gz', 'rb')
  146. droids_army = o_in.readlines()
  147. initial = len(droids_army)
  148. o_in.close()
  149. for droid in droids_community:
  150. if droid.strip() not in droids_army:
  151. droids_army.append(droid)
  152. else:
  153. pass
  154. fc = gzip.open(self.tmp_dir+'newdroids.txt.gz', 'wb')
  155. for droid in droids_army:
  156. fc.write(droid.strip()+os.linesep)
  157. fc.close()
  158. shutil.move(self.tmp_dir+'newdroids.txt.gz', self.target_dir+'robots.txt.gz')
  159. print("[Info] [AI] Droids tested : " +str(len(droids_community)) + " / initial : " +str(initial) + " / final : " + str(len(droids_army)))
  160. f_tested = open(self.tmp_dir+'arduino.txt')
  161. ucavs_community = f_tested.readlines()
  162. f_tested.close()
  163. o_in = gzip.open(self.target_dir+'drones.txt.gz', 'rb')
  164. ucavs_army = o_in.readlines()
  165. initial = len(ucavs_army)
  166. o_in.close()
  167. for ucav in ucavs_community:
  168. if ucav.strip() not in ucavs_army:
  169. ucavs_army.append(ucav)
  170. else:
  171. pass
  172. fc = gzip.open(self.tmp_dir+'newucavs.txt.gz', 'wb')
  173. for ucav in ucavs_army:
  174. fc.write(ucav.strip()+os.linesep)
  175. fc.close()
  176. shutil.move(self.tmp_dir+'newucavs.txt.gz', self.target_dir+'drones.txt.gz')
  177. print("[Info] [AI] UCAVs tested : " +str(len(ucavs_community)) + " / initial : " +str(initial) + " / final : " + str(len(ucavs_army)))
  178. f_tested = open(self.tmp_dir+'mirror.txt')
  179. rpcs_community = f_tested.readlines()
  180. f_tested.close()
  181. o_in = gzip.open(self.target_dir+'reflectors.txt.gz', 'rb')
  182. rpcs_army = o_in.readlines()
  183. initial = len(rpcs_army)
  184. o_in.close()
  185. for rpc in rpcs_community:
  186. if rpc.strip() not in rpcs_army:
  187. rpcs_army.append(rpc)
  188. else:
  189. pass
  190. fc = gzip.open(self.tmp_dir+'newrpcs.txt.gz', 'wb')
  191. for rpc in rpcs_army:
  192. fc.write(rpc.strip()+os.linesep)
  193. fc.close()
  194. shutil.move(self.tmp_dir+'newrpcs.txt.gz', self.target_dir+'reflectors.txt.gz')
  195. print("[Info] [AI] X-RPCs tested : " +str(len(rpcs_community)) + " / initial : " +str(initial) + " / final : " + str(len(rpcs_army)))
  196. f_tested = open(self.tmp_dir+'clock.txt')
  197. ntps_community = f_tested.readlines()
  198. f_tested.close()
  199. o_in = gzip.open(self.target_dir+'warps.txt.gz', 'rb')
  200. ntps_army = o_in.readlines()
  201. initial = len(ntps_army)
  202. o_in.close()
  203. for ntp in ntps_community:
  204. if ntp.strip() not in ntps_army:
  205. ntps_army.append(ntp)
  206. else:
  207. pass
  208. fc = gzip.open(self.tmp_dir+'newntps.txt.gz', 'wb')
  209. for ntp in ntps_army:
  210. fc.write(ntp.strip()+os.linesep)
  211. fc.close()
  212. shutil.move(self.tmp_dir+'newntps.txt.gz', self.target_dir+'warps.txt.gz')
  213. print("[Info] [AI] NTPs tested : " +str(len(ntps_community)) + " / initial : " +str(initial) + " / final : " + str(len(ntps_army)))
  214. f_tested = open(self.tmp_dir+'label.txt')
  215. dnss_community = f_tested.readlines()
  216. f_tested.close()
  217. o_in = gzip.open(self.target_dir+'crystals.txt.gz', 'rb')
  218. dnss_army = o_in.readlines()
  219. initial = len(dnss_army)
  220. o_in.close()
  221. for dns in dnss_community:
  222. if dns.strip() not in dnss_army:
  223. dnss_army.append(dns)
  224. else:
  225. pass
  226. fc = gzip.open(self.tmp_dir+'newdnss.txt.gz', 'wb')
  227. for dns in dnss_army:
  228. fc.write(dns.strip()+os.linesep)
  229. fc.close()
  230. shutil.move(self.tmp_dir+'newdnss.txt.gz', self.target_dir+'crystals.txt.gz')
  231. print("[Info] [AI] DNSs tested : " +str(len(dnss_community)) + " / initial : " +str(initial) + " / final : " + str(len(dnss_army)))
  232. f_tested = open(self.tmp_dir+'glass.txt')
  233. snmps_community = f_tested.readlines()
  234. f_tested.close()
  235. o_in = gzip.open(self.target_dir+'bosons.txt.gz', 'rb')
  236. snmps_army = o_in.readlines()
  237. initial = len(snmps_army)
  238. o_in.close()
  239. for snmp in snmps_community:
  240. if snmp.strip() not in snmps_army:
  241. snmps_army.append(snmp)
  242. else:
  243. pass
  244. fc = gzip.open(self.tmp_dir+'newsnmps.txt.gz', 'wb')
  245. for snmp in snmps_army:
  246. fc.write(snmp.strip()+os.linesep)
  247. fc.close()
  248. shutil.move(self.tmp_dir+'newsnmps.txt.gz', self.target_dir+'bosons.txt.gz')
  249. print("[Info] [AI] SNMPs tested : " +str(len(snmps_community)) + " / initial : " +str(initial) + " / final : " + str(len(snmps_army)))
  250. def run(self):
  251. self.power_on = True
  252. print("[Info] [AI] Power On")
  253. while self.power_on :
  254. # load list of files to process
  255. if self.find_meat():
  256. # if data -> process
  257. self.process()
  258. time.sleep(5)
  259. print("[Info] [AI] Power Off")
  260. class BlackRay(Thread):
  261. def __init__(self, parent):
  262. Thread.__init__(self)
  263. self.parent = parent
  264. self.active = False
  265. self.sock = None
  266. self.shining = False
  267. def run( self ):
  268. conn = None
  269. addr = None
  270. self.sock = self.parent.try_bind(9991)
  271. if self.sock is not None:
  272. self.sock.listen(1)
  273. print('[Info] [AI] [BlackRay] Emitting on port 9991')
  274. self.shining = True
  275. else:
  276. print('[Error] [AI] [BlackRay] Failed to emit on port 9991')
  277. while self.shining:
  278. try:
  279. conn,addr = self.sock.accept()
  280. print('[Info] [AI] [BlackRay] Got connection from', addr)
  281. except socket.timeout:
  282. pass
  283. except socket.error as e:
  284. if self.shining == False:
  285. print("[Error] [AI] [BlackRay] Socket Error /return : "+str(e))
  286. return
  287. else:
  288. print("[Error] [AI] [BlackRay] Socket Error /break : "+str(e))
  289. break
  290. else:
  291. data = conn.recv(1024)
  292. if data :
  293. if data[0:4] == "SEND" :
  294. print("[Info] [AI] [BlackRay] Meat ready : "+data[5:])
  295. conn.close()
  296. print('[Info] [AI] [BlackRay] End of emission')
  297. self.sock.close()
  298. class Eater(Thread):
  299. def __init__(self, client, parent):
  300. Thread.__init__(self)
  301. self.client = client
  302. self.parent = parent
  303. def run(self):
  304. print('[Info] [AI] Yum... got meat')
  305. zombie_meat = "community_zombies.txt.gz"
  306. alien_meat = "community_aliens.txt.gz"
  307. droid_meat = "community_droids.txt.gz"
  308. ucav_meat = "community_ucavs.txt.gz"
  309. rpc_meat = "community_rpcs.txt.gz"
  310. ntp_meat = "community_ntps.txt.gz"
  311. dns_meat = "community_dnss.txt.gz"
  312. snmp_meat = "community_snmps.txt.gz"
  313. while 1:
  314. try:
  315. data = self.client.recv(4096).decode("utf-8")
  316. except:
  317. data = ""
  318. if not data:
  319. break
  320. if zombie_meat in data: # get zombies
  321. r = re.compile(".*("+zombie_meat+").*") # regex magics
  322. meat_type = r.search(data)
  323. m = meat_type.group(1)
  324. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  325. f.write(data)
  326. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  327. f.close()
  328. elif alien_meat in data: # get aliens
  329. r = re.compile(".*("+alien_meat+").*") # regex magics
  330. meat_type = r.search(data)
  331. m = meat_type.group(1)
  332. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  333. f.write(data)
  334. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  335. f.close()
  336. elif droid_meat in data: # get zombies
  337. r = re.compile(".*("+droid_meat+").*") # regex magics
  338. meat_type = r.search(data)
  339. m = meat_type.group(1)
  340. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  341. f.write(data)
  342. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  343. f.close()
  344. elif ucav_meat in data: # get ucavs
  345. r = re.compile(".*("+ucav_meat+").*") # regex magics
  346. meat_type = r.search(data)
  347. m = meat_type.group(1)
  348. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  349. f.write(data)
  350. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  351. f.close()
  352. elif rpc_meat in data: # get rpcs
  353. r = re.compile(".*("+rpc_meat+").*") # regex magics
  354. meat_type = r.search(data)
  355. m = meat_type.group(1)
  356. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  357. f.write(data)
  358. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  359. f.close()
  360. elif ntp_meat in data: # get ntps
  361. r = re.compile(".*("+ntp_meat+").*") # regex magics
  362. meat_type = r.search(data)
  363. m = meat_type.group(1)
  364. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  365. f.write(data)
  366. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  367. f.close()
  368. elif dns_meat in data: # get dnss
  369. r = re.compile(".*("+dns_meat+").*") # regex magics
  370. meat_type = r.search(data)
  371. m = meat_type.group(1)
  372. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  373. f.write(data)
  374. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  375. f.close()
  376. elif snmp_meat in data: # get snmps
  377. r = re.compile(".*("+snmp_meat+").*") # regex magics
  378. meat_type = r.search(data)
  379. m = meat_type.group(1)
  380. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  381. f.write(data)
  382. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  383. f.close()
  384. self.client.close()
  385. self.parent.eater_full(self)
  386. class Absorber(Thread):
  387. def __init__(self, parent):
  388. Thread.__init__(self)
  389. self.parent = parent
  390. self.overflow = True
  391. self._eaters = []
  392. self.tmp_dir = parent.tmp_dir
  393. self.sock = False
  394. def run( self ):
  395. self.sock = self.parent.try_bind(9990)
  396. if self.sock is not None:
  397. self.sock.listen(1)
  398. print('[Info] [AI] Ready to feed on port 9990')
  399. self.overflow = False
  400. else:
  401. print('[Error] [AI] Failed to listen on port 9990')
  402. while not self.overflow:
  403. try:
  404. conn,addr = self.sock.accept()
  405. print('[Info] [AI] Got connection from', addr)
  406. except socket.timeout:
  407. pass
  408. except socket.error as e:
  409. if self.hungry == False:
  410. print("[Error] [AI] Socket Error /return : "+str(e))
  411. return
  412. else:
  413. print("[Error] [AI] Socket Error /break : "+str(e))
  414. break
  415. else:
  416. t = Eater(conn, self)
  417. t.start()
  418. self._eaters.append(t)
  419. self.sock.close()
  420. print('[Info] [AI] Dinner time is over')
  421. def eater_full(self, _thread):
  422. self._eaters.remove(_thread)
  423. class BlackHole ( Thread ):
  424. def __init__(self):
  425. Thread.__init__( self )
  426. self.daemon = True
  427. self.awake = True
  428. self.tmp_dir = "/tmp/"
  429. self.target_dir = '/var/www/ufonet/'
  430. self.blackray = None
  431. self.absorber = None
  432. self.computer = None
  433. def dream(self):
  434. if not os.path.exists(self.target_dir+"abductions.txt.gz"):
  435. abductions_fail = 0
  436. try:
  437. fc = gzip.open(self.target_dir+'abductions.txt.gz', 'wb')
  438. fc.close()
  439. except:
  440. print("[Error] [AI] Not 'abductions.txt.gz' file in "+self.target_dir)
  441. abductions_fail = abductions_fail + 1
  442. else:
  443. abductions_fail = 0
  444. if not os.path.exists(self.target_dir+"troops.txt.gz"):
  445. troops_fail = 0
  446. try:
  447. fc = gzip.open(self.target_dir+'troops.txt.gz', 'wb')
  448. fc.close()
  449. except:
  450. print("[Error] [AI] Not 'troops.txt.gz' file in "+self.target_dir)
  451. troops_fail = troops_fail + 1
  452. else:
  453. troops_fail = 0
  454. if not os.path.exists(self.target_dir+"robots.txt.gz"):
  455. robots_fail = 0
  456. try:
  457. fc = gzip.open(self.target_dir+'robots.txt.gz', 'wb')
  458. fc.close()
  459. except:
  460. print("[Error] [AI] Not 'robots.txt.gz' file in "+self.target_dir)
  461. robots_fail = robots_fail + 1
  462. else:
  463. robots_fail = 0
  464. if not os.path.exists(self.target_dir+"drones.txt.gz"):
  465. drones_fail = 0
  466. try:
  467. fc = gzip.open(self.target_dir+'drones.txt.gz', 'wb')
  468. fc.close()
  469. except:
  470. print("[Error] [AI] Not 'drones.txt.gz' file in "+self.target_dir)
  471. drones_fail = drones_fail + 1
  472. else:
  473. drones_fail = 0
  474. if not os.path.exists(self.target_dir+"reflectors.txt.gz"):
  475. reflectors_fail = 0
  476. try:
  477. fc = gzip.open(self.target_dir+'reflectors.txt.gz', 'wb')
  478. fc.close()
  479. except:
  480. print("[Error] [AI] Not 'reflectors.txt.gz' file in "+self.target_dir)
  481. reflectors_fail = reflectors_fail + 1
  482. else:
  483. reflectors_fail = 0
  484. if not os.path.exists(self.target_dir+"warps.txt.gz"):
  485. ntps_fail = 0
  486. try:
  487. fc = gzip.open(self.target_dir+'warps.txt.gz', 'wb')
  488. fc.close()
  489. except:
  490. print("[Error] [AI] Not 'warps.txt.gz' file in "+self.target_dir)
  491. ntps_fail = ntps_fail + 1
  492. else:
  493. ntps_fail = 0
  494. if not os.path.exists(self.target_dir+"crystals.txt.gz"):
  495. dnss_fail = 0
  496. try:
  497. fc = gzip.open(self.target_dir+'crystals.txt.gz', 'wb')
  498. fc.close()
  499. except:
  500. print("[Error] [AI] Not 'crystals.txt.gz' file in "+self.target_dir)
  501. dnss_fail = dnss_fail + 1
  502. else:
  503. dnss_fail = 0
  504. if not os.path.exists(self.target_dir+"bosons.txt.gz"):
  505. snmps_fail = 0
  506. try:
  507. fc = gzip.open(self.target_dir+'bosons.txt.gz', 'wb')
  508. fc.close()
  509. except:
  510. print("[Error] [AI] Not 'bosons.txt.gz' file in "+self.target_dir)
  511. snmps_fail = snmps_fail + 1
  512. else:
  513. snmps_fail = 0
  514. if not os.access(self.target_dir+"abductions.txt.gz",os.W_OK):
  515. print("[Error] [AI] Write access denied for 'abductions' file in "+self.target_dir)
  516. abductions_fail = abductions_fail + 1
  517. if not os.access(self.target_dir+"troops.txt.gz",os.W_OK):
  518. print("[Error] [AI] Write access denied for 'troops' file in "+self.target_dir)
  519. troops_fail = troops_fail + 1
  520. if not os.access(self.target_dir+"robots.txt.gz",os.W_OK):
  521. print("[Error] [AI] Write access denied for 'robots' file in "+self.target_dir)
  522. robots_fail = robots_fail + 1
  523. if not os.access(self.target_dir+"drones.txt.gz",os.W_OK):
  524. print("[Error] [AI] Write access denied for 'drones' file in "+self.target_dir)
  525. drones_fail = drones_fail + 1
  526. if not os.access(self.target_dir+"reflectors.txt.gz",os.W_OK):
  527. print("[Error] [AI] Write access denied for 'reflectors' file in "+self.target_dir)
  528. reflectors_fail = reflectors_fail + 1
  529. if not os.access(self.target_dir+"warps.txt.gz",os.W_OK):
  530. print("[Error] [AI] Write access denied for 'warps' file in "+self.target_dir)
  531. ntps_fail = ntps_fail + 1
  532. if not os.access(self.target_dir+"crystals.txt.gz",os.W_OK):
  533. print("[Error] [AI] Write access denied for 'crystals' file in "+self.target_dir)
  534. dnss_fail = dnss_fail + 1
  535. if not os.access(self.target_dir+"bosons.txt.gz",os.W_OK):
  536. print("[Error] [AI] Write access denied for 'bosons' file in "+self.target_dir)
  537. snmps_fail = snmps_fail + 1
  538. if abductions_fail > 0 and troops_fail > 0 and robots_fail > 0 and drones_fail > 0 and reflectors_fail > 0 and ntps_fail > 0 and dnss_fail > 0 and snmps_fail > 0:
  539. print("\n[Error] [AI] Cannot found any container... -> [Aborting!]")
  540. print("\n[Info] [AI] Suspend [Blackhole] with: Ctrl+z")
  541. sys.exit(2)
  542. if self.consume():
  543. os.mkdir(self.tmp_dir + "blackhole")
  544. else:
  545. print("[Error] [AI] [Blackhol] Unable to consume in "+self.tmp_dir+"blackhole...")
  546. sys.exit(2)
  547. if not os.path.isdir(self.tmp_dir + "blackhole"):
  548. print("[Error] [AI] [Blackhole] Unable to create "+self.tmp_dir+"blackhole...")
  549. sys.exit(2)
  550. self.blackray = BlackRay(self)
  551. self.absorber = Absorber(self)
  552. self.computer = AI(self)
  553. self.awake = False
  554. print("[Info] [AI] [Blackhole] Having sweet dreams...")
  555. def consume(self):
  556. if os.path.isdir(self.tmp_dir + "blackhole"):
  557. try:
  558. shutil.rmtree(self.tmp_dir + "blackhole")
  559. except OSError as e:
  560. print("[Error] [AI] [Blackhole] Unable to consume : " + str(e))
  561. return False
  562. return True
  563. def try_bind(self, port):
  564. s=None
  565. try:
  566. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  567. s.settimeout(10)
  568. s.bind(('', port))
  569. except socket.error as e:
  570. if e.errno == 98: # if is in use wait a bit and retry
  571. time.sleep(3)
  572. return self.try_bind(port)
  573. print(("[Error] [AI] [Blackhole] Socket busy, connection failed on port " + str(port)))
  574. return s
  575. def run(self):
  576. self.dream()
  577. try:
  578. self.blackray.start()
  579. self.absorber.start()
  580. self.computer.start()
  581. if not self.blackray.shining or self.absorber.overflow or not self.computer.power_on:
  582. print("[Info] [AI] Advancing time in another space (waiting for server)"+os.linesep)
  583. time.sleep(1)
  584. while not self.blackray.shining or self.absorber.overflow or not self.computer.power_on:
  585. time.sleep(1)
  586. print("\n[Info] [AI] [BlackHole] All up and running...")
  587. while self.blackray.shining and not self.absorber.overflow and self.computer.power_on:
  588. time.sleep(1)
  589. except:
  590. traceback.print_exc()
  591. self.awaken()
  592. print("[Info] [AI] [Blackhole] Lifespan is up...")
  593. def collapse(self):
  594. self.blackray.shining = False
  595. self.absorber.overflow = True
  596. self.computer.power_on = False
  597. self.computer.join()
  598. self.blackray.join()
  599. self.absorber.join()
  600. def awaken(self):
  601. self.consume()
  602. self.collapse()
  603. self.awake = True
  604. if __name__ == "__main__":
  605. try:
  606. print("\n[Info] [AI] Initiating void generation sequence...\n")
  607. print('='*22 + '\n')
  608. app = BlackHole()
  609. app.start()
  610. while True: time.sleep(1)
  611. except KeyboardInterrupt:
  612. print("\n[Info] [AI] Terminating void generation sequence...\n")
  613. app.collapse()
  614. except Exception as e:
  615. traceback.print_exc()
  616. print ("\n[Error] [AI] Something wrong creating [Blackhole] -> [Passing!]\n")