Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ targets = ["x86_64-unknown-linux-gnu"]
[features]
default = ["emulation", "gzip", "brotli", "deflate", "zstd"]

full = ["emulation", "emulation-rand", "gzip", "brotli", "deflate", "zstd"]

# Emulation devices
emulation = ["dep:typed-builder"]

Expand All @@ -37,11 +35,11 @@ deflate = ["wreq/deflate"]
zstd = ["wreq/zstd"]

[dependencies]
wreq = { version = ">=3.0.5,<6", default-features = false }
wreq = { version = "6.0.0-rc.2", default-features = false }
serde = { version = "1.0", features = ["derive"], optional = true }
typed-builder = { version = "0.21.0", optional = true }
strum = { version = "0.27.1", optional = true }
strum_macros = { version = "0.27.1", optional = true }
strum = { version = "0.27.2", optional = true }
strum_macros = { version = "0.27.2", optional = true }

[dev-dependencies]
tokio = { version = "1", features = ["full"] }
Expand All @@ -51,7 +49,7 @@ hyper = { version = "1.1.0", default-features = false, features = [
"client",
"server",
] }
hyper-util = { version = "0.1.10", features = [
hyper-util = { version = "0.1.16", features = [
"http1",
"http2",
"client",
Expand Down Expand Up @@ -93,14 +91,21 @@ path = "tests/emulation_safari.rs"
[[example]]
name = "emulation"
path = "examples/emulation.rs"
required-features = ["emulation", "gzip", "brotli", "deflate", "zstd", "wreq/full"]
required-features = ["emulation", "gzip", "brotli", "deflate", "zstd"]

[[example]]
name = "emulation_rand"
path = "examples/emulation_rand.rs"
required-features = ["emulation", "gzip", "brotli", "deflate", "zstd", "wreq/full", "emulation-rand"]
required-features = [
"emulation",
"gzip",
"brotli",
"deflate",
"zstd",
"emulation-rand",
]

[[example]]
name = "emulation_option"
path = "examples/emulation_option.rs"
required-features = ["emulation", "gzip", "brotli", "deflate", "zstd", "wreq/full"]
required-features = ["emulation", "gzip", "brotli", "deflate", "zstd"]
59 changes: 59 additions & 0 deletions src/emulation/device/chrome/header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use super::*;

pub fn header_initializer(
sec_ch_ua: &'static str,
ua: &'static str,
emulation_os: EmulationOS,
) -> HeaderMap {
let mut headers = HeaderMap::new();
header_chrome_sec_ch_ua!(
headers,
sec_ch_ua,
emulation_os.platform(),
emulation_os.is_mobile()
);
header_chrome_ua!(headers, ua);
header_chrome_sec_fetch!(headers);
header_chrome_accpet!(headers);
headers
}

pub fn header_initializer_with_zstd(
sec_ch_ua: &'static str,
ua: &'static str,
emulation_os: EmulationOS,
) -> HeaderMap {
let mut headers = HeaderMap::new();
header_chrome_sec_ch_ua!(
headers,
sec_ch_ua,
emulation_os.platform(),
emulation_os.is_mobile()
);
header_chrome_ua!(headers, ua);
header_chrome_sec_fetch!(headers);
header_chrome_accpet!(zstd, headers);
headers
}

pub fn header_initializer_with_zstd_priority(
sec_ch_ua: &'static str,
ua: &'static str,
emulation_os: EmulationOS,
) -> HeaderMap {
let mut headers = HeaderMap::new();
header_chrome_sec_ch_ua!(
headers,
sec_ch_ua,
emulation_os.platform(),
emulation_os.is_mobile()
);
header_chrome_ua!(headers, ua);
header_chrome_sec_fetch!(headers);
header_chrome_accpet!(zstd, headers);
headers.insert(
HeaderName::from_static("priority"),
HeaderValue::from_static("u=0, i"),
);
headers
}
201 changes: 201 additions & 0 deletions src/emulation/device/chrome/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
macro_rules! headers_stream_dependency {
() => {
StreamDependency::new(StreamId::zero(), 255, true)
};
}

macro_rules! pseudo_order {
() => {
PseudoOrder::builder()
.extend([
PseudoId::Method,
PseudoId::Authority,
PseudoId::Scheme,
PseudoId::Path,
])
.build()
};
}

macro_rules! settings_order {
() => {
SettingsOrder::builder()
.extend([
SettingId::HeaderTableSize,
SettingId::EnablePush,
SettingId::MaxConcurrentStreams,
SettingId::InitialWindowSize,
SettingId::MaxFrameSize,
SettingId::MaxHeaderListSize,
SettingId::EnableConnectProtocol,
SettingId::NoRfc7540Priorities,
])
.build()
};
}

macro_rules! tls_options {
(@build $builder:expr) => {
$builder.build().into()
};

(1) => {
tls_options!(@build ChromeTlsConfig::builder())
};
(2) => {
tls_options!(@build ChromeTlsConfig::builder().enable_ech_grease(true))
};
(3) => {
tls_options!(@build ChromeTlsConfig::builder().permute_extensions(true))
};
(4) => {
tls_options!(@build ChromeTlsConfig::builder()
.permute_extensions(true)
.enable_ech_grease(true))
};
(5) => {
tls_options!(@build ChromeTlsConfig::builder()
.permute_extensions(true)
.enable_ech_grease(true)
.pre_shared_key(true))
};
(6, $curves:expr) => {
tls_options!(@build ChromeTlsConfig::builder()
.permute_extensions(true)
.enable_ech_grease(true)
.pre_shared_key(true)
.curves($curves))
};
(7, $curves:expr) => {
tls_options!(@build ChromeTlsConfig::builder()
.permute_extensions(true)
.enable_ech_grease(true)
.pre_shared_key(true)
.curves($curves)
.alps_use_new_codepoint(true))
};
}

macro_rules! http2_options {
(@base $builder:expr) => {
$builder
.initial_window_size(6291456)
.initial_connection_window_size(15728640)
.max_header_list_size(262144)
.header_table_size(65536)
.headers_stream_dependency(headers_stream_dependency!())
.headers_pseudo_order(pseudo_order!())
.settings_order(settings_order!())
};

(1) => {
http2_options!(@base Http2Options::builder())
.max_concurrent_streams(1000)
.build()
};
(2) => {
http2_options!(@base Http2Options::builder())
.max_concurrent_streams(1000)
.enable_push(false)
.build()
};
(3) => {
http2_options!(@base Http2Options::builder())
.enable_push(false)
.build()
};
}

macro_rules! mod_generator {
(
$mod_name:ident,
$tls_options:expr,
$http2_options:expr,
$header_initializer:ident,
[($default_os:ident, $default_sec_ch_ua:tt, $default_ua:tt) $(, ($other_os:ident, $other_sec_ch_ua:tt, $other_ua:tt))*]
) => {
pub(crate) mod $mod_name {
use super::*;

#[inline(always)]
pub fn emulation(option: EmulationOption) -> Emulation {
let default_headers = if !option.skip_headers {
#[allow(unreachable_patterns)]
let default_headers = match option.emulation_os {
$(
EmulationOS::$other_os => $header_initializer(
$other_sec_ch_ua,
$other_ua,
option.emulation_os,
),
)*
_ => $header_initializer(
$default_sec_ch_ua,
$default_ua,
EmulationOS::$default_os,
),
};
Some(default_headers)
} else {
None
};

build_emulation(option, default_headers)
}

#[inline(always)]
pub fn build_emulation(
option: EmulationOption,
default_headers: Option<HeaderMap>
) -> Emulation {
let mut builder = Emulation::builder().tls_options($tls_options);

if !option.skip_http2 {
builder = builder.http2_options($http2_options);
}

if let Some(headers) = default_headers {
builder = builder.headers(headers);
}

builder.build()
}
}
};
(
$mod_name:ident,
$build_emulation:expr,
$header_initializer:ident,
[($default_os:ident, $default_sec_ch_ua:tt, $default_ua:tt) $(, ($other_os:ident, $other_sec_ch_ua:tt, $other_ua:tt))*]
) => {
pub(crate) mod $mod_name {
use super::*;

#[inline(always)]
pub fn emulation(option: EmulationOption) -> Emulation {
let default_headers = if !option.skip_headers {
#[allow(unreachable_patterns)]
let default_headers = match option.emulation_os {
$(
EmulationOS::$other_os => $header_initializer(
$other_sec_ch_ua,
$other_ua,
option.emulation_os,
),
)*
_ => $header_initializer(
$default_sec_ch_ua,
$default_ua,
EmulationOS::$default_os,
),
};
Some(default_headers)
} else {
None
};

$build_emulation(option, default_headers)
}
}
};
}
Loading
Loading