Finished backend of hijack-port

This commit is contained in:
DomySh
2022-08-11 18:01:07 +00:00
parent 90538a89dd
commit a0fba5b2f2
4 changed files with 58 additions and 48 deletions

View File

@@ -54,28 +54,24 @@ class ServiceManager:
async def enable(self): async def enable(self):
if not self.active: if not self.active:
async with self.lock: async with self.lock:
await self.restart() nft.delete(self.srv)
nft.add(self.srv)
self._set_status(True) self._set_status(True)
async def disable(self): async def disable(self):
if self.active: if self.active:
async with self.lock: async with self.lock:
await self.stop() nft.delete(self.srv)
self._set_status(False) self._set_status(False)
async def change_port(self, new_port):
self.srv.proxy_port = new_port
if self.active: await self.restart()
def _set_status(self,active): def _set_status(self,active):
self.active = active self.active = active
self.db.query("UPDATE services SET active = ? WHERE service_id = ?;", active, self.srv.service_id) self.db.query("UPDATE services SET active = ? WHERE service_id = ?;", active, self.srv.service_id)
async def start(self):
if not self.active:
nft.delete(self.srv)
nft.add(self.srv)
self._set_status(True)
async def stop(self):
nft.delete(self.srv)
async def restart(self): async def restart(self):
await self.stop() await self.disable()
await self.start() await self.enable()

View File

