From ea55f2e11c162db8edeac1a39f861d5ad2381a60 Mon Sep 17 00:00:00 2001 From: martin scharm Date: Sun, 14 Jan 2018 00:30:22 +0100 Subject: [PATCH 1/3] Add proper support for proxies There are situations where you want tt-rss to use a proxy (e.g. because of network restrictions, or privacy concerns). tt-rss already comes with an undocumented `_CURL_HTTP_PROXY` variable (see eg https://binfalse.de/2015/05/06/ttrss-with-proxy/), however that won't have an effect when, for example, php-curl is not installed, see https://git.tt-rss.org/git/tt-rss/src/c30f5e18119d1935e8fe6d422053b127e8f4f1b3/include/functions.php#L377 In this case it would use the `file_get_contents` with a stream context without a proxy definition: https://git.tt-rss.org/git/tt-rss/src/c30f5e18119d1935e8fe6d422053b127e8f4f1b3/include/functions.php#L487 Here I propose to properly support proxies, and I introduced a `PROXY` variable, that is respected in both scenarios, with and without curl installed. --- config.php-dist | 5 +++++ include/functions.php | 35 +++++++++++++++++------------------ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/config.php-dist b/config.php-dist index 7a076d976..f9cc181af 100644 --- a/config.php-dist +++ b/config.php-dist @@ -196,4 +196,9 @@ // Expected config version. Please update this option in config.php // if necessary (after migrating all new options from this file). + define('PROXY', ''); + // Connect to RSS feeds through a PROXY, this way tt-rss won't connect to + // webservers directly. + // Example format: '127.0.0.1:8123' (polipo proxy running on localhost) + // vim:ft=php diff --git a/include/functions.php b/include/functions.php index 649b77881..f58a2fc68 100644 --- a/include/functions.php +++ b/include/functions.php @@ -402,8 +402,8 @@ curl_setopt($ch, CURLOPT_COOKIEJAR, "/dev/null"); } - if (defined('_CURL_HTTP_PROXY')) { - curl_setopt($ch, CURLOPT_PROXY, _CURL_HTTP_PROXY); + if (defined('PROXY')) { + curl_setopt($ch, CURLOPT_PROXY, PROXY); } if ($post_query) { @@ -483,25 +483,24 @@ // TODO: should this support POST requests or not? idk + $context_options = array( + 'http' => array( + 'method' => 'GET', + 'ignore_errors' => true, + 'timeout' => $timeout ? $timeout : FILE_FETCH_TIMEOUT, + 'protocol_version'=> 1.1) + ); + if (!$post_query && $last_modified) { - $context = stream_context_create(array( - 'http' => array( - 'method' => 'GET', - 'ignore_errors' => true, - 'timeout' => $timeout ? $timeout : FILE_FETCH_TIMEOUT, - 'protocol_version'=> 1.1, - 'header' => "If-Modified-Since: $last_modified\r\n") - )); - } else { - $context = stream_context_create(array( - 'http' => array( - 'method' => 'GET', - 'ignore_errors' => true, - 'timeout' => $timeout ? $timeout : FILE_FETCH_TIMEOUT, - 'protocol_version'=> 1.1 - ))); + $context_options['http']['header'] = "If-Modified-Since: $last_modified\r\n"; } + if (defined('PROXY')) { + $context_options['http']['proxy'] = PROXY; + } + + $context = stream_context_create($context_options); + $old_error = error_get_last(); $data = @file_get_contents($url, false, $context); From 213c01d459455d0c9062e228e5bc581c92fcda69 Mon Sep 17 00:00:00 2001 From: martin scharm Date: Wed, 17 Jan 2018 12:28:47 +0100 Subject: [PATCH 2/3] some proxies require `request_fulluri` set to true [see #36] at least polipo won't work for plain HTTP URLs (HTTPS strangely also works without `request_fulluri`..?) see https://git.tt-rss.org/git/tt-rss/pulls/36 --- include/functions.php | 1 + 1 file changed, 1 insertion(+) diff --git a/include/functions.php b/include/functions.php index f58a2fc68..2c6b51337 100644 --- a/include/functions.php +++ b/include/functions.php @@ -496,6 +496,7 @@ } if (defined('PROXY')) { + $context_options['http']['request_fulluri'] = true; $context_options['http']['proxy'] = PROXY; } From 32dc9ec854f9c317266584ecb33127f469d4bd80 Mon Sep 17 00:00:00 2001 From: martin scharm Date: Thu, 18 Jan 2018 08:48:53 +0100 Subject: [PATCH 3/3] undocumenting the proxy settings [see #36] in response to https://git.tt-rss.org/git/tt-rss/pulls/36#issuecomment-119 --- config.php-dist | 5 ----- include/functions.php | 8 ++++---- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/config.php-dist b/config.php-dist index f9cc181af..7a076d976 100644 --- a/config.php-dist +++ b/config.php-dist @@ -196,9 +196,4 @@ // Expected config version. Please update this option in config.php // if necessary (after migrating all new options from this file). - define('PROXY', ''); - // Connect to RSS feeds through a PROXY, this way tt-rss won't connect to - // webservers directly. - // Example format: '127.0.0.1:8123' (polipo proxy running on localhost) - // vim:ft=php diff --git a/include/functions.php b/include/functions.php index 2c6b51337..dfa78a8ac 100644 --- a/include/functions.php +++ b/include/functions.php @@ -402,8 +402,8 @@ curl_setopt($ch, CURLOPT_COOKIEJAR, "/dev/null"); } - if (defined('PROXY')) { - curl_setopt($ch, CURLOPT_PROXY, PROXY); + if (defined('_HTTP_PROXY')) { + curl_setopt($ch, CURLOPT_PROXY, _HTTP_PROXY); } if ($post_query) { @@ -495,9 +495,9 @@ $context_options['http']['header'] = "If-Modified-Since: $last_modified\r\n"; } - if (defined('PROXY')) { + if (defined('_HTTP_PROXY')) { $context_options['http']['request_fulluri'] = true; - $context_options['http']['proxy'] = PROXY; + $context_options['http']['proxy'] = _HTTP_PROXY; } $context = stream_context_create($context_options);