blackhole.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. try:
  298. self.sock.close()
  299. except:
  300. pass
  301. class Eater(Thread):
  302. def __init__(self, client, parent):
  303. Thread.__init__(self)
  304. self.client = client
  305. self.parent = parent
  306. def run(self):
  307. print('[Info] [AI] Yum... got meat')
  308. zombie_meat = "community_zombies.txt.gz"
  309. alien_meat = "community_aliens.txt.gz"
  310. droid_meat = "community_droids.txt.gz"
  311. ucav_meat = "community_ucavs.txt.gz"
  312. rpc_meat = "community_rpcs.txt.gz"
  313. ntp_meat = "community_ntps.txt.gz"
  314. dns_meat = "community_dnss.txt.gz"
  315. snmp_meat = "community_snmps.txt.gz"
  316. while 1:
  317. try:
  318. data = self.client.recv(4096).decode("utf-8")
  319. except:
  320. data = ""
  321. if not data:
  322. break
  323. if zombie_meat in data: # get zombies
  324. r = re.compile(".*("+zombie_meat+").*") # regex magics
  325. meat_type = r.search(data)
  326. m = meat_type.group(1)
  327. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  328. f.write(data)
  329. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  330. f.close()
  331. elif alien_meat in data: # get aliens
  332. r = re.compile(".*("+alien_meat+").*") # regex magics
  333. meat_type = r.search(data)
  334. m = meat_type.group(1)
  335. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  336. f.write(data)
  337. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  338. f.close()
  339. elif droid_meat in data: # get zombies
  340. r = re.compile(".*("+droid_meat+").*") # regex magics
  341. meat_type = r.search(data)
  342. m = meat_type.group(1)
  343. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  344. f.write(data)
  345. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  346. f.close()
  347. elif ucav_meat in data: # get ucavs
  348. r = re.compile(".*("+ucav_meat+").*") # regex magics
  349. meat_type = r.search(data)
  350. m = meat_type.group(1)
  351. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  352. f.write(data)
  353. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  354. f.close()
  355. elif rpc_meat in data: # get rpcs
  356. r = re.compile(".*("+rpc_meat+").*") # regex magics
  357. meat_type = r.search(data)
  358. m = meat_type.group(1)
  359. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  360. f.write(data)
  361. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  362. f.close()
  363. elif ntp_meat in data: # get ntps
  364. r = re.compile(".*("+ntp_meat+").*") # regex magics
  365. meat_type = r.search(data)
  366. m = meat_type.group(1)
  367. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  368. f.write(data)
  369. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  370. f.close()
  371. elif dns_meat in data: # get dnss
  372. r = re.compile(".*("+dns_meat+").*") # regex magics
  373. meat_type = r.search(data)
  374. m = meat_type.group(1)
  375. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  376. f.write(data)
  377. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  378. f.close()
  379. elif snmp_meat in data: # get snmps
  380. r = re.compile(".*("+snmp_meat+").*") # regex magics
  381. meat_type = r.search(data)
  382. m = meat_type.group(1)
  383. f = open(self.parent.tmp_dir+"blackhole/"+m,"wb")
  384. f.write(data)
  385. print('\n[Info] [AI] Got "%s Closing media transfer"' % f.name)
  386. f.close()
  387. self.client.close()
  388. self.parent.eater_full(self)
  389. class Absorber(Thread):
  390. def __init__(self, parent):
  391. Thread.__init__(self)
  392. self.parent = parent
  393. self.overflow = True
  394. self._eaters = []
  395. self.tmp_dir = parent.tmp_dir
  396. self.sock = False
  397. def run( self ):
  398. self.sock = self.parent.try_bind(9990)
  399. if self.sock is not None:
  400. self.sock.listen(1)
  401. print('[Info] [AI] Ready to feed on port 9990')
  402. self.overflow = False
  403. else:
  404. print('[Error] [AI] Failed to listen on port 9990')
  405. while not self.overflow:
  406. try:
  407. conn,addr = self.sock.accept()
  408. print('[Info] [AI] Got connection from', addr)
  409. except socket.timeout:
  410. pass
  411. except socket.error as e:
  412. if self.hungry == False:
  413. print("[Error] [AI] Socket Error /return : "+str(e))
  414. return
  415. else:
  416. print("[Error] [AI] Socket Error /break : "+str(e))
  417. break
  418. else:
  419. t = Eater(conn, self)
  420. t.start()
  421. self._eaters.append(t)
  422. self.sock.close()
  423. print('[Info] [AI] Dinner time is over')
  424. def eater_full(self, _thread):
  425. self._eaters.remove(_thread)
  426. class BlackHole ( Thread ):
  427. def __init__(self):
  428. Thread.__init__( self )
  429. self.daemon = True
  430. self.awake = True
  431. self.tmp_dir = "/tmp/"
  432. self.target_dir = '/var/www/ufonet/'
  433. self.blackray = None
  434. self.absorber = None
  435. self.computer = None
  436. def dream(self):
  437. if not os.path.exists(self.target_dir+"abductions.txt.gz"):
  438. abductions_fail = 0
  439. try:
  440. fc = gzip.open(self.target_dir+'abductions.txt.gz', 'wb')
  441. fc.close()
  442. except:
  443. print("[Error] [AI] Not 'abductions.txt.gz' file in "+self.target_dir)
  444. abductions_fail = abductions_fail + 1
  445. else:
  446. abductions_fail = 0
  447. if not os.path.exists(self.target_dir+"troops.txt.gz"):
  448. troops_fail = 0
  449. try:
  450. fc = gzip.open(self.target_dir+'troops.txt.gz', 'wb')
  451. fc.close()
  452. except:
  453. print("[Error] [AI] Not 'troops.txt.gz' file in "+self.target_dir)
  454. troops_fail = troops_fail + 1
  455. else:
  456. troops_fail = 0
  457. if not os.path.exists(self.target_dir+"robots.txt.gz"):
  458. robots_fail = 0
  459. try:
  460. fc = gzip.open(self.target_dir+'robots.txt.gz', 'wb')
  461. fc.close()
  462. except:
  463. print("[Error] [AI] Not 'robots.txt.gz' file in "+self.target_dir)
  464. robots_fail = robots_fail + 1
  465. else:
  466. robots_fail = 0
  467. if not os.path.exists(self.target_dir+"drones.txt.gz"):
  468. drones_fail = 0
  469. try:
  470. fc = gzip.open(self.target_dir+'drones.txt.gz', 'wb')
  471. fc.close()
  472. except:
  473. print("[Error] [AI] Not 'drones.txt.gz' file in "+self.target_dir)
  474. drones_fail = drones_fail + 1
  475. else:
  476. drones_fail = 0
  477. if not os.path.exists(self.target_dir+"reflectors.txt.gz"):
  478. reflectors_fail = 0
  479. try:
  480. fc = gzip.open(self.target_dir+'reflectors.txt.gz', 'wb')
  481. fc.close()
  482. except:
  483. print("[Error] [AI] Not 'reflectors.txt.gz' file in "+self.target_dir)
  484. reflectors_fail = reflectors_fail + 1
  485. else:
  486. reflectors_fail = 0
  487. if not os.path.exists(self.target_dir+"warps.txt.gz"):
  488. ntps_fail = 0
  489. try:
  490. fc = gzip.open(self.target_dir+'warps.txt.gz', 'wb')
  491. fc.close()
  492. except:
  493. print("[Error] [AI] Not 'warps.txt.gz' file in "+self.target_dir)
  494. ntps_fail = ntps_fail + 1
  495. else:
  496. ntps_fail = 0
  497. if not os.path.exists(self.target_dir+"crystals.txt.gz"):
  498. dnss_fail = 0
  499. try:
  500. fc = gzip.open(self.target_dir+'crystals.txt.gz', 'wb')
  501. fc.close()
  502. except:
  503. print("[Error] [AI] Not 'crystals.txt.gz' file in "+self.target_dir)
  504. dnss_fail = dnss_fail + 1
  505. else:
  506. dnss_fail = 0
  507. if not os.path.exists(self.target_dir+"bosons.txt.gz"):
  508. snmps_fail = 0
  509. try:
  510. fc = gzip.open(self.target_dir+'bosons.txt.gz', 'wb')
  511. fc.close()
  512. except:
  513. print("[Error] [AI] Not 'bosons.txt.gz' file in "+self.target_dir)
  514. snmps_fail = snmps_fail + 1
  515. else:
  516. snmps_fail = 0
  517. if not os.access(self.target_dir+"abductions.txt.gz",os.W_OK):
  518. print("[Error] [AI] Write access denied for 'abductions' file in "+self.target_dir)
  519. abductions_fail = abductions_fail + 1
  520. if not os.access(self.target_dir+"troops.txt.gz",os.W_OK):
  521. print("[Error] [AI] Write access denied for 'troops' file in "+self.target_dir)
  522. troops_fail = troops_fail + 1
  523. if not os.access(self.target_dir+"robots.txt.gz",os.W_OK):
  524. print("[Error] [AI] Write access denied for 'robots' file in "+self.target_dir)
  525. robots_fail = robots_fail + 1
  526. if not os.access(self.target_dir+"drones.txt.gz",os.W_OK):
  527. print("[Error] [AI] Write access denied for 'drones' file in "+self.target_dir)
  528. drones_fail = drones_fail + 1
  529. if not os.access(self.target_dir+"reflectors.txt.gz",os.W_OK):
  530. print("[Error] [AI] Write access denied for 'reflectors' file in "+self.target_dir)
  531. reflectors_fail = reflectors_fail + 1
  532. if not os.access(self.target_dir+"warps.txt.gz",os.W_OK):
  533. print("[Error] [AI] Write access denied for 'warps' file in "+self.target_dir)
  534. ntps_fail = ntps_fail + 1
  535. if not os.access(self.target_dir+"crystals.txt.gz",os.W_OK):
  536. print("[Error] [AI] Write access denied for 'crystals' file in "+self.target_dir)
  537. dnss_fail = dnss_fail + 1
  538. if not os.access(self.target_dir+"bosons.txt.gz",os.W_OK):
  539. print("[Error] [AI] Write access denied for 'bosons' file in "+self.target_dir)
  540. snmps_fail = snmps_fail + 1
  541. 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:
  542. print("\n[Error] [AI] Cannot found any container... -> [Aborting!]")
  543. print("\n[Info] [AI] Suspend [Blackhole] with: Ctrl+z")
  544. sys.exit(2)
  545. if self.consume():
  546. os.mkdir(self.tmp_dir + "blackhole")
  547. else:
  548. print("[Error] [AI] [Blackhol] Unable to consume in "+self.tmp_dir+"blackhole...")
  549. sys.exit(2)
  550. if not os.path.isdir(self.tmp_dir + "blackhole"):
  551. print("[Error] [AI] [Blackhole] Unable to create "+self.tmp_dir+"blackhole...")
  552. sys.exit(2)
  553. self.blackray = BlackRay(self)
  554. self.absorber = Absorber(self)
  555. self.computer = AI(self)
  556. self.awake = False
  557. print("[Info] [AI] [Blackhole] Having sweet dreams...")
  558. def consume(self):
  559. if os.path.isdir(self.tmp_dir + "blackhole"):
  560. try:
  561. shutil.rmtree(self.tmp_dir + "blackhole")
  562. except OSError as e:
  563. print("[Error] [AI] [Blackhole] Unable to consume : " + str(e))
  564. return False
  565. return True
  566. def try_bind(self, port):
  567. s=None
  568. try:
  569. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  570. s.settimeout(10)
  571. s.bind(('', port))
  572. except socket.error as e:
  573. if e.errno == 98: # if is in use wait a bit and retry
  574. time.sleep(3)
  575. return self.try_bind(port)
  576. print(("[Error] [AI] [Blackhole] Socket busy, connection failed on port " + str(port)))
  577. return s
  578. def run(self):
  579. self.dream()
  580. try:
  581. self.blackray.start()
  582. self.absorber.start()
  583. self.computer.start()
  584. if not self.blackray.shining or self.absorber.overflow or not self.computer.power_on:
  585. print("[Info] [AI] Advancing time in another space (waiting for server)"+os.linesep)
  586. time.sleep(1)
  587. while not self.blackray.shining or self.absorber.overflow or not self.computer.power_on:
  588. time.sleep(1)
  589. print("\n[Info] [AI] [BlackHole] All up and running...")
  590. while self.blackray.shining and not self.absorber.overflow and self.computer.power_on:
  591. time.sleep(1)
  592. except:
  593. traceback.print_exc()
  594. self.awaken()
  595. print("[Info] [AI] [Blackhole] Lifespan is up...")
  596. def collapse(self):
  597. self.blackray.shining = False
  598. self.absorber.overflow = True
  599. self.computer.power_on = False
  600. self.computer.join()
  601. self.blackray.join()
  602. self.absorber.join()
  603. def awaken(self):
  604. self.consume()
  605. self.collapse()
  606. self.awake = True
  607. if __name__ == "__main__":
  608. try:
  609. print("\n[Info] [AI] Initiating void generation sequence...\n")
  610. print('='*22 + '\n')
  611. app = BlackHole()
  612. app.start()
  613. while True: time.sleep(1)
  614. except KeyboardInterrupt:
  615. print("\n[Info] [AI] Terminating void generation sequence...\n")
  616. app.collapse()
  617. except Exception as e:
  618. traceback.print_exc()
  619. print ("\n[Error] [AI] Something wrong creating [Blackhole] -> [Passing!]\n")