From 18edc5c5cfc045d39a6b7561b427cb80199b0aca Mon Sep 17 00:00:00 2001 From: Marco 'don' Kaulea Date: Fri, 24 Jul 2015 21:45:00 +0200 Subject: [PATCH] Add --socks-proxy parameter to use socks proxy This allows the user to specify a socks proxy to tunnel the connection through. --- youtube_dl/YoutubeDL.py | 28 ++++++++++++++++++++++++++++ youtube_dl/__init__.py | 1 + youtube_dl/options.py | 4 ++++ 3 files changed, 33 insertions(+) diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 702a6ad50..2423c0519 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -23,6 +23,12 @@ import sys import time import traceback +# Try to import socks +try: + import socks +except ImportError: + pass + if os.name == 'nt': import ctypes @@ -1825,6 +1831,28 @@ class YoutubeDL(object): proxies['https'] = proxies['http'] proxy_handler = PerRequestProxyHandler(proxies) + if 'socks' in sys.modules: # Check if socks was imported + opts_socks = self.params.get('socksproxy') + + print("Socks {}".format(opts_socks)) + if opts_socks is not None and opts_socks: + pair = opts_socks.split(':') + if len(pair) == 2: + socks.setdefaultproxy( + socks.PROXY_TYPE_SOCKS5, + pair[0], + int(pair[1])) + else: + socks.setdefaultproxy( + socks.PROXY_TYPE_SOCKS5, + 'localhost', + int(pair[0])) + + socks.wrapmodule(compat_urllib_request) + else: # Socks was not imported, but the use tried to use it. Tell them + if self.params.get('socksproxy'): + self.to_stdout("Can't use socks proxy, socks module not found") + debuglevel = 1 if self.params.get('debug_printtraffic') else 0 https_handler = make_HTTPS_handler(self.params, debuglevel=debuglevel) ydlh = YoutubeDLHandler(self.params, debuglevel=debuglevel) diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 55b22c889..9ba92c31f 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -346,6 +346,7 @@ def _real_main(argv=None): 'nocheckcertificate': opts.no_check_certificate, 'prefer_insecure': opts.prefer_insecure, 'proxy': opts.proxy, + 'socksproxy': opts.socksproxy, 'socket_timeout': opts.socket_timeout, 'bidi_workaround': opts.bidi_workaround, 'debug_printtraffic': opts.debug_printtraffic, diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 9016e3498..42cf18ca2 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -181,6 +181,10 @@ def parseOpts(overrideArguments=None): '--proxy', dest='proxy', default=None, metavar='URL', help='Use the specified HTTP/HTTPS proxy. Pass in an empty string (--proxy "") for direct connection') + network.add_option( + '--socks-proxy', dest='socksproxy', default=None, metavar='URL', + help=('Use the specified socks proxy. Pass in an empty string ' + '(--socks-proxy "") for direct connection')) network.add_option( '--socket-timeout', dest='socket_timeout', type=float, default=None, metavar='SECONDS',