-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.py
More file actions
181 lines (146 loc) · 5.89 KB
/
app.py
File metadata and controls
181 lines (146 loc) · 5.89 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
#!/usr/bin/python3
""" ProxyWeb - A Proxysql Web user interface
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
"""
__author__ = "Miklos Mukka Szel"
__contact__ = "email@miklos-szel.com"
__license__ = "GPLv3"
import logging
from collections import defaultdict
from flask import Flask, render_template, request, session, url_for, flash, redirect
from functools import wraps
import re
import mdb
app = Flask(__name__)
config = "config/config.yml"
db = defaultdict(lambda: defaultdict(dict))
# read/apply the flask config from the config file
flask_custom_config = mdb.get_config(config)
for key in flask_custom_config['flask']:
app.config[key] = flask_custom_config['flask'][key]
mdb.logging.debug(flask_custom_config)
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if not session.get('logged_in'):
flash('You must be logged in to access this page.', 'warning')
return redirect(url_for('login'))
return f(*args, **kwargs)
return decorated_function
@app.route('/login', methods=['GET', 'POST'])
def login():
session.clear()
message=""
admin_user = mdb.get_config(config)['auth']['admin_user']
admin_password = mdb.get_config(config)['auth']['admin_password']
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
if admin_user == username and admin_password == password:
session['logged_in'] = True
return redirect(url_for('render_list_dbs'))
message="Invalid credentials!"
return render_template("login.html", message=message)
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('login'))
@app.route('/')
@login_required
def render_list_dbs():
try:
server = mdb.get_config(config)['global']['default_server']
session['history'] = []
session['server'] = server
session['dblist'] = mdb.get_all_dbs_and_tables(db, server)
session['servers'] = mdb.get_servers()
session['read_only'] = mdb.get_read_only(server)
session['misc'] = mdb.get_config(config)['misc']
return render_template("list_dbs.html", server=server)
except Exception as e:
raise ValueError(e)
@app.route('/<server>/')
@app.route('/<server>/<database>/<table>/')
@login_required
def render_show_table_content(server, database="main", table="global_variables"):
try:
# refresh the tablelist if changing to a new server
if server not in session['dblist']:
session['dblist'].update(mdb.get_all_dbs_and_tables(db, server))
session['servers'] = mdb.get_servers()
session['server'] = server
session['table'] = table
session['database'] = database
session['misc'] = mdb.get_config(config)['misc']
session['read_only'] = mdb.get_read_only(server)
content = mdb.get_table_content(db, server, database, table)
mdb.process_table_content(table,content)
return render_template("show_table_info.html", content=content)
except Exception as e:
raise ValueError(e)
@app.route('/<server>/<database>/<table>/sql/', methods=['GET', 'POST'])
@login_required
def render_change(server, database, table):
try:
error = ""
message = ""
ret = ""
session['sql'] = request.form["sql"]
mdb.logging.debug(session['history'])
select = re.match(r'^SELECT.*FROM.*$', session['sql'], re.M | re.I)
if select:
content = mdb.execute_adhoc_query(db, server, session['sql'])
content['order'] = 'true'
else:
ret = mdb.execute_change(db, server, session['sql'])
content = mdb.get_table_content(db, server, database, table)
if "ERROR" in ret:
error = ret
else:
message = "Success"
if session['sql'].replace("\r\n","") not in session['history'] and not error:
session['history'].append(session['sql'].replace("\r\n",""))
return render_template("show_table_info.html", content=content, error=error, message=message)
except Exception as e:
raise ValueError(e)
@app.route('/<server>/adhoc/')
@login_required
def adhoc_report(server):
try:
adhoc_results = mdb.execute_adhoc_report(db, server)
return render_template("show_adhoc_report.html", adhoc_results=adhoc_results)
except Exception as e:
raise ValueError(e)
@app.route('/settings/<action>/', methods=['GET', 'POST'])
@login_required
def render_settings(action):
try:
config_file_content = ""
message = ""
if action == 'edit':
with open(config, "r") as f:
config_file_content = f.read()
if action == 'save':
# back it up first
with open(config, "r") as src, open(config + ".bak", "w") as dest:
dest.write(src.read())
with open(config, "w") as f:
f.write(request.form["settings"])
message = "success"
return render_template("settings.html", config_file_content=config_file_content, message=message)
except Exception as e:
raise ValueError(e)
@app.errorhandler(Exception)
def handle_exception(e):
print(e)
return render_template("error.html", error=e), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', use_debugger=False)