Imgur fails to load with INWI

I run a PHP script every night to download funny greentexts from the r/4chan subreddit. I like reading them offline when I'm in bed. I don't remember since when I started noticing it, but some of the downloaded pictures would occasionally show signs of corruption. The image viewer would fail to read them. I didn't think much of it and wrote it off as the symptom of an aging SD card, which I've been using it since 2015.

It wasn't until someone from r/morocco brought up issues with imgur that I made the connection. Most of the pictures I download from r/4chan are served by i.redd.it, but some of them are served by i.imgur.com. That explained why I thought they were corrupted. The retrieved images were essentially empty. A quick curl -I imgur.com check returned 429 Unknown Error, which confirmed my suspicion. r/morocco redditors submitted a bunch of reports and it quickly became apparent that the problem is limited to the inwi ISP, affecting not only their standard 4G plan but also their win by inwi and ADSL offerings.

I gave it some time to see if the problem would sort itself out, but to no avail. To circumvent the issue, I decided to route all imgur.com traffic through my VPS because imgur doesn't block it. To do so, I run an SSH tunnel and use it as a socks proxy.

$ ssh -D 1234 hassan@redacted

I updated the PHP script to use this proxy by adding it as a CURLOPT_PROXY option :

function download(string $remote, string $local, bool $resume = true)
{
    $fout = fopen($local, $resume ? 'ab' : 'wb');
    $ch = curl_init($remote);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => false,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_RESUME_FROM => $resume ? filesize($local) : 0,
        CURLOPT_FILE => $fout,
        CURLOPT_PROXY => 'socks5://localhost:1234'
    ]);
    $result = curl_exec($ch);
    fclose($fout);

    if($result === false)
    {
        throw new Exception('cURL error: ' + curl_error($ch));
    }
}

To get Firefox to use it only for imgur, I set up a proxy auto-configuration file that filters outgoing requests by hostname :

function FindProxyForURL(url, host) {
    if(!host.includes("imgur.com")) {
        return null;
    }
    return "SOCKS5 localhost:1234";
}

I then pointed Firefox's configuration address to this file

Firefox proxy configuration

Aaaand it's working

I confirmed that it only uses the proxy for imgur.com by checking my IP on another website and making sure that it doesn't return the VPS' IP

Thanks to superuser user Marwane Nabil for the PAC trick.

The only downside is that I still have to run the ssh command manually. A small price to pay for quality shitposts.

Update : An alternative approach would be to use DuckDuckGo as an imgur proxy. No VPS needed !

Commentaires

Posts les plus consultés de ce blog

Writing a fast(er) youtube downloader

My experience with Win by Inwi

Porting a Golang and Rust CLI tool to D