@@ -145,7 +145,7 @@ async def get_service_list():
""") """)
@app.get('/service/{service_id}', response_model=ServiceModel) @app.get('/service/{service_id}', response_model=ServiceModel)
async def get_service_by_id(service_id: str, ): async def get_service_by_id(service_id: str):
"""Get info about a specific service using his id""" """Get info about a specific service using his id"""
res = db.query(""" res = db.query("""
SELECT SELECT
@@ -164,21 +164,21 @@ async def get_service_by_id(service_id: str, ):
return res[0] return res[0]
@app.get('/service/{service_id}/stop', response_model=StatusMessageModel) @app.get('/service/{service_id}/stop', response_model=StatusMessageModel)
async def service_stop(service_id: str, ): async def service_stop(service_id: str):
"""Request the stop of a specific service""" """Request the stop of a specific service"""
await firewall.get(service_id).next(STATUS.STOP) await firewall.get(service_id).next(STATUS.STOP)
await refresh_frontend() await refresh_frontend()
return {'status': 'ok'} return {'status': 'ok'}
@app.get('/service/{service_id}/start', response_model=StatusMessageModel) @app.get('/service/{service_id}/start', response_model=StatusMessageModel)
async def service_start(service_id: str, ): async def service_start(service_id: str):
"""Request the start of a specific service""" """Request the start of a specific service"""
await firewall.get(service_id).next(STATUS.ACTIVE) await firewall.get(service_id).next(STATUS.ACTIVE)
await refresh_frontend() await refresh_frontend()
return {'status': 'ok'} return {'status': 'ok'}
@app.get('/service/{service_id}/delete', response_model=StatusMessageModel) @app.get('/service/{service_id}/delete', response_model=StatusMessageModel)
async def service_delete(service_id: str, ): async def service_delete(service_id: str):
"""Request the deletion of a specific service""" """Request the deletion of a specific service"""
db.query('DELETE FROM services WHERE service_id = ?;', service_id) db.query('DELETE FROM services WHERE service_id = ?;', service_id)
db.query('DELETE FROM regexes WHERE service_id = ?;', service_id) db.query('DELETE FROM regexes WHERE service_id = ?;', service_id)
@@ -187,7 +187,7 @@ async def service_delete(service_id: str, ):
return {'status': 'ok'} return {'status': 'ok'}
@app.post('/service/{service_id}/rename', response_model=StatusMessageModel) @app.post('/service/{service_id}/rename', response_model=StatusMessageModel)
async def service_rename(service_id: str, form: RenameForm, ): async def service_rename(service_id: str, form: RenameForm):
"""Request to change the name of a specific service""" """Request to change the name of a specific service"""
form.name = refactor_name(form.name) form.name = refactor_name(form.name)
if not form.name: return {'status': 'The name cannot be empty!'} if not form.name: return {'status': 'The name cannot be empty!'}
@@ -199,7 +199,7 @@ async def service_rename(service_id: str, form: RenameForm, ):
return {'status': 'ok'} return {'status': 'ok'}
@app.get('/service/{service_id}/regexes', response_model=List[RegexModel]) @app.get('/service/{service_id}/regexes', response_model=List[RegexModel])
async def get_service_regexe_list(service_id: str, ): async def get_service_regexe_list(service_id: str):
"""Get the list of the regexes of a service""" """Get the list of the regexes of a service"""
return db.query(""" return db.query("""
SELECT SELECT
@@ -209,7 +209,7 @@ async def get_service_regexe_list(service_id: str, ):
""", service_id) """, service_id)
@app.get('/regex/{regex_id}', response_model=RegexModel) @app.get('/regex/{regex_id}', response_model=RegexModel)
async def get_regex_by_id(regex_id: int, ): async def get_regex_by_id(regex_id: int):
"""Get regex info using his id""" """Get regex info using his id"""
res = db.query(""" res = db.query("""
SELECT SELECT
@@ -221,7 +221,7 @@ async def get_regex_by_id(regex_id: int, ):
return res[0] return res[0]
@app.get('/regex/{regex_id}/delete', response_model=StatusMessageModel) @app.get('/regex/{regex_id}/delete', response_model=StatusMessageModel)
async def regex_delete(regex_id: int, ): async def regex_delete(regex_id: int):
"""Delete a regex using his id""" """Delete a regex using his id"""
res = db.query('SELECT * FROM regexes WHERE regex_id = ?;', regex_id) res = db.query('SELECT * FROM regexes WHERE regex_id = ?;', regex_id)
if len(res) != 0: if len(res) != 0:
@@ -232,7 +232,7 @@ async def regex_delete(regex_id: int, ):
return {'status': 'ok'} return {'status': 'ok'}
@app.get('/regex/{regex_id}/enable', response_model=StatusMessageModel) @app.get('/regex/{regex_id}/enable', response_model=StatusMessageModel)
async def regex_enable(regex_id: int, ): async def regex_enable(regex_id: int):
"""Request the enabling of a regex""" """Request the enabling of a regex"""
res = db.query('SELECT * FROM regexes WHERE regex_id = ?;', regex_id) res = db.query('SELECT * FROM regexes WHERE regex_id = ?;', regex_id)
if len(res) != 0: if len(res) != 0:
@@ -242,7 +242,7 @@ async def regex_enable(regex_id: int, ):
return {'status': 'ok'} return {'status': 'ok'}
@app.get('/regex/{regex_id}/disable', response_model=StatusMessageModel) @app.get('/regex/{regex_id}/disable', response_model=StatusMessageModel)
async def regex_disable(regex_id: int, ): async def regex_disable(regex_id: int):
"""Request the deactivation of a regex""" """Request the deactivation of a regex"""
res = db.query('SELECT * FROM regexes WHERE regex_id = ?;', regex_id) res = db.query('SELECT * FROM regexes WHERE regex_id = ?;', regex_id)
if len(res) != 0: if len(res) != 0:
@@ -252,7 +252,7 @@ async def regex_disable(regex_id: int, ):
return {'status': 'ok'} return {'status': 'ok'}
@app.post('/regexes/add', response_model=StatusMessageModel) @app.post('/regexes/add', response_model=StatusMessageModel)
async def add_new_regex(form: RegexAddForm, ): async def add_new_regex(form: RegexAddForm):
"""Add a new regex""" """Add a new regex"""
try: try:
re.compile(b64decode(form.regex)) re.compile(b64decode(form.regex))
@@ -269,7 +269,7 @@ async def add_new_regex(form: RegexAddForm, ):
return {'status': 'ok'} return {'status': 'ok'}
@app.post('/services/add', response_model=ServiceAddResponse) @app.post('/services/add', response_model=ServiceAddResponse)
async def add_new_service(form: ServiceAddForm, ): async def add_new_service(form: ServiceAddForm):
"""Add a new service""" """Add a new service"""
try: try:
form.ip_int = ip_parse(form.ip_int) form.ip_int = ip_parse(form.ip_int)

View File

@@ -41,14 +41,15 @@ db = SQLite('db/port-hijacking.db', {
'services': { 'services': {
'service_id': 'VARCHAR(100) PRIMARY KEY', 'service_id': 'VARCHAR(100) PRIMARY KEY',
'active' : 'BOOLEAN NOT NULL CHECK (active IN (0, 1))', 'active' : 'BOOLEAN NOT NULL CHECK (active IN (0, 1))',
'public_port': 'INT NOT NULL CHECK(public_port > 0 and public_port < 65536)', 'public_port': 'INT NOT NULL CHECK(public_port > 0 and public_port < 65536) UNIQUE',
'proxy_port': 'INT NOT NULL CHECK(proxy_port > 0 and proxy_port < 65536)', 'proxy_port': 'INT NOT NULL CHECK(proxy_port > 0 and proxy_port < 65536 and proxy_port != public_port)',
'name': 'VARCHAR(100) NOT NULL UNIQUE', 'name': 'VARCHAR(100) NOT NULL UNIQUE',
'proto': 'VARCHAR(3) NOT NULL CHECK (proto IN ("tcp", "udp"))', 'proto': 'VARCHAR(3) NOT NULL CHECK (proto IN ("tcp", "udp"))',
'ip_int': 'VARCHAR(100) NOT NULL', 'ip_int': 'VARCHAR(100) NOT NULL',
}, },
'QUERY':[ 'QUERY':[
"CREATE UNIQUE INDEX IF NOT EXISTS unique_services ON services (public_port, ip_int, proto);", "CREATE UNIQUE INDEX IF NOT EXISTS unique_services ON services (public_port, ip_int, proto);",
""
] ]
}) })
@@ -98,28 +99,28 @@ async def get_service_list():
return db.query("SELECT service_id, active, public_port, proxy_port, name, proto, ip_int FROM services;") return db.query("SELECT service_id, active, public_port, proxy_port, name, proto, ip_int FROM services;")
@app.get('/service/{service_id}', response_model=ServiceModel) @app.get('/service/{service_id}', response_model=ServiceModel)
async def get_service_by_id(service_id: str, ): async def get_service_by_id(service_id: str):
"""Get info about a specific service using his id""" """Get info about a specific service using his id"""
res = db.query("SELECT service_id, active, public_port, proxy_port, name, proto, ip_int FROM services WHERE service_id = ?;", service_id) res = db.query("SELECT service_id, active, public_port, proxy_port, name, proto, ip_int FROM services WHERE service_id = ?;", service_id)
if len(res) == 0: raise HTTPException(status_code=400, detail="This service does not exists!") if len(res) == 0: raise HTTPException(status_code=400, detail="This service does not exists!")
return res[0] return res[0]
@app.get('/service/{service_id}/stop', response_model=StatusMessageModel) @app.get('/service/{service_id}/stop', response_model=StatusMessageModel)
async def service_stop(service_id: str, ): async def service_stop(service_id: str):
"""Request the stop of a specific service""" """Request the stop of a specific service"""
await firewall.get(service_id).disable() await firewall.get(service_id).disable()
await refresh_frontend() await refresh_frontend()
return {'status': 'ok'} return {'status': 'ok'}
@app.get('/service/{service_id}/start', response_model=StatusMessageModel) @app.get('/service/{service_id}/start', response_model=StatusMessageModel)
async def service_start(service_id: str, ): async def service_start(service_id: str):
"""Request the start of a specific service""" """Request the start of a specific service"""
await firewall.get(service_id).enable() await firewall.get(service_id).enable()
await refresh_frontend() await refresh_frontend()
return {'status': 'ok'} return {'status': 'ok'}
@app.get('/service/{service_id}/delete', response_model=StatusMessageModel) @app.get('/service/{service_id}/delete', response_model=StatusMessageModel)
async def service_delete(service_id: str, ): async def service_delete(service_id: str):
"""Request the deletion of a specific service""" """Request the deletion of a specific service"""
db.query('DELETE FROM services WHERE service_id = ?;', service_id) db.query('DELETE FROM services WHERE service_id = ?;', service_id)
await firewall.remove(service_id) await firewall.remove(service_id)
@@ -127,7 +128,7 @@ async def service_delete(service_id: str, ):
return {'status': 'ok'} return {'status': 'ok'}
@app.post('/service/{service_id}/rename', response_model=StatusMessageModel) @app.post('/service/{service_id}/rename', response_model=StatusMessageModel)
async def service_rename(service_id: str, form: RenameForm, ): async def service_rename(service_id: str, form: RenameForm):
"""Request to change the name of a specific service""" """Request to change the name of a specific service"""
form.name = refactor_name(form.name) form.name = refactor_name(form.name)
if not form.name: return {'status': 'The name cannot be empty!'} if not form.name: return {'status': 'The name cannot be empty!'}
@@ -138,9 +139,22 @@ async def service_rename(service_id: str, form: RenameForm, ):
await refresh_frontend() await refresh_frontend()
return {'status': 'ok'} return {'status': 'ok'}
class ChangePortRequest(BaseModel):
proxy_port: int
@app.post('/service/{service_id}/changeport', response_model=StatusMessageModel)
async def service_changeport(service_id: str, form: ChangePortRequest):
"""Request to change the proxy port of a specific service"""
try:
db.query('UPDATE services SET proxy_port=? WHERE service_id = ?;', form.proxy_port, service_id)
except sqlite3.IntegrityError:
return {'status': 'Invalid proxy port or service'}
await firewall.get(service_id).change_port(form.proxy_port)
await refresh_frontend()
return {'status': 'ok'}
@app.post('/services/add', response_model=ServiceAddResponse) @app.post('/services/add', response_model=ServiceAddResponse)
async def add_new_service(form: ServiceAddForm, ): async def add_new_service(form: ServiceAddForm):
"""Add a new service""" """Add a new service"""
try: try:
form.ip_int = ip_parse(form.ip_int) form.ip_int = ip_parse(form.ip_int)

