Autosuggestion for network interfaces ips

This commit is contained in:
DomySh
2022-07-20 00:21:22 +02:00
parent da2cebfabd
commit 1266aebe0e
12 changed files with 66 additions and 18 deletions

View File

@@ -11,7 +11,7 @@ from passlib.context import CryptContext
from fastapi_socketio import SocketManager
from modules import SQLite, FirewallManager
from modules.firewall import STATUS
from utils import ip_parse, refactor_name, gen_service_id
from utils import get_interfaces, ip_parse, refactor_name, gen_service_id
ON_DOCKER = len(sys.argv) > 1 and sys.argv[1] == "DOCKER"
DEBUG = len(sys.argv) > 1 and sys.argv[1] == "DEBUG"
@@ -365,6 +365,15 @@ async def add_new_service(form: ServiceAddForm, auth: bool = Depends(is_loggined
await refresh_frontend()
return {'status': 'ok', 'service_id': srv_id}
class IpInterface(BaseModel):
addr: str
name: str
@app.get('/api/interfaces', response_model=List[IpInterface])
async def get_ip_interfaces(auth: bool = Depends(is_loggined)):
"""Get a list of ip and ip6 interfaces"""
return get_interfaces()
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)

View File

@@ -2,6 +2,7 @@ fastapi[all]
httpx
uvicorn[standard]
passlib[bcrypt]
psutil
python-jose[cryptography]
fastapi-socketio
git+https://salsa.debian.org/pkg-netfilter-team/pkg-nftables#egg=nftables&subdirectory=py

View File

@@ -1,5 +1,5 @@
from ipaddress import ip_interface
import os, socket, secrets
import os, socket, secrets, psutil
LOCALHOST_IP = socket.gethostbyname(os.getenv("LOCALHOST_IP","127.0.0.1"))
@@ -19,4 +19,12 @@ def ip_parse(ip:str):
return str(ip_interface(ip).network)
def ip_family(ip:str):
return "ip6" if ip_interface(ip).version == 6 else "ip"
return "ip6" if ip_interface(ip).version == 6 else "ip"
def get_interfaces():
def _get_interfaces():
for int_name, interfs in psutil.net_if_addrs().items():
for interf in interfs:
if interf.family in [socket.AF_INET, socket.AF_INET6]:
yield {"name": int_name, "addr":interf.address}
return list(_get_interfaces())