0

有人可以解释一下如何使它工作吗?我想通过privoxy为我的scrapy项目同时运行多个tors......每个都应该使用不同的IP地址。

4

1 回答 1

4

首先对我糟糕的英语感到抱歉,因为它不是我的母语(我是俄罗斯人)。我在尝试设置多个 tor 和 privoxy 实例时遇到了许多“问题”,以便每个实例都可以为我的 scrapy 使用不同的 ip。我会告诉你它是如何完成的。

我将在 Kali Linux 2.0 上工作,我还将以 root 身份工作。

步骤 1. 获取 tor 和 privoxy:

apt-get install tor
apt-get install privoxy

步骤 2. 制作 tor 副本...

cp /var/lib/tor -r /var/lib/tor2
cp /var/lib/tor -r /var/lib/tor3
cp /var/lib/tor -r /var/lib/tor4

步骤 3. 制作 torrc 副本...

cp /etc/tor/torrc torrc2
cp /etc/tor/torrc torrc3
cp /etc/tor/torrc torrc4

步骤 4. 现在我们要编辑每个 torrc 文件,你可以删除里面的所有内容并输入这些配置:

SocksPort 9050 (9060, 9070, 9080)
ControlPort 9051 (9061, 9071, 9081)
DataDirectory /var/lib/tor (tor2, tor3, tor4)

步骤 5. 让我们测试一切正常,让我们打开 4 个终端并像这样在每个人中启动 tor:

Terminal 1: tor -f /etc/tor/torrc
Terminal 2: tor -f /etc/tor/torrc2
Terminal 3: tor -f /etc/tor/torrc3
Terminal 4: tor -f /etc/tor/torrc4

现在打开一个新终端,让我们 curl 这个站点“http://ipinfo.io/ip”,它会给我们我们的 ip。每个 tor 的命令如下:

curl --proxy socks5h://localhost:9050 http://ipinfo.io/ip
curl --proxy socks5h://localhost:9060 http://ipinfo.io/ip
curl --proxy socks5h://localhost:9070 http://ipinfo.io/ip
curl --proxy socks5h://localhost:9080 http://ipinfo.io/ip

如果你做得很好,每个人都应该返回一个不同的 ip。现在我们有 4 个 Tor 实例同时运行。但是 tor 使用 sock5 代理,对于我们的 scrapy 项目,我们需要它是 http 代理。所以我们要插入privoxy。

第 1 步。让我们像使用 tor 一样复制 privoxy 文件夹 3 次...

cp -a /etc/privoxy /etc/privoxy2
cp -a /etc/privoxy /etc/privoxy3 
cp -a /etc/privoxy /etc/privoxy4

步骤 2. 现在我们将编辑每个 privoxy 文件夹中的每个配置文件:

mousepad /etc/privoxy(2,3,4)/config

首先,我们必须取消选中“forward-socks5t”并更改每个配置文件的端口:

    forward-socks5t   /               127.0.0.1:9050 .
    forward-socks5t   /               127.0.0.1:9060 .
    forward-socks5t   /               127.0.0.1:9070 .
    forward-socks5t   /               127.0.0.1:9080 .

还要更改监听地址:

listen-address  127.0.0.1:8118
listen-address  127.0.0.1:8128
listen-address  127.0.0.1:8138
listen-address  127.0.0.1:8148

第 3 步。我们将复制另一个 privoxy 文件夹:

 cp /etc/init.d/privoxy /etc/init.d/privoxy2 
 cp /etc/init.d/privoxy /etc/init.d/privoxy3
 cp /etc/init.d/privoxy /etc/init.d/privoxy4

步骤 4. 编辑每个文件夹中的 privoxy 文件:

mousepad /etc/init.d/privoxy(2,3,4)
NAME=privoxy(2,3,4)  
OWNER=privoxy(2,3,4)    
LOGDIR=/var/log/privoxy(2,3,4)
CONFIGFILE=/etc/privoxy(2,3,4)/config 

步骤 5. 最后,为每个 privoxy 制作日志副本:

cp -a /usr/sbin/privoxy /usr/sbin/privoxy2 
cp -a /usr/sbin/privoxy /usr/sbin/privoxy3 
cp -a /usr/sbin/privoxy /usr/sbin/privoxy4

完毕。让我们测试一下,在不同的终端中重新启动 4 个 tors,就像在步骤 5 中一样。现在我们将在不同的终端 4 privoxys 中启动,如下所示:

start-stop-daemon --start --exec /usr/sbin/privoxy --pidfile /var/run/privoxy.pid -- --user root /etc/privoxy/config 
start-stop-daemon --start --exec /usr/sbin/privoxy2 --pidfile /var/run/privoxy2.pid -- --user root /etc/privoxy2/config 
start-stop-daemon --start --exec /usr/sbin/privoxy3 --pidfile /var/run/privoxy3.pid -- --user root /etc/privoxy3/config 
start-stop-daemon --start --exec /usr/sbin/privoxy4 --pidfile /var/run/privoxy4.pid -- --user root /etc/privoxy4/config 

为了测试它是否有效,让我们 curl 同一个站点:

curl --proxy http://127.0.0.1:8118 http://ipinfo.io/ip
curl --proxy http://127.0.0.1:8128 http://ipinfo.io/ip
curl --proxy http://127.0.0.1:8138 http://ipinfo.io/ip
curl --proxy http://127.0.0.1:8148 http://ipinfo.io/ip

如果它为您提供不同的 ips,那么您做得很好。如果您想关闭所有 privoxys,只需输入:

pkill privoxy(2,3,4)

现在,当你要在 scrapy 中制作蜘蛛时,你可以为每个蜘蛛使用不同的代理,这里有一个例子:

import requests
from stem import Signal
from stem.control import Controller


def get_new_ip(2,3,4)():
    with Controller.from_port(port=9051(9061,9071,9081) as controller:
        controller.authenticate()
        controller.signal(Signal.NEWNYM)
    response = requests.get('https://api.myip.com/', proxies={'https': '127.0.0.1:8118(8128,8138,8148)'})
    print(response.text)


class ProxyMiddleware(2,3,4)(object):
    _requests_count = 0

    def process_request(self, request, spider):
        self._requests_count += 1
        if self._requests_count > 5:
            self._requests_count = 0
            get_new_ip1()

        request.meta['proxy'] = 'http://127.0.0.1:8118(8128,8138,8148)'
        spider.log('Proxy : %s' % request.meta['proxy'])

就是这样,我希望这能帮到你!))我曾经为将所有这些工作在一起付出了很多努力,但没有找到任何教程来逐步解释如何使其工作。

祝你好运!)

-Mikhail Aleksandrovskiy(离岸47)

于 2020-11-14T15:50:50.547 回答