-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.py
350 lines (294 loc) · 10.8 KB
/
app.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
import socket
import json
import os
import secrets
from typing import Union, Any
from werkzeug.exceptions import HTTPException
import docker
import logging
from collections import OrderedDict
from shutil import copyfile
from bs4 import BeautifulSoup
from itsdangerous import URLSafeTimedSerializer, BadSignature, SignatureExpired
from requests import get
from flask import jsonify
from flask import make_response, send_file
from flask import Flask, Response, redirect, request, session, render_template, abort
from flask_login import LoginManager, UserMixin, login_required, login_user, logout_user
log = logging.getLogger(__name__)
FORMAT = "[%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s"
BACKUP_FILE_NAME = 'backup.tar.gz.cpt'
BACKUP_DIR = '/data/encrypted'
logging.basicConfig(format=FORMAT)
log.setLevel(logging.ERROR)
client = docker.DockerClient(base_url=os.environ.get('DOCKER_SOCK'))
with open('config.json', 'r') as f:
try:
config = json.load(f, object_pairs_hook=OrderedDict)
config_errors = None
except Exception:
config = dict()
config_errors = 'Configuration file config.json is not valid or missing'
app = Flask(__name__)
app.config.update(
DEBUG=False,
SECRET_KEY=secrets.token_urlsafe(64)
)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
@app.errorhandler(HTTPException)
def handle_exception(e):
return render_template('error.html', code=e.code, message=e.description, name=e.name)
def get_apps_name_version(apps_info: str) -> list:
"""
Get apps_info string with format => appName-version
And returns next data structure: [
{
'name':'appName',
'version':'version'
}
]
"""
if not apps_info:
return list()
app_list = apps_info.split(',')
result: list = []
for i in range(len(app_list)):
temp_list = app_list[i].split('-')
result.append({
'name': temp_list[0],
'version': temp_list[1]
})
return result
config['apps_info'] = get_apps_name_version(config.get('apps_info', ''))
def check_hash(hash: str) -> dict:
deployment_hash = os.environ.get('DEPLOYMENT_HASH')
hash_to_verify = hash
s = URLSafeTimedSerializer(deployment_hash)
new_hash = s.dumps(deployment_hash)
response = {
'status': 'OK',
'hash': new_hash
}
if not deployment_hash or not hash_to_verify or (hash_to_verify and not deployment_hash == hash_to_verify):
response = {
'status': "ERROR",
}
return response
class User(UserMixin):
def __init__(self, id: int):
self.id = id
self.name = os.environ.get('STATUS_PANEL_USERNAME')
self.password = os.environ.get('STATUS_PANEL_PASSWORD')
def __repr__(self):
return "%d/%s" % (self.id, self.name)
def get_self_hosted_services(port_bindings: dict, ip) -> list:
"""
Check if port opened in container is for self-hosted service
:param port_bindings:
:return: list of ports for self-hosted services with their titles or empty list [{port:1234, title:Status Panel}]
"""
service_ports: list = list()
for key in port_bindings:
for net in port_bindings[key]:
try:
r = get(f"http://{ip}:{net['HostPort']}")
soup = BeautifulSoup(r.text)
title = soup.find('title')
if r.status_code == 200:
service_ports.append({
'port': net.get('HostPort'),
'title': title.string
})
except Exception as e:
log.debug(e)
return service_ports
def get_ip_address():
"""
Gets machines IP address
:return: str
"""
try:
IP_API_MAP = [
'https://api.ipify.org',
'https://ipinfo.io/ip',
'https://ifconfig.me/ip'
]
for api in IP_API_MAP:
ip = get(api)
if ip.status_code == 200:
return ip.text
except Exception as e:
log.exception(e)
return 'undefined'
@app.route('/')
@login_required
def home():
ip = get_ip_address()
if 'ssl_enabled' not in session:
session['ssl_enabled'] = False
container_list = []
containers = client.containers.list()
for container in containers:
logs = ''.join([lg for lg in container.logs(tail=100, follow=False, stdout=True).decode('utf-8')])
ports = get_self_hosted_services(container.attrs['HostConfig']['PortBindings'], ip)
log.debug(ports)
if container.name != 'status':
container_list.append({"name": container.name, "status": container.status, "logs": logs, "ports": ports})
try:
domain_ip = socket.gethostbyname(config.get('domain'))
except Exception as e:
domain_ip = ""
log.exception(e)
can_enable = ip == domain_ip
return render_template('index.html', ip=ip, domainIp=domain_ip, can_enable=can_enable,
container_list=container_list, ssl_enabled=session['ssl_enabled'],
domain=config.get('domain'), apps_info=config.get('apps_info'),
panel_version='0.1.0', ip_help_link=os.environ.get('IP_HELP_LINK'), errors=config_errors)
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == 'POST':
user = User(1)
username = request.form['username']
password = request.form['password']
if password == user.password and username == user.name:
login_user(user)
return redirect("/")
else:
return render_template('login.html', error=True)
else:
return render_template('login.html')
def mk_cmd(_config: dict[str, Union[Any, Any]] = None):
# a string of domains and subdomains is expected in the newer config.json format.
# domains are separated by comma
doms = _config or config['subdomains']
# print(f"doms = {doms}")
if isinstance(doms, dict):
domains: str = '{}'.format(' '.join(map("-d {0} ".format, doms.values())))
elif doms is not None and isinstance(doms, str):
domains: str = '{}'.format(' '.join(map("-d {0} ".format, doms.split(','))))
else:
domains = ''
# Run registration command (with client email)
reg_cmd = f"certbot register --email {config['reqdata']['email']} --agree-tos -n"
# Run command to generate certificates with redirect HTTP traffic to HTTPS, removing HTTP access
crt_cmd = f"certbot --nginx --redirect {domains}"
# Run command to generate certificates without redirect
# certbot --nginx --no-redirect -d domain.com
log.info(f"Executing command: {crt_cmd}")
return reg_cmd, crt_cmd
@app.route('/enable_ssl')
@login_required
def enable_ssl():
domain_list = config['subdomains']
client.containers.get(os.environ.get('NGINX_CONTAINER')).exec_run(
"mkdir -p /tmp/letsencrypt/.well-known/acme-challenge"
)
if config['ssl'] == 'letsencrypt':
reg_cmd, crt_cmd = mk_cmd()
try:
log.info('Starting certbot..')
res = client.containers.get(os.environ.get('NGINX_CONTAINER')).exec_run(reg_cmd)
log.info(res)
res = client.containers.get(os.environ.get('NGINX_CONTAINER')).exec_run(crt_cmd)
log.info(res)
client.containers.get(os.environ.get('NGINX_CONTAINER')).restart()
except Exception as e:
log.exception(e)
return redirect("/")
else:
try:
for fname in domain_list:
copyfile("./origin_conf/ssl-conf.d/{}.conf".format(fname),
"./destination_conf/conf.d/{}.conf".format(fname))
client.containers.get(os.environ.get('NGINX_CONTAINER')).restart()
log.debug('Self signed SSL conf file was replaced')
except Exception as e:
log.debug(e)
return redirect("/")
session['ssl_enabled'] = True
return redirect("/")
@app.route('/disable_ssl')
@login_required
def disable_ssl():
domain_list = config['subdomains']
try:
log.debug('Disable SSL')
for fname in domain_list:
copyfile("./origin_conf/conf.d/{}.conf".format(fname), "./destination_conf/conf.d/{}.conf".format(fname))
client.containers.get(os.environ.get('NGINX_CONTAINER')).restart()
except Exception as e:
log.debug(e)
return redirect("/")
session['ssl_enabled'] = False
return redirect("/")
@app.route('/restart/<container>')
@login_required
def restart(container):
try:
client.containers.get(container).restart()
except Exception as e:
log.exception(e)
return redirect("/")
@app.route('/stop/<container>')
@login_required
def stop(container):
try:
client.containers.get(container).stop()
except Exception as e:
log.exception(e)
return redirect("/")
@app.route('/pause/<container>')
@login_required
def pause(container):
try:
client.containers.get(container).pause()
except Exception as e:
log.exception(e)
return redirect("/")
@app.route("/logout")
@login_required
def logout():
logout_user()
return redirect("/login")
# handle login failed
@app.errorhandler(401)
def page_not_found():
return Response('<p>Login failed</p>')
# callback to reload the user object
@login_manager.user_loader
def load_user(userid):
return User(userid)
@app.route("/backup/ping", methods=["POST"])
def backup_ping():
# Check IP
if request.environ['REMOTE_ADDR'] != os.environ.get('TRYDIRECT_IP'):
return make_response(jsonify({"error": "Invalid IP"}), 400)
try:
args = json.loads(request.data.decode("utf-8"))
except Exception:
return make_response(jsonify({"error": "Invalid JSON"}), 400)
response = check_hash(args.get('hash'))
return make_response(jsonify(response), 200)
@app.route("/backup/<hash>/<target_ip>", methods=["GET"])
def return_backup(hash: str, target_ip: str):
# Check hash
deployment_hash = os.environ.get('DEPLOYMENT_HASH')
s = URLSafeTimedSerializer(deployment_hash)
try:
s.loads(hash, max_age=1800) # 30 mins in secs
except (BadSignature, SignatureExpired) as ex:
log.exception(ex)
return make_response(jsonify({"error": "Invalid hash"}), 400)
# Check IP
if request.environ['REMOTE_ADDR'] != target_ip:
return make_response(jsonify({"error": "Invalid IP"}), 400)
# If back up file doesn't exist, issue an error
backup_url = '{}/{}'.format(BACKUP_DIR, BACKUP_FILE_NAME)
if os.path.isfile(backup_url):
return send_file(backup_url, attachment_filename=BACKUP_FILE_NAME, as_attachment=True)
else:
return make_response(jsonify({"error": "Backup not found"}), 400)
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0')