Skip to content

Commit 462dbf8

Browse files
committed
Overhaul the usage of libcurl-impersonate
libcurl-impersonate was not being used properly, as the code was overriding the headers set by it to prevent detection. - update the libcurl-impersonate to an actively managed lexiforest fork - impersonate Chrome 131 - move the defaultHttpHeaders to http.php, where it belongs - only set defaultHttpHeaders if curl-impersonate is not detected - make useragent ini setting optional and disabled by default - add necessary documentation updates
1 parent 0d20a8c commit 462dbf8

File tree

6 files changed

+46
-30
lines changed

6 files changed

+46
-30
lines changed

Dockerfile

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,39 @@ RUN set -xe && \
2525
# php-zlib is enabled by default with PHP 8.2 in Debian 12
2626
# for downloading libcurl-impersonate
2727
curl \
28+
# for patching libcurl-impersonate
29+
patchelf \
2830
&& \
2931
# install curl-impersonate library
30-
curlimpersonate_version=0.6.0 && \
32+
curlimpersonate_version=1.0.0rc2 && \
3133
{ \
3234
{ \
3335
[ $(arch) = 'aarch64' ] && \
3436
archive="libcurl-impersonate-v${curlimpersonate_version}.aarch64-linux-gnu.tar.gz" && \
35-
sha512sum="d04b1eabe71f3af06aa1ce99b39a49c5e1d33b636acedcd9fad163bc58156af5c3eb3f75aa706f335515791f7b9c7a6c40ffdfa47430796483ecef929abd905d" \
37+
sha512sum="c8add80e7a0430a074edea1a11f73d03044c48e848e164af2d6f362866623e29bede207a50f18f95b1bc5ab3d33f5c31408be60a6da66b74a0d176eebe299116" \
3638
; } \
3739
|| { \
3840
[ $(arch) = 'armv7l' ] && \
3941
archive="libcurl-impersonate-v${curlimpersonate_version}.arm-linux-gnueabihf.tar.gz" && \
40-
sha512sum="05906b4efa1a6ed8f3b716fd83d476b6eea6bfc68e3dbc5212d65a2962dcaa7bd1f938c9096a7535252b11d1d08fb93adccc633585ff8cb8cec5e58bfe969bc9" \
42+
sha512sum="d0403ca4ad55a8d499b120e5675c7b5a0dc4946af49c933e91fc24455ffe5e122aa21ee95554612ff5d1bd6faea1556e1e1b9c821918e2644cc9bcbddc05747a" \
4143
; } \
4244
|| { \
4345
[ $(arch) = 'x86_64' ] && \
4446
archive="libcurl-impersonate-v${curlimpersonate_version}.x86_64-linux-gnu.tar.gz" && \
45-
sha512sum="480bbe9452cd9aff2c0daaaf91f1057b3a96385f79011628a9237223757a9b0d090c59cb5982dc54ea0d07191657299ea91ca170a25ced3d7d410fcdff130ace" \
47+
sha512sum="35cafda2b96df3218a6d8545e0947a899837ede51c90f7ef2980bd2d99dbd67199bc620000df28b186727300b8c7046d506807fb48ee0fbc068dc8ae01986339" \
4648
; } \
4749
} && \
48-
curl -LO "https://github.com/lwthiker/curl-impersonate/releases/download/v${curlimpersonate_version}/${archive}" && \
50+
curl -LO "https://github.com/lexiforest/curl-impersonate/releases/download/v${curlimpersonate_version}/${archive}" && \
4951
echo "$sha512sum $archive" | sha512sum -c - && \
5052
mkdir -p /usr/local/lib/curl-impersonate && \
51-
tar xaf "$archive" -C /usr/local/lib/curl-impersonate --wildcards 'libcurl-impersonate-ff.so*' && \
53+
tar xaf "$archive" -C /usr/local/lib/curl-impersonate && \
54+
patchelf --set-soname libcurl.so.4 /usr/local/lib/curl-impersonate/libcurl-impersonate.so && \
5255
rm "$archive" && \
53-
apt-get purge --assume-yes curl && \
56+
apt-get purge --assume-yes curl patchelf && \
5457
rm -rf /var/lib/apt/lists/*
5558

56-
ENV LD_PRELOAD /usr/local/lib/curl-impersonate/libcurl-impersonate-ff.so
57-
ENV CURL_IMPERSONATE ff91esr
59+
ENV LD_PRELOAD /usr/local/lib/curl-impersonate/libcurl-impersonate.so
60+
ENV CURL_IMPERSONATE chrome131
5861

5962
# logs should go to stdout / stderr
6063
RUN ln -sfT /dev/stderr /var/log/nginx/error.log; \

config.default.ini.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@
5353
retries = 1
5454

5555
; Curl user agent
56-
useragent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0"
56+
; This is already set by curl-impersonate, which comes included as default
57+
; in RSS-Bridge docker container. Use only if you know what you're doing.
58+
; For reference, see https://github.com/lexiforest/curl-impersonate/tree/main/docs
59+
;useragent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0"
5760

5861
; Max http response size in MB
5962
max_filesize = 20

docs/03_For_Hosts/08_Custom_Configuration.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ Default network timeout.
119119

120120
### useragent
121121

122-
Default user agent.
122+
Overrides the user agent value. Note that the default value, together with a set of other detection-preventing options is set
123+
automatically by the [libcurl-impersonate](https://github.com/lexiforest/curl-impersonate), which is used by the default Docker container distributed together with RSS-Bridge. Use only if you know what you're doing, otherwise you may stop libcurl-impersonate
124+
from doing its job impersonating real browser.
123125

124126
## Authentication
125127

docs/09_Technical_recommendations/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Test a site before building a bridge
44

55
Some sites make use of anti-bot mechanisms (e.g.: by using JavaScript) in which case they work fine in regular browsers,
6-
but not in the PHP environment.
6+
but not in the PHP environment. RSS-Bridge Docker container by default resorts to using libcurl-impersonate, which helps mitigating anti-bot mechanisms.
77

88
To check if a site works with RSS-Bridge, create a new bridge using the
99
[template](../05_Bridge_API/02_BridgeAbstract.md#template)

lib/contents.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,7 @@ function getContents(
6464
}
6565
}
6666

67-
// Snagged from https://github.com/lwthiker/curl-impersonate/blob/main/firefox/curl_ff102
68-
$defaultHttpHeaders = [
69-
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
70-
'Accept-Language' => 'en-US,en;q=0.5',
71-
'Upgrade-Insecure-Requests' => '1',
72-
'Sec-Fetch-Dest' => 'document',
73-
'Sec-Fetch-Mode' => 'navigate',
74-
'Sec-Fetch-Site' => 'none',
75-
'Sec-Fetch-User' => '?1',
76-
'TE' => 'trailers',
77-
];
78-
79-
$config['headers'] = array_merge($defaultHttpHeaders, $httpHeadersNormalized);
67+
$config['headers'] = $httpHeadersNormalized;
8068

8169
$maxFileSize = Configuration::getConfig('http', 'max_filesize');
8270
if ($maxFileSize) {

lib/http.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ final class CurlHttpClient implements HttpClient
6666
{
6767
public function request(string $url, array $config = []): Response
6868
{
69+
$ch = curl_init($url);
70+
6971
$defaults = [
7072
'useragent' => null,
7173
'timeout' => 5,
@@ -77,18 +79,36 @@ public function request(string $url, array $config = []): Response
7779
'max_filesize' => null,
7880
'max_redirections' => 5,
7981
];
82+
83+
// if curl-impersonate is not detected, use some basic defaults
84+
if (curl_version()['ssl_version'] != 'BoringSSL') {
85+
// Snagged from https://github.com/lwthiker/curl-impersonate/blob/main/firefox/curl_ff102
86+
$defaults['headers'] = [
87+
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
88+
'Accept-Language' => 'en-US,en;q=0.5',
89+
'Upgrade-Insecure-Requests' => '1',
90+
'Sec-Fetch-Dest' => 'document',
91+
'Sec-Fetch-Mode' => 'navigate',
92+
'Sec-Fetch-Site' => 'none',
93+
'Sec-Fetch-User' => '?1',
94+
'TE' => 'trailers',
95+
];
96+
$defaults['useragent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0';
97+
98+
curl_setopt($ch, CURLOPT_HEADER, false);
99+
}
100+
80101
$config = array_merge($defaults, $config);
81102

82-
$ch = curl_init($url);
83-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
84-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
85-
curl_setopt($ch, CURLOPT_MAXREDIRS, $config['max_redirections']);
86-
curl_setopt($ch, CURLOPT_HEADER, false);
87103
$httpHeaders = [];
88104
foreach ($config['headers'] as $name => $value) {
89105
$httpHeaders[] = sprintf('%s: %s', $name, $value);
90106
}
91107
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
108+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
109+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
110+
curl_setopt($ch, CURLOPT_MAXREDIRS, $config['max_redirections']);
111+
92112
if ($config['useragent']) {
93113
curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']);
94114
}

0 commit comments

Comments
 (0)