referrerpolicy=no-referrer-when-downgrade

sc_telemetry/
endpoints.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use libp2p::multiaddr::{self, Multiaddr};
20use serde::{Deserialize, Deserializer, Serialize};
21
22/// List of telemetry servers we want to talk to. Contains the URL of the server, and the
23/// maximum verbosity level.
24///
25/// The URL string can be either a URL or a multiaddress.
26#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
27pub struct TelemetryEndpoints(
28	#[serde(deserialize_with = "url_or_multiaddr_deser")] pub(crate) Vec<(Multiaddr, u8)>,
29);
30
31/// Custom deserializer for TelemetryEndpoints, used to convert urls or multiaddr to multiaddr.
32fn url_or_multiaddr_deser<'de, D>(deserializer: D) -> Result<Vec<(Multiaddr, u8)>, D::Error>
33where
34	D: Deserializer<'de>,
35{
36	Vec::<(String, u8)>::deserialize(deserializer)?
37		.iter()
38		.map(|e| url_to_multiaddr(&e.0).map_err(serde::de::Error::custom).map(|m| (m, e.1)))
39		.collect()
40}
41
42impl TelemetryEndpoints {
43	/// Create a `TelemetryEndpoints` based on a list of `(String, u8)`.
44	pub fn new(endpoints: Vec<(String, u8)>) -> Result<Self, multiaddr::Error> {
45		let endpoints: Result<Vec<(Multiaddr, u8)>, multiaddr::Error> =
46			endpoints.iter().map(|e| Ok((url_to_multiaddr(&e.0)?, e.1))).collect();
47		endpoints.map(Self)
48	}
49}
50
51impl TelemetryEndpoints {
52	/// Return `true` if there are no telemetry endpoints, `false` otherwise.
53	pub fn is_empty(&self) -> bool {
54		self.0.is_empty()
55	}
56}
57
58/// Parses a WebSocket URL into a libp2p `Multiaddr`.
59fn url_to_multiaddr(url: &str) -> Result<Multiaddr, multiaddr::Error> {
60	// First, assume that we have a `Multiaddr`.
61	let parse_error = match url.parse() {
62		Ok(ma) => return Ok(ma),
63		Err(err) => err,
64	};
65
66	// If not, try the `ws://path/url` format.
67	if let Ok(ma) = multiaddr::from_url(url) {
68		return Ok(ma)
69	}
70
71	// If we have no clue about the format of that string, assume that we were expecting a
72	// `Multiaddr`.
73	Err(parse_error)
74}
75
76#[cfg(test)]
77mod tests {
78	use super::{url_to_multiaddr, Multiaddr, TelemetryEndpoints};
79
80	#[test]
81	fn valid_endpoints() {
82		let endp = vec![
83			("wss://telemetry.polkadot.io/submit/".into(), 3),
84			("/ip4/80.123.90.4/tcp/5432".into(), 4),
85		];
86		let telem =
87			TelemetryEndpoints::new(endp.clone()).expect("Telemetry endpoint should be valid");
88		let mut res: Vec<(Multiaddr, u8)> = vec![];
89		for (a, b) in endp.iter() {
90			res.push((url_to_multiaddr(a).expect("provided url should be valid"), *b))
91		}
92		assert_eq!(telem.0, res);
93	}
94
95	#[test]
96	fn invalid_endpoints() {
97		let endp = vec![
98			("/ip4/...80.123.90.4/tcp/5432".into(), 3),
99			("/ip4/no:!?;rlkqre;;::::///tcp/5432".into(), 4),
100		];
101		let telem = TelemetryEndpoints::new(endp);
102		assert!(telem.is_err());
103	}
104
105	#[test]
106	fn valid_and_invalid_endpoints() {
107		let endp = vec![
108			("/ip4/80.123.90.4/tcp/5432".into(), 3),
109			("/ip4/no:!?;rlkqre;;::::///tcp/5432".into(), 4),
110		];
111		let telem = TelemetryEndpoints::new(endp);
112		assert!(telem.is_err());
113	}
114}