📜 ⬆️ ⬇️

Privoxy + opera get rid of ads

After the acquisition of unlimited and the inclusion of images (yes, the small joys of a provincial student), the question of choosing an advertisement re-cutter arose. Here is a small digression: I hate advertising. No, everyone on the Internet hates advertising, but I have at least 3 compelling reasons to hate it:
1) Bright, checkered, often interferes with surfing
2) My old comp computer hardly turns colorful flushes. Father's computer and even more so.
3) It hurts unpleasantly, showing the mother a book on the vareznik, contemplating jumping bare ass and stuff.
Both I and my father used the opera and were not going to change it for anything, so the problem of choosing a cutter was quite acute. For some reason, I didn’t want to use Block CSS in the opera, but now I don’t want to. After a short googling, my choice fell on privoxy. This is a powerful advertiser that works on the side of the router (in my case, because I have a home local router on the slak, my computer and my father) as a proxy server. This approach in filtering ads suited me, because it was easy to prescribe a proxy, if you want to disable it, and so on.
In this article I will describe my steps for installing and configuring privoxy on my home server under skackware 13.
To begin with, we had to create a user and a group privoxy. I leave the creation method of your choice.
I decided to install the program using src2pkg . And then it turned out that without a few gestures can not do.
An example installation looks like this:
mkdir /tmp/privoxy
cd /tmp/privoxy
wget 'http://heanet.dl.sourceforge.net/project/ijbswa/Sources/3.0.16%20(stable)/privoxy-3.0.16-stable-src.tar.gz'

Next, create two scripts in the same folder:
privoxy.src2pkg:
#!/bin/bash
## src2pkg script for: privoxy
## Auto-generated by src2pkg-2.2
## src2pkg - Copyright 2005-2009 Gilbert Ashley <amigo@ibilio.org>

SOURCE_NAME='privoxy-3.0.16-stable-src.tar.gz'
NAME='privoxy' # Use ALT_NAME to override guessed value
VERSION='3.0.16' # Use ALT_VERSION to override guessed value
# ARCH=''
# BUILD='1'
# PRE_FIX='usr'

# from the privoxy.SlackBuild:
PRIVOXY_USER=${PRIVOXY_USER:-privoxy}
PRIVOXY_GROUP=${PRIVOXY_GROUP:-privoxy}
if ! grep -q ^$PRIVOXY_GROUP: /etc/group 2>/dev/null ; then
echo " Error: PRIVOXY group ($PRIVOXY_GROUP) doesn't exist."
echo " Try creating one with: groupadd -g 206 $PRIVOXY_GROUP"
exit 1
fi
if ! grep -q ^$PRIVOXY_USER: /etc/passwd 2>/dev/null ; then
echo " Error: PRIVOXY user ($PRIVOXY_USER) doesn't exist."
echo " Try creating one with: useradd -u 206 -g $PRIVOXY_GROUP -d /dev/null -s /bin/false $PRIVOXY_USER"
exit 1
fi

# Any extra options go here:
EXTRA_CONFIGS="--sysconfdir=/etc/$NAME \
--localstatedir=/var \
--docdir=$docdir/$NAME-$VERSION \
--with-docbook=no \
--with-user=$PRIVOXY_USER \
--with-group=$PRIVOXY_GROUP"

# Optional function replaces configure_source, compile_source, fake_install
# To use, uncomment and write/paste CODE between the {} brackets.
# build() { CODE }

# Get the functions and configs
. /usr/libexec/src2pkg/FUNCTIONS ;

# Execute the named packaging steps:
pre_process
find_source
make_dirs
unpack_source
fix_source_perms

#cd $SRC_DIR
#autoheader
#autoconf

configure_source #
compile_source # If used, the 'build' function replaces these 3
fake_install #

