-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
209 lines (161 loc) · 6.92 KB
/
Copy pathapp.py
File metadata and controls
209 lines (161 loc) · 6.92 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
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
from flask import Flask, render_template, redirect, url_for, request
import requests
from pprint import pprint
import sqlite3
from db import dbaccess,timer
import json
# create the application object
app = Flask(__name__)
# Global variable
success_string=""
sdnController=""
# Redirects to user login page
@app.route('/')
def home():
return redirect(url_for('userLogin'))
# User adding page
@app.route('/user_add',methods=['GET', 'POST'])
def userAdd():
error =None
if request.method == 'GET':
items = dbaccess.getAllUsers()
return render_template('user_add.html',items=items)
if request.method == 'POST':
username = str(request.form['username'])
passwd = str(request.form['password'])
user_group = str(request.form['user_group'])
evict_time = str(request.form['evict_time'])
if not evict_time:
evict_time_int = 0
else:
evict_time_int = int(evict_time)
status = "Offline"
dbaccess.insertBeforeLogin(username,passwd,user_group,evict_time_int,status)
items = dbaccess.getAllUsers()
return render_template('user_add.html',items=items)
return render_template('user_add.html',error=error)
# Adding new server details
@app.route('/server_add', methods=['GET','POST'])
def serverAdd():
error =None
global sdnController
if request.method == 'GET':
items = dbaccess.getServertable()
return render_template('server_add.html',items=items)
if request.method == 'POST':
internal_ip = str(request.form['internal'])
external_ip = str(request.form['external'])
authServer_ip = str(request.form['authentication'])
internal_mac = str(request.form['internal_mac'])
external_mac = str(request.form['external_mac'])
authServer_mac = str(request.form['authentication_mac'])
internal_port = str(request.form['internal_port'])
external_port = str(request.form['external_port'])
authServer_port = str(request.form['authentication_port'])
sdnController_ip = str(request.form['sdnController'])
dbaccess.insertServertable(internal_ip,external_ip,authServer_ip,internal_mac,external_mac, \
authServer_mac,internal_port,external_port,authServer_port,sdnController_ip)
items = dbaccess.getServertable()
server_config = dbaccess.getServerconfig()
sdnController = server_config['sdnController_ip']
url = 'http://'+sdnController+':5010/serverconfig'
del(server_config['sdnController_ip'])
sendConfig(server_config,url)
return render_template('server_add.html',items=items)
return render_template('server_add.html')
# Returns login success page
@app.route('/login_success', methods=['GET','POST'])
def loginSuccess():
return render_template('login_success.html', success_string=success_string)
# Admin login page
@app.route('/admin_login',methods=['GET','POST'])
def admin():
error =None
sample_dict = (vars(request))
sam_dict = sample_dict['environ']
if request.method == 'POST':
if request.form['username'] != 'admin' or request.form['password'] != 'admin':
error = 'Invalid Credentials'
else:
return redirect(url_for('userAdd'))
return render_template('admin_login.html',error=error)
#Deleting the user
@app.route('/user_delete',methods=['GET','POST'])
def userDelete():
error =None
sample_dict = (vars(request))
if request.method == 'GET':
items = dbaccess.getAllUsers()
return render_template('user_delete.html',items=items)
if request.method == 'POST':
username = str(request.form['username'])
user_dict = dbaccess.getUserDetails(username)
dbaccess.deleteUser(username)
success = "User successfully deleted !!"
data = {'user_name':user_dict['name'], 'ip_address':user_dict['ip_addr'],'policy_type':user_dict['user_group']}
server_config = dbaccess.getServerconfig()
sdnController = server_config['sdnController_ip']
url = 'http://'+sdnController+':5010/evictuser'
sendConfig(data,url)
items = dbaccess.getAllUsers()
return render_template('user_delete.html',items=items)
return render_template('user_delete.html',error=error)
# User Login page
@app.route('/user_login',methods=['GET','POST'])
def userLogin():
error =None
global success_string
request_dict = (vars(request))
environ_dict = request_dict['environ']
ip_addr = str(environ_dict['REMOTE_ADDR'])
if request.method == 'POST':
if dbaccess.isOnline(str(request.form['username'])):
error = 'You are already logged in'
elif not dbaccess.isValid(str(request.form['username']), str(request.form['password'])):
error = 'Invalid Credentials'
else:
username = str(request.form['username'])
# Device types being detected when the user logs in
device = getDeviceType(environ_dict)
ip_addr = str(environ_dict['REMOTE_ADDR'])
status = "Online"
dbaccess.insertAfterLogin(username,device,ip_addr,status)
user_dict = dbaccess.getUserDetails(username)
if user_dict['user_group'] != 'Employee':
# Link timer here
timer.startTimer(username,user_dict['evict_time'])
data = {'user_name':user_dict['name'], 'ip_address':user_dict['ip_addr'],'policy_type':user_dict['user_group']}
server_config = dbaccess.getServerconfig()
sdnController = server_config['sdnController_ip']
url = 'http://'+sdnController+':5010/authenticateduser'
sendConfig(data,url)
success_string = 'Your login is successful from your ' + device + " device with IP " + ip_addr + "."
return redirect(url_for('loginSuccess'))
return render_template('user_login.html',error=error)
# Sends configuration data to the controller
def sendConfig(data,url):
data_json = json.dumps(data)
print 'JSON being sent - ', data_json
print 'URL - ', url
headers = {'Content-type': 'application/json'}
# response = requests.post(url, data=data_json, headers=headers)
# pprint.pprint(response.json())
def getDeviceType(environ_dict):
if 'Ubuntu' in environ_dict['HTTP_USER_AGENT']:
device = "Ubuntu"
if 'Macintosh' in environ_dict['HTTP_USER_AGENT']:
device = "Macintosh"
if 'iPad' in environ_dict['HTTP_USER_AGENT']:
device = "iPad"
if 'iPhone' in environ_dict['HTTP_USER_AGENT']:
device = "iPhone"
if 'iPod' in environ_dict['HTTP_USER_AGENT']:
device = "iPod"
if 'Android' in environ_dict['HTTP_USER_AGENT']:
device = "Android"
if 'Windows' in environ_dict['HTTP_USER_AGENT']:
device = "Windows"
return device
# start the server with the 'run()' method
if __name__ == '__main__':
app.run(debug=True, host='127.0.0.1', port=80)