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