Add Iroh relay mode config
This commit is contained in:
parent
8334cc5e00
commit
0f2ad755d9
13 changed files with 210 additions and 21 deletions
|
|
@ -23,9 +23,17 @@ pub struct GethIrohConfig {
|
|||
impl GethIrohConfig {
|
||||
#[must_use]
|
||||
pub fn local(secret_key_path: impl Into<PathBuf>) -> Self {
|
||||
Self::local_with_relay(secret_key_path, GethRelayMode::Default)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn local_with_relay(
|
||||
secret_key_path: impl Into<PathBuf>,
|
||||
relay_mode: GethRelayMode,
|
||||
) -> Self {
|
||||
Self {
|
||||
secret_key_path: secret_key_path.into(),
|
||||
relay_mode: GethRelayMode::Disabled,
|
||||
relay_mode,
|
||||
bind_ipv4: None,
|
||||
bind_ipv6: None,
|
||||
alpns: all_alpns(),
|
||||
|
|
@ -49,6 +57,15 @@ impl GethRelayMode {
|
|||
Self::Staging => iroh::RelayMode::Staging,
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Disabled => "disabled",
|
||||
Self::Default => "default",
|
||||
Self::Staging => "staging",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GethIrohEndpoint {
|
||||
|
|
@ -90,6 +107,7 @@ pub async fn start_endpoint(config: &GethIrohConfig) -> Result<GethIrohEndpoint,
|
|||
let status = EndpointStatus {
|
||||
enabled: true,
|
||||
endpoint_id: Some(endpoint.node_id().to_string()),
|
||||
relay_mode: config.relay_mode.as_str().to_owned(),
|
||||
note: format!(
|
||||
"Iroh endpoint started with iroh 0.90.0; relay mode: {:?}",
|
||||
config.relay_mode
|
||||
|
|
@ -143,6 +161,7 @@ pub fn all_alpns() -> Vec<Vec<u8>> {
|
|||
pub struct EndpointStatus {
|
||||
pub enabled: bool,
|
||||
pub endpoint_id: Option<String>,
|
||||
pub relay_mode: String,
|
||||
pub note: String,
|
||||
}
|
||||
|
||||
|
|
@ -152,6 +171,7 @@ impl EndpointStatus {
|
|||
Self {
|
||||
enabled: false,
|
||||
endpoint_id: None,
|
||||
relay_mode: "not-started".to_owned(),
|
||||
note: "Iroh endpoint is not running outside daemon mode".to_owned(),
|
||||
}
|
||||
}
|
||||
|
|
@ -197,15 +217,29 @@ mod tests {
|
|||
assert_eq!(first.public(), second.public());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_config_selects_relay_mode() {
|
||||
let config = GethIrohConfig::local_with_relay("iroh.ed25519", GethRelayMode::Staging);
|
||||
assert_eq!(config.relay_mode, GethRelayMode::Staging);
|
||||
assert!(matches!(
|
||||
config.relay_mode.to_iroh(),
|
||||
iroh::RelayMode::Staging
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn endpoint_status_tracks_node_id_when_bind_is_available() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let config = GethIrohConfig::local(dir.path().join("iroh.ed25519"));
|
||||
let config = GethIrohConfig::local_with_relay(
|
||||
dir.path().join("iroh.ed25519"),
|
||||
GethRelayMode::Disabled,
|
||||
);
|
||||
match start_endpoint(&config).await {
|
||||
Ok(endpoint) => {
|
||||
let status = endpoint.status();
|
||||
assert!(status.enabled);
|
||||
assert_eq!(status.endpoint_id, Some(endpoint.node_id()));
|
||||
assert_eq!(status.relay_mode, "disabled");
|
||||
endpoint.shutdown().await;
|
||||
}
|
||||
Err(IrohError::Bind(error)) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue