From 94bd49aa379cf561b066f9f9b2a71de276ef5e3f Mon Sep 17 00:00:00 2001 From: Arun Prakash Jana Date: Fri, 18 Nov 2016 22:06:09 +0530 Subject: [PATCH] Username and password support in proxy auth. --- README.md | 4 +++- buku.1 | 2 +- buku.py | 21 +++++++++++++++++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7409703..72058cd 100644 --- a/README.md +++ b/README.md @@ -239,7 +239,9 @@ Shell completion scripts for Bash, Fish and Zsh can be found in respective subdi - --stag : search bookmarks by a tag, or show all tags alphabetically (if no arguments). - Search results are indexed serially. This index is different from actual database index of a bookmark record which is shown in bold within `[]` after the URL. - **Encryption** is optional and manual. AES256 algorithm is used. To use encryption, the database file should be unlocked (-k) before using buku and locked (-l) afterwards. Between these 2 operations, the database file lies unencrypted on the disk, and NOT in memory. Also, note that the database file is *unencrypted on creation*. -- **Proxy** support: environment variable *https_proxy*, if defined, is used to tunnel data for both http and https connections. The supported format is `http[s]://proxyhost:proxyport/`. +- **Proxy** support: environment variable *https_proxy*, if defined, is used to tunnel data for both http and https connections. The supported format is: + + http[s]://[username:password@]proxyhost:proxyport/ ## GUI integration diff --git a/buku.1 b/buku.1 index 099e01c..0fa601e 100644 --- a/buku.1 +++ b/buku.1 @@ -228,7 +228,7 @@ Overrides the default browser. Ref: .BI https_proxy If defined, will be used to access http and https resources through the configured proxy. Supported format: - http[s]://proxyhost:proxyport/ + http[s]://[username:password@]proxyhost:proxyport/ .SH EXAMPLES .PP diff --git a/buku.py b/buku.py index 8d7e39e..b2db5e0 100755 --- a/buku.py +++ b/buku.py @@ -1498,11 +1498,20 @@ def get_PoolManager(): :return: ProxyManager if https_proxy is defined, else PoolManager. ''' - https_proxy = os.environ.get('https_proxy') - print(https_proxy) + proxy = os.environ.get('https_proxy') - if https_proxy: - return urllib3.ProxyManager(https_proxy) + if proxy: + headers = None + url = urlparse(proxy) + # Strip username and password and create header, if present + if url.username: + proxy = proxy.replace(url.username + ':' + url.password + '@', '') + headers = urllib3.util.make_headers( + basic_auth=url.username + ':' + url.password + ) + + logger.debug('proxy: [%s]' % proxy) + return urllib3.ProxyManager(proxy, headers=headers) return urllib3.PoolManager() @@ -1914,6 +1923,10 @@ def open_in_browser(url): url = url.replace('%22', '\"') if not urlparse(url).scheme: + # Prefix with 'http://' is no scheme + # Otherwise, opening in browser fails anyway + # We expect http to https redirection + # will happen for https-only websites logger.error('scheme missing in URI, trying http') url = '%s%s' % ('http://', url)