Added reset button
This commit is contained in:
@@ -11,6 +11,7 @@ from passlib.context import CryptContext
|
||||
from fastapi_socketio import SocketManager
|
||||
from modules import SQLite, FirewallManager
|
||||
from modules.firewall import STATUS
|
||||
from modules.firegex import FiregexTables
|
||||
from utils import get_interfaces, ip_parse, refactor_name, gen_service_id
|
||||
|
||||
ON_DOCKER = len(sys.argv) > 1 and sys.argv[1] == "DOCKER"
|
||||
@@ -69,7 +70,7 @@ async def check_login(token: str = Depends(oauth2_scheme)):
|
||||
try:
|
||||
payload = jwt.decode(token, JWT_SECRET(), algorithms=[settings.JWT_ALGORITHM])
|
||||
logged_in: bool = payload.get("logged_in")
|
||||
except JWTError:
|
||||
except Exception:
|
||||
return False
|
||||
return logged_in
|
||||
|
||||
@@ -374,6 +375,27 @@ async def get_ip_interfaces(auth: bool = Depends(is_loggined)):
|
||||
"""Get a list of ip and ip6 interfaces"""
|
||||
return get_interfaces()
|
||||
|
||||
class ResetRequest(BaseModel):
|
||||
delete:bool
|
||||
|
||||
@app.post('/api/reset', response_model=StatusMessageModel)
|
||||
async def reset_firegex(form: ResetRequest, auth: bool = Depends(is_loggined)):
|
||||
"""Reset firegex nftables rules and optionally all the database"""
|
||||
if not form.delete:
|
||||
db.backup()
|
||||
await firewall.close()
|
||||
FiregexTables().reset()
|
||||
if form.delete:
|
||||
db.delete()
|
||||
db.init()
|
||||
db.put("secret", secrets.token_hex(32))
|
||||
else:
|
||||
db.restore()
|
||||
await firewall.init()
|
||||
await refresh_frontend()
|
||||
|
||||
return {'status': 'ok'}
|
||||
|
||||
async def frontend_debug_proxy(path):
|
||||
httpc = httpx.AsyncClient()
|
||||
req = httpc.build_request("GET",f"http://127.0.0.1:{os.getenv('F_PORT','3000')}/"+path)
|
||||
|
||||
@@ -39,6 +39,7 @@ class FiregexTables:
|
||||
else: raise Exception(err)
|
||||
|
||||
def init(self):
|
||||
self.reset()
|
||||
code, out, err = self.raw_cmd({"create":{"table":{"name":self.table_name,"family":"inet"}}})
|
||||
if code == 0:
|
||||
self.cmd(
|
||||
@@ -61,10 +62,13 @@ class FiregexTables:
|
||||
"policy":"accept"
|
||||
}}}
|
||||
)
|
||||
self.reset()
|
||||
|
||||
|
||||
def reset(self):
|
||||
self.cmd({"flush":{"table":{"name":"firegex","family":"inet"}}})
|
||||
self.raw_cmd(
|
||||
{"flush":{"table":{"name":"firegex","family":"inet"}}},
|
||||
{"delete":{"table":{"name":"firegex","family":"inet"}}},
|
||||
)
|
||||
|
||||
def list(self):
|
||||
return self.cmd({"list": {"ruleset": None}})["nftables"]
|
||||
|
||||
@@ -50,18 +50,24 @@ class SQLite():
|
||||
self.conn.row_factory = dict_factory
|
||||
|
||||
def backup(self):
|
||||
if self.conn:
|
||||
with open(self.db_name, "rb") as f:
|
||||
self.__backup = f.read()
|
||||
with open(self.db_name, "rb") as f:
|
||||
self.__backup = f.read()
|
||||
|
||||
def restore(self):
|
||||
were_active = True if self.conn else False
|
||||
self.disconnect()
|
||||
if self.__backup:
|
||||
with open(self.db_name, "wb") as f:
|
||||
f.write(self.__backup)
|
||||
self.__backup = None
|
||||
|
||||
if were_active: self.connect()
|
||||
|
||||
def delete_backup(self):
|
||||
self.__backup = None
|
||||
|
||||
def disconnect(self) -> None:
|
||||
if self.conn: self.conn.close()
|
||||
self.conn = None
|
||||
|
||||
def create_schema(self, tables = {}) -> None:
|
||||
if self.conn:
|
||||
|
||||
Reference in New Issue
Block a user