add/fix: frontend and backend improves
This commit is contained in:
@@ -117,24 +117,24 @@ def parse_and_check_rule(rule:RuleModel):
|
||||
rule.ip_src = ip_parse(rule.ip_src)
|
||||
rule.ip_dst = ip_parse(rule.ip_dst)
|
||||
except ValueError:
|
||||
return {"status":"Invalid address"}
|
||||
raise HTTPException(status_code=400, detail="Invalid address")
|
||||
if ip_family(rule.ip_dst) != ip_family(rule.ip_src):
|
||||
raise HTTPException(status_code=400, detail="Destination and source addresses must be of the same family")
|
||||
|
||||
rule.port_dst_from, rule.port_dst_to = min(rule.port_dst_from, rule.port_dst_to), max(rule.port_dst_from, rule.port_dst_to)
|
||||
rule.port_src_from, rule.port_src_to = min(rule.port_src_from, rule.port_src_to), max(rule.port_src_from, rule.port_src_to)
|
||||
|
||||
if ip_family(rule.ip_dst) != ip_family(rule.ip_src):
|
||||
return {"status":"Destination and source addresses must be of the same family"}
|
||||
|
||||
if rule.proto not in ["tcp", "udp", "any"]:
|
||||
return {"status":"Invalid protocol"}
|
||||
raise HTTPException(status_code=400, detail="Invalid protocol")
|
||||
if rule.action not in ["accept", "drop", "reject"]:
|
||||
return {"status":"Invalid action"}
|
||||
raise HTTPException(status_code=400, detail="Invalid action")
|
||||
return rule
|
||||
|
||||
@app.post('/rules/set', response_model=RuleAddResponse)
|
||||
async def add_new_service(form: RuleFormAdd):
|
||||
"""Add a new service"""
|
||||
if form.policy not in ["accept", "drop", "reject"]:
|
||||
return {"status": "Invalid policy"}
|
||||
raise HTTPException(status_code=400, detail="Invalid policy")
|
||||
rules = [parse_and_check_rule(ele) for ele in form.rules]
|
||||
errors = [({"rule":i} | ele) for i, ele in enumerate(rules) if isinstance(ele, dict)]
|
||||
if len(errors) > 0:
|
||||
@@ -160,5 +160,5 @@ async def add_new_service(form: RuleFormAdd):
|
||||
)
|
||||
db.set("POLICY", form.policy)
|
||||
except sqlite3.IntegrityError:
|
||||
return {'status': 'Error saving the rules: maybe there are duplicated rules'}
|
||||
raise HTTPException(status_code=400, detail="Error saving the rules: maybe there are duplicated rules")
|
||||
return await apply_changes()
|
||||
|
||||
@@ -174,11 +174,11 @@ async def service_delete(service_id: str):
|
||||
async def service_rename(service_id: str, form: RenameForm):
|
||||
"""Request to change the name of a specific service"""
|
||||
form.name = refactor_name(form.name)
|
||||
if not form.name: return {'status': 'The name cannot be empty!'}
|
||||
if not form.name: raise HTTPException(status_code=400, detail="The name cannot be empty!")
|
||||
try:
|
||||
db.query('UPDATE services SET name=? WHERE service_id = ?;', form.name, service_id)
|
||||
except sqlite3.IntegrityError:
|
||||
return {'status': 'This name is already used'}
|
||||
raise HTTPException(status_code=400, detail="This name is already used")
|
||||
await refresh_frontend()
|
||||
return {'status': 'ok'}
|
||||
|
||||
@@ -242,12 +242,12 @@ async def add_new_regex(form: RegexAddForm):
|
||||
try:
|
||||
re.compile(b64decode(form.regex))
|
||||
except Exception:
|
||||
return {"status":"Invalid regex"}
|
||||
raise HTTPException(status_code=400, detail="Invalid regex")
|
||||
try:
|
||||
db.query("INSERT INTO regexes (service_id, regex, is_blacklist, mode, is_case_sensitive, active ) VALUES (?, ?, ?, ?, ?, ?);",
|
||||
form.service_id, form.regex, form.is_blacklist, form.mode, form.is_case_sensitive, True if form.active is None else form.active )
|
||||
except sqlite3.IntegrityError:
|
||||
return {'status': 'An identical regex already exists'}
|
||||
raise HTTPException(status_code=400, detail="An identical regex already exists")
|
||||
|
||||
await firewall.get(form.service_id).update_filters()
|
||||
await refresh_frontend()
|
||||
@@ -259,16 +259,16 @@ async def add_new_service(form: ServiceAddForm):
|
||||
try:
|
||||
form.ip_int = ip_parse(form.ip_int)
|
||||
except ValueError:
|
||||
return {"status":"Invalid address"}
|
||||
raise HTTPException(status_code=400, detail="Invalid address")
|
||||
if form.proto not in ["tcp", "udp"]:
|
||||
return {"status":"Invalid protocol"}
|
||||
raise HTTPException(status_code=400, detail="Invalid protocol")
|
||||
srv_id = None
|
||||
try:
|
||||
srv_id = gen_service_id()
|
||||
db.query("INSERT INTO services (service_id ,name, port, status, proto, ip_int) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
srv_id, refactor_name(form.name), form.port, STATUS.STOP, form.proto, form.ip_int)
|
||||
except sqlite3.IntegrityError:
|
||||
return {'status': 'This type of service already exists'}
|
||||
raise HTTPException(status_code=400, detail="This type of service already exists")
|
||||
await firewall.reload()
|
||||
await refresh_frontend()
|
||||
return {'status': 'ok', 'service_id': srv_id}
|
||||
|
||||
Reference in New Issue
Block a user