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