6

该站点http://openbook.etoro.com/#/main/有一个实时提要,该提要由 javascript 通过 XHR 保持活动请求生成,并从服务器作为 gzip 压缩 JSON 字符串获取答案。

我想将提要捕获到文件中。

通常的方法(WWW::Mech..)(可能)不可行,因为需要对页面中的所有 Javascript 进行反向工程并模拟浏览器确实是一项艰巨的任务,因此,寻找替代解决方案。

我的想法是使用中间人策略,因此浏览器将完成他的工作,我想通过 perl 代理捕获通信 - 仅用于此任务。

我能够捕捉到最初的交流,但不能捕捉到提要本身。代理工作正常,因为在浏览器中,提要只运行我的文件管理器不起作用。

use HTTP::Proxy;
use HTTP::Proxy::HeaderFilter::simple;
use HTTP::Proxy::BodyFilter::simple;
use Data::Dumper;
use strict;
use warnings;

my $proxy = HTTP::Proxy->new(
     port => 3128, max_clients => 100, max_keep_alive_requests => 100
);

my $hfilter = HTTP::Proxy::HeaderFilter::simple->new(
    sub {
        my ( $self, $headers, $message ) = @_;
        print STDERR "headers", Dumper($headers);
    }
);

my $bfilter = HTTP::Proxy::BodyFilter::simple->new(
    filter => sub {
        my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
        print STDERR "dataref", Dumper($dataref);
    }
);

$proxy->push_filter( response => $hfilter); #header dumper
$proxy->push_filter( response => $bfilter); #body dumper
$proxy->start;

Firefox 使用上述代理配置所有通信。

提要在浏览器中运行,因此代理向其提供数据。(当我停止代理时,提要也停止了)。随机(不知道何时)我收到以下错误:

[Tue Jul 10 17:13:58 2012] (42289) ERROR: Getting request failed: Client closed

任何人都可以告诉我一种方法,如何为 Dumper 构建正确的 HTTP::Proxy 过滤器,无论 keep_alive XHR 是浏览器和服务器之间的所有通信吗?

4

2 回答 2

5

这是我认为你所追求的东西:

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

use HTTP::Proxy;
use HTTP::Proxy::BodyFilter::complete;
use HTTP::Proxy::BodyFilter::simple;
use JSON::XS     qw( decode_json );
use Data::Dumper qw( Dumper );

my $proxy = HTTP::Proxy->new(
    port                     => 3128,
    max_clients              => 100,
    max_keep_alive_requests  => 100,
);

my $filter = HTTP::Proxy::BodyFilter::simple->new(
    sub {
        my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
        return unless $$dataref;
        my $content_type = $message->headers->content_type or return;
        say "\nContent-type: $content_type";
        my $data = decode_json( $$dataref );
        say Dumper( $data );
    }
);

$proxy->push_filter(
    method   => 'GET',
    mime     => 'application/json',
    response => HTTP::Proxy::BodyFilter::complete->new,
    response => $filter
);

$proxy->start;

我认为您不需要单独的标题过滤器,因为您可以访问要$message->headers在正文过滤器中使用的任何标题。

你会注意到我将两个过滤器推到了管道上。第一个是类型HTTP::Proxy::BodyFilter::complete,它的工作是收集响应块,并确保后面的真正过滤器总是在$dataref. 但是对于接收和缓冲的 foreach 块,将调用以下过滤器并传递一个空的$dataref. 我的过滤器通过提前返回来忽略这些。

我还设置了过滤器管道以忽略除导致 JSON 响应的 GET 请求之外的所有内容——因为这些似乎是最有趣的。

感谢您提出这个问题 - 这是一个有趣的小问题,您似乎已经完成了大部分艰苦的工作。

于 2012-07-11T09:46:01.397 回答
2

Set the mime parameter, default is to filter text types only.

$proxy->push_filter(response => $hfilter, mime => 'application/json');
$proxy->push_filter(response => $bfilter, mime => 'application/json');
于 2012-07-11T09:05:24.453 回答