xai1981's blog

http://twitter.com/xai1981

Proxy Server 経由でリクエストする

概要

WEBスクレイピングの際に Proxy Server と接続して自身(リクエスト元)のIPアドレスを、固定IPから変動するIPに変更する方法。Proxy Server からWEBスクレイピングサーバーへ接続した Port Forwarding した上でWEBスクレイピングサーバーから Proxy Server 経由でスクレイピングするため、アクセス先のアクセスログには Proxy Server のIPが残ります。

手順

  1. Proxy Server を用意する
  2. Proxy Server から WEBスクレイピングするサーバーに ssh の Port Forwarding で接続する
  3. 呼び出し元のプログラムを修正してオプションで Proxy Server と Proxy Port を設定できるようにする

Proxy Server を用意する

[root@my.proxy.server ~]# yum -y install squid
===========================================================
 Package            Arch   Version         Repository Size
===========================================================
Installing:
 squid              x86_64 7:3.1.10-29.el6 base       1.7 M

Transaction Summary
===========================================================
Install       1 Package(s)
Complete!
[root@my.proxy.server ~]# chkconfig --level 35 squid on
[root@my.proxy.server ~]# chkconfig --list squid
squid           0:off   1:off   2:off   3:on    4:off   5:on    6:off
[root@my.proxy.server squid]# diff -u squid.conf.default squid.conf
--- squid.conf.default  2014-10-16 02:52:24.000000000 +0900
+++ squid.conf  2015-01-26 17:36:31.252896670 +0900
@@ -61,6 +61,8 @@
 # Squid normally listens to port 3128
 http_port 3128
 
+forwarded_for off
+
 # We recommend you to use at least the following line.
 hierarchy_stoplist cgi-bin ?
 
@@ -75,3 +77,7 @@
 refresh_pattern ^gopher:       1440    0%      1440
 refresh_pattern -i (/cgi-bin/|¥?) 0    0%      0
 refresh_pattern .              0       20%     4320
+
+header_access X-Forwarded-For deny all
+header_access Via deny all
+header_access Cache-Control deny all

Proxy Server から WEBスクレイピングするサーバーに ssh の Port Forwarding で接続する

[xai1981@my.proxy.com ~]$ ssh -i buff/key.pem -NR 5000:127.0.0.1:3128 ec2-user@my.scraping.com

呼び出し元のプログラムを修正してオプションで Proxy Server と Proxy Port を設定できるようにする

diff --git a/app/lib/ XXXX/Net/Curl.php b/app/lib/XXXX/Net/Curl.php
index 6a13ee9..2d9e767 100644
--- a/app/lib/XXXX/Net/Curl.php
+++ b/app/lib/XXXX/Net/Curl.php
@@ -14,6 +14,8 @@ class Curl
     public static $receivePath = null;
     public static $isRedirectable = false;
     public static $redirectDepth = 1;
+    public static $proxy = '';
+    public static $proxyPort;
 
     // CURLOPT_COOKIEJAR 設定関数
     public static function setCookieJar($path){
@@ -31,6 +33,15 @@ class Curl
     public static function setMaxRedirectCount($count){
         self::$redirectDepth = $count;
     }
+    // CURLOPT_PROXY 設定関数
+    public static function setProxy($url){
+        self::$proxy = $url;
+    }
+    // CURLOPT_PROXYPORT 設定関数
+    public static function setProxyPort($port){
+        self::$proxyPort = $port;
+    }
+
     /**
     * 指定のURLのリソースを取得します。
     *
@@ -51,6 +62,11 @@ class Curl
             curl_setopt($ch, CURLOPT_MAXREDIRS, self::$redirectDepth); 
         }
 
+        if (self::$proxy && is_numeric(self::$proxyPort)) {
+            curl_setopt($ch, CURLOPT_PROXY, self::$proxy);
+            curl_setopt($ch, CURLOPT_PROXYPORT, self::$proxyPort);
+        }
+
 //        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
         curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0');
diff --git a/app/models/Sites/SiteA/SiteA/Crawler.php b/app/models/Sites/SiteA/SiteA/Crawler.php
index a71b952..a05252b 100644
--- a/app/models/Sites/SiteA/SiteA/Crawler.php
+++ b/app/models/Sites/SiteA/SiteA/Crawler.php
@@ -286,6 +286,8 @@ class Crawler extends SitesCrawler implements CrawlerInterface
         try {
             $info = ['schedule_id' => $sId, 'url' => $url];
             Log::info('['.__CLASS__.'.'.__FUNCTION__.'] Try. ', $info);
+            Curl::setProxy('127.0.0.1');
+            Curl::setProxyPort('5000');
             $topHTML = Curl::get($url);
             Log::info('['.__CLASS__.'.'.__FUNCTION__.'] Done.', $info);
         } catch (CurlException $ce) {
参考サイト