-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_controller.py
410 lines (361 loc) · 19 KB
/
main_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import cgi
import os
import datetime
import time
import re
# Import webapp
from google.appengine.ext import webapp
# Import handler
from google.appengine.ext.webapp.util import run_wsgi_app
# import webapp template
from google.appengine.ext.webapp import template
# import django context and template render
from django.template import Context, Template
# import load game objects class
from StartMaker import StartMaker
from Reset import Reset
# Global Value to store all game classes objects
# Ugly I know
OB = StartMaker()
reset=Reset()
global ping_flag
ping_flag = [0,0]
class MainPage(webapp.RequestHandler):
def get(self):
# Load index.html page
template_values = {}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
class TurnOn(webapp.RequestHandler):
def post(self):
# synchronizing canvas button
power = self.request.get('power')
cable = self.request.get('cable')
power_ctrl = self.request.get('pctrl')
cable_ctrl = self.request.get('cctrl')
i=0
if power_ctrl == '1':
if power[0] == 'S':
# Switchs power
for n in range(0,3):
if power == 'S'+str(n):
i=n*5
if OB.switch[n].getLigado() == False:
# Turn on Switch
OB.switch[n].setLigado(True)
# Turn on NIC 01 of Switch
OB.nic_switch[0+i].setLigado(True)
# Turn on NIC 02 of Switch
OB.nic_switch[1+i].setLigado(True)
# Turn on NIC 03 of Switch
OB.nic_switch[2+i].setLigado(True)
# Turn on NIC 04 of Switch
OB.nic_switch[3+i].setLigado(True)
# Turn on NIC 05 of Switch
OB.nic_switch[4+i].setLigado(True)
break
elif power[0] == 'R':
# Routers power
for n in range(0,3):
if power == 'R'+str(n):
i=n*4
if OB.router[n].getLigado() == False:
# Turn on Router
OB.router[n].setLigado(True)
# Turn on NIC 01 of Router
OB.nic_router[0+i].setLigado(True)
# Turn on NIC 02 of Router
OB.nic_router[1+i].setLigado(True)
# Turn on NIC 03 of Router
OB.nic_router[2+i].setLigado(True)
# Turn on NIC 04 of Router
OB.nic_router[3+i].setLigado(True)
break
elif power[0] == 'H':
# Hosts power
for n in range(0,12):
if power == 'H'+str(n):
if OB.nic_host[n].getLigado() == False:
# Turn on Host
OB.host[n].setLigado(True)
# Turn on NIC
OB.nic_host[n].setLigado(True)
break
elif power_ctrl == '0':
if power[0] == 'H':
# Hosts power off
for n in range(0,12):
if power == 'H'+str(n):
if OB.nic_host[n].getLigado() == True:
# Turn off Host
OB.host[n].setLigado(False)
# Turn off NIC
OB.nic_host[n].setLigado(False)
break
elif power[0] == 'S':
# Switchs power off
for n in range(0,3):
if power == 'S'+str(n):
i=n*5
if OB.switch[n].getLigado() == True:
# Turn off Switch
OB.switch[n].setLigado(False)
# Turn off NIC 01 of Switch
OB.nic_switch[0+i].setLigado(False)
# Turn off NIC 02 of Switch
OB.nic_switch[1+i].setLigado(False)
# Turn off NIC 03 of Switch
OB.nic_switch[2+i].setLigado(False)
# Turn off NIC 04 of Switch
OB.nic_switch[3+i].setLigado(False)
# Turn off NIC 05 of Switch
OB.nic_switch[4+i].setLigado(False)
break
elif power[0] == 'R':
# Routers power off
for n in range(0,3):
if power == 'R'+str(n):
i=n*4
if OB.router[n].getLigado() == True:
# Turn on Router
OB.router[n].setLigado(False)
# Turn on NIC 01 of Router
OB.nic_router[0+i].setLigado(False)
# Turn on NIC 02 of Router
OB.nic_router[1+i].setLigado(False)
# Turn on NIC 03 of Router
OB.nic_router[2+i].setLigado(False)
# Turn on NIC 04 of Router
OB.nic_router[3+i].setLigado(False)
break
if cable_ctrl == '1':
if cable[0] == 'S':
# Switchs cable
for n in range(0,3):
if cable == 'S'+str(n):
i=n*5
if OB.nic_switch[0+i].getTDados() == False:
# Connect NIC 01 of Switch
OB.nic_switch[0+i].setTDados(True)
# Connect NIC 02 of Switch
OB.nic_switch[1+i].setTDados(True)
# Connect NIC 03 of Switch
OB.nic_switch[2+i].setTDados(True)
# Connect NIC 04 of Switch
OB.nic_switch[3+i].setTDados(True)
# Connect NIC 05 of Switch
OB.nic_switch[4+i].setTDados(True)
break
elif cable[0] == 'R':
# Routers cable
for n in range(0,3):
if cable == 'R'+str(n):
i=n*4
if OB.nic_router[0+i].getTDados() == False:
# Connect NIC 01 of Router
OB.nic_router[0+i].setTDados(True)
# Connect NIC 02 of Router
OB.nic_router[1+i].setTDados(True)
# Connect NIC 03 of Router
OB.nic_router[2+i].setTDados(True)
# Connect NIC 04 of Router
OB.nic_router[3+i].setTDados(True)
break
elif cable[0] == 'H':
# Hosts cable
for n in range(0,12):
if cable == 'H'+str(n):
if OB.nic_host[n].getTDados() == False:
# Connect Host
OB.nic_host[n].setTDados(True)
break
elif cable_ctrl == '0':
if cable[0] == 'H':
# Hosts cable
for n in range(0,12):
if cable == 'H'+str(n):
if OB.nic_host[n].getTDados() == True:
# Disconnect Host
OB.nic_host[n].setTDados(False)
break
elif cable[0] == 'S':
# Switchs off cable
for n in range(0,3):
if cable == 'S'+str(n):
i=n*5
if OB.nic_switch[0+i].getTDados() == True:
# Disconnect NIC 01 of Switch
OB.nic_switch[0+i].setTDados(False)
# Disconnect NIC 02 of Switch
OB.nic_switch[1+i].setTDados(False)
# Disconnect NIC 03 of Switch
OB.nic_switch[2+i].setTDados(False)
# Disconnect NIC 04 of Switch
OB.nic_switch[3+i].setTDados(False)
# Disconnect NIC 05 of Switch
OB.nic_switch[4+i].setTDados(False)
break
elif cable[0] == 'R':
# Routers off cable
for n in range(0,3):
if cable == 'R'+str(n):
i=n*4
if OB.nic_router[0+i].getTDados() == True:
# Disconnect NIC 01 of Router
OB.nic_router[0+i].setTDados(False)
# Disconnect NIC 02 of Router
OB.nic_router[1+i].setTDados(False)
# Disconnect NIC 03 of Router
OB.nic_router[2+i].setTDados(False)
# Disconnect NIC 04 of Router
OB.nic_router[3+i].setTDados(False)
break
# Exit 0
return 0
class Ping(webapp.RequestHandler):
def consIP(self, n, ip, mask):
# Check same of itself
if ip == OB.host[n].getIP():
return True
# Check netID
if ip == OB.ip.netID(ip,mask):
return False
# Check gateway
for i in range(0,3):
if (OB.router[i].getIP() == ip):
return False
# Check duplicate IP
for i in range(0,12):
if (OB.host[i].getIP() == ip):
return False
return True
def post(self):
global ping_flag
message = ''
cmd = self.request.get('textarea');
cmd_split = cmd.split()
n = int(self.request.get('host'))
exp = re.compile('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
# Bash commands dictionary
try:
# check command $ ping <ip>
if cmd_split[0] == 'ping' and exp.match(cmd_split[1])!= None and len(cmd_split) == 2:
try:
# first time
if ping_flag[0] == 0 and ping_flag[1] == 0:
OB.host[n].resetPing()
ping_flag=[1,cmd_split[1]]
# Second and more times
# important test and correction
elif ping_flag[0] == 1 and cmd_split[1] != '0.0.0.0':
OB.host[n].resetPing()
ping_flag=[1,cmd_split[1]]
# check valid host ip
if OB.ip.validaIP(OB.host[n].getIP()) == False:
bash='\nHost '+str(n+1)+'-IP: "'+OB.host[n].getIP()+'" invalid\nSet a valid Host '+str(n+1)+'-IP'
# check valid ip
elif OB.ip.validaIP(ping_flag[1]):
request = OB.host[n].ping(ping_flag[1])
sub_req = request.split(' ')
if sub_req[5] != 'icmp_seq=4':
bash='\n'+request
time.sleep(0.5)
else:
# Statistics
stcs=OB.host[n].statisticsPing()
bash='\n'+request
bash+='\n'+stcs[0]+'\n'+stcs[1]+'\n'+stcs[2]
ping_flag=[0,0]
# ping: unknown host
else:
bash='\nping: unknown host '+cmd_split[1]
except (IndexError):
bash='\nping: unknown host'
# clear
elif cmd_split[0] == 'clear' and len(cmd_split) == 1:
bash='clear'
# bash --version
elif cmd_split[0] == 'bash' and cmd_split[1] == '--version' and len(cmd_split) == 2:
bash='bash --version'
# ifconfig
elif cmd_split[0] == 'ifconfig' and len(cmd_split) == 1:
bash='\neth0 Link encap:Ethernet HWaddr '+OB.host[n].getMAC()+'\n inet addr:'+OB.host[n].getIP()+' Bcast:[not avaiable] Mask:'+OB.host[n].getMascara()+'\n\nCable connect: '+str(OB.nic_host[n].getTDados())
# ifconfig <IP>
elif cmd_split[0] == 'ifconfig' and exp.match(cmd_split[1])!= None and len(cmd_split) == 2:
if OB.ip.validaIP(cmd_split[1]) == False:
bash='\nIP: "'+cmd_split[1]+'" invalid'
elif self.consIP(n,cmd_split[1],OB.host[n].getMascara()) == False:
bash='\nIP: "'+cmd_split[1]+'" unavailable'
else:
OB.host[n].setIP(cmd_split[1])
bash='\neth0 Link encap:Ethernet HWaddr '+OB.host[n].getMAC()+'\n inet addr:'+OB.host[n].getIP()+' Bcast:[not avaiable] Mask:'+OB.host[n].getMascara()+'\n\nCable connect: '+str(OB.nic_host[n].getTDados())
# netmask <mask>
elif cmd_split[0] == 'netmask' and exp.match(cmd_split[1])!= None and len(cmd_split) == 2:
if OB.ip.validaMascara(cmd_split[1]) == False:
bash='\nNetmask: "'+cmd_split[1]+'" invalid'
else:
OB.host[n].setMascara(cmd_split[1])
bash='\neth0 Link encap:Ethernet HWaddr '+OB.host[n].getMAC()+'\n inet addr:'+OB.host[n].getIP()+' Bcast:[not avaiable] Mask:'+OB.host[n].getMascara()+'\n\nCable connect: '+str(OB.nic_host[n].getTDados())
# ifconfig <IP> netmask <mask>
elif cmd_split[0] == 'ifconfig' and exp.match(cmd_split[1])!= None and cmd_split[2] == 'netmask' and exp.match(cmd_split[3])!= None and len(cmd_split) == 4:
if OB.ip.validaIP(cmd_split[1]) == False:
bash='\nIP: "'+cmd_split[1]+'" invalid'
elif self.consIP(n,cmd_split[1],OB.host[n].getMascara()) == False:
bash='\nIP: "'+cmd_split[1]+'" unavailable'
elif OB.ip.validaMascara(cmd_split[3]) == False:
OB.host[n].setIP(cmd_split[1])
bash='\nNetmask: "'+cmd_split[3]+'" invalid\neth0 Link encap:Ethernet HWaddr '+OB.host[n].getMAC()+'\n inet addr:'+OB.host[n].getIP()+' Bcast:[not avaiable] Mask:'+OB.host[n].getMascara()+'\n\nCable connect: '+str(OB.nic_host[n].getTDados())
else:
OB.host[n].setIP(cmd_split[1])
OB.host[n].setMascara(cmd_split[3])
bash='\neth0 Link encap:Ethernet HWaddr '+OB.host[n].getMAC()+'\n inet addr:'+OB.host[n].getIP()+' Bcast:[not avaiable] Mask:'+OB.host[n].getMascara()+'\n\nCable connect: '+str(OB.nic_host[n].getTDados())
# route
elif cmd_split[0] == 'route' and len(cmd_split) == 1:
if (n < 4):
bash='\nKernel IP routing table\nDestination Gateway Genmask Flags Metric Ref Use Iface\n201.10.0.0 * '+OB.host[n].getMascara()+' U 1 0 0 eth0\nlink-local * '+OB.host[n].getMascara()+' U 1000 0 0 eth0\ndefault '+OB.router[0].getIP()+' 0.0.0.0 UG 0 0 0 eth0'
elif (n >= 4 and n < 8):
bash='\nKernel IP routing table\nDestination Gateway Genmask Flags Metric Ref Use Iface\n200.171.0.0 * '+OB.host[n].getMascara()+' U 1 0 0 eth0\nlink-local * '+OB.host[n].getMascara()+' U 1000 0 0 eth0\ndefault '+OB.router[1].getIP()+' 0.0.0.0 UG 0 0 0 eth0'
elif (n >= 8):
bash='\nKernel IP routing table\nDestination Gateway Genmask Flags Metric Ref Use Iface\n192.168.0.0 * '+OB.host[n].getMascara()+' U 1 0 0 eth0\nlink-local * '+OB.host[n].getMascara()+' U 1000 0 0 eth0\ndefault '+OB.router[2].getIP()+' 0.0.0.0 UG 0 0 0 eth0'
else:
bash='\nRoute-not-found'
# help
elif cmd_split[0] == 'help' and len(cmd_split) == 1:
bash='help'
# date
elif cmd_split[0] == 'date' and len(cmd_split) == 1:
bash='\n'+str(datetime.date.today())
# bash easteregg
elif cmd_split[0] == 'bash' and cmd_split[1] == 'easteregg' and cmd_split[2] == 'momo' and len(cmd_split) == 3:
bash='\n^>.>^ ^<.<^ ^>_>^ ^<_<^\nMoreno Cunha Tokyo-2010'
# exit
elif cmd_split[0] == 'exit' and len(cmd_split) == 1:
bash='exit'
# command-not-found
else:
bash='\n"'+cmd+'" command-not-found\nType help to command list'
except (IndexError):
bash=''
# send n, bash, terminal_title
message=str(n)+'|:|'+str(bash)+'|:|'+OB.host[n].getIP()
self.response.out.write(message)
class Reset(webapp.RequestHandler):
def post(self):
reset.doReset(OB)
self.redirect('/')
class HowTo(webapp.RequestHandler):
def post(self):
template_values = {}
path = os.path.join(os.path.dirname(__file__), 'howto.html')
self.response.out.write(template.render(path, template_values))
application = webapp.WSGIApplication(
[('/', MainPage),
('/on', TurnOn),
('/reset', Reset),
('/howto', HowTo),
('/ping', Ping)],
debug=False)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()