blackhole.py 27 KB

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