-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmiminet_simulation.py
More file actions
79 lines (56 loc) · 2.64 KB
/
Copy pathmiminet_simulation.py
File metadata and controls
79 lines (56 loc) · 2.64 KB
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
import os
from flask import request, flash, redirect, jsonify, make_response, url_for
from flask_login import login_required, current_user
from miminet_model import db, Simulate, Network, SimulateLog
@login_required
def run_simulation():
user = current_user
network_guid = request.args.get('guid', type=str)
if not network_guid:
ret = {'simulation_id': 0, 'message': 'Пропущен параметр GUID. И какую сеть мне симулировать?!'}
return make_response(jsonify(ret), 400)
net = Network.query.filter(Network.guid == network_guid).filter(Network.author_id == user.id).first()
if not net:
ret = {'simulation_id': 0, 'message': 'Нет такой сети'}
return make_response(jsonify(ret), 400)
if request.method == "POST":
sims = Simulate.query.filter(Simulate.network_id == net.id).all()
# Remove all previous simulations
for s in sims:
db.session.delete(s)
db.session.commit()
simlog = SimulateLog(author_id=net.author_id, network=net.network, network_guid=net.guid)
sim = Simulate(network_id=net.id, packets='')
db.session.add(sim)
db.session.add(simlog)
db.session.flush()
db.session.refresh(sim)
db.session.commit()
ret = {'simulation_id': sim.id}
return make_response(jsonify(ret), 201)
return redirect(url_for('home'))
@login_required
def check_simulation():
user = current_user
simulation_id = request.args.get('simulation_id', type=int)
network_guid = request.args.get('network_guid', type=str)
if not simulation_id:
ret = {'message': 'Пропущен параметр simulation_id.'}
return make_response(jsonify(ret), 400)
if not network_guid:
ret = {'message': 'Пропущен параметр network_guid.'}
return make_response(jsonify(ret), 400)
sim = Simulate.query.filter(Simulate.id == simulation_id).first()
if not sim:
ret = {'message': 'Нет такой симуляции'}
return make_response(jsonify(ret), 400)
if sim.ready:
# Check for a pcaps
pcap_dir = 'static/pcaps/' + network_guid
pcaps = []
if os.path.exists(pcap_dir):
pcaps = [os.path.splitext(f)[0] for f in os.listdir(pcap_dir) if os.path.isfile(os.path.join(pcap_dir, f))]
ret = {'message': 'Симуляция завершена', 'packets': sim.packets, 'pcaps': pcaps}
return make_response(jsonify(ret), 200)
ret = {'message': 'Сеть в процессе симуляции'}
return make_response(jsonify(ret), 210)