1from fastapi import APIRouter, Depends, Query
2from fastapi import HTTPException
3from fastapi_pagination.ext.sqlalchemy import paginate
4from fastapi_pagination.links import Page
5
6from app.db import crud
7from app.db.models import InboundHost as DBInboundHost, Inbound as DBInbound
8from app.dependencies import DBDep, sudo_admin
9from app.models.proxy import Inbound, InboundHost, InboundHostResponse
10
11HOST_NOT_FOUND_ERROR_MSG = "Host not found"
12
13router = APIRouter(
14 prefix="/inbounds", dependencies=[Depends(sudo_admin)], tags=["Inbounds"]
15)
16
17
18@router.get("", response_model=Page[Inbound])
19def get_inbounds(db: DBDep, tag: str = Query(None)):
20 """
21 Get all inbounds
22 """
23 query = db.query(DBInbound)
24 if tag:
25 query = query.filter(DBInbound.tag.ilike(f"%{tag}%"))
26
27 return paginate(db, query)
28
29
30@router.get("/hosts", response_model=Page[InboundHostResponse])
31def get_hosts(db: DBDep):
32 return paginate(db.query(DBInboundHost))
33
34
35@router.post("/hosts", response_model=InboundHostResponse)
36def create_unbound_host(host: InboundHost, db: DBDep):
37 """
38 Add a host without an inbound
39 """
40 return crud.add_host(db, None, host)
41
42
43@router.get("/hosts/{id}", response_model=InboundHostResponse)
44def get_host(id: int, db: DBDep):
45 """
46 Get a host
47 """
48 host = crud.get_host(db, id)
49 if not host:
50 raise HTTPException(status_code=404, detail=HOST_NOT_FOUND_ERROR_MSG)
51
52 return host
53
54
55@router.put("/hosts/{id}", response_model=InboundHostResponse)
56def update_host(id: int, host: InboundHost, db: DBDep):
57 """
58 Modify a host
59 """
60
61 db_host = crud.get_host(db, id)
62 if not db_host:
63 raise HTTPException(status_code=404, detail=HOST_NOT_FOUND_ERROR_MSG)
64
65 return crud.update_host(db, db_host, host)
66
67
68@router.delete("/hosts/{id}")
69def delete_host(id: int, db: DBDep):
70 """
71 Remove a host
72 """
73 db_host = crud.get_host(db, id)
74 if not db_host:
75 raise HTTPException(status_code=404, detail=HOST_NOT_FOUND_ERROR_MSG)
76
77 db.delete(db_host)
78 db.commit()
79 return {}
80
81
82@router.get("/{id}", response_model=Inbound)
83def get_inbound(id: int, db: DBDep):
84 """
85 Get a specific inbound
86 """
87 inbound = crud.get_inbound(db, id)
88 if not inbound:
89 raise HTTPException(status_code=404, detail="Inbound not found")
90
91 return inbound
92
93
94@router.get("/{id}/hosts", response_model=Page[InboundHostResponse])
95def get_inbound_hosts(id: int, db: DBDep):
96 """
97 Get hosts of a specific inbound
98 """
99 inbound = crud.get_inbound(db, id)
100 if not inbound:
101 raise HTTPException(status_code=404, detail="Inbound not found")
102
103 return paginate(
104 db, db.query(DBInboundHost).filter(DBInboundHost.inbound_id == id)
105 )
106
107
108@router.post("/{id}/hosts", response_model=InboundHostResponse)
109def create_host(id: int, host: InboundHost, db: DBDep):
110 """
111 Add a host to the inbound
112 """
113 inbound = crud.get_inbound(db, id)
114 if not inbound:
115 raise HTTPException(status_code=404, detail="Inbound not found")
116
117 return crud.add_host(db, inbound, host)