Skip to content

Commit 5a31cee

Browse files
author
root
committed
youtube sni rewrite
1 parent 4b57007 commit 5a31cee

6 files changed

Lines changed: 57 additions & 10 deletions

File tree

‎.cargo/config.toml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[target.x86_64-pc-windows-gnu]
2+
linker = "x86_64-w64-mingw32-gcc"
3+
4+
[target.aarch64-apple-darwin]
5+
linker = ".cargo/zig-cc-aarch64-apple-darwin.sh"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
exec /opt/zig-linux-x86_64-0.14.0/zig cc -target aarch64-macos "$@"

‎Cargo.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/bin/ui.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ struct FormState {
208208
scan_batch_size:usize,
209209
google_ip_validation: bool,
210210
normalize_x_graphql: bool,
211+
skip_youtube_sni_rewrite: bool,
211212
}
212213

213214
#[derive(Clone, Debug)]
@@ -284,6 +285,7 @@ fn load_form() -> (FormState, Option<String>) {
284285
google_ip_validation: c.google_ip_validation,
285286
scan_batch_size:c.scan_batch_size,
286287
normalize_x_graphql: c.normalize_x_graphql,
288+
skip_youtube_sni_rewrite: c.skip_youtube_sni_rewrite,
287289
}
288290
} else {
289291
FormState {
@@ -308,6 +310,7 @@ fn load_form() -> (FormState, Option<String>) {
308310
google_ip_validation:true,
309311
scan_batch_size:500,
310312
normalize_x_graphql: false,
313+
skip_youtube_sni_rewrite: false,
311314
}
312315
};
313316
(form, load_err)
@@ -435,6 +438,7 @@ impl FormState {
435438
google_ip_validation:self.google_ip_validation,
436439
scan_batch_size:self.scan_batch_size,
437440
normalize_x_graphql: self.normalize_x_graphql,
441+
skip_youtube_sni_rewrite: self.skip_youtube_sni_rewrite,
438442
})
439443
}
440444
}
@@ -473,6 +477,8 @@ struct ConfigWire<'a> {
473477
sni_hosts: Option<Vec<&'a str>>,
474478
#[serde(skip_serializing_if = "is_false")]
475479
normalize_x_graphql: bool,
480+
#[serde(skip_serializing_if = "is_false")]
481+
skip_youtube_sni_rewrite: bool,
476482
// IP-scan knobs. These used to be missing from the wire struct, so
477483
// every Save-config silently dropped them — the user would toggle
478484
// "fetch from API" on, save, reopen, and find it off again. Add
@@ -524,6 +530,7 @@ impl<'a> From<&'a Config> for ConfigWire<'a> {
524530
.as_ref()
525531
.map(|v| v.iter().map(String::as_str).collect()),
526532
normalize_x_graphql: c.normalize_x_graphql,
533+
skip_youtube_sni_rewrite: c.skip_youtube_sni_rewrite,
527534
fetch_ips_from_api: c.fetch_ips_from_api,
528535
max_ips_to_scan: c.max_ips_to_scan,
529536
scan_batch_size: c.scan_batch_size,
@@ -831,6 +838,19 @@ impl eframe::App for App {
831838
Credit: seramo_ir + Persian Python community (issue #16).",
832839
);
833840
});
841+
ui.horizontal(|ui| {
842+
ui.add_space(120.0 + 8.0);
843+
ui.checkbox(
844+
&mut self.form.skip_youtube_sni_rewrite,
845+
"Send YouTube through relay (no SNI rewrite)",
846+
)
847+
.on_hover_text(
848+
"YouTube normally uses the same direct Google-edge tunnel as google.com (TLS SNI is \
849+
the front domain, not youtube.com). That can trigger restricted mode or sign-out \
850+
prompts. Enable this to route youtube.com / youtu.be / ytimg.com through the Apps \
851+
Script relay instead — slower for video, but the visible SNI matches the site.",
852+
);
853+
});
834854
});
835855
});
836856

