Skip to content

feature: addition of a Cloudflare bypass for the anti-bot - #577

Open
yeshua-aguilar wants to merge 44 commits into
tgbot-collection:masterfrom
yeshua-aguilar:master
Open

feature: addition of a Cloudflare bypass for the anti-bot#577
yeshua-aguilar wants to merge 44 commits into
tgbot-collection:masterfrom
yeshua-aguilar:master

Conversation

@yeshua-aguilar

Copy link
Copy Markdown

Summary

Adds automatic Cloudflare bypass support to avoid anti-bot detection when downloading videos from protected websites.

Problem

Many websites use Cloudflare protection which blocks bot requests, even for legitimate video downloads. This causes failures when users try to download content from these sites.

Solution
Implemented a new HTTP client module using cloudscraper library that:

  • Automatically bypasses Cloudflare challenges
  • Uses realistic browser headers (Chrome/Windows)
  • Falls back to normal requests if bypass fails
  • Configurable via BYPASS_CLOUDFLARE env variable
    Files Changed
File Change
pyproject.toml Added cloudscraper>=1.2.71
requirements.txt Added cloudscraper>=1.2.71
src/utils/http_client.py NEW - HTTP client with bypass
src/config/config.py Added BYPASS_CLOUDFLARE setting
src/engine/direct.py Uses new HTTP client
src/engine/generic.py Added realistic headers for yt-dlp
src/engine/instagram.py Uses new HTTP client
.env.example Documented new setting

Testing

Testing: NowSecure (Cloudflare protected)
URL: https://nowsecure.nl/
Status: 200
Content length: 58817 chars
Result: OK

Testing: Google (no protection)
URL: https://www.google.com/
Status: 200
Content length: 46302 chars
Result: OK

Configuration

.env
BYPASS_CLOUDFLARE=True # Default: True

Dependencies

  • cloudscraper>=1.2.71 — Automatic Cloudflare bypass

Comment thread src/engine/instagram.py

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use personal API for this. So no need for additional workarounds here.

Comment thread src/engine/generic.py

@SanujaNS SanujaNS Feb 22, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yt-dlp handles this part on own, specially for YT. Why do we need to manually inject headers here?
I need your opinion.

Comment thread src/engine/direct.py

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about aria2 part?
Do you have any suggestions?

@SanujaNS

Copy link
Copy Markdown
Collaborator

Hi @yeshua-aguilar ,

Thank you for your interest in the project and for submitting this PR!

I left some comments on the code, but I had a few general questions:

  • Version: Why go with the older cloudscraper? The same dev seems to have a newer version here.

  • Alternatives: Any thoughts on using ai-cloudscraper instead?

  • Context: Was there a specific issue you ran into that prompted adding this to ytdlbot?

  • Implementation: While yt-dlp likely doesn’t need this, our direct download part could definitely benefit. Why not implement this fully for that section, or do you have other suggestions?

Appreciate the help!

@yeshua-aguilar

Copy link
Copy Markdown
Author

Hi @SanujaNS , thanks for the feedback! Let me address each point:

Version:
You're right! I wasn't aware of ai-cloudscraper when I created this PR. The original cloudscraper (1.2.71) hasn't been updated since April 2023, while ai-cloudscraper is actively maintained

I'll switch to ai-cloudscraper for this PR.
Alternatives
ai-cloudscraper looks like the best option. It's free (MIT license), open source, and a drop-in replacement - same import syntax works. No concerns from my side.

Context
I encountered Cloudflare 403 errors when trying to download videos from several sites. The bot was blocked even for legitimate download requests. This happens especially with sites that have aggressive anti-bot settings enabled.

Implementation

Great point, You're right that:

  • yt-dlp already handles Cloudflare well with curl-cffi - no bypass needed in generic.py
  • direct download (direct.py) uses requests directly and would benefit from this
    I'll update the PR to:
  1. Switch to ai-cloudscraper
  2. Remove bypass from generic.py (yt-dlp doesn't need it)
  3. Keep it only in direct.py
  4. Remove from instagram.py unless you think it's needed there
    I'll make these changes soon. Thanks for the guidance!

@SanujaNS

Copy link
Copy Markdown
Collaborator

Hi @yeshua-aguilar ,

Sorry about the delayed response and thank you for the update.

I will go through the changes and get back to you.

@SanujaNS
SanujaNS requested a review from BennyThink March 2, 2026 14:10
yeshua-aguilar and others added 14 commits June 6, 2026 13:08
- Add `silent_mode` column to `Setting` model and `get_silent_mode()` function
  to allow users to suppress progress updates.
- Update `edit_text()` method to skip sending progress when silent mode is active.
- Add `_reencode_videos()` method using `ensure_streamable_video` to convert
  non-streamable videos to mp4/h264 for Telegram compatibility.
- Show download percentage in progress display.
feat: add silent mode and video re-encoding features
…e reference

- Replace pathlib imports with Path in helper.py, instagram.py, pixeldrain.py; remove unused import in direct.py
- Fix krakenfiles.py: use BeautifulSoup.select_one instead of non-existent xpath method
- Fix main.py: use BotText.private attribute instead of bare string
- Add BotText.private constant in config/constant.py
fix: correct pathlib imports, krakenfiles parsing, and BotText.privat…
…liability

- In base.py, skip thumbnails/images when selecting video file in get_metadata.
- In generic.py, fix format selection for non-YouTube URLs and add format_sort option.
- In helper.py, switch to ffmpeg-python wrapper for better error handling, timeout, and output validation.
- In main.py, remove unused queue counter and position tracking; simplify queue position update.
fix: improve video file detection and refactor ffmpeg encoding for re…
…upport

- Configure Cloudflare DNS (1.1.1.1, 1.0.0.1) in docker-compose.yml
- Update pdm.lock to include ai-cloudscraper and other dependency additions
- Enhance network reliability and bypass capabilities
feat: add custom DNS and update dependencies with Cloudflare bypass s…
- Modified .gitignore to standardize Python project exclusions including virtual environments, test caches, build artifacts, OS files, logs, and editor files
- Updated src/engine/helper.py to reduce maximum video resolution from 1080p to 720p in ensure_streamable_video function for re-encoded videos
- Kept existing test file src/test_convert_webm.py which validates webm to mp4 conversion workflow

The changes standardize repository ignore patterns and optimize video processing by capping output resolution at 720p instead of 1080p, while maintaining existing test coverage for the conversion process.
…n-conversion-417ad

Optimizing video resolution conversion 417ad
qwen-intl and others added 21 commits June 24, 2026 00:13
- Updated README.md to document new features including automatic video conversion to 720p, smart scaling logic, and enhanced FFmpeg integration
- Modified .gitignore to streamline ignored files and directories
- Enhanced video processing in src/engine/helper.py with improved `ensure_streamable_video` function using smarter scaling logic to resize videos to max 720p while preserving aspect ratio
- Added comprehensive test suite in src/test_convert_webm.py to validate the new video conversion and scaling logic with synthetic webm inputs
…loader-8b357

Update from task 4afd7062-7535-4bbc-9b0d-ebea3b38b357
Key features implemented:
- Modified src/engine/base.py to always call _reencode_videos during upload, ensuring all videos are processed even from cache
- Updated src/engine/helper.py's ensure_streamable_video to re-encode videos exceeding 720p resolution, regardless of being MP4/H.264 already
- Refactored README.md to highlight automatic video conversion and smart scaling features
- Adjusted .gitignore for cleaner file tracking
- Enhanced test suite in src/test_convert_webm.py to validate 720p scaling logic

The changes enforce consistent video output at 720p maximum resolution for optimal Telegram playback, improving user experience by standardizing video quality across different source formats.
…loader-8b357

Update from task 4afd7062-7535-4bbc-9b0d-ebea3b38b357
…loader-8b357

Update from task 4afd7062-7535-4bbc-9b0d-ebea3b38b357
…loader-8b357

Update from task 4afd7062-7535-4bbc-9b0d-ebea3b38b357
fix: solucion de un bug de algunos archivos no se llega a enviar a te…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants