From fc46e3d6e0172e3ee7f4358c0bbe0b5720238ee9 Mon Sep 17 00:00:00 2001 From: Abolfazl Date: Thu, 23 Apr 2026 23:02:04 +0330 Subject: [PATCH] Fixed the youtube safesearch problems. --- README.md | 1 + README_FA.md | 3 ++- config.example.json | 1 + src/proxy_server.py | 20 ++++++++++++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ba1e6a2..364b2c2 100644 --- a/README.md +++ b/README.md @@ -289,6 +289,7 @@ This project focuses entirely on the **Apps Script** relay — a free Google acc | `bypass_hosts` | `["localhost", ".local", ".lan", ".home.arpa"]` | Hosts that go direct (no MITM, no relay). Useful for LAN resources or sites that break under MITM. | | `direct_google_exclude` | see [config.example.json](config.example.json) | Google apps that must use the MITM relay path instead of the fast direct tunnel. | | `hosts` | `{}` | Manual DNS override: map a hostname to a specific IP. | +| `youtube_via_relay` | `false` | Route YouTube (`youtube.com`, `youtu.be`, `youtube-nocookie.com`) through the Apps Script relay instead of the SNI-rewrite path. The SNI-rewrite path uses Google's frontend IP which enforces SafeSearch and can cause **"Video Unavailable"** errors. Setting this to `true` fixes playback at the cost of using more Apps Script executions and slightly higher latency. | ### Optional Dependencies diff --git a/README_FA.md b/README_FA.md index 246a1c4..accb3f4 100644 --- a/README_FA.md +++ b/README_FA.md @@ -24,7 +24,7 @@ `masterking32.ton` -- آدرس روی شبکه‌های EVM (ETH و سازگارها): +- آدرس روی شبکه‌های EVM (ETH و سازگارها): `0x517f07305D6ED781A089322B6cD93d1461bF8652` @@ -237,6 +237,7 @@ json | `block_hosts` | `[]` | هاست‌هایی که هرگز نباید tunnel شوند (پاسخ 403). نام دقیق (`ads.example.com`) یا پسوند با نقطه‌ی ابتدایی (`.doubleclick.net`). | | `bypass_hosts` | `["localhost", ".local", ".lan", ".home.arpa"]` | هاست‌هایی که مستقیم می‌روند (بدون MITM و بدون رله). برای منابع داخلی شبکه یا سایت‌هایی که با MITM مشکل دارند. | | `direct_google_exclude` | مراجعه به [config.example.json](config.example.json) | اپ‌های Google که باید از مسیر MITM برای رله استفاده کنند به‌جای tunnel مستقیم. | +| `youtube_via_relay` | `false` | مسیردهی YouTube (`youtube.com`، `youtu.be`، `youtube-nocookie.com`) از طریق رله Apps Script به‌جای مسیر SNI-rewrite. مسیر SNI-rewrite از IP فرانت‌اند Google عبور می‌کند که SafeSearch را اجباری می‌کند و می‌تواند باعث خطای **«ویدیو در دسترس نیست»** شود. با فعال کردن این گزینه، پخش ویدیو درست می‌شود اما تعداد اجراهای Apps Script بیشتر و تأخیر اندکی بالاتر می‌رود. | ### وابستگی‌های اختیاری diff --git a/config.example.json b/config.example.json index 7c709ba..371343e 100644 --- a/config.example.json +++ b/config.example.json @@ -42,5 +42,6 @@ "www.google.com", "safebrowsing.google.com" ], + "youtube_via_relay": false, "hosts": {} } diff --git a/src/proxy_server.py b/src/proxy_server.py index 6511c0b..0249298 100644 --- a/src/proxy_server.py +++ b/src/proxy_server.py @@ -201,6 +201,20 @@ class ProxyServer: self._block_hosts = self._load_host_rules(config.get("block_hosts", [])) self._bypass_hosts = self._load_host_rules(config.get("bypass_hosts", [])) + # youtube_via_relay: route YouTube through Apps Script relay instead of + # the SNI-rewrite path. The SNI-rewrite path shares Google's frontend + # IP which enforces SafeSearch and can cause "Video Unavailable". + # Enabling this fixes YouTube playback at the cost of using more + # Apps Script executions and slightly higher latency. + if config.get("youtube_via_relay", False): + self._SNI_REWRITE_SUFFIXES = tuple( + s for s in SNI_REWRITE_SUFFIXES + if s not in self._YOUTUBE_SNI_SUFFIXES + ) + log.info("youtube_via_relay enabled — YouTube routed through relay") + else: + self._SNI_REWRITE_SUFFIXES = SNI_REWRITE_SUFFIXES + try: from mitm import MITMCertManager self.mitm = MITMCertManager() @@ -624,6 +638,12 @@ class ProxyServer: # Built-in list of domains that must be reached via Google's frontend IP # with SNI rewritten to `front_domain` (default: www.google.com). # Source: constants.SNI_REWRITE_SUFFIXES. + # When youtube_via_relay is enabled the YouTube suffixes are removed so + # YouTube goes through the Apps Script relay instead (avoids SafeSearch + # forced by the Google frontend IP, at the cost of extra relay executions). + _YOUTUBE_SNI_SUFFIXES = frozenset({ + "youtube.com", "youtu.be", "youtube-nocookie.com", + }) _SNI_REWRITE_SUFFIXES = SNI_REWRITE_SUFFIXES def _sni_rewrite_ip(self, host: str) -> str | None: