gitscope
https://dev.kronoz.win/r/marzneshin.git
app/routes/service.py · v0.4.0
Source
1 a47eb4c Dawsh 2y ago import sqlalchemy
2 9ddfbc8 Mardin 2y ago from fastapi import APIRouter, Query
3 c13a1aa Dawsh 2y ago from fastapi import HTTPException
4 d4ec019 Dawsh 2y ago from fastapi_pagination.ext.sqlalchemy import paginate
5 d4ec019 Dawsh 2y ago from fastapi_pagination.links import Page
6 a47eb4c Dawsh 2y ago
7 d6e2aad Dawsh 2y ago from app import marznode
8 c13a1aa Dawsh 2y ago from app.db import crud
9 db68a58 Dawsh 2y ago from app.db.models import Service, User
10 9ddfbc8 Mardin 2y ago from app.dependencies import DBDep, AdminDep, SudoAdminDep
11 db68a58 Dawsh 2y ago from app.models.proxy import Inbound
12 01cdf2e Dawsh 2y ago from app.models.service import ServiceCreate, ServiceModify, ServiceResponse
13 db68a58 Dawsh 2y ago from app.models.user import UserResponse
14 a47eb4c Dawsh 2y ago
15 9ddfbc8 Mardin 2y ago router = APIRouter(prefix="/services", tags=["Service"])
16 a47eb4c Dawsh 2y ago
17 f7af2b3 Dawsh 2y ago
18 d4ec019 Dawsh 2y ago @router.get("", response_model=Page[ServiceResponse])
19 9ddfbc8 Mardin 2y ago def get_services(db: DBDep, admin: AdminDep, name: str = Query(None)):
20 d4ec019 Dawsh 2y ago query = db.query(Service)
21 4708fcb Dawsh 2y ago
22 d4ec019 Dawsh 2y ago if name:
23 d4ec019 Dawsh 2y ago query = query.filter(Service.name.ilike(f"%{name}%"))
24 4708fcb Dawsh 2y ago
25 4708fcb Dawsh 2y ago if not admin.is_sudo and not admin.all_services_access:
26 4708fcb Dawsh 2y ago query = query.filter(Service.id.in_(admin.service_ids))
27 4708fcb Dawsh 2y ago
28 d4ec019 Dawsh 2y ago return paginate(query)
29 d4ec019 Dawsh 2y ago
30 d4ec019 Dawsh 2y ago
31 2de9d72 Dawsh 2y ago @router.post("", response_model=ServiceResponse)
32 9ddfbc8 Mardin 2y ago def add_service(new_service: ServiceCreate, db: DBDep, admin: SudoAdminDep):
33 a47eb4c Dawsh 2y ago """
34 d6e2aad Dawsh 2y ago Add a new service
35 a47eb4c Dawsh 2y ago
36 d6e2aad Dawsh 2y ago - **name** service name
37 d6e2aad Dawsh 2y ago - **inbounds** list of inbound ids
38 a47eb4c Dawsh 2y ago """
39 a47eb4c Dawsh 2y ago try:
40 a47eb4c Dawsh 2y ago return crud.create_service(db, new_service)
41 a47eb4c Dawsh 2y ago except sqlalchemy.exc.IntegrityError:
42 a47eb4c Dawsh 2y ago db.rollback()
43 01cdf2e Dawsh 2y ago raise HTTPException(
44 01cdf2e Dawsh 2y ago status_code=409, detail="Service by this name already exists"
45 01cdf2e Dawsh 2y ago )
46 a47eb4c Dawsh 2y ago
47 a47eb4c Dawsh 2y ago
48 2de9d72 Dawsh 2y ago @router.get("/{id}", response_model=ServiceResponse)
49 9ddfbc8 Mardin 2y ago def get_service(id: int, db: DBDep, admin: AdminDep):
50 a47eb4c Dawsh 2y ago """
51 a47eb4c Dawsh 2y ago Get Service information with id
52 a47eb4c Dawsh 2y ago """
53 a47eb4c Dawsh 2y ago dbservice = crud.get_service(db, id)
54 a47eb4c Dawsh 2y ago if not dbservice:
55 a47eb4c Dawsh 2y ago raise HTTPException(status_code=404, detail="Service not found")
56 a47eb4c Dawsh 2y ago
57 33f2c28 Dawsh 2y ago if not (
58 33f2c28 Dawsh 2y ago admin.is_sudo or admin.all_services_access or id in admin.service_ids
59 33f2c28 Dawsh 2y ago ):
60 4708fcb Dawsh 2y ago raise HTTPException(status_code=403, detail="You're not allowed")
61 4708fcb Dawsh 2y ago
62 a47eb4c Dawsh 2y ago return dbservice
63 a47eb4c Dawsh 2y ago
64 a47eb4c Dawsh 2y ago
65 db68a58 Dawsh 2y ago @router.get("/{id}/users", response_model=Page[UserResponse])
66 9ddfbc8 Mardin 2y ago def get_service_users(id: int, db: DBDep, admin: SudoAdminDep):
67 db68a58 Dawsh 2y ago """
68 db68a58 Dawsh 2y ago Get service users
69 db68a58 Dawsh 2y ago """
70 db68a58 Dawsh 2y ago service = crud.get_service(db, id)
71 db68a58 Dawsh 2y ago
72 db68a58 Dawsh 2y ago if not service:
73 db68a58 Dawsh 2y ago raise HTTPException(status_code=404, detail="Service not found")
74 db68a58 Dawsh 2y ago
75 db68a58 Dawsh 2y ago query = db.query(User).join(User.services).where(Service.id == service.id)
76 db68a58 Dawsh 2y ago
77 db68a58 Dawsh 2y ago return paginate(query)
78 db68a58 Dawsh 2y ago
79 db68a58 Dawsh 2y ago
80 db68a58 Dawsh 2y ago @router.get("/{id}/inbounds", response_model=Page[Inbound])
81 9ddfbc8 Mardin 2y ago def get_service_inbounds(id: int, db: DBDep, admin: SudoAdminDep):
82 db68a58 Dawsh 2y ago """
83 db68a58 Dawsh 2y ago Get service inbounds
84 db68a58 Dawsh 2y ago """
85 db68a58 Dawsh 2y ago service = crud.get_service(db, id)
86 db68a58 Dawsh 2y ago
87 db68a58 Dawsh 2y ago if not service:
88 db68a58 Dawsh 2y ago raise HTTPException(status_code=404, detail="Service not found")
89 db68a58 Dawsh 2y ago
90 f1ab16c Dawsh 2y ago query = (
91 f1ab16c Dawsh 2y ago db.query(Inbound)
92 f1ab16c Dawsh 2y ago .join(Inbound.services)
93 f1ab16c Dawsh 2y ago .where(Service.id == service.id)
94 f1ab16c Dawsh 2y ago )
95 db68a58 Dawsh 2y ago
96 db68a58 Dawsh 2y ago return paginate(query)
97 db68a58 Dawsh 2y ago
98 db68a58 Dawsh 2y ago
99 2de9d72 Dawsh 2y ago @router.put("/{id}", response_model=ServiceResponse)
100 9ddfbc8 Mardin 2y ago async def modify_service(
101 9ddfbc8 Mardin 2y ago id: int, modification: ServiceModify, db: DBDep, admin: SudoAdminDep
102 9ddfbc8 Mardin 2y ago ):
103 a47eb4c Dawsh 2y ago """
104 a47eb4c Dawsh 2y ago Modify Service
105 a47eb4c Dawsh 2y ago
106 a47eb4c Dawsh 2y ago - **name** can be up to 64 characters
107 ee1351a Dawsh 2y ago - **inbounds** list of inbound ids. if not specified no change will be applied;
108 ee1351a Dawsh 2y ago in case of an empty list all inbounds would be removed.
109 a47eb4c Dawsh 2y ago """
110 a47eb4c Dawsh 2y ago dbservice = crud.get_service(db, id)
111 c13a1aa Dawsh 2y ago if not dbservice:
112 01cdf2e Dawsh 2y ago raise HTTPException(status_code=404, detail="Service not found")
113 d6e2aad Dawsh 2y ago old_inbounds = {(i.node_id, i.protocol, i.tag) for i in dbservice.inbounds}
114 a47eb4c Dawsh 2y ago try:
115 d6e2aad Dawsh 2y ago response = crud.update_service(db, dbservice, modification)
116 a47eb4c Dawsh 2y ago except sqlalchemy.exc.IntegrityError:
117 a47eb4c Dawsh 2y ago db.rollback()
118 f1ab16c Dawsh 2y ago raise HTTPException(
119 f1ab16c Dawsh 2y ago status_code=409, detail="problem updating the service"
120 f1ab16c Dawsh 2y ago )
121 d6e2aad Dawsh 2y ago else:
122 d6e2aad Dawsh 2y ago for user in response.users:
123 d377c4c Dawsh 2y ago if user.activated:
124 d377c4c Dawsh 2y ago marznode.operations.update_user(
125 d377c4c Dawsh 2y ago user, old_inbounds=old_inbounds
126 d377c4c Dawsh 2y ago )
127 d6e2aad Dawsh 2y ago return response
128 a47eb4c Dawsh 2y ago
129 a47eb4c Dawsh 2y ago
130 2de9d72 Dawsh 2y ago @router.delete("/{id}")
131 9ddfbc8 Mardin 2y ago def remove_service(id: int, db: DBDep, admin: SudoAdminDep):
132 a47eb4c Dawsh 2y ago dbservice = crud.get_service(db, id)
133 c13a1aa Dawsh 2y ago if not dbservice:
134 a47eb4c Dawsh 2y ago raise HTTPException(status_code=404, detail="Service not found")
135 01cdf2e Dawsh 2y ago
136 ee1351a Dawsh 2y ago crud.remove_service(db, dbservice)
137 ee1351a Dawsh 2y ago return dict()