-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathregion.rs
More file actions
301 lines (260 loc) · 9.89 KB
/
Copy pathregion.rs
File metadata and controls
301 lines (260 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2025 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::error::Error as StdError;
use http::header::{HeaderMap, HeaderValue, AUTHORIZATION};
use serde::Deserialize;
use crate::http_client;
use super::{SignalError, SignalResult, REGION_FETCH_TIMEOUT};
/// Converts an error into a string that includes the full error chain.
/// This is important for debugging TLS errors, where the root cause
/// (e.g., "invalid peer certificate: UnknownIssuer") is often buried
/// in the source chain.
fn error_with_chain(err: &dyn StdError) -> String {
let mut source = err.source();
std::iter::once(err.to_string())
.chain(std::iter::from_fn(move || {
let err = source?;
source = err.source();
Some(err.to_string())
}))
.collect::<Vec<_>>()
.join(": ")
}
pub struct RegionUrlProvider;
#[derive(Deserialize)]
pub struct RegionUrlResponse {
pub regions: Vec<RegionUrlInfo>,
}
#[derive(Deserialize)]
pub struct RegionUrlInfo {
pub region: String,
pub url: String,
pub distance: String,
}
impl RegionUrlProvider {
pub async fn fetch_region_urls(url: &str, token: &str) -> SignalResult<Vec<String>> {
if is_cloud_url(url)? {
let endpoint = region_endpoint(url)?;
fetch_from_endpoint(&endpoint, token).await
} else {
Ok(vec![])
}
}
}
pub(crate) async fn fetch_from_endpoint(
endpoint_url: &str,
token: &str,
) -> SignalResult<Vec<String>> {
let fetch_fut = async {
let client = http_client::Client::new();
let mut headers = HeaderMap::new();
headers.insert(AUTHORIZATION, HeaderValue::from_str(&format!("Bearer {}", token)).unwrap());
let res = client
.get(endpoint_url)
.headers(headers)
.send()
.await
.map_err(|e| SignalError::RegionError(error_with_chain(&e)))?;
if !res.status().is_success() {
return Err(SignalError::Client(res.status(), res.text().await.unwrap_or_default()));
}
let res = res
.json::<RegionUrlResponse>()
.await
.map_err(|e| SignalError::RegionError(error_with_chain(&e)))?;
Ok(res.regions.into_iter().map(|i| i.url).collect())
};
livekit_runtime::timeout(REGION_FETCH_TIMEOUT, fetch_fut)
.await
.map_err(|_| SignalError::RegionError("region fetch timed out".into()))?
}
fn is_cloud_url(url: &str) -> SignalResult<bool> {
let url = url::Url::parse(url).map_err(|err| SignalError::UrlParse(err.to_string()))?;
let host = match url.host() {
Some(host) => host.to_string(),
None => {
return Err(SignalError::UrlParse("invalid hostname".into()));
}
};
Ok(host.ends_with(".livekit.cloud") || host.ends_with(".livekit.run"))
}
fn region_endpoint(url: &str) -> SignalResult<String> {
let mut url = url::Url::parse(url).map_err(|err| SignalError::UrlParse(err.to_string()))?;
match url.scheme() {
"wss" => url.set_scheme("https").unwrap(),
"ws" => url.set_scheme("http").unwrap(),
_ => (),
}
url.set_path("/settings/regions");
Ok(url.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
use std::fmt;
use std::io;
// Mock error types to test error chain preservation
#[derive(Debug)]
struct RootCauseError {
message: String,
}
impl fmt::Display for RootCauseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for RootCauseError {}
#[derive(Debug)]
struct MiddleError {
message: String,
source: RootCauseError,
}
impl fmt::Display for MiddleError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for MiddleError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.source)
}
}
#[derive(Debug)]
struct OuterError {
message: String,
source: MiddleError,
}
impl fmt::Display for OuterError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for OuterError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.source)
}
}
#[test]
fn test_error_with_chain_single_error() {
let err = RootCauseError { message: "root cause".to_string() };
let result = error_with_chain(&err);
assert_eq!(result, "root cause");
}
#[test]
fn test_error_with_chain_two_level_chain() {
let root =
RootCauseError { message: "invalid peer certificate: UnknownIssuer".to_string() };
let middle = MiddleError { message: "error trying to connect".to_string(), source: root };
let result = error_with_chain(&middle);
assert_eq!(result, "error trying to connect: invalid peer certificate: UnknownIssuer");
}
#[test]
fn test_error_with_chain_three_level_chain() {
// Simulates the actual error chain from reqwest -> hyper -> TLS
let root =
RootCauseError { message: "invalid peer certificate: UnknownIssuer".to_string() };
let middle = MiddleError { message: "error trying to connect".to_string(), source: root };
let outer = OuterError {
message:
"error sending request for url (https://example.livekit.cloud/settings/regions)"
.to_string(),
source: middle,
};
let result = error_with_chain(&outer);
assert_eq!(
result,
"error sending request for url (https://example.livekit.cloud/settings/regions): error trying to connect: invalid peer certificate: UnknownIssuer"
);
}
#[test]
fn test_error_with_chain_preserves_tls_error_info() {
// Verify that TLS-specific error messages are preserved in the chain
let root =
RootCauseError { message: "invalid peer certificate: UnknownIssuer".to_string() };
let outer = MiddleError { message: "TLS connection error".to_string(), source: root };
let result = error_with_chain(&outer);
// The error message should contain both the outer message and the root cause
assert!(result.contains("TLS connection error"));
assert!(result.contains("UnknownIssuer"));
assert!(result.contains("invalid peer certificate"));
}
#[test]
fn test_region_error_includes_full_chain() {
// Test that SignalError::RegionError properly includes the full error chain
let root =
RootCauseError { message: "invalid peer certificate: UnknownIssuer".to_string() };
let middle = MiddleError { message: "error trying to connect".to_string(), source: root };
let outer = OuterError { message: "error sending request".to_string(), source: middle };
let signal_error = SignalError::RegionError(error_with_chain(&outer));
let error_string = signal_error.to_string();
// Verify the full chain is in the error message
assert!(
error_string.contains("UnknownIssuer"),
"Error should contain root cause 'UnknownIssuer', got: {}",
error_string
);
assert!(
error_string.contains("error trying to connect"),
"Error should contain middle error, got: {}",
error_string
);
assert!(
error_string.contains("error sending request"),
"Error should contain outer error, got: {}",
error_string
);
}
#[test]
fn test_error_with_chain_io_error() {
// Test with a real std::io::Error chain
let inner = io::Error::new(io::ErrorKind::ConnectionRefused, "connection refused");
let outer = io::Error::new(io::ErrorKind::Other, inner);
let result = error_with_chain(&outer);
assert!(
result.contains("connection refused"),
"Should contain the inner error message, got: {}",
result
);
}
#[test]
fn test_is_cloud_url() {
assert!(is_cloud_url("wss://myapp.livekit.cloud").unwrap());
assert!(is_cloud_url("wss://myapp.livekit.run").unwrap());
assert!(is_cloud_url("https://myapp.livekit.cloud").unwrap());
assert!(!is_cloud_url("wss://localhost:7880").unwrap());
assert!(!is_cloud_url("wss://example.com").unwrap());
assert!(!is_cloud_url("wss://livekit.cloud.example.com").unwrap());
}
#[test]
fn test_region_endpoint() {
assert_eq!(
region_endpoint("wss://myapp.livekit.cloud").unwrap(),
"https://myapp.livekit.cloud/settings/regions"
);
assert_eq!(
region_endpoint("ws://myapp.livekit.run").unwrap(),
"http://myapp.livekit.run/settings/regions"
);
assert_eq!(
region_endpoint("https://myapp.livekit.cloud").unwrap(),
"https://myapp.livekit.cloud/settings/regions"
);
}
#[tokio::test]
async fn test_fetch_non_cloud_url_returns_empty() {
let result =
RegionUrlProvider::fetch_region_urls("wss://localhost:7880", "fake-token").await;
assert_eq!(result.unwrap(), Vec::<String>::new());
}
}