Skip to main content

leyline/profile/
types.rs

1use std::collections::HashMap;
2
3use serde::Deserialize;
4
5use crate::profile::H3Profile;
6use crate::profile::preset::HeaderStyle;
7use crate::profile::registry::ProfileError;
8use crate::{Error, Kind};
9
10#[expect(
11    missing_docs,
12    reason = "profile schema mirrors the embedded TOML tables; variant and field names are the documentation"
13)]
14#[derive(Debug, Clone, Deserialize)]
15#[serde(deny_unknown_fields)]
16#[non_exhaustive]
17pub struct BrowserProfile {
18    pub meta: ProfileMeta,
19    pub tls: TlsProfile,
20    pub h2: H2Profile,
21    #[serde(default)]
22    pub identity: HashMap<String, PlatformIdentity>,
23    #[serde(default)]
24    pub h3: Option<H3Profile>,
25    #[serde(skip)]
26    source: Option<source::ProfileSource>,
27}
28
29#[expect(
30    missing_docs,
31    reason = "profile schema mirrors the embedded TOML tables; variant and field names are the documentation"
32)]
33#[derive(Debug, Clone, Deserialize)]
34#[serde(deny_unknown_fields)]
35#[non_exhaustive]
36pub struct ProfileMeta {
37    pub name: String,
38    pub browser: String,
39    pub version: u32,
40    #[serde(default)]
41    pub family: String,
42    #[serde(default)]
43    pub verified_against: String,
44    #[serde(default)]
45    pub captured_against: Option<String>,
46    #[serde(default)]
47    pub chromium_major: Option<u32>,
48    #[serde(default)]
49    pub ch_ua_brand: Option<String>,
50    #[serde(default)]
51    pub header_style: HeaderStyle,
52    #[serde(skip)]
53    pub(crate) header_order: Option<Vec<String>>,
54    #[serde(default, rename = "capture")]
55    _capture: serde::de::IgnoredAny,
56    #[serde(default, rename = "variant")]
57    _variant: serde::de::IgnoredAny,
58    #[serde(default, rename = "hello")]
59    _hello: serde::de::IgnoredAny,
60    #[serde(default, rename = "platform_browser")]
61    _platform_browser: serde::de::IgnoredAny,
62    #[serde(default, rename = "deprecated")]
63    _deprecated: serde::de::IgnoredAny,
64    #[serde(default, rename = "verified_at")]
65    _verified_at: serde::de::IgnoredAny,
66}
67
68#[expect(
69    missing_docs,
70    reason = "profile schema mirrors the embedded TOML tables; variant and field names are the documentation"
71)]
72#[derive(Debug, Clone, Deserialize)]
73#[serde(deny_unknown_fields)]
74#[non_exhaustive]
75pub struct TlsProfile {
76    pub ciphers: Vec<String>,
77    pub curves: Vec<String>,
78    #[serde(default)]
79    pub key_shares: Option<Vec<String>>,
80    pub sigalgs: Vec<String>,
81    #[serde(default)]
82    pub delegated_credentials: Option<String>,
83    #[serde(default)]
84    pub record_size_limit: Option<u16>,
85    #[serde(default)]
86    pub alps: Option<String>,
87    #[serde(default)]
88    pub alps_new_codepoint: bool,
89    #[serde(default)]
90    pub ocsp_stapling: bool,
91    #[serde(default)]
92    pub signed_cert_timestamps: bool,
93    #[serde(default)]
94    pub cert_compression: Vec<String>,
95    #[serde(default)]
96    pub permute_extensions: bool,
97    #[serde(default)]
98    pub extension_permutation: Option<Vec<u16>>,
99    #[serde(default)]
100    pub extension_tail: Vec<u16>,
101    #[serde(default = "default_grease")]
102    pub grease: bool,
103    #[serde(default)]
104    pub sigalg_grease: bool,
105    #[serde(default)]
106    pub ech_grease: bool,
107    #[serde(default)]
108    pub pre_shared_key: bool,
109    #[serde(default = "default_true")]
110    pub session_tickets: bool,
111    #[serde(default)]
112    pub request_trust_anchors: bool,
113    #[serde(default)]
114    pub fingerprint: Option<TlsFingerprint>,
115    #[serde(default)]
116    pub padding: bool,
117    #[serde(default)]
118    pub min_tls_version: Option<String>,
119    #[serde(default)]
120    pub tls12_extensions: bool,
121}
122
123const fn default_grease() -> bool {
124    true
125}
126
127const fn default_true() -> bool {
128    true
129}
130
131#[expect(
132    missing_docs,
133    reason = "profile schema mirrors the embedded TOML tables; variant and field names are the documentation"
134)]
135#[derive(Debug, Clone, Default, Deserialize)]
136#[serde(deny_unknown_fields)]
137#[non_exhaustive]
138pub struct TlsFingerprint {
139    #[serde(default)]
140    pub ja4: Option<String>,
141    #[serde(default)]
142    pub resumed_ja4: Option<String>,
143    #[serde(default)]
144    pub platforms: HashMap<String, TlsFingerprint>,
145}
146
147#[expect(
148    missing_docs,
149    reason = "profile schema mirrors the embedded TOML tables; variant and field names are the documentation"
150)]
151#[derive(Debug, Clone, Deserialize)]
152#[serde(deny_unknown_fields)]
153#[non_exhaustive]
154pub struct H2Profile {
155    #[serde(default)]
156    pub header_table_size: Option<u32>,
157    #[serde(default)]
158    pub enable_push: Option<bool>,
159    #[serde(default)]
160    pub max_concurrent_streams: Option<u32>,
161    #[serde(default)]
162    pub initial_stream_window_size: Option<u32>,
163    #[serde(default)]
164    pub initial_connection_window_size: Option<u32>,
165    #[serde(default)]
166    pub max_frame_size: Option<u32>,
167    #[serde(default)]
168    pub max_header_list_size: Option<u32>,
169    #[serde(default)]
170    pub unknown_setting8: Option<u32>,
171    #[serde(default)]
172    pub unknown_setting9: Option<u32>,
173    pub pseudo_order: Vec<String>,
174    pub settings_order: Vec<String>,
175    #[serde(default)]
176    pub default_priority: Option<H2PriorityProfile>,
177    #[serde(default)]
178    pub fingerprint: Option<H2Fingerprint>,
179    #[serde(default)]
180    pub platforms: HashMap<String, H2PlatformOverride>,
181}
182
183#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
184#[serde(deny_unknown_fields)]
185#[non_exhaustive]
186pub struct H2PriorityProfile {
187    pub exclusive: bool,
188    pub stream_dependency: u32,
189    pub weight: u8,
190}
191
192#[expect(
193    missing_docs,
194    reason = "profile schema mirrors the embedded TOML tables; variant and field names are the documentation"
195)]
196#[derive(Debug, Clone, Default, Deserialize)]
197#[serde(deny_unknown_fields)]
198#[non_exhaustive]
199pub struct H2PlatformOverride {
200    #[serde(default)]
201    pub header_table_size: Option<u32>,
202    #[serde(default)]
203    pub enable_push: Option<bool>,
204    #[serde(default)]
205    pub max_concurrent_streams: Option<u32>,
206    #[serde(default)]
207    pub initial_stream_window_size: Option<u32>,
208    #[serde(default)]
209    pub initial_connection_window_size: Option<u32>,
210    #[serde(default)]
211    pub max_frame_size: Option<u32>,
212    #[serde(default)]
213    pub max_header_list_size: Option<u32>,
214    #[serde(default)]
215    pub unknown_setting8: Option<u32>,
216    #[serde(default)]
217    pub unknown_setting9: Option<u32>,
218    #[serde(default)]
219    pub omit_settings: Vec<String>,
220    #[serde(default)]
221    pub pseudo_order: Option<Vec<String>>,
222    #[serde(default)]
223    pub settings_order: Option<Vec<String>>,
224    #[serde(default)]
225    pub fingerprint: Option<H2Fingerprint>,
226}
227
228#[expect(
229    missing_docs,
230    reason = "profile schema mirrors the embedded TOML tables; variant and field names are the documentation"
231)]
232#[derive(Debug, Clone, Default, Deserialize)]
233#[serde(deny_unknown_fields)]
234#[non_exhaustive]
235pub struct H2Fingerprint {
236    #[serde(default)]
237    pub akamai: Option<String>,
238}
239
240#[expect(
241    missing_docs,
242    reason = "profile schema mirrors the embedded TOML tables; variant and field names are the documentation"
243)]
244#[derive(Debug, Clone, Deserialize)]
245#[serde(deny_unknown_fields)]
246#[non_exhaustive]
247pub struct PlatformIdentity {
248    pub user_agent: String,
249    #[serde(default)]
250    pub sec_ch_ua: String,
251    #[serde(default)]
252    pub accept_language: Option<String>,
253}
254
255impl BrowserProfile {
256    pub fn from_toml(toml_str: &str) -> Result<Self, ProfileError> {
257        let mut profile: Self = toml::from_str(toml_str).map_err(ProfileError::parse)?;
258        profile.derive_sec_ch_ua();
259        profile.source = Some(source::ProfileSource::new(toml_str));
260        profile
261            .tls
262            .validate(false)
263            .map_err(|why| ProfileError::parse(format!("{}: {why}", profile.meta.name)))?;
264        if let Some(h3) = &profile.h3 {
265            h3.validate()
266                .map_err(|why| ProfileError::parse(format!("{}: {why}", profile.meta.name)))?;
267        }
268        for warning in profile.load_warnings() {
269            tracing::warn!(target: "leyline::profile", "{warning}");
270        }
271        Ok(profile)
272    }
273
274    fn derive_sec_ch_ua(&mut self) {
275        let Some(brand) = self.meta.ch_ua_brand.as_deref() else {
276            return;
277        };
278        let derived =
279            crate::profile::brand::sec_ch_ua(self.meta.version, Some((brand, self.meta.version)));
280        for identity in self.identity.values_mut() {
281            if identity.sec_ch_ua.is_empty() {
282                identity.sec_ch_ua.clone_from(&derived);
283            }
284        }
285    }
286
287    bench_pub! {
288        fn load_warnings(&self) -> Vec<String> {
289            let mut warnings = Vec::new();
290            let capture_unrecorded = self
291                .meta
292                .captured_against
293                .as_deref()
294                .is_none_or(|s| s.trim().is_empty());
295            if capture_unrecorded {
296                warnings.push(format!(
297                    "{}: [meta] captured_against is missing — the exact browser build this \
298                     profile was captured against is unrecorded",
299                    self.meta.name
300                ));
301            }
302            warnings
303        }
304    }
305
306    bench_pub! {
307        fn identity_for(&self, platform: crate::profile::Platform) -> Option<&PlatformIdentity> {
308            self.identity.get(platform.identity_key())
309        }
310    }
311
312    pub(crate) fn source(&self) -> Option<&str> {
313        self.source.as_ref().map(source::ProfileSource::as_str)
314    }
315
316    pub fn expected_ja4(&self) -> Option<&str> {
317        self.tls.fingerprint.as_ref()?.ja4.as_deref()
318    }
319
320    pub fn expected_h2_fingerprint(&self) -> Option<&str> {
321        self.h2.fingerprint.as_ref()?.akamai.as_deref()
322    }
323}
324
325impl H2Profile {
326    bench_pub! {
327        fn resolve_for_platform(
328            &self,
329            platform: crate::profile::Platform,
330        ) -> Result<H2Profile, Error> {
331            let Some(over) = self.platforms.get(platform.identity_key()) else {
332                return Ok(self.clone());
333            };
334            let mut out = self.clone();
335
336            if let Some(v) = over.header_table_size {
337                out.header_table_size = Some(v);
338            }
339            if let Some(v) = over.enable_push {
340                out.enable_push = Some(v);
341            }
342            if let Some(v) = over.max_concurrent_streams {
343                out.max_concurrent_streams = Some(v);
344            }
345            if let Some(v) = over.initial_stream_window_size {
346                out.initial_stream_window_size = Some(v);
347            }
348            if let Some(v) = over.initial_connection_window_size {
349                out.initial_connection_window_size = Some(v);
350            }
351            if let Some(v) = over.max_frame_size {
352                out.max_frame_size = Some(v);
353            }
354            if let Some(v) = over.max_header_list_size {
355                out.max_header_list_size = Some(v);
356            }
357            if let Some(v) = over.unknown_setting8 {
358                out.unknown_setting8 = Some(v);
359            }
360            if let Some(v) = over.unknown_setting9 {
361                out.unknown_setting9 = Some(v);
362            }
363            if let Some(ref order) = over.pseudo_order {
364                out.pseudo_order = order.clone();
365            }
366            if let Some(ref order) = over.settings_order {
367                out.settings_order = order.clone();
368            }
369
370            for name in &over.omit_settings {
371                match name.as_str() {
372                    "header_table_size" => out.header_table_size = None,
373                    "enable_push" => out.enable_push = None,
374                    "max_concurrent_streams" => out.max_concurrent_streams = None,
375                    "initial_stream_window_size" => out.initial_stream_window_size = None,
376                    "max_frame_size" => out.max_frame_size = None,
377                    "max_header_list_size" => out.max_header_list_size = None,
378                    "unknown_setting8" => out.unknown_setting8 = None,
379                    "unknown_setting9" => out.unknown_setting9 = None,
380                    other => {
381                        return Err(Error::new(Kind::Config).with_message(format!(
382                            "unknown name in [h2.platforms.{}].omit_settings: {other:?}",
383                            platform.identity_key()
384                        )));
385                    }
386                }
387            }
388
389            Ok(out)
390        }
391    }
392}
393
394mod source;
395
396#[cfg(test)]
397mod tests;