-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support multiple listeners #12
Changes from all commits
c422d6d
c5d6a4c
c0f31b2
ce34b02
b5ab6d8
36c8eb9
d760e69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
use anyhow::anyhow; | ||
use givc_common::types::TransportConfig; | ||
use std::path::PathBuf; | ||
use std::path::{Path, PathBuf}; | ||
use std::time::Duration; | ||
use tonic::transport::Endpoint; | ||
|
||
use anyhow::anyhow; | ||
use hyper_util::rt::TokioIo; | ||
use tokio::net::UnixStream; | ||
use tokio_vsock::{VsockAddr, VsockStream}; | ||
use tonic::transport::{Certificate, Channel, ClientTlsConfig, Identity, ServerTlsConfig}; | ||
use tonic::transport::{Endpoint, Uri}; | ||
use tower::service_fn; | ||
use tracing::info; | ||
|
||
use givc_common::address::EndpointAddress; | ||
use givc_common::types::TransportConfig; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct TlsConfig { | ||
pub ca_cert_file_path: PathBuf, | ||
|
@@ -50,25 +57,65 @@ impl TlsConfig { | |
} | ||
} | ||
|
||
fn transport_config_to_url(tc: &TransportConfig, with_tls: bool) -> String { | ||
fn transport_config_to_url(ea: &EndpointAddress, with_tls: bool) -> String { | ||
let scheme = match with_tls { | ||
true => "https", | ||
false => "http", | ||
}; | ||
format!("{}://{}:{}", scheme, tc.address, tc.port) | ||
match ea { | ||
EndpointAddress::Tcp { addr, port } => format!("{}://{}:{}", scheme, addr, port), | ||
_ => format!("{}://[::]:443", scheme), // Bogus url, to make tonic connector happy | ||
} | ||
} | ||
|
||
async fn connect_unix_socket(endpoint: Endpoint, path: &String) -> anyhow::Result<Channel> { | ||
let mut path = Some(path.to_owned()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use the |
||
let ch = endpoint | ||
.connect_with_connector(service_fn(move |_: Uri| { | ||
let path = path.take(); | ||
async move { | ||
if let Some(path) = path { | ||
// Connect to a Uds socket | ||
Ok::<_, std::io::Error>(TokioIo::new(UnixStream::connect(path).await?)) | ||
} else { | ||
Err(std::io::Error::new( | ||
std::io::ErrorKind::Other, | ||
"Path already taken", | ||
)) | ||
} | ||
} | ||
})) | ||
.await?; | ||
Ok(ch) | ||
} | ||
|
||
async fn connect_vsock_socket(endpoint: Endpoint, vs: &VsockAddr) -> anyhow::Result<Channel> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
let vs = vs.to_owned(); | ||
let ch = endpoint | ||
.connect_with_connector(service_fn(move |_: Uri| async move { | ||
let stream = VsockStream::connect(vs).await?; | ||
Ok::<_, std::io::Error>(TokioIo::new(stream)) | ||
})) | ||
.await?; | ||
Ok(ch) | ||
} | ||
|
||
impl EndpointConfig { | ||
pub async fn connect(&self) -> anyhow::Result<Channel> { | ||
let url = transport_config_to_url(&self.transport, self.tls.is_some()); | ||
let url = transport_config_to_url(&self.transport.address, self.tls.is_some()); | ||
info!("Connecting to {url}, TLS name {:?}", &self.tls); | ||
let mut endpoint = Endpoint::try_from(url)? | ||
.timeout(Duration::from_secs(5)) | ||
.concurrency_limit(30); | ||
if let Some(tls) = &self.tls { | ||
endpoint = endpoint.tls_config(tls.client_config()?)?; | ||
}; | ||
let channel = endpoint.connect().await?; | ||
let channel = match &self.transport.address { | ||
EndpointAddress::Tcp { .. } => endpoint.connect().await?, | ||
EndpointAddress::Unix(unix) => connect_unix_socket(endpoint, unix).await?, | ||
EndpointAddress::Abstract(abs) => connect_unix_socket(endpoint, abs).await?, | ||
EndpointAddress::Vsock(vs) => connect_vsock_socket(endpoint, vs).await?, | ||
}; | ||
Ok(channel) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use std::convert::{Into, TryFrom}; | ||
//use std::net::SocketAddr; | ||
use std::path::PathBuf; | ||
|
||
use tokio_vsock::VsockAddr; | ||
|
||
use crate::pb; | ||
|
||
#[derive(Clone, Debug, PartialEq)] | ||
pub enum EndpointAddress { | ||
Tcp { | ||
// IP + port (FIXME: should be SocketAddres) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
addr: String, | ||
port: u16, | ||
}, | ||
Unix(String), // "/path/to/sock" (same host only) | ||
Abstract(String), // "@abstract-socket-name" (same host only) | ||
Vsock(VsockAddr), // cid+port. FIXME: cid have two magic numbers for host and local | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod address; | ||
pub mod query; | ||
pub mod types; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&str
instead of&String