‎src/config.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ pub struct Config {
105105
/// features. Turn on and observe.
106106
#[serde(default)]
107107
pub normalize_x_graphql: bool,
108+
/// When true, YouTube hostnames (`youtube.com`, `youtu.be`, …) are not
109+
/// routed through the direct SNI-rewrite tunnel; they use the Apps Script
110+
/// relay like other domains. Helps avoid YouTube restricted / logged-out
111+
/// treatment that can occur when TLS SNI does not match the real host.
112+
#[serde(default)]
113+
pub skip_youtube_sni_rewrite: bool,
108114
}
109115

110116
fn default_fetch_ips_from_api() -> bool { false }

‎src/proxy_server.rs‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,41 @@ use crate::mitm::MitmCertManager;
3030
// User-Agent locking and no Apps Script quota.
3131
// When in doubt leave it out: sites that aren't actually on GFE will 404 or
3232
// return a wrong-cert error instead of loading.
33-
const SNI_REWRITE_SUFFIXES: &[&str] = &[
34-
// Core Google
33+
const SNI_REWRITE_CORE_SUFFIXES: &[&str] = &[
3534
"google.com",
3635
"gstatic.com",
3736
"googleusercontent.com",
3837
"googleapis.com",
3938
"ggpht.com",
40-
// YouTube family
39+
"blogspot.com",
40+
"blogger.com",
41+
];
42+
43+
const SNI_REWRITE_YOUTUBE_SUFFIXES: &[&str] = &[
4144
"youtube.com",
4245
"youtu.be",
4346
"youtube-nocookie.com",
4447
"ytimg.com",
45-
// Blogger / Blog.google
46-
"blogspot.com",
47-
"blogger.com",
4848
];
4949

50-
fn matches_sni_rewrite(host: &str) -> bool {
50+
fn host_matches_sni_suffixes(host: &str, suffixes: &[&str]) -> bool {
5151
let h = host.to_ascii_lowercase();
5252
let h = h.trim_end_matches('.');
53-
SNI_REWRITE_SUFFIXES
53+
suffixes
5454
.iter()
5555
.any(|s| h == *s || h.ends_with(&format!(".{}", s)))
5656
}
5757

58+
fn matches_sni_rewrite(host: &str, skip_youtube_sni_rewrite: bool) -> bool {
59+
if host_matches_sni_suffixes(host, SNI_REWRITE_CORE_SUFFIXES) {
60+
return true;
61+
}
62+
if skip_youtube_sni_rewrite {
63+
return false;
64+
}
65+
host_matches_sni_suffixes(host, SNI_REWRITE_YOUTUBE_SUFFIXES)
66+
}
67+
5868
fn hosts_override<'a>(
5969
hosts: &'a std::collections::HashMap<String, String>,
6070
host: &str,
@@ -95,6 +105,7 @@ pub struct RewriteCtx {
95105
pub hosts: std::collections::HashMap<String, String>,
96106
pub tls_connector: TlsConnector,
97107
pub upstream_socks5: Option<String>,
108+
pub skip_youtube_sni_rewrite: bool,
98109
}
99110

100111
impl ProxyServer {
@@ -122,6 +133,7 @@ impl ProxyServer {
122133
hosts: config.hosts.clone(),
123134
tls_connector,
124135
upstream_socks5: config.upstream_socks5.clone(),
136+
skip_youtube_sni_rewrite: config.skip_youtube_sni_rewrite,
125137
});
126138

127139
let socks5_port = config.socks5_port.unwrap_or(config.listen_port + 1);
@@ -416,7 +428,9 @@ async fn dispatch_tunnel(
416428
rewrite_ctx: Arc<RewriteCtx>,
417429
) -> std::io::Result<()> {
418430
// 1. Explicit hosts override or SNI-rewrite suffix: always use the tunnel.
419-
if matches_sni_rewrite(&host) || hosts_override(&rewrite_ctx.hosts, &host).is_some() {
431+
if matches_sni_rewrite(&host, rewrite_ctx.skip_youtube_sni_rewrite)
432+
|| hosts_override(&rewrite_ctx.hosts, &host).is_some()
433+
{
420434
tracing::info!("dispatch {}:{} -> sni-rewrite tunnel (Google edge direct)", host, port);
421435
return do_sni_rewrite_tunnel_from_tcp(sock, &host, port, mitm, rewrite_ctx).await;
422436
}

0 commit comments

Comments
 (0)