-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
50 lines (41 loc) · 1.37 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
import os
from flask import Flask, send_file
from lib.opnsense_api import OpnsenseClient, OpnsenseApi
HOST = os.getenv("OPNSENSE_HOST")
API_KEY = os.getenv("OPNSENSE_API_KEY")
API_SECRET = os.getenv("OPNSENSE_API_SECRET")
client = OpnsenseClient(HOST, API_KEY, API_SECRET)
api = OpnsenseApi(client)
app = Flask(__name__)
@app.context_processor
def inject_globals():
return {
'HOST': HOST,
'APP_NAME': "OPNsense Manager",
# You can even add functions
'is_production': lambda: os.getenv('FLASK_ENV') == 'production'
}
@app.route("/", methods=["GET"])
def index():
return send_file('templates/index.html')
@app.route("/status", methods=["GET"])
def get_status():
try:
enabled = api.get_blocklist_status()
return {"status": "success", "enabled": enabled}, 200
except Exception as e:
return {"status": "error", "message": str(e)}, 500
@app.route("/enable", methods=["POST"])
def enable_blocklist():
try:
api.toggle_unbound_blocklist(True)
return {"status": "success"}, 200
except Exception as e:
return {"status": "error", "message": str(e)}, 500
@app.route("/disable", methods=["POST"])
def disable_blocklist():
try:
api.toggle_unbound_blocklist(False)
return {"status": "success"}, 200
except Exception as e:
return {"status": "error", "message": str(e)}, 500