How to use yt-dlp with Tor

Introduction to Tor’s socks5

SOCKS is a network protocol that is used to route packets in a client-server architecture. It hides your IP address but doesn’t encrypt the traffic.

SOCKS5 is the latest version of this protocol. It supports both UDP and TCP. Tor runs a SOCKS5 proxy by default. To find out, open Tor and visit:

http://localhost:9150
socks proxy in tor
SOCKS proxy in Tor

The Tor’s SOCKS5 proxy can be used in an application.

How to route yt-dlp network traffic through Tor

According to yt-dlp’s documentation the –proxy option helps to route the traffic through a proxy. To use Tor as a proxy with yt-dlp type:

yt-dlp --verbose --proxy socks5:\\localhost:9150 video_url_here 

The socks5 informs yt-dlp about the protocol, it’s not a http proxy.

The main difference between a SOCKS5 and an HTTP proxy is that the latter supports only HTTP traffic.

The DNS leak

Local DNS resolution takes place when you visit a website. To query the DNS server of your ISP is a bad idea especially when you try to route the traffic over Tor.

This issue on github is proof of a DNS leak. Although a bad combination of options, torproject.org also advises about this type of leak.

torrc is a configuration text file that instructs Tor how to behave. It’s location on Windows or Linux is in the Browser/TorBrowser/Data/Tor inside your Tor Browser directory.

To test if you are having a DNS leak while using Tor as a proxy, add the following to the torrc configuration file.

TestSocks 1

The above configuration parameter helps with the logs of Tor.

To launch yt-dlp type:

yt-dlp --verbose --proxy socks5:\\localhost:9150 video_url_here

Go to the Tor browser, settings, Connection, and View Logs as the download takes place. If a message similar to the one shown below is present, then your setup is good.

2024-03-01 02:11:27.262 [NOTICE] Your application (using socks5 to port 443) instructed Tor to take care of the DNS resolution itself if necessary. This is good.

The above log indicates that Tor is doing the DNS resolution. To disable all connections leaking DNS add the following to the torrc file.

SafeSocks 1

Then close and reopen the Tor browser.

Wireshark helps to investigate DNS leaks.

how to use yt-dlp with tor
Local DNS resolution through my router 192.168.0.1, yt-dlp downloading without Tor


how to use yt-dlp with tor
Wireshark monitoring local traffic, yt-dlp downloading through Tor


Final thoughts

A SOCKS5 proxy is handy to hide your real IP address. It also helps to bypass regional restrictions.

Tor offers a free solution. yt-dlp easily integrates Tor’s SOCKS5 through the –proxy option.

Leave comment

Your email address will not be published. Required fields are marked with *.