react-query + enable/disable firewall

This commit is contained in:
Domingo Dirutigliano
2023-09-24 05:48:54 +02:00
parent 732680753c
commit 4247dc181d
36 changed files with 283 additions and 331 deletions

View File

@@ -20,5 +20,8 @@ class FirewallManager:
async def reload(self):
async with self.lock:
nft.set(map(Rule.from_dict, self.db.query('SELECT * FROM rules WHERE active = 1 ORDER BY rule_id;')), policy=self.db.get('POLICY', 'accept'))
if self.db.get("ENABLED", "0") == "1":
nft.set(map(Rule.from_dict, self.db.query('SELECT * FROM rules WHERE active = 1 ORDER BY rule_id;')), policy=self.db.get('POLICY', 'accept'))
else:
nft.reset()

View File

@@ -20,9 +20,14 @@ class RuleModel(BaseModel):
action: str
mode:str
class RuleForm(BaseModel):
class RuleFormAdd(BaseModel):
rules: list[RuleModel]
policy: str
class RuleInfo(BaseModel):
rules: list[RuleModel]
policy: str
enabled: bool
class RuleAddResponse(BaseModel):
status:str|list[dict]
@@ -30,9 +35,6 @@ class RuleAddResponse(BaseModel):
class RenameForm(BaseModel):
name:str
class GeneralStatModel(BaseModel):
rules: int
app = APIRouter()
db = SQLite('db/firewall-rules.db', {
@@ -85,18 +87,27 @@ async def apply_changes():
await refresh_frontend()
return {'status': 'ok'}
@app.get('/stats', response_model=GeneralStatModel)
async def get_general_stats():
"""Get firegex general status about rules"""
return db.query("SELECT (SELECT COUNT(*) FROM rules) rules")[0]
@app.get('/rules', response_model=RuleForm)
@app.get('/rules', response_model=RuleInfo)
async def get_rule_list():
"""Get the list of existent firegex rules"""
return {
"policy": db.get("POLICY", "accept"),
"rules": db.query("SELECT active, name, proto, ip_src, ip_dst, port_src_from, port_dst_from, port_src_to, port_dst_to, action, mode FROM rules ORDER BY rule_id;")
"rules": db.query("SELECT active, name, proto, ip_src, ip_dst, port_src_from, port_dst_from, port_src_to, port_dst_to, action, mode FROM rules ORDER BY rule_id;"),
"enabled": db.get("ENABLED", "0") == "1"
}
@app.get('/enable', response_model=StatusMessageModel)
async def enable_firewall():
"""Request enabling the firewall"""
db.set("ENABLED", "1")
return await apply_changes()
@app.get('/disable', response_model=StatusMessageModel)
async def disable_firewall():
"""Request disabling the firewall"""
db.set("ENABLED", "0")
return await apply_changes()
@app.get('/rule/{rule_id}/disable', response_model=StatusMessageModel)
async def service_disable(rule_id: str):
"""Request disabling a specific rule"""
@@ -147,7 +158,7 @@ def parse_and_check_rule(rule:RuleModel):
@app.post('/rules/set', response_model=RuleAddResponse)
async def add_new_service(form: RuleForm):
async def add_new_service(form: RuleFormAdd):
"""Add a new service"""
if form.policy not in ["accept", "drop", "reject"]:
return {"status": "Invalid policy"}

View File

@@ -10,11 +10,6 @@ from utils.sqlite import SQLite
from utils import ip_parse, refactor_name, refresh_frontend, PortType
from utils.models import ResetRequest, StatusMessageModel
class GeneralStatModel(BaseModel):
closed:int
regexes: int
services: int
class ServiceModel(BaseModel):
status: str
service_id: str
@@ -116,16 +111,6 @@ def gen_service_id():
firewall = FirewallManager(db)
@app.get('/stats', response_model=GeneralStatModel)
async def get_general_stats():
"""Get firegex general status about services"""
return db.query("""
SELECT
(SELECT COALESCE(SUM(blocked_packets),0) FROM regexes) closed,
(SELECT COUNT(*) FROM regexes) regexes,
(SELECT COUNT(*) FROM services) services
""")[0]
@app.get('/services', response_model=list[ServiceModel])
async def get_service_list():
"""Get the list of existent firegex services"""
@@ -200,6 +185,7 @@ async def service_rename(service_id: str, form: RenameForm):
@app.get('/service/{service_id}/regexes', response_model=list[RegexModel])
async def get_service_regexe_list(service_id: str):
"""Get the list of the regexes of a service"""
if not db.query("SELECT 1 FROM services s WHERE s.service_id = ?;", service_id): raise HTTPException(status_code=400, detail="This service does not exists!")
return db.query("""
SELECT
regex, mode, regex_id `id`, service_id, is_blacklist,

View File

@@ -34,9 +34,6 @@ class ServiceAddResponse(BaseModel):
status:str
service_id: str|None = None
class GeneralStatModel(BaseModel):
services: int
app = APIRouter()
db = SQLite('db/port-hijacking.db', {
@@ -87,14 +84,6 @@ def gen_service_id():
firewall = FirewallManager(db)
@app.get('/stats', response_model=GeneralStatModel)
async def get_general_stats():
"""Get firegex general status about services"""
return db.query("""
SELECT
(SELECT COUNT(*) FROM services) services
""")[0]
@app.get('/services', response_model=list[ServiceModel])
async def get_service_list():
"""Get the list of existent firegex services"""

View File

@@ -196,6 +196,7 @@ class RegexModel(BaseModel):
@app.get('/service/{service_id}/regexes', response_model=list[RegexModel])
async def get_service_regexe_list(service_id: str):
"""Get the list of the regexes of a service"""
if not db.query("SELECT 1 FROM services s WHERE s.service_id = ?;", service_id): raise HTTPException(status_code=400, detail="This service does not exists!")
return db.query("""
SELECT
regex, mode, regex_id `id`, service_id, is_blacklist,