Skip to content

Commit

Permalink
switch from bottle to flask
Browse files Browse the repository at this point in the history
  • Loading branch information
furlongm committed Dec 4, 2024
1 parent fa4b2b7 commit 4d92c20
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
49 changes: 28 additions & 21 deletions openvpn-monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,36 +843,43 @@ def monitor_wsgi():
else:
image_dir = ''

app = Bottle()
app = Flask(__name__)
app.url_map.strict_slashes = False
if app.debug:
args.debug = True

def render(**kwargs):
global wsgi_output
wsgi_output = ''
main(**kwargs)
response.content_type = 'text/html;'
return wsgi_output
return make_response(wsgi_output)

@app.hook('before_request')
@app.before_request
def strip_slash():
request.environ['PATH_INFO'] = request.environ.get('PATH_INFO', '/').rstrip('/')
if args.debug:
if args.debug or app.debug:
debug(pformat(request.environ))
rp = request.path
if rp != '/' and rp.endswith('/'):
return redirect(rp.rstrip('/'))

Check warning

Code scanning / CodeQL

URL redirection from remote source Medium

Untrusted URL redirection depends on a
user-provided value
.

@app.route('/', method='GET')
def get_slash():
return render()

@app.route('/', method='POST')
def post_slash():
vpn_id = request.forms.get('vpn_id')
ip = request.forms.get('ip')
port = request.forms.get('port')
client_id = request.forms.get('client_id')
return render(vpn_id=vpn_id, ip=ip, port=port, client_id=client_id)

@app.route('/<filename:re:.*\.(jpg|png)>', method='GET')
@app.route('/', methods=['GET', 'POST'])
def handle_root():
if args.debug or app.debug:
debug(pformat(request.environ))
if request.method == 'GET':
return render()
elif request.method == 'POST':
vpn_id = request.forms.get('vpn_id')
ip = request.forms.get('ip')
port = request.forms.get('port')
client_id = request.forms.get('client_id')
return render(vpn_id=vpn_id, ip=ip, port=port, client_id=client_id)

@app.route('/images/flags/<filename>', methods=['GET'])
def get_images(filename):
return static_file(filename, image_dir)
if args.debug or app.debug:
debug(pformat(request.environ))
return send_from_directory(image_dir + 'images/flags', filename)

return app

Expand All @@ -883,7 +890,7 @@ def get_images(filename):
if __file__ != 'openvpn-monitor.py':
os.chdir(os.path.dirname(__file__))
sys.path.append(os.path.dirname(__file__))
from bottle import Bottle, response, request, static_file
from flask import Flask, request, redirect, make_response, send_from_directory

class args(object):
debug = False
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
geoip2==4.7.0
humanize==4.8.0
bottle==0.12.25
Flask==3.1.0
semver==2.13.0

0 comments on commit 4d92c20

Please sign in to comment.