From f25402e1914bc4dfcc3334c18774da1155e494d9 Mon Sep 17 00:00:00 2001 From: magine Date: Sat, 20 Jul 2024 00:25:23 +0800 Subject: [PATCH] feat: define auth server --- build.rs | 6 ++- examples/authserver.rs | 89 ++++++++++++++++++++++++++++++++++++++++++ proto/auth_v1.proto | 21 ++++++++++ src/auth.rs | 3 ++ src/lib.rs | 1 + src/main.rs | 2 +- 6 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 examples/authserver.rs create mode 100644 proto/auth_v1.proto create mode 100644 src/auth.rs diff --git a/build.rs b/build.rs index 20cb68b..3fb4cc1 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,9 @@ fn main() -> Result<(), Box> { - let protos = ["proto/command_v1.proto", "proto/tunnel_v1.proto"]; + let protos = [ + "proto/auth_v1.proto", + "proto/command_v1.proto", + "proto/tunnel_v1.proto", + ]; tonic_build::configure() .protoc_arg("--experimental_allow_proto3_optional") .compile(&protos, &["proto"])?; diff --git a/examples/authserver.rs b/examples/authserver.rs new file mode 100644 index 0000000..cf41849 --- /dev/null +++ b/examples/authserver.rs @@ -0,0 +1,89 @@ +use clap::Arg; +use clap::ArgAction; +use clap::ArgMatches; +use clap::Command; +use dephy_pproxy::auth::proto; +use tonic::transport::Server; +use tonic::Request; +use tonic::Response; +use tonic::Status; + +struct PProxyAuth { + peer_ids: Vec, +} + +#[tonic::async_trait] +impl proto::auth_service_server::AuthService for PProxyAuth { + async fn get_tokens( + &self, + request: Request, + ) -> Result, Status> { + let request = request.into_inner(); + + let tokens = self + .peer_ids + .iter() + .filter(|peer_id| { + request.peer_id.is_none() || request.peer_id == Some(peer_id.to_owned().to_owned()) + }) + .cloned() + .map(|peer_id| proto::Token { + resource_id: request.resource_id.clone(), + peer_id, + ttl: 3600, + }) + .collect(); + + Ok(Response::new(proto::GetTokensResponse { tokens })) + } +} + +fn parse_args() -> ArgMatches { + Command::new("pproxy-auth-server") + .about("An example pproxy auth server") + .version(dephy_pproxy::VERSION) + .arg( + Arg::new("SERVER_ADDR") + .long("server-addr") + .num_args(1) + .default_value("127.0.0.1:3000") + .action(ArgAction::Set) + .help("Server address"), + ) + .arg( + Arg::new("PEER_IDS") + .num_args(0..) + .action(ArgAction::Set) + .help("Will generate tokens for those peers"), + ) + .arg_required_else_help(true) + .get_matches() +} + +#[tokio::main] +async fn main() { + let _ = tracing_subscriber::fmt() + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) + .try_init(); + + let args = parse_args(); + + let server_addr = args + .get_one::("SERVER_ADDR") + .unwrap() + .parse() + .expect("Invalid server address"); + let peer_ids = args.get_many("PEER_IDS").unwrap().cloned().collect(); + println!("server_addr: {}", server_addr); + println!("peer_ids: {:?}", peer_ids); + + let auth = PProxyAuth { peer_ids }; + + let auth_server = proto::auth_service_server::AuthServiceServer::new(auth); + + Server::builder() + .add_service(tonic_web::enable(auth_server)) + .serve(server_addr) + .await + .expect("Auth server failed"); +} diff --git a/proto/auth_v1.proto b/proto/auth_v1.proto new file mode 100644 index 0000000..2d9711d --- /dev/null +++ b/proto/auth_v1.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; +package auth.v1; + +message GetTokensRequest { + string resource_id = 1; + optional string peer_id = 2; +} + +message GetTokensResponse { + repeated Token tokens = 1; +} + +message Token { + string resource_id = 1; + string peer_id = 2; + uint64 ttl = 3; +} + +service AuthService { + rpc GetTokens(GetTokensRequest) returns (GetTokensResponse); +} diff --git a/src/auth.rs b/src/auth.rs new file mode 100644 index 0000000..2fd6df2 --- /dev/null +++ b/src/auth.rs @@ -0,0 +1,3 @@ +pub mod proto { + tonic::include_proto!("auth.v1"); +} diff --git a/src/lib.rs b/src/lib.rs index 1ad76c3..7755986 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,6 +26,7 @@ use crate::tunnel::Tunnel; use crate::tunnel::TunnelServer; use crate::types::*; +pub mod auth; pub mod command; pub mod error; mod server; diff --git a/src/main.rs b/src/main.rs index 8cb5d20..da9d758 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ use tonic::transport::Server; fn parse_args() -> Command { let mut app = Command::new("pproxy") .about("A proxy tool based on libp2p network") - .version("0.1.0"); + .version(dephy_pproxy::VERSION); let serve = Command::new("serve") .about("Start a pproxy server")