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