View File

@@ -100,7 +100,7 @@ async def get_service_list():
""") """)
@app.get('/service/{service_id}', response_model=ServiceModel) @app.get('/service/{service_id}', response_model=ServiceModel)
async def get_service_by_id(service_id: str, ): async def get_service_by_id(service_id: str):
"""Get info about a specific service using his id""" """Get info about a specific service using his id"""
res = db.query(""" res = db.query("""
SELECT SELECT
@@ -118,28 +118,28 @@ async def get_service_by_id(service_id: str, ):
return res[0] return res[0]
@app.get('/service/{service_id}/stop', response_model=StatusMessageModel) @app.get('/service/{service_id}/stop', response_model=StatusMessageModel)
async def service_stop(service_id: str, ): async def service_stop(service_id: str):
"""Request the stop of a specific service""" """Request the stop of a specific service"""
await firewall.get(service_id).next(STATUS.STOP) await firewall.get(service_id).next(STATUS.STOP)
await refresh_frontend() await refresh_frontend()
return {'status': 'ok'} return {'status': 'ok'}
@app.get('/service/{service_id}/pause', response_model=StatusMessageModel) @app.get('/service/{service_id}/pause', response_model=StatusMessageModel)
async def service_pause(service_id: str, ): async def service_pause(service_id: str):
"""Request the pause of a specific service""" """Request the pause of a specific service"""
await firewall.get(service_id).next(STATUS.PAUSE) await firewall.get(service_id).next(STATUS.PAUSE)
await refresh_frontend() await refresh_frontend()
return {'status': 'ok'} return {'status': 'ok'}
@app.get('/service/{service_id}/start', response_model=StatusMessageModel) @app.get('/service/{service_id}/start', response_model=StatusMessageModel)
async def service_start(service_id: str, ): async def service_start(service_id: str):
"""Request the start of a specific service""" """Request the start of a specific service"""
await firewall.get(service_id).next(STATUS.ACTIVE) await firewall.get(service_id).next(STATUS.ACTIVE)
await refresh_frontend() await refresh_frontend()
return {'status': 'ok'} return {'status': 'ok'}
@app.get('/service/{service_id}/delete', response_model=StatusMessageModel) @app.get('/service/{service_id}/delete', response_model=StatusMessageModel)
async def service_delete(service_id: str, ): async def service_delete(service_id: str):
"""Request the deletion of a specific service""" """Request the deletion of a specific service"""
db.query('DELETE FROM services WHERE service_id = ?;', service_id) db.query('DELETE FROM services WHERE service_id = ?;', service_id)
db.query('DELETE FROM regexes WHERE service_id = ?;', service_id) db.query('DELETE FROM regexes WHERE service_id = ?;', service_id)
@@ -149,7 +149,7 @@ async def service_delete(service_id: str, ):
@app.get('/service/{service_id}/regen-port', response_model=StatusMessageModel) @app.get('/service/{service_id}/regen-port', response_model=StatusMessageModel)
async def regen_service_port(service_id: str, ): async def regen_service_port(service_id: str):
"""Request the regeneration of a the internal proxy port of a specific service""" """Request the regeneration of a the internal proxy port of a specific service"""
db.query('UPDATE services SET internal_port = ? WHERE service_id = ?;', gen_internal_port(db), service_id) db.query('UPDATE services SET internal_port = ? WHERE service_id = ?;', gen_internal_port(db), service_id)
await firewall.get(service_id).update_port() await firewall.get(service_id).update_port()
@@ -195,7 +195,7 @@ class RegexModel(BaseModel):
active:bool active:bool
@app.get('/service/{service_id}/regexes', response_model=List[RegexModel]) @app.get('/service/{service_id}/regexes', response_model=List[RegexModel])
async def get_service_regexe_list(service_id: str, ): async def get_service_regexe_list(service_id: str):
"""Get the list of the regexes of a service""" """Get the list of the regexes of a service"""
return db.query(""" return db.query("""
SELECT SELECT
@@ -205,7 +205,7 @@ async def get_service_regexe_list(service_id: str, ):
""", service_id) """, service_id)
@app.get('/regex/{regex_id}', response_model=RegexModel) @app.get('/regex/{regex_id}', response_model=RegexModel)
async def get_regex_by_id(regex_id: int, ): async def get_regex_by_id(regex_id: int):
"""Get regex info using his id""" """Get regex info using his id"""
res = db.query(""" res = db.query("""
SELECT SELECT
@@ -217,7 +217,7 @@ async def get_regex_by_id(regex_id: int, ):
return res[0] return res[0]
@app.get('/regex/{regex_id}/delete', response_model=StatusMessageModel) @app.get('/regex/{regex_id}/delete', response_model=StatusMessageModel)
async def regex_delete(regex_id: int, ): async def regex_delete(regex_id: int):
"""Delete a regex using his id""" """Delete a regex using his id"""
res = db.query('SELECT * FROM regexes WHERE regex_id = ?;', regex_id) res = db.query('SELECT * FROM regexes WHERE regex_id = ?;', regex_id)
if len(res) != 0: if len(res) != 0:
@@ -227,7 +227,7 @@ async def regex_delete(regex_id: int, ):
return {'status': 'ok'} return {'status': 'ok'}
@app.get('/regex/{regex_id}/enable', response_model=StatusMessageModel) @app.get('/regex/{regex_id}/enable', response_model=StatusMessageModel)
async def regex_enable(regex_id: int, ): async def regex_enable(regex_id: int):
"""Request the enabling of a regex""" """Request the enabling of a regex"""
res = db.query('SELECT * FROM regexes WHERE regex_id = ?;', regex_id) res = db.query('SELECT * FROM regexes WHERE regex_id = ?;', regex_id)
if len(res) != 0: if len(res) != 0:
@@ -237,7 +237,7 @@ async def regex_enable(regex_id: int, ):
return {'status': 'ok'} return {'status': 'ok'}
@app.get('/regex/{regex_id}/disable', response_model=StatusMessageModel) @app.get('/regex/{regex_id}/disable', response_model=StatusMessageModel)
async def regex_disable(regex_id: int, ): async def regex_disable(regex_id: int):
"""Request the deactivation of a regex""" """Request the deactivation of a regex"""
res = db.query('SELECT * FROM regexes WHERE regex_id = ?;', regex_id) res = db.query('SELECT * FROM regexes WHERE regex_id = ?;', regex_id)
if len(res) != 0: if len(res) != 0:
@@ -255,7 +255,7 @@ class RegexAddForm(BaseModel):
is_case_sensitive: bool is_case_sensitive: bool
@app.post('/regexes/add', response_model=StatusMessageModel) @app.post('/regexes/add', response_model=StatusMessageModel)
async def add_new_regex(form: RegexAddForm, ): async def add_new_regex(form: RegexAddForm):
"""Add a new regex""" """Add a new regex"""
try: try:
re.compile(b64decode(form.regex)) re.compile(b64decode(form.regex))
@@ -283,7 +283,7 @@ class RenameForm(BaseModel):
name:str name:str
@app.post('/service/{service_id}/rename', response_model=StatusMessageModel) @app.post('/service/{service_id}/rename', response_model=StatusMessageModel)
async def service_rename(service_id: str, form: RenameForm, ): async def service_rename(service_id: str, form: RenameForm):
"""Request to change the name of a specific service""" """Request to change the name of a specific service"""
form.name = refactor_name(form.name) form.name = refactor_name(form.name)
if not form.name: return {'status': 'The name cannot be empty!'} if not form.name: return {'status': 'The name cannot be empty!'}
@@ -295,7 +295,7 @@ async def service_rename(service_id: str, form: RenameForm, ):
return {'status': 'ok'} return {'status': 'ok'}
@app.post('/services/add', response_model=ServiceAddStatus) @app.post('/services/add', response_model=ServiceAddStatus)
async def add_new_service(form: ServiceAddForm, ): async def add_new_service(form: ServiceAddForm):
"""Add a new service""" """Add a new service"""
serv_id = gen_service_id(db) serv_id = gen_service_id(db)
form.name = refactor_name(form.name) form.name = refactor_name(form.name)