Free literature or a tale about friendship CoolReader c Tor

A neighbor lives in my house: a very good woman, a pensioner, a labor veteran, a former medical worker who worked as a nurse in a hospital all her life. Currently retired. I help her as much as I can, but the tale is not about a neighbor, but about what the hospital management gave her, where she used to work on a tablet. Everything would be nothing in this tablet (there is WIFI, there is no 3G, the diagonal of the screen is 7 inches, on aliexpress there are such for $ 45 with delivery), but naturally they did not explain what to do with this tablet. And I had to answer the question, what am I doing with my tablet, and I read books on the tablet, mainly Yasinsky and Elterrus.
Naturally, I connected the tablet to my laptop and transferred the CoolReader apk, as well as my library. Unfortunately, two days later it turned out that our literary predilections were different and my books were not interesting to my neighbor. I had to share the password from my Wi-Fi and set up the Flibusta opds, as the neighbor knew very well what the Internet was and she never had a computer or an Internet connection.
After the weekend, I was fascinated by the fact that the necessary books from Flibusta are not downloaded. To say that I was at a loss - to say nothing - all the literature of interest to me was always in the public domain, but the neighbor wanted Daria Dontsova and “50 shades of gray” and, to my surprise, they really didn’t download.
Googling the network, I found out that on the TOR network, good people made a Mirror of Flibusta from which everything downloads fine, but all attempts to explain to CoolReader that you need to go to the .onion address stumbled upon his persistent unwillingness to connect to .onion addresses in principle. Which, in principle, is logical. I successfully failed to explain the principles of modern copyright and buying books at Liters: my neighbor doesn’t have a card, like a computer, but I can resist the naive “why pay if I don’t have any books?” And “ where is the store where you can come and get a book on your tablet? ”turned out to be beyond my modest strength, so I had to solve this problem using technical methods, because I don’t know how to say“ no, ”especially to those who need help and protection.
Fortunately, I already had VRS (virtual root server), which was running my little project on managing Ubuntu and Apache.
Step 1 . Install on Ubuntu tor
apt-get install tor
The default settings suited me perfectly.
Step 2 . We write a script for Apache, naming it, for example: /home/user/flibusta/nph-flibusta.pl. The nph- prefix in the file name is very important, because it affects how the script will be processed by Apache.
#!/usr/bin/perl
use warnings;
use HTTP::Proxy;
use LWP::UserAgent;
use LWP::Protocol::socks;
use URI::Escape;
use CGI qw(:standard -nph);
my $q = new CGI;
my $ua = LWP::UserAgent->new(max_redirect => 0, agent => $q->user_agent(),);
$ua->timeout(60);
$ua->proxy([qw/ http https /] => 'socks://127.0.0.1:9050'); # Tor proxy
my $refer = $q->referer();
$refer = "" unless defined($refer);
$refer =~ s/proxy\.flibusta\.net/flibustahezeous3.onion/g;
#handle extra parameters
my $ep ="";
foreach my $par ($q->param) {$ep .= $par.'='.uri_escape($q->param($par)).'&' unless ($par eq 'url');}
chop $ep;
$ep = '?'.$ep if ($ep ne '');
my $rsp = $ua->get('http://flibustahezeous3.onion'.$q->param('url').$ep, 'Referer'=>$refer, 'Accept'=>$q->http('Accept'),
'Accept-Language'=>$q->http('Accept-Language'),
'Accept-Encoding'=>'identity',
'Cookie'=>$q->http('Cookie') );
my $fstr = $rsp->as_string;
$fstr =~ s/flibustahezeous3\.onion/proxy.flibusta.net/g;
#define templates
my $nbl = <
DATA1
my $olb = quotemeta <
DATA2
#get all book IDs
my @IDs = $fstr =~ /\/b\/(\d+)\/download/g;
foreach my $id (@IDs)
{
# prepare search and replace strings
my $ao = $olb;
$ao =~ s/BOOOOOOOK/$id/g;
my $an= $nbl;
$an =~ s/BOOOOOOOK/$id/g;
# do replace to fix format for cool reader
$fstr =~ s/$ao/$an/g;
}
print $fstr;
What the script does: it receives the $ url script parameter, converts the host name to Tor, sends the request via TOR and corrects the response in the form in which CoolReader wants to see it.
Step 3 . Configure Apache.
We create the file /etc/apache2/sites-available/proxy.flibusta.net with approximately the following contents:
ServerAdmin webmaster@flibusta.net
ServerName proxy.flibusta.net
DirectoryIndex /home/user/flibusta/nph-flibusta.pl
DocumentRoot /home/user/flibusta/
AllowOverride None
Options -Indexes +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{HTTP_HOST} =proxy.flibusta.net
RewriteRule ^(.*)$ /nph-flibusta.pl?url=$1 [H=cgi-script,QSA,L]
LogLevel warn
CustomLog /var/log/apache2/access.log combined
And give the command:
a2ensite proxy.flibusta.net
Restarting the Apache along the way:
service apache2 restart
What happens here: all requests to the proxy.flibusta.net website are sent to our magic script.
Step 4 . We fix the DNS configuration on the router.
I have an ASUS RT-N66U router, so in order to be able to carry out the necessary modification, MerlinWRT firmware was installed using DNSmasq from the developer's site . The JFFS section is included and the line was added to the /jffs/configs/dnsmasq.conf.add file:
address=/proxy.flibusta.net/99.99.99.99
Where 99.99.99.99 is the IP address of my VRS (it was specially changed to avoid the Habra effect), taking into account the fact that I have a dynamic address and I have no filtering of IP addresses on the server when accessing the script. Obviously, for another router model, step 4 will be different, but similar.
The last step . Add the address proxy.flibusta.net/opds to the list of electronic libraries in CoolReader. I don’t need to explain anything here. Just use the appropriate menu in the CoolReader ~ a.
I hope that my experience will be useful to other people and will help to make not only my neighbor happier, but also anyone else in this world. Special thanks to the person who made it possible to download books through the Tor network and to the author of the CoolReader program, which is used by "both pioneers and senior citizens."
PS: After two weeks, I found out that “Dontsova is an excellent writer, and“ 50 shades of gray “chernukha, which should not be read by such educated and polite young people like me.” I was also almost forced to shove the chocolate bar Alenka along with a request to remove from the CoolReader ~ a Litres menu, which for some reason "constantly asks for registration." Obviously, this method can also be used to organize access of any other software to .onion and .i2p sites.