diff --git a/app/db/crud.py b/app/db/crud.py index a406a62..fcf8cd7 100644 --- a/app/db/crud.py +++ b/app/db/crud.py @@ -19,7 +19,7 @@ from app.models.proxy import InboundHost as InboundHostModify from app.models.user import (ReminderType, UserCreate, UserDataLimitResetStrategy, UserModify, UserResponse, UserStatus, UserUsageResponse) -from app.models.service import Service as ServiceModify +from app.models.service import Service as ServiceModify, ServiceCreate # from app.models.user_template import UserTemplateCreate, UserTemplateModify from app.utils.helpers import (calculate_expiration_days, calculate_usage_percent) @@ -482,10 +482,7 @@ def get_admins(db: Session, return query.all() -def create_service(db: Session, service: Service) -> Service: - - #for _, i in service.inbounds.items(): - # inbound_tags.extend(i) +def create_service(db: Session, service: ServiceCreate) -> Service: dbservice = Service( name=service.name, inbounds=db.query(Inbound).filter(Inbound.id.in_(service.inbounds)).all(), @@ -497,14 +494,32 @@ def create_service(db: Session, service: Service) -> Service: return dbservice -def get_service(db: Session, service_name: str) -> Service: - return db.query(Service).filter( Service.name == service_name ).first() +def get_service(db: Session, service_id: id) -> Service: + return db.query(Service).filter(Service.id == service_id).first() def get_services(db: Session) -> List[Service]: return db.query(Service).all() +def update_service(db: Session, db_service: Service, modification: ServiceModify): + if modification.name is not None: + db_service.name = modification.name + + if modification.inbounds is not None: + db_service.inbounds = db.query(Inbound).filter(Inbound.id.in_(modification.inbounds)).all() + + db.commit() + db.refresh(db_service) + return db_service + + +def remove_service(db: Session, db_service: Service): + db.delete(db_service) + db.commit() + return db_service + + def get_node(db: Session, name: str): return db.query(Node).filter(Node.name == name).first() diff --git a/app/models/service.py b/app/models/service.py index 0e1b95a..7919aed 100644 --- a/app/models/service.py +++ b/app/models/service.py @@ -1,8 +1,6 @@ -from typing import Dict, List, Optional, Union +from typing import List, Optional -from pydantic import ConfigDict, BaseModel, Field, validator, computed_field - -# from app import xray +from pydantic import ConfigDict, BaseModel, Field, computed_field class ServiceBase(BaseModel): @@ -20,7 +18,7 @@ class Service(ServiceBase): inbounds: List[InboundBase] = Field([]) -class ServiceCreate(Service): +class ServiceCreate(ServiceBase): users: List[int] = Field([]) inbounds: List[int] = Field([]) model_config = ConfigDict(json_schema_extra={ @@ -32,22 +30,19 @@ class ServiceCreate(Service): }) -class ServiceModify(Service): - id: int +class ServiceModify(ServiceBase): + inbounds: Optional[List[int]] = Field(None) model_config = ConfigDict(json_schema_extra={ "example": { "id": 2, "name": "my service 2", - "inbounds": [1, 2, 3], - "users": [1, 2, 3], + "inbounds": [1, 2, 3] } }) class ServiceResponse(Service): id: int - users: List[UserBase] = Field([]) - inbounds: List[InboundBase] = Field([]) @computed_field @property @@ -59,18 +54,4 @@ class ServiceResponse(Service): def inbound_ids(self) -> List[int]: return [i.id for i in self.inbounds] - """ - @validator("inbounds", pre=True) - def validate_inbounds(cls, v): - final = {} - inbound_tags = [i.tag for i in v] - for protocol, inbounds in xray.config.inbounds_by_protocol.items(): - for inbound in inbounds: - if inbound["tag"] in inbound_tags: - if protocol in final: - final[protocol].append(inbound["tag"]) - else: - final[protocol] = [inbound["tag"]] - return final - """ model_config = ConfigDict(from_attributes=True) diff --git a/app/routes/__init__.py b/app/routes/__init__.py index 95e1be0..3ee40bc 100644 --- a/app/routes/__init__.py +++ b/app/routes/__init__.py @@ -1,3 +1,12 @@ import logging -logger = logging.getLogger(__name__) \ No newline at end of file +logger = logging.getLogger(__name__) + +__all__ = [ + "admin", + "node", + "service", + "subscription", + "system", + "user" +] diff --git a/app/routes/service.py b/app/routes/service.py index cf47911..752b219 100644 --- a/app/routes/service.py +++ b/app/routes/service.py @@ -20,7 +20,7 @@ def add_service(new_service: ServiceCreate, - **name** can be up to 64 characters - **data_limit** must be in bytes and larger or equal to 0 - - **expire_duration** must be in seconds and larger or equat to 0 + - **expire_duration** must be in seconds and larger or equal to 0 - **inbounds** dictionary of protocol:inbound_tags, empty means all inbounds """ try: @@ -52,8 +52,10 @@ def modify_service(id: int, - **name** can be up to 64 characters - **data_limit** must be in bytes and larger or equal to 0 - **expire_duration** must be in seconds and larger or equat to 0 - - **inbounds** dictionary of protocol:inbound_tags, empty means all inbounds + - **inbounds** list of inbound ids. if not specified no change will be applied; + in case of an empty list all inbounds would be removed. """ + # TODO: Update all affected users in nodes dbservice = crud.get_service(db, id) if not dbservice: raise HTTPException(status_code=404, detail="Service not found") @@ -72,7 +74,8 @@ def remove_service(id: int, if not dbservice: raise HTTPException(status_code=404, detail="Service not found") - return crud.remove_service(db, dbservice) + crud.remove_service(db, dbservice) + return dict() @router.get("/api/services", response_model=List[ServiceResponse])