以编程方式扭曲连接并更改出口节点 [英] Torify connection and change exit node programmatically

查看:86
本文介绍了以编程方式扭曲连接并更改出口节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个PHP脚本,该脚本基本上循环遍历存储在数据库中的某些数据。此项目的两个主要重要字段是 URL和国家/地区。

I am writing a PHP script that basically loops thru some data I have stored in my database. The two main important fields for this project are 'url' and 'country'.

基本上,我想做的是遍历所有这些URL并确定哪些链接已损坏(使用某些PHP解析器,但了解此问题并不重要)。但是,我遇到的问题是,除非您拥有来自该国家/地区的IP地址,否则您将无法访问分配给该国家/地区的URL。我已经在美国网址中设置了此脚本,因为我在这里,所以它很简单。

Basically what I am trying to do is loop thru all of these URLs and determine which links are broken (using some PHP resolver, but isn't important to know for this problem). However the issue I am having is that you cannot access a URL that is assigned to a certain country, unless you have an IP address coming from that country. I already have this script set up for U.S URLs because I am here and so it makes that simple.

我想知道是否以及如何(将我的资源,技巧,解决方案),我可以使用Tor来以编程方式检查此2个字母的国家/地区代码,然后使用该IP转到该URL。我猜想这与使用该国的退出节点有关,但我不太确定。

I was wondering if and how (point me to resources, tips, solutions) I can use Tor to programmatically check this 2 letter country code and go to that URL using that IP. I am guessing it will have something to do with using an Exit Node from that country, but I am not too sure.

推荐答案

使用 ExitNodes 配置选项,可以使用语法 {US} 指定来自特定国家/地区的退出节点美国是国家代码。

Using the ExitNodes configuration option, it is possible to specify exit nodes from a specific country using the syntax {US} where US is the country code.

使用PHP TorUtils 库,这可以通过简单的方式实现自动化。

Using the PHP TorUtils library this can be automated in a simple way.

以下是代码示例:

<?php

require 'TorUtils/src/ControlClient.php';
require 'TorUtils/src/TorCurlWrapper.php';

// list of country codes to use
$countries = array('US', 'FR', 'RU', 'GB', 'CA');

// get new control client for connecting to Tor's control port
$tc = new Dapphp\TorUtils\ControlClient();

$tc->connect(); // connect
$tc->authenticate('password'); // authenticate

foreach($countries as $country) {
    $country = '{' . $country . '}'; // e.g. {US}

    $tc->setConf(array('ExitNodes' => $country)); // set config to use exit node from country

    // get new curl wrapped through Tor SOCKS5 proxy
    $curl = new Dapphp\TorUtils\TorCurlWrapper();
    $curl->setopt(CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox 41.0');

    // make request - should go through exit node from specified country
    if ($curl->httpGet('http://whatismycountry.com')) {
        echo $curl->getResponseBody();
    }
}

TorCurlWrapper是一个简单的cURL包装器,可通过Tor SOCKS5代理。 ControlClient用于在每次请求从某个国家切换到ExitNode之前设置配置选项。

TorCurlWrapper is a simple cURL wrapper that routes cURL through the Tor SOCKS5 proxy. The ControlClient is used to set the configuration option before each request to switch to ExitNodes from a certain country.

使用作曲家,您可以安装TorUtils:

Using composer you can install TorUtils:

php composer.phar require dapphp/torutils

然后在代码中使用 require'vendor / autoload.php'; 自动加载TorUtils类。

Then in your code use require 'vendor/autoload.php'; to autoload the TorUtils classes.

这篇关于以编程方式扭曲连接并更改出口节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