Skip to content

Commit f7590f0

Browse files
committed
feat: add user agent support
1 parent 281cc0e commit f7590f0

4 files changed

Lines changed: 40 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/charon-p2p/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ charon-tracing.workspace = true
2323
tracing.workspace = true
2424
serde.workspace = true
2525
serde_json.workspace = true
26+
charon-core.workspace = true
2627

2728
[dev-dependencies]
2829
charon-testutil.workspace = true

crates/charon-p2p/src/behaviours/pluto.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Pluto behaviour.
22
3+
use std::sync::LazyLock;
4+
35
use libp2p::{identify, identity::Keypair, ping, relay, swarm::NetworkBehaviour};
46

57
use crate::{config::default_ping_config, gater::ConnGater};
@@ -29,18 +31,28 @@ impl PlutoBehaviour {
2931
}
3032
}
3133

34+
/// The default user agent for the Pluto network.
35+
pub static DEFAULT_USER_AGENT: LazyLock<String> =
36+
LazyLock::new(|| format!("pluto/{}", *charon_core::version::VERSION));
37+
38+
/// The default identify protocol for the Pluto network.
39+
pub static DEFAULT_IDENTIFY_PROTOCOL: LazyLock<String> =
40+
LazyLock::new(|| format!("/pluto/{}", *charon_core::version::VERSION));
41+
3242
/// Builder for [`PlutoBehaviour`].
3343
#[derive(Debug, Clone)]
3444
pub struct PlutoBehaviourBuilder {
3545
gater: Option<ConnGater>,
3646
identify_protocol: String,
47+
user_agent: String,
3748
}
3849

3950
impl Default for PlutoBehaviourBuilder {
4051
fn default() -> Self {
4152
Self {
4253
gater: None,
43-
identify_protocol: "/pluto/1.0.0-alpha".into(),
54+
identify_protocol: DEFAULT_IDENTIFY_PROTOCOL.clone(),
55+
user_agent: DEFAULT_USER_AGENT.clone(),
4456
}
4557
}
4658
}
@@ -63,6 +75,12 @@ impl PlutoBehaviourBuilder {
6375
self
6476
}
6577

78+
/// Sets the user agent string.
79+
pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
80+
self.user_agent = user_agent.into();
81+
self
82+
}
83+
6684
/// Builds the [`PlutoBehaviour`] with the provided keypair and relay
6785
/// client.
6886
pub fn build(self, key: &Keypair, relay_client: relay::client::Behaviour) -> PlutoBehaviour {
@@ -71,10 +89,10 @@ impl PlutoBehaviourBuilder {
7189
.gater
7290
.unwrap_or_else(|| ConnGater::new_conn_gater(vec![], vec![])),
7391
relay: relay_client,
74-
identify: identify::Behaviour::new(identify::Config::new(
75-
self.identify_protocol,
76-
key.public(),
77-
)),
92+
identify: identify::Behaviour::new(
93+
identify::Config::new(self.identify_protocol, key.public())
94+
.with_agent_version(self.user_agent),
95+
),
7896
ping: ping::Behaviour::new(default_ping_config()),
7997
}
8098
}

crates/relay-server/src/behaviour.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub struct RelayServerBehaviourBuilder {
3535
gater: Option<ConnGater>,
3636
identify_protocol: String,
3737
relay_config: Option<relay::Config>,
38+
user_agent: Option<String>,
3839
}
3940

4041
impl Default for RelayServerBehaviourBuilder {
@@ -43,6 +44,7 @@ impl Default for RelayServerBehaviourBuilder {
4344
gater: None,
4445
identify_protocol: "/pluto/relay/1.0.0-alpha".into(),
4546
relay_config: None,
47+
user_agent: None,
4648
}
4749
}
4850
}
@@ -71,17 +73,26 @@ impl RelayServerBehaviourBuilder {
7173
self
7274
}
7375

76+
/// Sets the user agent string.
77+
pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
78+
self.user_agent = Some(user_agent.into());
79+
self
80+
}
81+
7482
/// Builds the [`RelayServerBehaviour`] with the provided keypair.
7583
pub fn build(self, key: &Keypair) -> RelayServerBehaviour {
7684
RelayServerBehaviour {
7785
relay: relay::Behaviour::new(
7886
key.public().to_peer_id(),
7987
self.relay_config.unwrap_or_default(),
8088
),
81-
identify: identify::Behaviour::new(identify::Config::new(
82-
self.identify_protocol,
83-
key.public(),
84-
)),
89+
identify: identify::Behaviour::new(
90+
identify::Config::new(self.identify_protocol, key.public()).with_agent_version(
91+
self.user_agent.unwrap_or_else(|| {
92+
charon_p2p::behaviours::pluto::DEFAULT_USER_AGENT.clone()
93+
}),
94+
),
95+
),
8596
ping: ping::Behaviour::new(charon_p2p::config::default_ping_config()),
8697
gater: self
8798
.gater

0 commit comments

Comments
 (0)