mirror of
https://github.com/house-of-vanity/OutFleet.git
synced 2025-10-24 17:29:08 +00:00
API works. next: generate URI
This commit is contained in:
@@ -12,6 +12,8 @@ pub struct Model {
|
||||
|
||||
pub hostname: String,
|
||||
|
||||
pub grpc_hostname: String,
|
||||
|
||||
pub grpc_port: i32,
|
||||
|
||||
#[serde(skip_serializing)]
|
||||
@@ -117,6 +119,7 @@ impl From<String> for ServerStatus {
|
||||
pub struct CreateServerDto {
|
||||
pub name: String,
|
||||
pub hostname: String,
|
||||
pub grpc_hostname: Option<String>, // Optional, defaults to hostname if not provided
|
||||
pub grpc_port: Option<i32>,
|
||||
pub api_credentials: Option<String>,
|
||||
pub default_certificate_id: Option<Uuid>,
|
||||
@@ -126,6 +129,7 @@ pub struct CreateServerDto {
|
||||
pub struct UpdateServerDto {
|
||||
pub name: Option<String>,
|
||||
pub hostname: Option<String>,
|
||||
pub grpc_hostname: Option<String>,
|
||||
pub grpc_port: Option<i32>,
|
||||
pub api_credentials: Option<String>,
|
||||
pub status: Option<String>,
|
||||
@@ -137,6 +141,7 @@ pub struct ServerResponse {
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
pub hostname: String,
|
||||
pub grpc_hostname: String,
|
||||
pub grpc_port: i32,
|
||||
pub status: String,
|
||||
pub default_certificate_id: Option<Uuid>,
|
||||
@@ -148,8 +153,9 @@ pub struct ServerResponse {
|
||||
impl From<CreateServerDto> for ActiveModel {
|
||||
fn from(dto: CreateServerDto) -> Self {
|
||||
Self {
|
||||
name: Set(dto.name),
|
||||
hostname: Set(dto.hostname),
|
||||
name: Set(dto.name.clone()),
|
||||
hostname: Set(dto.hostname.clone()),
|
||||
grpc_hostname: Set(dto.grpc_hostname.unwrap_or(dto.hostname)), // Default to hostname if not provided
|
||||
grpc_port: Set(dto.grpc_port.unwrap_or(2053)),
|
||||
api_credentials: Set(dto.api_credentials),
|
||||
status: Set("unknown".to_string()),
|
||||
@@ -165,6 +171,7 @@ impl From<Model> for ServerResponse {
|
||||
id: server.id,
|
||||
name: server.name,
|
||||
hostname: server.hostname,
|
||||
grpc_hostname: server.grpc_hostname,
|
||||
grpc_port: server.grpc_port,
|
||||
status: server.status,
|
||||
default_certificate_id: server.default_certificate_id,
|
||||
@@ -185,6 +192,9 @@ impl Model {
|
||||
if let Some(hostname) = dto.hostname {
|
||||
active_model.hostname = Set(hostname);
|
||||
}
|
||||
if let Some(grpc_hostname) = dto.grpc_hostname {
|
||||
active_model.grpc_hostname = Set(grpc_hostname);
|
||||
}
|
||||
if let Some(grpc_port) = dto.grpc_port {
|
||||
active_model.grpc_port = Set(grpc_port);
|
||||
}
|
||||
@@ -202,7 +212,16 @@ impl Model {
|
||||
}
|
||||
|
||||
pub fn get_grpc_endpoint(&self) -> String {
|
||||
format!("{}:{}", self.hostname, self.grpc_port)
|
||||
let hostname = if self.grpc_hostname.is_empty() {
|
||||
tracing::debug!("Using public hostname '{}' for gRPC (grpc_hostname is empty)", self.hostname);
|
||||
&self.hostname
|
||||
} else {
|
||||
tracing::debug!("Using dedicated gRPC hostname '{}' (different from public hostname '{}')", self.grpc_hostname, self.hostname);
|
||||
&self.grpc_hostname
|
||||
};
|
||||
let endpoint = format!("{}:{}", hostname, self.grpc_port);
|
||||
tracing::info!("gRPC endpoint for server '{}': {}", self.name, endpoint);
|
||||
endpoint
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.alter_table(
|
||||
Table::alter()
|
||||
.table(Servers::Table)
|
||||
.add_column(
|
||||
ColumnDef::new(Servers::GrpcHostname)
|
||||
.string()
|
||||
.not_null()
|
||||
.default(""),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Update existing servers: set grpc_hostname to hostname value
|
||||
let db = manager.get_connection();
|
||||
|
||||
// Use raw SQL to copy hostname to grpc_hostname for existing records
|
||||
// Handle both empty strings and default empty values
|
||||
db.execute_unprepared("UPDATE servers SET grpc_hostname = hostname WHERE grpc_hostname = '' OR grpc_hostname IS NULL")
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.alter_table(
|
||||
Table::alter()
|
||||
.table(Servers::Table)
|
||||
.drop_column(Servers::GrpcHostname)
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Iden)]
|
||||
enum Servers {
|
||||
Table,
|
||||
GrpcHostname,
|
||||
}
|
||||
@@ -8,6 +8,7 @@ mod m20241201_000005_create_server_inbounds_table;
|
||||
mod m20241201_000006_create_user_access_table;
|
||||
mod m20241201_000007_create_inbound_users_table;
|
||||
mod m20250919_000001_update_inbound_users_schema;
|
||||
mod m20250922_000001_add_grpc_hostname_to_servers;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
@@ -23,6 +24,7 @@ impl MigratorTrait for Migrator {
|
||||
Box::new(m20241201_000006_create_user_access_table::Migration),
|
||||
Box::new(m20241201_000007_create_inbound_users_table::Migration),
|
||||
Box::new(m20250919_000001_update_inbound_users_schema::Migration),
|
||||
Box::new(m20250922_000001_add_grpc_hostname_to_servers::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -74,6 +74,6 @@ impl ServerRepository {
|
||||
let server = self.find_by_id(id).await?
|
||||
.ok_or_else(|| anyhow::anyhow!("Server not found"))?;
|
||||
|
||||
Ok(format!("{}:{}", server.hostname, server.grpc_port))
|
||||
Ok(server.get_grpc_endpoint())
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
use anyhow::Result;
|
||||
use sea_orm::{DatabaseConnection, EntityTrait, QueryFilter, ColumnTrait, QueryOrder, PaginatorTrait};
|
||||
use sea_orm::{DatabaseConnection, EntityTrait, QueryFilter, ColumnTrait, QueryOrder, PaginatorTrait, QuerySelect};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::database::entities::user::{Entity as User, Column, Model, ActiveModel, CreateUserDto, UpdateUserDto};
|
||||
@@ -44,7 +44,7 @@ impl UserRepository {
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
/// Search users by name
|
||||
/// Search users by name (with pagination for backward compatibility)
|
||||
pub async fn search_by_name(&self, query: &str, page: u64, per_page: u64) -> Result<Vec<Model>> {
|
||||
let users = User::find()
|
||||
.filter(Column::Name.contains(query))
|
||||
@@ -56,6 +56,35 @@ impl UserRepository {
|
||||
Ok(users)
|
||||
}
|
||||
|
||||
/// Universal search - searches by name, telegram_id, or user_id
|
||||
pub async fn search(&self, query: &str) -> Result<Vec<Model>> {
|
||||
use sea_orm::Condition;
|
||||
|
||||
let mut condition = Condition::any();
|
||||
|
||||
// Search by name (case-insensitive partial match)
|
||||
condition = condition.add(Column::Name.contains(query));
|
||||
|
||||
// Try to parse as telegram_id (i64)
|
||||
if let Ok(telegram_id) = query.parse::<i64>() {
|
||||
condition = condition.add(Column::TelegramId.eq(telegram_id));
|
||||
}
|
||||
|
||||
// Try to parse as UUID (user_id)
|
||||
if let Ok(user_id) = Uuid::parse_str(query) {
|
||||
condition = condition.add(Column::Id.eq(user_id));
|
||||
}
|
||||
|
||||
let users = User::find()
|
||||
.filter(condition)
|
||||
.order_by_desc(Column::CreatedAt)
|
||||
.limit(100) // Reasonable limit to prevent huge results
|
||||
.all(&self.db)
|
||||
.await?;
|
||||
|
||||
Ok(users)
|
||||
}
|
||||
|
||||
/// Create a new user
|
||||
pub async fn create(&self, dto: CreateUserDto) -> Result<Model> {
|
||||
let active_model: ActiveModel = dto.into();
|
||||
|
||||
@@ -215,7 +215,7 @@ async fn sync_xray_state(db: DatabaseManager, xray_service: XrayService) -> Resu
|
||||
|
||||
for server in servers {
|
||||
|
||||
let endpoint = format!("{}:{}", server.hostname, server.grpc_port);
|
||||
let endpoint = server.get_grpc_endpoint();
|
||||
|
||||
// Test connection first
|
||||
match xray_service.test_connection(server.id, &endpoint).await {
|
||||
@@ -394,7 +394,7 @@ async fn sync_single_server_by_id(
|
||||
let desired_inbounds = get_desired_inbounds_from_db(db, &server, &inbound_repo, &template_repo).await?;
|
||||
|
||||
// Build endpoint
|
||||
let endpoint = format!("{}:{}", server.hostname, server.grpc_port);
|
||||
let endpoint = server.get_grpc_endpoint();
|
||||
|
||||
// Sync server
|
||||
sync_server_inbounds(xray_service, server_id, &endpoint, &desired_inbounds).await?;
|
||||
|
||||
@@ -108,6 +108,7 @@ pub async fn test_server_connection(
|
||||
let update_dto = server::UpdateServerDto {
|
||||
name: None,
|
||||
hostname: None,
|
||||
grpc_hostname: None,
|
||||
grpc_port: None,
|
||||
api_credentials: None,
|
||||
default_certificate_id: None,
|
||||
@@ -126,6 +127,7 @@ pub async fn test_server_connection(
|
||||
let update_dto = server::UpdateServerDto {
|
||||
name: None,
|
||||
hostname: None,
|
||||
grpc_hostname: None,
|
||||
grpc_port: None,
|
||||
api_credentials: None,
|
||||
default_certificate_id: None,
|
||||
@@ -586,7 +588,7 @@ pub async fn remove_user_from_inbound(
|
||||
let inbound_tag = &inbound.tag;
|
||||
|
||||
// Remove user from xray server
|
||||
match app_state.xray_service.remove_user(server_id, &format!("{}:{}", server.hostname, server.grpc_port), &inbound_tag, &email).await {
|
||||
match app_state.xray_service.remove_user(server_id, &server.get_grpc_endpoint(), &inbound_tag, &email).await {
|
||||
Ok(_) => {
|
||||
tracing::info!("Removed user '{}' from inbound", email);
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
|
||||
@@ -23,8 +23,6 @@ pub struct PaginationQuery {
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct SearchQuery {
|
||||
pub q: Option<String>,
|
||||
#[serde(flatten)]
|
||||
pub pagination: PaginationQuery,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
@@ -86,34 +84,24 @@ pub async fn get_users(
|
||||
Ok(Json(response))
|
||||
}
|
||||
|
||||
/// Search users by name
|
||||
/// Search users by name, telegram_id or user_id
|
||||
pub async fn search_users(
|
||||
State(app_state): State<AppState>,
|
||||
Query(query): Query<SearchQuery>,
|
||||
) -> Result<Json<UsersResponse>, StatusCode> {
|
||||
) -> Result<Json<Vec<UserResponse>>, StatusCode> {
|
||||
let repo = UserRepository::new(app_state.db.connection().clone());
|
||||
|
||||
let users = if let Some(search_query) = query.q {
|
||||
repo.search_by_name(&search_query, query.pagination.page, query.pagination.per_page)
|
||||
// Search by name, telegram_id, or UUID
|
||||
repo.search(&search_query)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
} else {
|
||||
repo.get_all(query.pagination.page, query.pagination.per_page)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
};
|
||||
|
||||
let total = repo.count()
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
let response = UsersResponse {
|
||||
users: users.into_iter().map(UserResponse::from).collect(),
|
||||
total,
|
||||
page: query.pagination.page,
|
||||
per_page: query.pagination.per_page,
|
||||
// If no query, return empty array
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
let response: Vec<UserResponse> = users.into_iter().map(UserResponse::from).collect();
|
||||
Ok(Json(response))
|
||||
}
|
||||
|
||||
@@ -203,4 +191,33 @@ pub async fn delete_user(
|
||||
} else {
|
||||
Err(StatusCode::NOT_FOUND)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get user access (inbound associations)
|
||||
pub async fn get_user_access(
|
||||
State(app_state): State<AppState>,
|
||||
Path(user_id): Path<Uuid>,
|
||||
) -> Result<Json<Vec<serde_json::Value>>, StatusCode> {
|
||||
use crate::database::repository::InboundUsersRepository;
|
||||
|
||||
let inbound_users_repo = InboundUsersRepository::new(app_state.db.connection().clone());
|
||||
|
||||
let access_list = inbound_users_repo
|
||||
.find_by_user_id(user_id)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
let response: Vec<serde_json::Value> = access_list
|
||||
.into_iter()
|
||||
.map(|access| serde_json::json!({
|
||||
"id": access.id,
|
||||
"user_id": access.user_id,
|
||||
"server_inbound_id": access.server_inbound_id,
|
||||
"xray_user_id": access.xray_user_id,
|
||||
"level": access.level,
|
||||
"is_active": access.is_active,
|
||||
}))
|
||||
.collect();
|
||||
|
||||
Ok(Json(response))
|
||||
}
|
||||
@@ -24,4 +24,5 @@ fn user_routes() -> Router<AppState> {
|
||||
.route("/:id", get(handlers::get_user)
|
||||
.put(handlers::update_user)
|
||||
.delete(handlers::delete_user))
|
||||
.route("/:id/access", get(handlers::get_user_access))
|
||||
}
|
||||
Reference in New Issue
Block a user