mkdir -p $PKG_DIR/etc/$NAME/templates
cp $SRC_DIR/templates/* $PKG_DIR/etc/$NAME/templates

#Adapted from the privoxy.SlackBuild:
rm -rf $PKG_DIR/etc/rc.d
mkdir -p $PKG_DIR/etc/rc.d
cat $SRC_DIR/slackware/rc.privoxy.orig > $PKG/etc/rc.d/rc.$NAME.new
sed -i " s/%PROGRAM%/$NAME/
s,%SBIN_DEST%,/usr/bin,
s,%CONF_DEST%,/etc/$NAME,
s/%USER%/$NAME/
s/%GROUP%/$NAME/
" $PKG_DIR/etc/rc.d/rc.$NAME.new

# Fix Path within the configuration files (thanks to h4kteur)
sed -i "s#$PKG_DIR##g" $PKG_DIR/etc/$NAME/config
# Fix Path with the config file to point to right usermanual (thanks to BP{k})
sed -i \
"s#user-manual /usr/doc/$NAME#user-manual /$docdir/$NAME-$VERSION#" \
$PKG_DIR/etc/privoxy/config

# Make .new files so we dont clobber existing configuration
find $PKG_DIR/etc/privoxy -type f -exec mv {} {}.new \;
# Don't clobber the logfile either
mv $PKG_DIR/var/log/privoxy/logfile $PKG_DIR/var/log/privoxy/logfile.new
# Remove this directory since it's empty and part of Slackware base
rmdir $PKG_DIR/var/run &> /dev/null

fix_pkg_perms
strip_bins
create_docs
compress_man_pages
make_description
make_doinst
make_package
post_process


doinst.prepend:
# Keep same perms on rc.privoxy.new:
if [ -e etc/rc.d/rc.privoxy ]; then
cp -a etc/rc.d/rc.privoxy etc/rc.d/rc.privoxy.new.incoming
cat etc/rc.d/rc.privoxy.new > etc/rc.d/rc.privoxy.new.incoming
mv etc/rc.d/rc.privoxy.new.incoming etc/rc.d/rc.privoxy.new
fi

# If there's no existing log file, move this one over;
# otherwise, kill the new one
if [ ! -e var/log/privoxy/logfile ]; then
mv var/log/privoxy/logfile.new var/log/privoxy/logfile
else
rm -f var/log/privoxy/logfile.new
fi

if ! grep -q ^privoxy: /etc/group 2>/dev/null ; then
echo " Error: PRIVOXY group 'privoxy' doesn't exist."
echo " Creating one with: groupadd -g 206 privoxy"
groupadd -g 206 @PRIVOXY_GROU@
fi

if ! grep -q ^privoxy: /etc/passwd 2>/dev/null ; then
echo " Error: PRIVOXY user 'privoxy' doesn't exist."
echo " Try creating one with: useradd -u 206 -g privoxy -d /dev/null -s /bin/false privoxy"
exit 1
fi

if ! [ -x etc/rc.d/rc.privoxy ]; then
echo " etc/rc.d/rc.privoxy must be set executable to enable privoxy at boot-time:"
echo " chmod 755 /etc/rc.d/rc.privoxy"
fi

For the scripts, we say thanks to the developer src2pkg Gilbert Ashley.
We put the right to perform. After that, we collect the package and put the privoxy team
src2pkg -X -C -I
The program was installed successfully. It took me a minimal config change:
1. commented listen-address 127.0.0.1:8118
2. listen-address 192.168.0.1:8118
3. permit-access 192.168.0.0/24
Next, you need to change the /etc/rc.d/rc.privoxy file

PRIVOXY_USER="privoxy"
PRIVOXY_GROUP="privoxy"


Now you can start privoxy for the first time:
root@ironnet:/tmp/privoxy# /etc/rc.d/rc.privoxy start
Starting privoxy: OK


Now all I have to do is register in the proxy browser 192.168.0.1:8118

Unfortunately, after some time, it turned out that the built-in filter does not cope with filtering. After a brief googling, a python script was borrowed from here to automatically generate rules based on the blocklist from the marine.
I changed it a little bit by adding another blocklist for parsing. Total left this:
#!/usr/bin/perl

# Updated: 2010/03/17 12:58:26

###############################################################################
# adblock2privoxy.pl
###############################################################################
# Copyright 2010 Arcady N. Shpak (Greignar) arsengine.org.ru
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
################################################################################

# codepage utf8

use strict;

# ( '/')
my $cfg = '/etc/privoxy';
# ( '/')
my $tmp = '/tmp';

my $file = '/adblock.tmp';

&WriteFile(">$cfg/adblock.action", '');

#
&process('http://ruadlist.googlecode.com/svn/trunk/adblock.txt', 'ru-adlist-list');
&process('http://ruadlist.googlecode.com/svn/trunk/antinuha.txt', 'ru-adult-list');
&process('http://ruadlist.googlecode.com/svn/trunk/adblockeh.txt', 'ru-adult-list');
# , :
# &process(' ', ' - ');

exit;

sub process
{
my ($url, $name) = @_;
my (@adblock, @white, @black);
&WriteFile(">$tmp$file", '');
system("wget $url -O $tmp$file");
return unless(-e "$tmp$file" );
@adblock = &ReadFile("<$tmp$file");
foreach(@adblock) {
#
$_ = &clean($_);
#
next if($_ =~ /^$/);
#
next if($_ =~ s/^[\[\!]+.*//);
#
next if($_ =~ s/\#.*//);
#
next if($_ =~ s/\$.+//);
# ,
next if($_ =~ s/^.*[\[\(\\]+.*//);
#
if($_ =~ /^\@.*/) { unshift(@white, &mask($_)); next; }
#
if($_ =~ /^(\*|\||).*/) { unshift(@black, &mask($_)); next; }
}
@black = sort(@black);
@white = sort(@white);
#
&WriteFile(">>$cfg/adblock.action", "{+block{$name}}\n@black\n");
&WriteFile(">>$cfg/adblock.action", "{-block}\n@white\n");
#
system("chmod a+rw $cfg/adblock.action");
}

sub mask
{
my $url = shift;
my($host, $path);
# URL
$url =~ s/^[\@\|\.]+//g;
$url =~ s/^.*?:\/\/+//g;
$url =~ s/\$.*$//g;
$url =~ s/[|]+$//g;
$url =~ s/\*$//g;
$url =~ s/\^/\//g;
$url =~ s/^\*/\/*/;
$url =~ s/^([-_]+)/\/*$1/;
$url =~ s/\/+/\//;
#
$url =~ s/(\/.*)$//; $path = $1;
$url =~ s/^([^\/]*)//; $host = $1;
# www
$host =~ s/^www\.//ig;
# ( )
if($path =~ /[\*\?]/) {
$path =~ s/([\\\?\&\.\,])/\\\1/g;
$path =~ s/\*/.*/g;
}
#
$host = '.' . $host if($host ne '');
return "$host$path\n";
}

sub clean()
{
my ($str) = @_;
chomp $str;
#
$str =~ s/^\s+|\s+$//g;
$str =~ s/[\r\n]+//g;
return $str;
}

sub ReadFile
{
my($file, @body);
$file = $_[0];
open(local *F, $file) || die("Can't open data-file $file\n",$!);
binmode F;
@body = ;
close(F);
return @body;
}

sub WriteFile
{
my($file, $body, $mode, $access);
$file = $_[0];
$body = $_[1];
open(local *F, $file) || die("Can't create data-file $file\n",$!);
binmode F;
flock(F, 2);
print F $body;
close(F);
return 1;
}

1;

')
We save this miracle under the name adblock2privoxy.pl in the / etc / privoxy directory, issue the execution rights. Further extract from the readme to this script:

adblock2privoxy.pl Perl AdBlock Privoxy.

(Linux):

1. .
2. ($cfg, $tmp)
3. .
4. Privoxy "actionsfile":
: actionsfile user.action
: actionsfile adblock.action
5. ( sudo) cron'
6. Privoxy


What we are doing.

Well, for complete happiness we add crowns:

40 4 * * * /etc/privoxy/adblock2privoxy.pl
42 4 * * * /etc/rc.d/rc.privoxy restart


Total: we get a very smart, easily customizable, regularly updated and most importantly correctly and logically working advertizer for the opera. Isn't it a beauty?

Well, as a final touch, I will provide you with a couple of screenshots.
Before privoxy:

After:

Source: https://habr.com/ru/post/91147/


All Articles