颠倒的Ternet

出自ArchWiki

本文介绍如何使用 Squid 代理服务器和 imagemagick 的 mogrify 命令创建一个透明代理服务器,将图片上下颠倒。

安装

安装 squidapachewgetimagemagick 软件包。

配置

创建 flip.pl,将其放置在 /usr/local/bin 文件夹中,并使其可执行

/usr/local/bin/flip.pl
#!/usr/bin/perl
$|=1;
$count = 0;
$pid = $$;
while (<>) {
       @splitted=split(/ /,$_);
       chomp $_;
       if ($_ =~ /(.*\.jpg)/i) {
               $url = $1;
               system("/usr/bin/wget", "-q", "-O","/srv/http/images/$pid-$count.jpg", "$url");
               system("/usr/bin/mogrify", "-flip","/srv/http/images/$pid-$count.jpg");
               print "http://127.0.0.1/images/$pid-$count.jpg\n";
       }
       elsif ($_ =~ /(.*\.gif)/i) {
               $url = $1;
               system("/usr/bin/wget", "-q", "-O","/srv/http/images/$pid-$count.gif", "$url");
               system("/usr/bin/mogrify", "-flip","/srv/http/images/$pid-$count.gif");
               print "http://127.0.0.1/images/$pid-$count.gif\n";
       }
       elsif ($_ =~ /(.*\.png)/i) {
               $url = $1;
               system("/usr/bin/wget", "-q", "-O","/srv/http/images/$pid-$count.png", "$url");
               system("/usr/bin/mogrify", "-flip","/srv/http/images/$pid-$count.png");
               print "http://127.0.0.1/images/$pid-$count.png\n";
       }
       else {
               print "$splitted[0]\n";
       }
       $count++;
}

接下来,虽然不是必须的,但可以清理 Squid 配置文件,使其更易于阅读

# sed -i "/^#/d;/^ *$/d" /etc/squid/squid.conf

现在,编辑您的 squid.conf 文件并将以下内容追加到文件末尾

squid.conf
url_rewrite_program /usr/local/bin/flip.pl

同时找到 http_port 行,并将其修改为如下内容

squid.conf
http_port 3128 intercept

最后,我们必须创建用于存放翻转后图片的文件夹,并设置其权限

用于存储图片的目录必须归代理用户所有。

# mkdir /srv/http/images
# chown proxy:proxy /srv/http/images
# chmod 755 /srv/http/images

最后,将 http 用户添加到 proxy 组

# usermod -aG proxy http

验证 http 用户是 proxy 组的成员

# groups http

或者

# id -Gn http

路由器设置

您需要编辑路由器或网关上的防火墙,以将 HTTP 流量重定向到您的代理。

启动

启动/启用 httpd.servicesquid.service

参见