| 1 |
a47eb4c |
Dawsh |
2y ago |
from typing import Dict, List, Union |
| 2 |
a47eb4c |
Dawsh |
2y ago |
|
| 3 |
f7af2b3 |
Dawsh |
2y ago |
from fastapi import APIRouter |
| 4 |
f7af2b3 |
Dawsh |
2y ago |
|
| 5 |
f7af2b3 |
Dawsh |
2y ago |
from app import __version__ |
| 6 |
f7af2b3 |
Dawsh |
2y ago |
# from .. import __version__ |
| 7 |
c13a1aa |
Dawsh |
2y ago |
from app.db import crud |
| 8 |
f7af2b3 |
Dawsh |
2y ago |
from app.dependencies import DBDep, AdminDep, SudoAdminDep |
| 9 |
a47eb4c |
Dawsh |
2y ago |
from app.models.admin import Admin |
| 10 |
f7af2b3 |
Dawsh |
2y ago |
from app.models.proxy import InboundHost, Inbound |
| 11 |
a47eb4c |
Dawsh |
2y ago |
from app.models.system import SystemStats |
| 12 |
a47eb4c |
Dawsh |
2y ago |
from app.models.user import UserStatus |
| 13 |
a47eb4c |
Dawsh |
2y ago |
from app.utils.system import memory_usage, cpu_usage, realtime_bandwidth |
| 14 |
f7af2b3 |
Dawsh |
2y ago |
|
| 15 |
f7af2b3 |
Dawsh |
2y ago |
router = APIRouter(tags=["System"]) |
| 16 |
a47eb4c |
Dawsh |
2y ago |
|
| 17 |
a47eb4c |
Dawsh |
2y ago |
|
| 18 |
2de9d72 |
Dawsh |
2y ago |
@router.get("/system", response_model=SystemStats) |
| 19 |
c13a1aa |
Dawsh |
2y ago |
def get_system_stats(db: DBDep, admin: AdminDep): |
| 20 |
a47eb4c |
Dawsh |
2y ago |
mem = memory_usage() |
| 21 |
a47eb4c |
Dawsh |
2y ago |
cpu = cpu_usage() |
| 22 |
a47eb4c |
Dawsh |
2y ago |
system = crud.get_system_usage(db) |
| 23 |
a47eb4c |
Dawsh |
2y ago |
dbadmin: Union[Admin, None] = crud.get_admin(db, admin.username) |
| 24 |
a47eb4c |
Dawsh |
2y ago |
|
| 25 |
a47eb4c |
Dawsh |
2y ago |
total_user = crud.get_users_count(db, admin=dbadmin if not admin.is_sudo else None) |
| 26 |
a47eb4c |
Dawsh |
2y ago |
users_active = crud.get_users_count(db, status=UserStatus.active, admin=dbadmin if not admin.is_sudo else None) |
| 27 |
a47eb4c |
Dawsh |
2y ago |
realtime_bandwidth_stats = realtime_bandwidth() |
| 28 |
a47eb4c |
Dawsh |
2y ago |
|
| 29 |
a47eb4c |
Dawsh |
2y ago |
return SystemStats( |
| 30 |
a47eb4c |
Dawsh |
2y ago |
version=__version__, |
| 31 |
a47eb4c |
Dawsh |
2y ago |
mem_total=mem.total, |
| 32 |
a47eb4c |
Dawsh |
2y ago |
mem_used=mem.used, |
| 33 |
a47eb4c |
Dawsh |
2y ago |
cpu_cores=cpu.cores, |
| 34 |
a47eb4c |
Dawsh |
2y ago |
cpu_usage=cpu.percent, |
| 35 |
a47eb4c |
Dawsh |
2y ago |
total_user=total_user, |
| 36 |
a47eb4c |
Dawsh |
2y ago |
users_active=users_active, |
| 37 |
a47eb4c |
Dawsh |
2y ago |
incoming_bandwidth=system.uplink, |
| 38 |
a47eb4c |
Dawsh |
2y ago |
outgoing_bandwidth=system.downlink, |
| 39 |
a47eb4c |
Dawsh |
2y ago |
incoming_bandwidth_speed=realtime_bandwidth_stats.incoming_bytes, |
| 40 |
a47eb4c |
Dawsh |
2y ago |
outgoing_bandwidth_speed=realtime_bandwidth_stats.outgoing_bytes, |
| 41 |
a47eb4c |
Dawsh |
2y ago |
) |
| 42 |
a47eb4c |
Dawsh |
2y ago |
|
| 43 |
a47eb4c |
Dawsh |
2y ago |
|
| 44 |
2de9d72 |
Dawsh |
2y ago |
@router.get("/hosts", response_model=Dict[int, List[InboundHost]]) |
| 45 |
c13a1aa |
Dawsh |
2y ago |
def get_hosts(db: DBDep, admin: SudoAdminDep): |
| 46 |
a47eb4c |
Dawsh |
2y ago |
hosts = crud.get_all_hosts(db) |
| 47 |
a47eb4c |
Dawsh |
2y ago |
|
| 48 |
a47eb4c |
Dawsh |
2y ago |
return hosts |
| 49 |
a47eb4c |
Dawsh |
2y ago |
|
| 50 |
a47eb4c |
Dawsh |
2y ago |
|
| 51 |
f7af2b3 |
Dawsh |
2y ago |
@router.put('/api/hosts', response_model=Dict[str, List[InboundHost]]) |
| 52 |
a47eb4c |
Dawsh |
2y ago |
def modify_hosts(modified_hosts: Dict[str, List[InboundHost]], |
| 53 |
c13a1aa |
Dawsh |
2y ago |
db: DBDep, |
| 54 |
c13a1aa |
Dawsh |
2y ago |
admin: SudoAdminDep): |
| 55 |
a47eb4c |
Dawsh |
2y ago |
# validate |
| 56 |
c13a1aa |
Dawsh |
2y ago |
# for inbound_id, hosts in modified_hosts.items(): |
| 57 |
c13a1aa |
Dawsh |
2y ago |
# if not xray.config.inbounds_by_id.get(inbound_id): |
| 58 |
c13a1aa |
Dawsh |
2y ago |
# raise HTTPException(status_code=400, detail=f"Inbound {inbound_id} doesn't exist") |
| 59 |
a47eb4c |
Dawsh |
2y ago |
|
| 60 |
a47eb4c |
Dawsh |
2y ago |
for inbound_id, hosts in modified_hosts.items(): |
| 61 |
a47eb4c |
Dawsh |
2y ago |
crud.update_hosts(db, inbound_id, hosts) |
| 62 |
a47eb4c |
Dawsh |
2y ago |
|
| 63 |
c13a1aa |
Dawsh |
2y ago |
# xray.hosts.update() |
| 64 |
a47eb4c |
Dawsh |
2y ago |
|
| 65 |
a47eb4c |
Dawsh |
2y ago |
hosts = {} |
| 66 |
c13a1aa |
Dawsh |
2y ago |
# for inbound_tag in xray.config.inbounds_by_tag: |
| 67 |
c13a1aa |
Dawsh |
2y ago |
# hosts[inbound_tag] = crud.get_hosts(db, inbound_tag) |
| 68 |
a47eb4c |
Dawsh |
2y ago |
|
| 69 |
a47eb4c |
Dawsh |
2y ago |
return hosts |