Skip to content

Commit

Permalink
feat(core): add grpc stats api
Browse files Browse the repository at this point in the history
  • Loading branch information
DefectingCat committed Jan 6, 2025
1 parent 8fc89a2 commit 9c4645f
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion venus-core/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Config {
let mut buffer = String::new();
config_file.read_to_string(&mut buffer)?;
let mut rua_config = toml::from_str::<VenusConfig>(&buffer)?;
debug!("reloading rua config: {:?}", rua_config);
// debug!("reloading rua config: {:?}", rua_config);
rua_config.version = VERSION.into();
self.venus = rua_config;
Ok(())
Expand Down
1 change: 1 addition & 0 deletions venus-core/src/config/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ pub struct CoreConfig {
#[serde(skip_serializing_if = "Option::is_none")]
pub api: Option<Api>,
pub inbounds: Vec<Inbound>,
pub inbound_detour: Vec<Inbound>,
pub outbounds: Vec<Outbound>,
pub routing: Routing,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
7 changes: 7 additions & 0 deletions venus-core/src/grpc/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[derive(Debug, thiserror::Error)]
pub enum GrpcError {
#[error("grpc error: {0}")]
Transport(#[from] tonic::transport::Error),
#[error("grpc error: {0}")]
Status(#[from] tonic::Status),
}
2 changes: 2 additions & 0 deletions venus-core/src/grpc/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod error;
pub mod stats;
19 changes: 19 additions & 0 deletions venus-core/src/grpc/stats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use log::error;

use crate::grpc::error::GrpcError;
use crate::v2ray_core::stats_service_client::StatsServiceClient;
use crate::v2ray_core::GetStatsRequest;

const TEST: &str = "inbound>>>api>>>traffic>>>uplink";

pub async fn stats_test(url: String) -> Result<(), GrpcError> {
let mut client = StatsServiceClient::connect(url).await?;
let request = GetStatsRequest {
name: TEST.to_string(),
reset: false,
};
error!("test");
let response = client.get_stats(request).await?;
error!("RESPONSE={:?}", response);
Ok(())
}
1 change: 1 addition & 0 deletions venus-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use tonic::async_trait;
pub mod config;
pub mod consts;
pub mod error;
pub mod grpc;
pub mod message;

pub mod v2ray_core {
Expand Down
3 changes: 3 additions & 0 deletions venus/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub enum AppError {
VenusCore(#[from] venus_core::error::VenusError),
#[error("venus config error {0}")]
VenusConfig(#[from] venus_core::config::error::ConfigError),
#[error("venus config error {0}")]
VenusGrpc(#[from] venus_core::grpc::error::GrpcError),
#[error("venus poison {0}")]
GlobalPoison(#[from] std::sync::PoisonError<std::sync::MutexGuard<'static, venus_core::Venus>>),
#[error("{0}")]
Expand Down Expand Up @@ -134,6 +136,7 @@ impl IntoResponse for AppError {
"Invalid token".to_string(),
),
},
AppError::VenusGrpc(grpc_error) => todo!(),
// core
};
let body = Json(json!({
Expand Down
4 changes: 3 additions & 1 deletion venus/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
};

pub mod proxies;
pub mod stats;
pub mod user;
pub mod version;

Expand Down Expand Up @@ -58,7 +59,8 @@ pub fn routes() -> Router {
Router::new()
.route("/version", get(version::version))
.nest("/user", user::routes())
.nest("/subscription", proxies::routes()),
.nest("/subscription", proxies::routes())
.nest("/stats", stats::routes()),
)
.layer(
ServiceBuilder::new()
Expand Down
21 changes: 21 additions & 0 deletions venus/src/routes/stats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use axum::{response::IntoResponse, routing::get, Router};
use venus_core::grpc::stats::stats_test;

use crate::error::AppResult;

use super::RouteResponse;

pub async fn stats() -> AppResult<impl IntoResponse> {
let mut res: RouteResponse<Option<()>> = RouteResponse {
..RouteResponse::default()
};

stats_test("http://localhost:10086".to_string()).await?;

res.message = Some("ok".into());
Ok(res)
}

pub fn routes() -> Router {
Router::new().route("/all", get(stats))
}

0 comments on commit 9c4645f

Please sign in to comment.