1from fastapi import APIRouter, Depends
2from fastapi import HTTPException
3
4from app.db import crud
5from app.dependencies import DBDep, sudo_admin
6from app.models.proxy import Inbound, InboundHost, InboundHostResponse
7from app.db.models import InboundHost as DBInboundHost
8
9router = APIRouter(prefix="/inbounds", dependencies=[Depends(sudo_admin)], tags=['Inbounds'])
10
11
12@router.get("", response_model=list[Inbound])
13def get_inbounds(db: DBDep):
14 """
15 Get all inbounds
16 """
17 inbounds = crud.get_all_inbounds(db)
18
19 return inbounds
20
21
22@router.get('/hosts', response_model=list[InboundHost])
23def get_hosts(db: DBDep):
24 return db.query(DBInboundHost).all()
25
26
27@router.put("/hosts/{id}", response_model=InboundHostResponse)
28def update_host(id: int, host: InboundHost, db: DBDep):
29 """
30 Modify a host
31 """
32
33 db_host = crud.get_host(db, id)
34 if not db_host:
35 raise HTTPException(status_code=404, detail="Host not found")
36
37 return crud.update_host(db, db_host, host)
38
39
40@router.delete("/hosts/{id}")
41def delete_host(id: int, db: DBDep):
42 """
43 Remove a host
44 """
45 db_host = crud.get_host(db, id)
46 if not db_host:
47 raise HTTPException(status_code=404, detail="Host not found")
48
49 db.delete(db_host)
50 db.commit()
51 return {}
52
53
54@router.get("/{id}", response_model=Inbound)
55def get_inbound(id: int, db: DBDep):
56 """
57 Get a specific inbound
58 """
59 inbound = crud.get_inbound(db, id)
60 if not inbound:
61 raise HTTPException(status_code=404, detail="Inbound not found")
62
63 return inbound
64
65
66@router.get("/{id}/hosts", response_model=list[InboundHostResponse])
67def get_inbound_hosts(id: int | None, db: DBDep):
68 """
69 Get hosts of a specific inbound
70 """
71 if not id:
72 print()
73 return
74 inbound = crud.get_inbound(db, id)
75 if not inbound:
76 raise HTTPException(status_code=404, detail="Inbound not found")
77
78 return inbound.hosts
79
80
81@router.post("/{id}/hosts", response_model=InboundHostResponse)
82def create_host(id: int, host: InboundHost, db: DBDep):
83 """
84 Add a host to the inbound
85 """
86 inbound = crud.get_inbound(db, id)
87 if not inbound:
88 raise HTTPException(status_code=404, detail="Inbound not found")
89
90 return crud.add_host(db, inbound, host)