Skip to content

Commit

Permalink
feat: define auth server
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma233 committed Jul 19, 2024
1 parent 7aa4002 commit f25402e
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 2 deletions.
6 changes: 5 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
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"])?;
Expand Down
89 changes: 89 additions & 0 deletions examples/authserver.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
}

#[tonic::async_trait]
impl proto::auth_service_server::AuthService for PProxyAuth {
async fn get_tokens(
&self,
request: Request<proto::GetTokensRequest>,
) -> Result<Response<proto::GetTokensResponse>, 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::<String>("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");
}
21 changes: 21 additions & 0 deletions proto/auth_v1.proto
Original file line number Diff line number Diff line change
@@ -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);
}
3 changes: 3 additions & 0 deletions src/auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod proto {
tonic::include_proto!("auth.v1");
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit f25402e

Please sign in to comment.