1use serde::{Deserialize, Deserializer, Serialize};
5use serde_json::{Map, Value};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
13pub enum SolariRegion {
14 #[default]
16 UsWest,
17}
18
19impl SolariRegion {
20 pub fn base_url(&self) -> &'static str {
22 match self {
23 SolariRegion::UsWest => "https://api.getsolari.com",
24 }
25 }
26}
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
34pub enum SameSite {
35 Strict,
36 Lax,
37 None,
38}
39
40#[derive(Debug, Clone, Default, Serialize, Deserialize)]
42#[serde(rename_all = "camelCase")]
43pub struct Cookie {
44 pub name: String,
45 pub value: String,
46 #[serde(default, skip_serializing_if = "Option::is_none")]
47 pub domain: Option<String>,
48 #[serde(default, skip_serializing_if = "Option::is_none")]
49 pub path: Option<String>,
50 #[serde(default, skip_serializing_if = "Option::is_none")]
51 pub expires: Option<f64>,
52 #[serde(default, skip_serializing_if = "Option::is_none")]
53 pub http_only: Option<bool>,
54 #[serde(default, skip_serializing_if = "Option::is_none")]
55 pub secure: Option<bool>,
56 #[serde(default, skip_serializing_if = "Option::is_none")]
57 pub same_site: Option<SameSite>,
58}
59
60#[derive(Debug, Clone, Default, Serialize, Deserialize)]
62pub struct LocalStorageEntry {
63 pub name: String,
64 pub value: String,
65}
66
67#[derive(Debug, Clone, Default, Serialize, Deserialize)]
69#[serde(rename_all = "camelCase")]
70pub struct StorageOrigin {
71 pub origin: String,
72 #[serde(default, skip_serializing_if = "Option::is_none")]
73 pub local_storage: Option<Vec<LocalStorageEntry>>,
74}
75
76#[derive(Debug, Clone, Default, Serialize, Deserialize)]
79#[serde(rename_all = "camelCase")]
80pub struct StorageState {
81 #[serde(default, skip_serializing_if = "Option::is_none")]
82 pub cookies: Option<Vec<Cookie>>,
83 #[serde(default, skip_serializing_if = "Option::is_none")]
84 pub origins: Option<Vec<StorageOrigin>>,
85 #[serde(flatten)]
87 pub extra: Map<String, Value>,
88}
89
90#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
96#[serde(rename_all = "lowercase")]
97pub enum ProxyTier {
98 Residential,
100 Static,
102 Mobile,
104}
105
106#[derive(Debug, Clone, Default, Serialize)]
108#[serde(rename_all = "camelCase")]
109pub struct ProxyRequest {
110 #[serde(skip_serializing_if = "Option::is_none")]
112 pub country: Option<String>,
113 #[serde(skip_serializing_if = "Option::is_none")]
115 pub tier: Option<ProxyTier>,
116 #[serde(skip_serializing_if = "Option::is_none")]
118 pub asn: Option<String>,
119 #[serde(skip_serializing_if = "Option::is_none")]
122 pub session: Option<String>,
123 #[serde(skip_serializing_if = "Option::is_none")]
125 pub session_duration: Option<u32>,
126 #[serde(skip_serializing_if = "Option::is_none")]
128 pub state: Option<String>,
129 #[serde(skip_serializing_if = "Option::is_none")]
131 pub city: Option<String>,
132}
133
134impl ProxyRequest {
135 pub fn country(code: impl Into<String>) -> Self {
137 ProxyRequest {
138 country: Some(code.into()),
139 ..Default::default()
140 }
141 }
142
143 pub fn tier(mut self, tier: ProxyTier) -> Self {
144 self.tier = Some(tier);
145 self
146 }
147
148 pub fn asn(mut self, asn: impl Into<String>) -> Self {
149 self.asn = Some(asn.into());
150 self
151 }
152
153 pub fn session(mut self, id: impl Into<String>) -> Self {
155 self.session = Some(id.into());
156 self
157 }
158
159 pub fn session_duration(mut self, minutes: u32) -> Self {
160 self.session_duration = Some(minutes);
161 self
162 }
163
164 pub fn state(mut self, state: impl Into<String>) -> Self {
165 self.state = Some(state.into());
166 self
167 }
168
169 pub fn city(mut self, city: impl Into<String>) -> Self {
170 self.city = Some(city.into());
171 self
172 }
173}
174
175#[derive(Debug, Clone, Serialize)]
187#[serde(untagged)]
188pub enum ProxySpec {
189 Shorthand(String),
191 Request(ProxyRequest),
193}
194
195impl ProxySpec {
196 pub fn country(code: impl Into<String>) -> Self {
198 ProxySpec::Shorthand(code.into())
199 }
200
201 pub fn off() -> Self {
203 ProxySpec::Shorthand("off".to_string())
204 }
205
206 pub fn smart() -> Self {
208 ProxySpec::Shorthand("smart".to_string())
209 }
210}
211
212impl From<ProxyRequest> for ProxySpec {
213 fn from(r: ProxyRequest) -> Self {
214 ProxySpec::Request(r)
215 }
216}
217
218#[derive(Debug, Clone, Deserialize)]
224#[serde(rename_all = "camelCase")]
225pub struct ResolvedProxyConfig {
226 pub timezone_id: String,
227 pub country: String,
228 #[serde(default)]
229 pub tier: Option<ProxyTier>,
230}
231
232#[derive(Debug, Clone, Default, Deserialize)]
235pub struct ProxyCountries {
236 #[serde(default)]
239 pub enabled: bool,
240 #[serde(default)]
242 pub countries: Vec<String>,
243}
244
245#[derive(Debug, Clone)]
251pub struct Session {
252 pub id: String,
253 pub ws_endpoint: String,
255 pub cdp_endpoint: String,
258 pub expires_at: String,
260 pub storage_state: Option<Option<StorageState>>,
265 pub proxy: Option<ResolvedProxyConfig>,
267}
268
269#[derive(Debug, Clone, Default, Deserialize)]
272#[serde(rename_all = "camelCase")]
273pub struct SessionView {
274 #[serde(default)]
275 pub session_id: Option<String>,
276 #[serde(default)]
277 pub status: Option<String>,
278 #[serde(default)]
279 pub expires_at: Option<String>,
280 #[serde(flatten)]
281 pub extra: Map<String, Value>,
282}
283
284#[derive(Debug, Clone)]
286pub struct ReplayUrl {
287 pub url: String,
288 pub expires_in_seconds: u64,
290 pub content_encoding: String,
292}
293
294#[derive(Debug, Clone, Deserialize)]
300#[serde(rename_all = "camelCase")]
301pub struct Profile {
302 pub id: String,
303 #[serde(default)]
304 pub name: String,
305 #[serde(flatten)]
307 pub extra: Map<String, Value>,
308}
309
310#[derive(Debug, Clone, Default)]
312pub struct ProfileSaveResult {
313 pub version: u64,
315 pub size_bytes: u64,
317}
318
319#[derive(Debug, Clone, Default, Serialize)]
326#[serde(rename_all = "camelCase")]
327pub(crate) struct CreateSessionRequest {
328 #[serde(skip_serializing_if = "Option::is_none")]
329 pub profile_id: Option<String>,
330 #[serde(skip_serializing_if = "is_false")]
331 pub recording: bool,
332 #[serde(skip_serializing_if = "is_false")]
333 pub stealth: bool,
334 #[serde(skip_serializing_if = "is_false")]
335 pub captcha: bool,
336 #[serde(skip_serializing_if = "Option::is_none")]
337 pub proxy: Option<ProxySpec>,
338}
339
340fn is_false(b: &bool) -> bool {
341 !*b
342}
343
344#[derive(Debug, Clone, Deserialize)]
346#[serde(rename_all = "camelCase")]
347pub(crate) struct StorageStateUrl {
348 #[serde(default)]
349 pub url: Option<String>,
350 #[allow(dead_code)]
351 #[serde(default)]
352 pub expires_in_seconds: Option<u64>,
353}
354
355#[derive(Debug, Clone, Deserialize)]
357#[serde(rename_all = "camelCase")]
358pub(crate) struct CreateSessionResponse {
359 #[serde(default)]
360 pub session_id: Option<String>,
361 #[serde(default)]
362 pub ws_endpoint: Option<String>,
363 #[serde(default)]
364 pub cdp_endpoint: Option<String>,
365 #[serde(default)]
366 pub expires_at: Option<String>,
367 #[serde(default, deserialize_with = "double_option")]
369 pub storage_state_url: Option<Option<StorageStateUrl>>,
370 #[serde(default, deserialize_with = "double_option")]
372 pub storage_state: Option<Option<StorageState>>,
373 #[serde(default)]
374 pub proxy: Option<ResolvedProxyConfig>,
375}
376
377#[derive(Debug, Clone, Deserialize)]
379#[serde(rename_all = "camelCase")]
380pub(crate) struct ReplayUrlResponse {
381 #[serde(default)]
382 pub url: Option<String>,
383 #[serde(default)]
384 pub expires_in_seconds: Option<u64>,
385 #[serde(default)]
386 pub content_encoding: Option<String>,
387}
388
389#[derive(Debug, Clone, Deserialize)]
391#[serde(rename_all = "camelCase")]
392pub(crate) struct ProfileSaveResponse {
393 #[serde(default)]
394 pub version: Option<u64>,
395 #[serde(default)]
396 pub size_bytes: Option<u64>,
397}
398
399fn double_option<'de, T, D>(de: D) -> Result<Option<Option<T>>, D::Error>
402where
403 T: serde::Deserialize<'de>,
404 D: Deserializer<'de>,
405{
406 serde::Deserialize::deserialize(de).map(Some)
407}
408
409#[cfg(test)]
412mod resolved_proxy_tests {
413 use super::*;
414
415 fn debug_field_names(dbg: &str) -> Vec<String> {
428 let inner = dbg
429 .split_once('{')
430 .and_then(|(_, rest)| rest.rsplit_once('}'))
431 .map(|(inner, _)| inner)
432 .unwrap_or("");
433 let mut names = Vec::new();
434 let mut depth = 0usize;
435 for part in inner.split(',') {
436 if depth == 0 {
438 if let Some((name, _)) = part.split_once(':') {
439 let name = name.trim();
440 if !name.is_empty() {
441 names.push(name.to_string());
442 }
443 }
444 }
445 depth += part.matches(['(', '{', '[']).count();
446 depth = depth.saturating_sub(part.matches([')', '}', ']']).count());
447 }
448 names
449 }
450
451 #[test]
452 fn resolved_proxy_config_has_exactly_the_confirmation_fields() {
453 let wire = r#"{"timezoneId":"America/Los_Angeles","country":"us","tier":"mobile"}"#;
454 let rp: ResolvedProxyConfig = serde_json::from_str(wire).unwrap();
455
456 let names = debug_field_names(&format!("{rp:?}"));
457 assert_eq!(
458 names,
459 vec!["timezone_id", "country", "tier"],
460 "ResolvedProxyConfig must stay a COARSE CONFIRMATION: egress is applied \
461 server-side, so a caller never dials the proxy and must never receive \
462 its address or account. Got: {names:?}"
463 );
464 }
465
466 #[test]
467 fn legacy_credential_keys_are_dropped_not_resurrected() {
468 let wire = r#"{
469 "timezoneId":"America/Los_Angeles",
470 "country":"us",
471 "tier":"mobile",
472 "server":"http://resi.vendor.example:8000",
473 "username":"acct-12345",
474 "password":"hunter2"
475 }"#;
476 let rp: ResolvedProxyConfig = serde_json::from_str(wire).unwrap();
477
478 assert_eq!(rp.timezone_id, "America/Los_Angeles");
479 assert_eq!(rp.country, "us");
480
481 let rendered = format!("{rp:?}");
482 for secret in ["resi.vendor.example", "acct-12345", "hunter2", "server", "username", "password"] {
483 assert!(
484 !rendered.contains(secret),
485 "resolved proxy leaks {secret:?}: {rendered}"
486 );
487 }
488 }
489}